Fix #12218 syntaxError with typedef in namespace (#5694)

This commit is contained in:
chrchr-github 2023-11-25 22:59:51 +01:00 committed by GitHub
parent 33981fe42c
commit 42a64d4d39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 0 deletions

View File

@ -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

View File

@ -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"