Skip to content

Commit c0b4c73

Browse files
authored
fix #14106: Using Anonymous enum as a return type for a function creates variable (danmar#7789)
1 parent 749e98a commit c0b4c73

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/tokenize.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9117,7 +9117,7 @@ static bool isAnonymousEnum(const Token* tok)
91179117
while (Token::Match(end, "%name%|::"))
91189118
end = end->next();
91199119
}
9120-
return end && Token::Match(end->link(), "} (| %type%| )| [,;[({=]");
9120+
return end && Token::Match(end->link(), "} (| %type%| )| [*,;[({=]");
91219121
}
91229122

91239123
void Tokenizer::simplifyStructDecl()
@@ -9218,7 +9218,8 @@ void Tokenizer::simplifyStructDecl()
92189218
}
92199219

92209220
// check for initialization
9221-
if (Token::Match(after, "%any% (|{")) {
9221+
bool isFuncDecl = Token::Match(after, "%name% (") && Token::simpleMatch(after->linkAt(1), ") {");
9222+
if (Token::Match(after, "%any% (|{") && !isFuncDecl) {
92229223
after->insertToken("=");
92239224
after = after->next();
92249225
const bool isEnum = start->str() == "enum";

test/testtokenize.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ class TestTokenizer : public TestFixture {
218218
TEST_CASE(vardecl29); // #9282
219219
TEST_CASE(vardecl30);
220220
TEST_CASE(vardecl31); // function pointer init
221+
TEST_CASE(vardecl32);
222+
TEST_CASE(vardecl33);
223+
TEST_CASE(vardecl34);
221224
TEST_CASE(vardecl_stl_1);
222225
TEST_CASE(vardecl_stl_2);
223226
TEST_CASE(vardecl_stl_3);
@@ -2771,6 +2774,27 @@ class TestTokenizer : public TestFixture {
27712774
}
27722775
}
27732776

2777+
void vardecl32() {
2778+
{
2779+
const char code[] = "static enum { E } f() { return E; }";
2780+
ASSERT_EQUALS("enum Anonymous0 { E } ; static enum Anonymous0 f ( ) { return E ; }", tokenizeAndStringify(code, true, Platform::Type::Native, false));
2781+
}
2782+
}
2783+
2784+
void vardecl33() {
2785+
{
2786+
const char code[] = "static enum { E } *f() { return NULL; }";
2787+
ASSERT_EQUALS("enum Anonymous0 { E } ; static enum Anonymous0 * f ( ) { return NULL ; }", tokenizeAndStringify(code, true, Platform::Type::Native, false));
2788+
}
2789+
}
2790+
2791+
void vardecl34() {
2792+
{
2793+
const char code[] = "static enum { E } const *f() { return NULL; }";
2794+
ASSERT_EQUALS("enum Anonymous0 { E } ; static enum Anonymous0 const * f ( ) { return NULL ; }", tokenizeAndStringify(code, true, Platform::Type::Native, false));
2795+
}
2796+
}
2797+
27742798
void volatile_variables() {
27752799
{
27762800
const char code[] = "volatile int a=0;\n"

0 commit comments

Comments
 (0)