Skip to content

Commit fae65de

Browse files
Fix #12647 Repeated typedef not handled (danmar#6355)
1 parent 5d0aa2a commit fae65de

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

lib/tokenize.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,8 @@ void Tokenizer::simplifyTypedef()
10771077

10781078
if (indentlevel == 0 && tok->str() == "typedef") {
10791079
TypedefSimplifier ts(tok);
1080-
if (!ts.fail() && numberOfTypedefs[ts.name()] == 1) {
1080+
if (!ts.fail() && numberOfTypedefs[ts.name()] == 1 &&
1081+
(numberOfTypedefs.find(ts.getTypedefToken()->strAt(1)) == numberOfTypedefs.end() || ts.getTypedefToken()->strAt(2) == "(")) {
10811082
if (mSettings.severity.isEnabled(Severity::portability) && ts.isInvalidConstFunctionType(typedefs))
10821083
reportError(tok->next(), Severity::portability, "invalidConstFunctionType",
10831084
"It is unspecified behavior to const qualify a function type.");

test/testgarbage.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -725,7 +725,7 @@ class TestGarbage : public TestFixture {
725725
}
726726

727727
void garbageCode63() { // #6739
728-
ASSERT_THROW_INTERNAL(checkCode("{ } { } typedef int u_array[]; typedef u_array &u_array_ref; (u_array_ref arg) { } u_array_ref u_array_ref_gbl_obj0"), INTERNAL);
728+
ASSERT_THROW_INTERNAL(checkCode("{ } { } typedef int u_array[]; typedef u_array &u_array_ref; (u_array_ref arg) { } u_array_ref u_array_ref_gbl_obj0"), SYNTAX);
729729
}
730730

731731
void garbageCode64() { // #6740

test/testsimplifytypedef.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class TestSimplifyTypedef : public TestFixture {
216216
TEST_CASE(simplifyTypedef150);
217217
TEST_CASE(simplifyTypedef151);
218218
TEST_CASE(simplifyTypedef152);
219+
TEST_CASE(simplifyTypedef153);
219220

220221
TEST_CASE(simplifyTypedefFunction1);
221222
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
@@ -1226,7 +1227,7 @@ class TestSimplifyTypedef : public TestFixture {
12261227
"LPCSTR ccp;";
12271228

12281229
const char expected[] =
1229-
"char c ; "
1230+
"; char c ; "
12301231
"char * cp ; "
12311232
"const char * ccp ;";
12321233

@@ -3563,6 +3564,16 @@ class TestSimplifyTypedef : public TestFixture {
35633564
ASSERT_EQUALS(exp, tok(code));
35643565
}
35653566

3567+
void simplifyTypedef153() {
3568+
const char* code{}, *exp{}; // #12647
3569+
code = "typedef unsigned long X;\n"
3570+
"typedef unsigned long X;\n"
3571+
"typedef X Y;\n"
3572+
"Y y;\n";
3573+
exp = "long y ;";
3574+
ASSERT_EQUALS(exp, tok(code));
3575+
}
3576+
35663577
void simplifyTypedefFunction1() {
35673578
{
35683579
const char code[] = "typedef void (*my_func)();\n"

0 commit comments

Comments
 (0)