From c553acf2286379e5edfb19c4d128db41349c4a5c Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 27 Apr 2024 14:41:55 +0200 Subject: [PATCH 1/2] Fix #12647 Repeated typedef not handled --- lib/tokenize.cpp | 3 ++- test/testsimplifytypedef.cpp | 13 ++++++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 5b63400d226..d1058b496e2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1072,7 +1072,8 @@ void Tokenizer::simplifyTypedef() if (indentlevel == 0 && tok->str() == "typedef") { TypedefSimplifier ts(tok); - if (!ts.fail() && numberOfTypedefs[ts.name()] == 1) { + if (!ts.fail() && numberOfTypedefs[ts.name()] == 1 && + (numberOfTypedefs.find(ts.getTypedefToken()->strAt(1)) == numberOfTypedefs.end() || ts.getTypedefToken()->strAt(2) == "(")) { if (mSettings.severity.isEnabled(Severity::portability) && ts.isInvalidConstFunctionType(typedefs)) reportError(tok->next(), Severity::portability, "invalidConstFunctionType", "It is unspecified behavior to const qualify a function type."); diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index c21ab199e0c..7d1a59e1a89 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -216,6 +216,7 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(simplifyTypedef150); TEST_CASE(simplifyTypedef151); TEST_CASE(simplifyTypedef152); + TEST_CASE(simplifyTypedef153); TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -1226,7 +1227,7 @@ class TestSimplifyTypedef : public TestFixture { "LPCSTR ccp;"; const char expected[] = - "char c ; " + "; char c ; " "char * cp ; " "const char * ccp ;"; @@ -3563,6 +3564,16 @@ class TestSimplifyTypedef : public TestFixture { ASSERT_EQUALS(exp, tok(code)); } + void simplifyTypedef153() { + const char* code{}, *exp{}; // #12647 + code = "typedef unsigned long X;\n" + "typedef unsigned long X;\n" + "typedef X Y;\n" + "Y y;\n"; + exp = "long y ;"; + ASSERT_EQUALS(exp, tok(code)); + } + void simplifyTypedefFunction1() { { const char code[] = "typedef void (*my_func)();\n" From 749a8eb49cb657713191830fe6347605bf4b21d9 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Sat, 27 Apr 2024 14:58:52 +0200 Subject: [PATCH 2/2] Fix --- test/testgarbage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testgarbage.cpp b/test/testgarbage.cpp index ca8e981fd1c..30d0e617fff 100644 --- a/test/testgarbage.cpp +++ b/test/testgarbage.cpp @@ -725,7 +725,7 @@ class TestGarbage : public TestFixture { } void garbageCode63() { // #6739 - 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); + 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); } void garbageCode64() { // #6740