Skip to content

Commit

Permalink
Partial fix for #12486 Improve simplification of using statements
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 4, 2024
1 parent 779141c commit afeb3fc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
12 changes: 10 additions & 2 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=
continue;
tok->insertToken(end->strAt(-1))->insertToken("=");
if (end->str() == ",") { // comma-separated list
end->str(";");
end->insertToken("using");
}
tok = end;
}

Expand Down
25 changes: 17 additions & 8 deletions test/testsimplifyusing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit afeb3fc

Please sign in to comment.