Skip to content

Commit 42a64d4

Browse files
Fix #12218 syntaxError with typedef in namespace (#5694)
1 parent 33981fe commit 42a64d4

2 files changed

Lines changed: 15 additions & 0 deletions

File tree

lib/tokenize.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,6 +1942,8 @@ void Tokenizer::simplifyTypedefCpp()
19421942
tok2 = tok2->next();
19431943
tok2->next()->insertToken("0");
19441944
}
1945+
if (Token::Match(tok2->tokAt(-1), "class|struct|union") && tok2->strAt(-1) == typeStart->str())
1946+
tok2->deletePrevious();
19451947
tok2->str(typeStart->str());
19461948

19471949
// restore qualification if it was removed

test/testsimplifytypedef.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class TestSimplifyTypedef : public TestFixture {
212212
TEST_CASE(simplifyTypedef146);
213213
TEST_CASE(simplifyTypedef147);
214214
TEST_CASE(simplifyTypedef148);
215+
TEST_CASE(simplifyTypedef149);
215216

216217
TEST_CASE(simplifyTypedefFunction1);
217218
TEST_CASE(simplifyTypedefFunction2); // ticket #1685
@@ -3443,6 +3444,18 @@ class TestSimplifyTypedef : public TestFixture {
34433444
ASSERT_EQUALS("int & r = i ;", tok(code));
34443445
}
34453446

3447+
void simplifyTypedef149() { // #12218
3448+
const char* code{};
3449+
code = "namespace N {\n"
3450+
" typedef struct S {} S;\n"
3451+
"}\n"
3452+
"void g(int);\n"
3453+
"void f() {\n"
3454+
" g(sizeof(struct N::S));\n"
3455+
"}\n";
3456+
ASSERT_EQUALS("namespace N { struct S { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( struct N :: S ) ) ; }", tok(code));
3457+
}
3458+
34463459
void simplifyTypedefFunction1() {
34473460
{
34483461
const char code[] = "typedef void (*my_func)();\n"

0 commit comments

Comments
 (0)