Skip to content

Commit fe4fac7

Browse files
committed
Fixed cppcheck-opensource#7058 (Tokenizer::simplifyTypedef: wrong simplification of enum constant 'AB::A' if AB is a struct typedef)
1 parent ec87b09 commit fe4fac7

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

lib/tokenize.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,7 @@ void Tokenizer::simplifyTypedef()
12011201
tok2 = tok2->next();
12021202
}
12031203
}
1204-
} else if (tok2->tokAt(-2) && Token::Match(tok2->tokAt(-2), "%type% *|&")) {
1204+
} else if (Token::Match(tok2->tokAt(-2), "%type% *|&")) {
12051205
// Ticket #5868: Don't substitute variable names
12061206
} else if (tok2->previous()->str() != ".") {
12071207
simplifyType = true;
@@ -1264,6 +1264,8 @@ void Tokenizer::simplifyTypedef()
12641264
structRemoved = true;
12651265
typeStart = typeStart->next();
12661266
}
1267+
if (typeStart->str() == "struct" && Token::Match(tok2, "%name% ::"))
1268+
typeStart = typeStart->next();
12671269

12681270
// start substituting at the typedef name by replacing it with the type
12691271
tok2->str(typeStart->str());

test/testsimplifytypedef.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ class TestSimplifyTypedef : public TestFixture {
150150
TEST_CASE(simplifyTypedef111); // ticket #6345
151151
TEST_CASE(simplifyTypedef112); // ticket #6048
152152
TEST_CASE(simplifyTypedef113); // ticket #7030
153+
TEST_CASE(simplifyTypedef114); // ticket #7058 - skip "struct", AB::..
153154

154155
TEST_CASE(simplifyTypedefFunction1);
155156
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
@@ -2414,6 +2415,13 @@ class TestSimplifyTypedef : public TestFixture {
24142415
ASSERT_EQUALS(expected, tok(code));
24152416
}
24162417

2418+
void simplifyTypedef114() { // ticket #7058
2419+
const char code[] = "typedef struct { enum {A,B}; } AB;\n"
2420+
"x=AB::B;";
2421+
const char expected[] = "struct AB { } ; x = 1 ;";
2422+
ASSERT_EQUALS(expected, tok(code));
2423+
}
2424+
24172425
void simplifyTypedefFunction1() {
24182426
{
24192427
const char code[] = "typedef void (*my_func)();\n"

0 commit comments

Comments
 (0)