From 986c28c341a33bde62bc0078c6491c77555d646c Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Tue, 14 Apr 2026 11:08:00 +0200 Subject: [PATCH 1/2] Fix #14669 Typedef not simplified: typedef struct T* T; --- lib/tokenize.cpp | 6 ++++-- test/testsimplifytypedef.cpp | 9 ++++++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index eae02b4aba2..978d656c262 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1028,8 +1028,10 @@ bool Tokenizer::isFunctionPointer(const Token* tok) { return Token::Match(tok, "%name% ) ("); } -static bool matchCurrentType(const std::string& typeStr, const std::map& types) +static bool matchCurrentType(const std::string& typeStr, const std::map& types, bool isC) { + if (isC) + return false; return std::any_of(types.begin(), types.end(), [&](const std::pair& element) { return typeStr == element.second; }); @@ -1086,7 +1088,7 @@ void Tokenizer::simplifyTypedef() } auto it = typedefs.find(tok->str()); - if (it != typedefs.end() && it->second.canReplace(tok) && !matchCurrentType(tok->str(), inType)) { + if (it != typedefs.end() && it->second.canReplace(tok) && !matchCurrentType(tok->str(), inType, tok->isC())) { std::set r; std::string originalname; while (it != typedefs.end() && r.insert(tok->str()).second) { diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 03ac9b45a9e..092c9e9df75 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -3824,7 +3824,7 @@ class TestSimplifyTypedef : public TestFixture { " explicit S2(int& i) : B(i) {}\n" " };\n" "}\n"; - const char exp[] = "struct S1 { } ; " + const char exp[] = "struct S1 { } ; " // #12623 "namespace N { " "struct B { " "explicit B ( int & i ) ; } ; " @@ -3833,6 +3833,13 @@ class TestSimplifyTypedef : public TestFixture { "} ; " "}"; ASSERT_EQUALS(exp, tok(code)); + + const char code2[] = "typedef stuct T* T;\n" // #14669 + "struct T {\n" + " T p;\n" + "};\n"; + const char exp2[] = "struct T { stuct T * p ; } ;"; + ASSERT_EQUALS(exp2, simplifyTypedefC(code2)); } void simplifyTypedefFunction1() { From a36cf81196ac80798c92c86edde1428675fc4e1c Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Tue, 14 Apr 2026 20:42:42 +0200 Subject: [PATCH 2/2] Refactor --- lib/tokenize.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 978d656c262..6b657a3cc45 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1028,12 +1028,12 @@ bool Tokenizer::isFunctionPointer(const Token* tok) { return Token::Match(tok, "%name% ) ("); } -static bool matchCurrentType(const std::string& typeStr, const std::map& types, bool isC) +static bool matchCurrentType(const Token* tok, std::map& types) { - if (isC) + if (tok->isC()) return false; return std::any_of(types.begin(), types.end(), [&](const std::pair& element) { - return typeStr == element.second; + return tok->str() == element.second; }); } @@ -1088,7 +1088,7 @@ void Tokenizer::simplifyTypedef() } auto it = typedefs.find(tok->str()); - if (it != typedefs.end() && it->second.canReplace(tok) && !matchCurrentType(tok->str(), inType, tok->isC())) { + if (it != typedefs.end() && it->second.canReplace(tok) && !matchCurrentType(tok, inType)) { std::set r; std::string originalname; while (it != typedefs.end() && r.insert(tok->str()).second) {