Skip to content

Commit

Permalink
Partial fix for #12014 Outer typedef incorrectly applied within names…
Browse files Browse the repository at this point in the history
…pace (danmar#6288)
  • Loading branch information
chrchr-github committed Apr 17, 2024
1 parent c6ae9ae commit 8b518bf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,18 @@ void Tokenizer::simplifyTypedef()
simplifyTypedefCpp();
}

static bool isEnumScope(const Token* tok)
{
if (!Token::simpleMatch(tok, "{"))
return false;
tok = tok->previous();
while (tok && !tok->isKeyword() && Token::Match(tok, "%name%|::|:"))
tok = tok->previous();
if (Token::simpleMatch(tok, "class"))
tok = tok->previous();
return Token::simpleMatch(tok, "enum");
}

void Tokenizer::simplifyTypedefCpp()
{
bool isNamespace = false;
Expand Down Expand Up @@ -1622,7 +1634,7 @@ void Tokenizer::simplifyTypedefCpp()
bool globalScope = false;
int classLevel = spaceInfo.size();
bool inTypeDef = false;
bool inEnumClass = false;
bool inEnum = false;
std::string removed;
std::string classPath;
for (size_t i = 1; i < spaceInfo.size(); ++i) {
Expand Down Expand Up @@ -1676,7 +1688,7 @@ void Tokenizer::simplifyTypedefCpp()
if (memberScope == 0)
inMemberFunc = false;
}
inEnumClass = false;
inEnum = false;

if (classLevel > 1 && tok2 == spaceInfo[classLevel - 1].bodyEnd2) {
--classLevel;
Expand Down Expand Up @@ -1736,8 +1748,8 @@ void Tokenizer::simplifyTypedefCpp()
}
++scope;
}
if (Token::Match(tok2->tokAt(-3), "enum class %name%"))
inEnumClass = true;
if (isEnumScope(tok2))
inEnum = true;
}

// keep track of scopes within member function
Expand Down Expand Up @@ -1863,7 +1875,7 @@ void Tokenizer::simplifyTypedefCpp()
}
}

simplifyType = simplifyType && (!inEnumClass || Token::simpleMatch(tok2->previous(), "="));
simplifyType = simplifyType && (!inEnum || !Token::simpleMatch(tok2->next(), "="));
simplifyType = simplifyType && !(Token::simpleMatch(tok2->next(), "<") && Token::simpleMatch(typeEnd, ">"));

if (simplifyType) {
Expand Down
20 changes: 20 additions & 0 deletions test/testsimplifytypedef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class TestSimplifyTypedef : public TestFixture {
TEST_CASE(simplifyTypedef149);
TEST_CASE(simplifyTypedef150);
TEST_CASE(simplifyTypedef151);
TEST_CASE(simplifyTypedef152);

TEST_CASE(simplifyTypedefFunction1);
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
Expand Down Expand Up @@ -3543,6 +3544,25 @@ class TestSimplifyTypedef : public TestFixture {
ASSERT_EQUALS(exp, tok(code));
}

void simplifyTypedef152() {
const char* code{}, *exp{};
code = "namespace O { struct T {}; }\n"
"typedef O::T S;\n"
"namespace M {\n"
" enum E { E0, S = 1 };\n"
" enum class F : ::std::int8_t { F0, S = 1 };\n"
"}\n"
"namespace N { enum { G0, S = 1 }; }\n";
exp = "namespace O { struct T { } ; } "
"namespace M { "
"enum E { E0 , S = 1 } ; "
"enum class F : :: std :: int8_t { F0 , S = 1 } ; "
"}"
" namespace N { enum Anonymous0 { G0 , S = 1 } ; "
"}";
ASSERT_EQUALS(exp, tok(code));
}

void simplifyTypedefFunction1() {
{
const char code[] = "typedef void (*my_func)();\n"
Expand Down

0 comments on commit 8b518bf

Please sign in to comment.