Skip to content

Commit

Permalink
Don't simplify within class definition
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Mar 4, 2024
1 parent ae433cb commit 79c22c6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
9 changes: 6 additions & 3 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2847,15 +2847,15 @@ 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();
}
if (!end)
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");
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 18 additions & 0 deletions test/testsimplifyusing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <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 79c22c6

Please sign in to comment.