Skip to content

Commit 8b518bf

Browse files
Partial fix for #12014 Outer typedef incorrectly applied within namespace (#6288)
1 parent c6ae9ae commit 8b518bf

2 files changed

Lines changed: 37 additions & 5 deletions

File tree

lib/tokenize.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,18 @@ void Tokenizer::simplifyTypedef()
11251125
simplifyTypedefCpp();
11261126
}
11271127

1128+
static bool isEnumScope(const Token* tok)
1129+
{
1130+
if (!Token::simpleMatch(tok, "{"))
1131+
return false;
1132+
tok = tok->previous();
1133+
while (tok && !tok->isKeyword() && Token::Match(tok, "%name%|::|:"))
1134+
tok = tok->previous();
1135+
if (Token::simpleMatch(tok, "class"))
1136+
tok = tok->previous();
1137+
return Token::simpleMatch(tok, "enum");
1138+
}
1139+
11281140
void Tokenizer::simplifyTypedefCpp()
11291141
{
11301142
bool isNamespace = false;
@@ -1622,7 +1634,7 @@ void Tokenizer::simplifyTypedefCpp()
16221634
bool globalScope = false;
16231635
int classLevel = spaceInfo.size();
16241636
bool inTypeDef = false;
1625-
bool inEnumClass = false;
1637+
bool inEnum = false;
16261638
std::string removed;
16271639
std::string classPath;
16281640
for (size_t i = 1; i < spaceInfo.size(); ++i) {
@@ -1676,7 +1688,7 @@ void Tokenizer::simplifyTypedefCpp()
16761688
if (memberScope == 0)
16771689
inMemberFunc = false;
16781690
}
1679-
inEnumClass = false;
1691+
inEnum = false;
16801692

16811693
if (classLevel > 1 && tok2 == spaceInfo[classLevel - 1].bodyEnd2) {
16821694
--classLevel;
@@ -1736,8 +1748,8 @@ void Tokenizer::simplifyTypedefCpp()
17361748
}
17371749
++scope;
17381750
}
1739-
if (Token::Match(tok2->tokAt(-3), "enum class %name%"))
1740-
inEnumClass = true;
1751+
if (isEnumScope(tok2))
1752+
inEnum = true;
17411753
}
17421754

17431755
// keep track of scopes within member function
@@ -1863,7 +1875,7 @@ void Tokenizer::simplifyTypedefCpp()
18631875
}
18641876
}
18651877

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

18691881
if (simplifyType) {

test/testsimplifytypedef.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ class TestSimplifyTypedef : public TestFixture {
215215
TEST_CASE(simplifyTypedef149);
216216
TEST_CASE(simplifyTypedef150);
217217
TEST_CASE(simplifyTypedef151);
218+
TEST_CASE(simplifyTypedef152);
218219

219220
TEST_CASE(simplifyTypedefFunction1);
220221
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
@@ -3543,6 +3544,25 @@ class TestSimplifyTypedef : public TestFixture {
35433544
ASSERT_EQUALS(exp, tok(code));
35443545
}
35453546

3547+
void simplifyTypedef152() {
3548+
const char* code{}, *exp{};
3549+
code = "namespace O { struct T {}; }\n"
3550+
"typedef O::T S;\n"
3551+
"namespace M {\n"
3552+
" enum E { E0, S = 1 };\n"
3553+
" enum class F : ::std::int8_t { F0, S = 1 };\n"
3554+
"}\n"
3555+
"namespace N { enum { G0, S = 1 }; }\n";
3556+
exp = "namespace O { struct T { } ; } "
3557+
"namespace M { "
3558+
"enum E { E0 , S = 1 } ; "
3559+
"enum class F : :: std :: int8_t { F0 , S = 1 } ; "
3560+
"}"
3561+
" namespace N { enum Anonymous0 { G0 , S = 1 } ; "
3562+
"}";
3563+
ASSERT_EQUALS(exp, tok(code));
3564+
}
3565+
35463566
void simplifyTypedefFunction1() {
35473567
{
35483568
const char code[] = "typedef void (*my_func)();\n"

0 commit comments

Comments
 (0)