From c8c25bb6a87bf0bcc9535507024c89af1a0c10e5 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 16 Mar 2026 09:41:57 +0100 Subject: [PATCH 1/5] Update testsimplifytypedef.cpp --- test/testsimplifytypedef.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 12ee24ee5e1..c3488475219 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -230,6 +230,7 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(simplifyTypedef157); TEST_CASE(simplifyTypedef158); TEST_CASE(simplifyTypedef159); + TEST_CASE(simplifyTypedef160); TEST_CASE(simplifyTypedefFunction1); TEST_CASE(simplifyTypedefFunction2); // ticket #1685 @@ -3812,6 +3813,28 @@ class TestSimplifyTypedef : public TestFixture { ASSERT_EQUALS(exp, tok(code)); } + void simplifyTypedef160() { + const char code[] = "struct S1 {};\n" + "typedef struct S1 S2;\n" + "namespace N {\n" + " struct B {\n" + " explicit B(int& i);\n" + " };\n" + " struct S2 : B {\n" + " explicit S2(int& i) : B(i) {}\n" + " };\n" + "}\n"; + const char exp[] = "struct S1 { } ; " + "namespace N { " + "struct B { " + "explicit B ( int & i ) ; } ; " + "struct S2 : B { " + "explicit S2 ( int & i ) : B ( i ) { } " + "} ;" + " }"; + ASSERT_EQUALS(exp, tok(code)); + } + void simplifyTypedefFunction1() { { const char code[] = "typedef void (*my_func)();\n" From 851b63d4d2f6ec96ba32b64f1f9df5a44b34557d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 16 Mar 2026 09:44:27 +0100 Subject: [PATCH 2/5] Update tokenize.cpp --- lib/tokenize.cpp | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 8ad49dedd37..bcd91f8fd65 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -103,14 +103,14 @@ static void skipEnumBody(T *&tok) /** * is tok the start brace { of a class, struct, union, or enum */ -static bool isClassStructUnionEnumStart(const Token * tok) +static const Token* isClassStructUnionEnumStart(const Token* tok) { if (!Token::Match(tok->previous(), "class|struct|union|enum|%name%|>|>> {")) - return false; + return nullptr; const Token * tok2 = tok->previous(); while (tok2 && !Token::Match(tok2, "class|struct|union|enum|{|}|)|;")) tok2 = tok2->previous(); - return Token::Match(tok2, "class|struct|union|enum") && !Token::simpleMatch(tok2->tokAt(-1), "->"); + return (Token::Match(tok2, "class|struct|union|enum") && !Token::simpleMatch(tok2->tokAt(-1), "->")) ? tok2 : nullptr; } //--------------------------------------------------------------------------- @@ -1028,6 +1028,14 @@ bool Tokenizer::isFunctionPointer(const Token* tok) { return Token::Match(tok, "%name% ) ("); } +static bool matchCurrentType(const std::string& typeStr, const std::map& types) +{ + auto it = std::find_if(types.begin(), types.end(), [&](const auto& element) { + return typeStr == element.second; + }); + return it != types.end(); +} + void Tokenizer::simplifyTypedef() { // Simplify global typedefs that are not redefined with the fast 1-pass simplification. @@ -1050,12 +1058,19 @@ void Tokenizer::simplifyTypedef() int indentlevel = 0; std::map typedefs; + std::map inType; for (Token* tok = list.front(); tok; tok = tok->next()) { if (!tok->isName()) { - if (tok->str()[0] == '{') + if (tok->str()[0] == '{') { ++indentlevel; - else if (tok->str()[0] == '}') + if (const Token* typeStart = isClassStructUnionEnumStart(tok)) { + inType.emplace(indentlevel, typeStart->strAt(1)); + } + } + else if (tok->str()[0] == '}') { + inType.erase(indentlevel); --indentlevel; + } continue; } @@ -1072,7 +1087,7 @@ void Tokenizer::simplifyTypedef() } auto it = typedefs.find(tok->str()); - if (it != typedefs.end() && it->second.canReplace(tok)) { + if (it != typedefs.end() && it->second.canReplace(tok) && !matchCurrentType(tok->str(), inType)) { std::set r; std::string originalname; while (it != typedefs.end() && r.insert(tok->str()).second) { From 2da4fb0ab11bc7a2d759e44c6549795167ec8738 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 16 Mar 2026 09:50:56 +0100 Subject: [PATCH 3/5] Update tokenize.cpp --- lib/tokenize.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index bcd91f8fd65..72db569d189 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1030,7 +1030,7 @@ bool Tokenizer::isFunctionPointer(const Token* tok) { static bool matchCurrentType(const std::string& typeStr, const std::map& types) { - auto it = std::find_if(types.begin(), types.end(), [&](const auto& element) { + auto it = std::find_if(types.begin(), types.end(), [&](const std::pair& element) { return typeStr == element.second; }); return it != types.end(); From 95130735012a293c665724a0f399311f3cb4ccb1 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 16 Mar 2026 12:32:59 +0100 Subject: [PATCH 4/5] Update tokenize.cpp --- lib/tokenize.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 72db569d189..a642bbd278b 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1030,10 +1030,9 @@ bool Tokenizer::isFunctionPointer(const Token* tok) { static bool matchCurrentType(const std::string& typeStr, const std::map& types) { - auto it = std::find_if(types.begin(), types.end(), [&](const std::pair& element) { + return std::any_of(types.begin(), types.end(), [&](const std::pair& element) { return typeStr == element.second; }); - return it != types.end(); } void Tokenizer::simplifyTypedef() From 036606beea1fdfa3e49e5c0b21d86fb474bdc1d6 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 19 Mar 2026 16:30:45 +0100 Subject: [PATCH 5/5] Format [skip ci] --- test/testsimplifytypedef.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index c3488475219..49934ab864c 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -3830,8 +3830,8 @@ class TestSimplifyTypedef : public TestFixture { "explicit B ( int & i ) ; } ; " "struct S2 : B { " "explicit S2 ( int & i ) : B ( i ) { } " - "} ;" - " }"; + "} ; " + "}"; ASSERT_EQUALS(exp, tok(code)); }