From 42a64d4d39f89036969bce17d7334ca65a564ec9 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Sat, 25 Nov 2023 22:59:51 +0100 Subject: [PATCH] Fix #12218 syntaxError with typedef in namespace (#5694) --- lib/tokenize.cpp | 2 ++ test/testsimplifytypedef.cpp | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 3cfef4049..7358a088f 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1942,6 +1942,8 @@ void Tokenizer::simplifyTypedefCpp() tok2 = tok2->next(); tok2->next()->insertToken("0"); } + if (Token::Match(tok2->tokAt(-1), "class|struct|union") && tok2->strAt(-1) == typeStart->str()) + tok2->deletePrevious(); tok2->str(typeStart->str()); // restore qualification if it was removed diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 89a38f394..d321026a3 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -212,6 +212,7 @@ private: TEST_CASE(simplifyTypedef146); TEST_CASE(simplifyTypedef147); TEST_CASE(simplifyTypedef148); + TEST_CASE(simplifyTypedef149); TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -3443,6 +3444,18 @@ private: ASSERT_EQUALS("int & r = i ;", tok(code)); } + void simplifyTypedef149() { // #12218 + const char* code{}; + code = "namespace N {\n" + " typedef struct S {} S;\n" + "}\n" + "void g(int);\n" + "void f() {\n" + " g(sizeof(struct N::S));\n" + "}\n"; + ASSERT_EQUALS("namespace N { struct S { } ; } void g ( int ) ; void f ( ) { g ( sizeof ( struct N :: S ) ) ; }", tok(code)); + } + void simplifyTypedefFunction1() { { const char code[] = "typedef void (*my_func)();\n"