From afeb3fcd8c3e288382d043b6b5cea2a94385a3af Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 4 Mar 2024 12:04:22 +0100 Subject: [PATCH 1/5] Partial fix for #12486 Improve simplification of using statements --- lib/tokenize.cpp | 12 ++++++++++-- test/testsimplifyusing.cpp | 25 +++++++++++++++++-------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c62973cc048..c0adca2b79a 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2845,13 +2845,21 @@ bool Tokenizer::simplifyUsing() if (!Token::Match(tok, "using ::| %name% ::")) continue; Token* end = tok->tokAt(3); - while (end && end->str() != ";") - end = end->next(); + while (end && !Token::Match(end, "[;,]")) { + if (end->str() == "<" && end->link()) // skip template args + end =end->link()->next(); + else + end = end->next(); + } if (!end) continue; if (!end->tokAt(-1)->isNameOnly()) // e.g. operator= continue; tok->insertToken(end->strAt(-1))->insertToken("="); + if (end->str() == ",") { // comma-separated list + end->str(";"); + end->insertToken("using"); + } tok = end; } diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index f1a3ff49a94..464138c413e 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -683,14 +683,23 @@ class TestSimplifyUsing : public TestFixture { ASSERT_EQUALS("", errout.str()); } - void simplifyUsing30() { // #8454 - const char code[] = "using std::to_string;\n" - "void f() {\n" - " std::string str = to_string(1);\n" - "}\n"; - const char expected[] = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); - ASSERT_EQUALS("", errout.str()); + void simplifyUsing30() { + { + const char code[] = "using std::to_string;\n" // #8454 + "void f() {\n" + " std::string str = to_string(1);\n" + "}\n"; + const char expected[] = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }"; + ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS("", errout.str()); + } + { + const char code[] = "using std::cout, std::endl, std::cerr, std::ostringstream;\n" + "cerr << \"abc\";\n"; + const char expected[] = "std :: cerr << \"abc\" ;"; + ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS("", errout.str()); + } } void simplifyUsing8970() { From e7fafc889c443d395f4561745a9785ed81a99c0d Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 4 Mar 2024 12:14:16 +0100 Subject: [PATCH 2/5] Don't simplify user-defined literal --- lib/tokenize.cpp | 2 +- test/testsimplifyusing.cpp | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index c0adca2b79a..1dbbf0e7719 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2853,7 +2853,7 @@ bool Tokenizer::simplifyUsing() } if (!end) continue; - if (!end->tokAt(-1)->isNameOnly()) // e.g. operator= + if (!end->tokAt(-1)->isNameOnly() || end->tokAt(-2)->isLiteral()) // e.g. operator=, operator""sv continue; tok->insertToken(end->strAt(-1))->insertToken("="); if (end->str() == ",") { // comma-separated list diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 464138c413e..5978401c0c6 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -684,19 +684,25 @@ class TestSimplifyUsing : public TestFixture { } void simplifyUsing30() { + //{ + // const char code[] = "using std::to_string;\n" // #8454 + // "void f() {\n" + // " std::string str = to_string(1);\n" + // "}\n"; + // const char expected[] = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }"; + // ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + // ASSERT_EQUALS("", errout.str()); + //} + //{ + // const char code[] = "using std::cout, std::endl, std::cerr, std::ostringstream;\n" + // "cerr << \"abc\";\n"; + // const char expected[] = "std :: cerr << \"abc\" ;"; + // ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + // ASSERT_EQUALS("", errout.str()); + //} { - const char code[] = "using std::to_string;\n" // #8454 - "void f() {\n" - " std::string str = to_string(1);\n" - "}\n"; - const char expected[] = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }"; - ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); - ASSERT_EQUALS("", errout.str()); - } - { - const char code[] = "using std::cout, std::endl, std::cerr, std::ostringstream;\n" - "cerr << \"abc\";\n"; - const char expected[] = "std :: cerr << \"abc\" ;"; + const char code[] = "using std::string_view_literals::operator\"\"sv;\n"; + const char expected[] = "using std :: string_view_literals :: operator\"\"sv ;"; ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); ASSERT_EQUALS("", errout.str()); } From ae433cba07b1dd3cd8e49a02f8d8ab00400de93e Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 4 Mar 2024 12:14:48 +0100 Subject: [PATCH 3/5] Undo --- test/testsimplifyusing.cpp | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 5978401c0c6..bfdf8b9a1d1 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -684,22 +684,22 @@ class TestSimplifyUsing : public TestFixture { } void simplifyUsing30() { - //{ - // const char code[] = "using std::to_string;\n" // #8454 - // "void f() {\n" - // " std::string str = to_string(1);\n" - // "}\n"; - // const char expected[] = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }"; - // ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); - // ASSERT_EQUALS("", errout.str()); - //} - //{ - // const char code[] = "using std::cout, std::endl, std::cerr, std::ostringstream;\n" - // "cerr << \"abc\";\n"; - // const char expected[] = "std :: cerr << \"abc\" ;"; - // ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); - // ASSERT_EQUALS("", errout.str()); - //} + { + const char code[] = "using std::to_string;\n" // #8454 + "void f() {\n" + " std::string str = to_string(1);\n" + "}\n"; + const char expected[] = "void f ( ) { std :: string str ; str = std :: to_string ( 1 ) ; }"; + ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS("", errout.str()); + } + { + const char code[] = "using std::cout, std::endl, std::cerr, std::ostringstream;\n" + "cerr << \"abc\";\n"; + const char expected[] = "std :: cerr << \"abc\" ;"; + ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS("", errout.str()); + } { const char code[] = "using std::string_view_literals::operator\"\"sv;\n"; const char expected[] = "using std :: string_view_literals :: operator\"\"sv ;"; From 79c22c6f0edc5470edca945d4d766c6a7a86b957 Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 4 Mar 2024 16:15:36 +0100 Subject: [PATCH 4/5] Don't simplify within class definition --- lib/tokenize.cpp | 9 ++++++--- test/testsimplifyusing.cpp | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 1dbbf0e7719..eae3438d18d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -2847,7 +2847,7 @@ bool Tokenizer::simplifyUsing() Token* end = tok->tokAt(3); while (end && !Token::Match(end, "[;,]")) { if (end->str() == "<" && end->link()) // skip template args - end =end->link()->next(); + end = end->link()->next(); else end = end->next(); } @@ -2855,7 +2855,7 @@ bool Tokenizer::simplifyUsing() continue; if (!end->tokAt(-1)->isNameOnly() || end->tokAt(-2)->isLiteral()) // e.g. operator=, operator""sv continue; - tok->insertToken(end->strAt(-1))->insertToken("="); + tok->insertToken(end->strAt(-1))->insertToken("=")->isSimplifiedTypedef(true); if (end->str() == ",") { // comma-separated list end->str(";"); end->insertToken("using"); @@ -2930,8 +2930,11 @@ bool Tokenizer::simplifyUsing() std::string scope = currentScope->fullName; Token *usingStart = tok; Token *start; - if (tok->strAt(2) == "=") + if (tok->strAt(2) == "=") { + if (currentScope->type == ScopeInfo3::Record && tok->tokAt(2)->isSimplifiedTypedef()) // don't simplify within class definition + continue; start = tok->tokAt(3); + } else start = tok->linkAt(2)->tokAt(3); Token *usingEnd = findSemicolon(start); diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index bfdf8b9a1d1..abf15dd0048 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -706,6 +706,24 @@ class TestSimplifyUsing : public TestFixture { ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); ASSERT_EQUALS("", errout.str()); } + { + const char code[] = "template \n" + "class vector : public ::std::vector {\n" + "public:\n" + " using ::std::vector::vector;\n" + " vector() {}\n" + "};\n" + "vector v;\n"; + const char expected[] = "class vector ; " + "vector v ; " + "class vector : public :: std :: vector { " + "public: " + "using vector = :: std :: vector :: vector ; " + "vector ( ) { } " + "} ;"; + ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true)); + ASSERT_EQUALS("", errout.str()); + } } void simplifyUsing8970() { From d94a6f87e9a25fa90a2c1e33f154a7213aec90de Mon Sep 17 00:00:00 2001 From: chrchr Date: Mon, 4 Mar 2024 17:24:03 +0100 Subject: [PATCH 5/5] Fix test --- test/testsimplifytemplate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testsimplifytemplate.cpp b/test/testsimplifytemplate.cpp index 784c4b36c7a..66a1e2e0fd6 100644 --- a/test/testsimplifytemplate.cpp +++ b/test/testsimplifytemplate.cpp @@ -2880,7 +2880,7 @@ class TestSimplifyTemplate : public TestFixture { "struct f> ; " "} " "} " - "b :: f> g1 ; struct b :: B<0> { } ; " + "b :: f> g1 ; struct b :: B<0> { using d = B<0> :: d ; } ; " "struct b :: f> { } ;"; ASSERT_EQUALS(exp, tok(code)); }