Skip to content

Commit

Permalink
Fix #12486 Improve simplification of using statements (#6080)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Mar 5, 2024
1 parent 14833a4 commit f3cccd0
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 14 deletions.
21 changes: 16 additions & 5 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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=
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");
}
tok = end;
}

Expand Down Expand Up @@ -2922,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);
Expand Down
2 changes: 1 addition & 1 deletion test/testsimplifytemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2880,7 +2880,7 @@ class TestSimplifyTemplate : public TestFixture {
"struct f<b::B<0>> ; "
"} "
"} "
"b :: f<b::B<0>> g1 ; struct b :: B<0> { } ; "
"b :: f<b::B<0>> g1 ; struct b :: B<0> { using d = B<0> :: d ; } ; "
"struct b :: f<b::B<0>> { } ;";
ASSERT_EQUALS(exp, tok(code));
}
Expand Down
49 changes: 41 additions & 8 deletions test/testsimplifyusing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -683,14 +683,47 @@ 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());
}
{
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());
}
{
const char code[] = "template <typename T>\n"
"class vector : public ::std::vector<T> {\n"
"public:\n"
" using ::std::vector<T>::vector;\n"
" vector() {}\n"
"};\n"
"vector <int> v;\n";
const char expected[] = "class vector<int> ; "
"vector<int> v ; "
"class vector<int> : public :: std :: vector<int> { "
"public: "
"using vector<int> = :: std :: vector<int> :: vector<int> ; "
"vector<int> ( ) { } "
"} ;";
ASSERT_EQUALS(expected, tok(code, Platform::Type::Native, /*debugwarnings*/ true));
ASSERT_EQUALS("", errout.str());
}
}

void simplifyUsing8970() {
Expand Down

0 comments on commit f3cccd0

Please sign in to comment.