From 94f45d430b99fb2a6574cf0303a9098d357e29d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 8 Nov 2023 19:32:32 +0100 Subject: [PATCH 1/3] test --- test/testsimplifytypedef.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index af51e0171dc..e6075c73601 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -52,6 +52,7 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(cstruct3); TEST_CASE(cstruct3); TEST_CASE(cstruct4); + TEST_CASE(cenum1); TEST_CASE(cfunction1); TEST_CASE(cfunction2); TEST_CASE(cfunction3); @@ -388,6 +389,12 @@ class TestSimplifyTypedef : public TestFixture { ASSERT_EQUALS("struct s { int a ; int b ; } ; struct s x { } ;", simplifyTypedefC(code)); } + void cenum1() { + const char code[] = "typedef enum { a, b } E;\n" + "E e;"; + ASSERT_EQUALS("123", simplifyTypedefC(code)); + } + void cfunction1() { const char code[] = "typedef int callback(int);\n" "callback* cb;"; From eb8fbddddf41b89a6338515240b9fc7e3b0587d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 8 Nov 2023 20:17:05 +0100 Subject: [PATCH 2/3] Fixed #12165 (simplifyTypedef: the "enum" token is not inserted) --- lib/tokenize.cpp | 4 +++- test/testcondition.cpp | 11 ++++++++++- test/testsimplifytypedef.cpp | 20 ++++++++++---------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index d6571b68d47..550a423c0f6 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -630,7 +630,7 @@ namespace { start = start->next(); // TODO handle unnamed structs etc - if (Token::Match(start, "const| enum|struct|union|class %name% {")) { + if (Token::Match(start, "const| enum|struct|union|class %name%| {")) { const std::pair rangeBefore(start, Token::findsimplematch(start, "{")); // find typedef name token @@ -640,6 +640,8 @@ namespace { const std::pair rangeQualifiers(rangeBefore.second->link()->next(), nameToken); if (Token::Match(nameToken, "%name% ;")) { + if (Token::Match(rangeBefore.second->previous(), "enum|struct|union|class {")) + rangeBefore.second->previous()->insertToken(nameToken->str()); mRangeType = rangeBefore; mRangeTypeQualifiers = rangeQualifiers; Token* typeName = rangeBefore.second->previous(); diff --git a/test/testcondition.cpp b/test/testcondition.cpp index d603bd5edbc..e89031a818a 100644 --- a/test/testcondition.cpp +++ b/test/testcondition.cpp @@ -5025,7 +5025,7 @@ class TestCondition : public TestFixture { } // #9353 - check("typedef struct { std::string s; } X;\n" + check("struct X { std::string s; };\n" "void f(const std::vector&v) {\n" " for (std::vector::const_iterator it = v.begin(); it != v.end(); ++it)\n" " if (!it->s.empty()) {\n" @@ -5034,6 +5034,15 @@ class TestCondition : public TestFixture { "}\n"); ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style) Condition '!it->s.empty()' is always true\n", errout.str()); + check("struct X { std::string s; };\n" + "void f(const std::vector&v) {\n" + " for (std::vector::const_iterator it = v.begin(); it != v.end(); ++it)\n" + " if (!it->s.empty()) {\n" + " if (!it->s.empty()) {}\n" + " }\n" + "}\n"); + TODO_ASSERT_EQUALS("[test.cpp:4] -> [test.cpp:5]: (style) Condition '!it->s.empty()' is always true\n", "", errout.str()); + // #10508 check("bool f(const std::string& a, const std::string& b) {\n" " return a.empty() || (b.empty() && a.empty());\n" diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index e6075c73601..192ea343a9d 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -373,8 +373,8 @@ class TestSimplifyTypedef : public TestFixture { void cstruct2() { const char code[] = "typedef enum { A, B } t;\n" "t x;"; - ASSERT_EQUALS("enum t { A , B } ; t x ;", simplifyTypedef(code)); - ASSERT_EQUALS("enum t { A , B } ; t x ;", simplifyTypedefC(code)); + ASSERT_EQUALS("enum t { A , B } ; enum t x ;", simplifyTypedef(code)); + ASSERT_EQUALS("enum t { A , B } ; enum t x ;", simplifyTypedefC(code)); } void cstruct3() { @@ -392,7 +392,7 @@ class TestSimplifyTypedef : public TestFixture { void cenum1() { const char code[] = "typedef enum { a, b } E;\n" "E e;"; - ASSERT_EQUALS("123", simplifyTypedefC(code)); + ASSERT_EQUALS("enum E { a , b } ; enum E e ;", simplifyTypedefC(code)); } void cfunction1() { @@ -701,13 +701,13 @@ class TestSimplifyTypedef : public TestFixture { const char expected[] = "struct t { int a ; } ; " "struct U { int a ; } ; " - "struct Unnamed0 { int a ; } ; " + "struct V { int a ; } ; " "struct s s ; " "struct s * ps ; " "struct t t ; " "struct t * tp ; " "struct U u ; " - "struct Unnamed0 * v ;"; + "struct V * v ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -727,13 +727,13 @@ class TestSimplifyTypedef : public TestFixture { const char expected[] = "union t { int a ; float b ; } ; " "union U { int a ; float b ; } ; " - "union Unnamed0 { int a ; float b ; } ; " + "union V { int a ; float b ; } ; " "union s s ; " "union s * ps ; " "union t t ; " "union t * tp ; " "union U u ; " - "union Unnamed0 * v ;"; + "union V * v ;"; ASSERT_EQUALS(expected, tok(code, false)); } @@ -746,7 +746,7 @@ class TestSimplifyTypedef : public TestFixture { const char expected[] = "enum abc { a = 0 , b = 1 , c = 2 } ; " "enum xyz { x = 0 , y = 1 , z = 2 } ; " - "abc e1 ; " + "enum abc e1 ; " "enum xyz e2 ;"; ASSERT_EQUALS(expected, tok(code, false)); @@ -1844,7 +1844,7 @@ class TestSimplifyTypedef : public TestFixture { " localEntitiyAddFunc_t f;\n" "}"; // The expected result.. - const char expected[] = "enum qboolean { qfalse , qtrue } ; void f ( ) { qboolean b ; qboolean ( * f ) ( struct le_s * , entity_t * ) ; }"; + const char expected[] = "enum qboolean { qfalse , qtrue } ; void f ( ) { enum qboolean b ; enum qboolean ( * f ) ( struct le_s * , entity_t * ) ; }"; ASSERT_EQUALS(expected, tok(code, false)); ASSERT_EQUALS("", errout.str()); } @@ -3335,7 +3335,7 @@ class TestSimplifyTypedef : public TestFixture { void simplifyTypedef144() { // #9353 const char code[] = "typedef struct {} X;\n" "std::vector v;\n"; - ASSERT_EQUALS("struct X { } ; std :: vector < X > v ;", tok(code)); + ASSERT_EQUALS("struct X { } ; std :: vector < struct X > v ;", tok(code)); } void simplifyTypedef145() { From 72e0a7ecc8d48a0874f5072fb2528d24ee964990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Marjam=C3=A4ki?= Date: Wed, 8 Nov 2023 22:03:39 +0100 Subject: [PATCH 3/3] fix --- lib/tokenize.cpp | 2 +- test/testsimplifytypedef.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 550a423c0f6..63f5c850f1c 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -776,7 +776,7 @@ namespace { } // Inherited type => skip "struct" / "class" - if (Token::Match(mRangeType.first, "const| struct|class %name% {") && Token::Match(tok->previous(), "public|protected|private")) { + if (Token::Match(mRangeType.first, "const| struct|class %name% {") && Token::Match(tok->previous(), "public|protected|private|<")) { tok->originalName(tok->str()); tok->str(mRangeType.second->previous()->str()); return; diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 192ea343a9d..7d4eb221b99 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -3335,7 +3335,7 @@ class TestSimplifyTypedef : public TestFixture { void simplifyTypedef144() { // #9353 const char code[] = "typedef struct {} X;\n" "std::vector v;\n"; - ASSERT_EQUALS("struct X { } ; std :: vector < struct X > v ;", tok(code)); + ASSERT_EQUALS("struct X { } ; std :: vector < X > v ;", tok(code)); } void simplifyTypedef145() {