From 7a647e2f5f05cbc3d9f563387ebef8553ab68990 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Mon, 2 Nov 2026 16:16:37 +0100 Subject: [PATCH 1/5] fix --- lib/tokenize.cpp | 2 ++ lib/tokenlist.cpp | 2 ++ 2 files changed, 4 insertions(+) diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index a13b05cef62..f4b19014844 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1033,6 +1033,8 @@ namespace { to = to->next(); to->isSimplifiedTypedef(true); to->isExternC(from->isExternC()); + if (from->isExpandedMacro()) + to->setMacroName(from->getMacroName()); } return to; } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 26b24d649c8..20d0bc1400b 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -267,6 +267,8 @@ Token *TokenList::copyTokens(Token *dest, const Token *first, const Token *last, tok2->flags(tok->flags()); tok2->varId(tok->varId()); tok2->setTokenDebug(tok->getTokenDebug()); + if (tok->isExpandedMacro()) + tok2->setMacroName(tok->getMacroName()); // Check for links and fix them up if (Token::Match(tok2, "(|[|{")) From 5417dc00f09219bd2d3cbf7fdcae63aa95d5f390 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Mon, 2 Nov 2026 16:16:43 +0100 Subject: [PATCH 2/5] add test --- test/testsimplifytypedef.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/testsimplifytypedef.cpp b/test/testsimplifytypedef.cpp index 8ea0766c977..ef6c9a3a6f9 100644 --- a/test/testsimplifytypedef.cpp +++ b/test/testsimplifytypedef.cpp @@ -254,6 +254,7 @@ class TestSimplifyTypedef : public TestFixture { TEST_CASE(simplifyTypedefShadow); // #4445 - shadow variable TEST_CASE(simplifyTypedefMacro); + TEST_CASE(simplifyTypedefMacroName); TEST_CASE(simplifyTypedefOriginalName1); TEST_CASE(simplifyTypedefOriginalName2); @@ -4547,6 +4548,16 @@ class TestSimplifyTypedef : public TestFixture { simplifyTypedefP(code)); } + void simplifyTypedefMacroName() { + const char code[] = "#define A int\n" + "typedef void (*F)(A x);\n" + "void f(F g);\n"; + SimpleTokenizer2 tokenizer(settings0, *this, code, "test.c"); + tokenizer.createLinks(); + tokenizer.simplifyTypedef(); + ASSERT_EQUALS("void f ( void ( * g ) ( $int x ) ) ;", tokenizer.tokens()->stringifyList(false, true, false, false, false)); + } + void simplifyTypedefOriginalName1() { const char code[] = "typedef unsigned char uint8_t;\n" "typedef float (*rFunctionPointer_fp)(uint8_t, uint8_t);\n" From 9cd865e8aaa896de62faa5905921e344f518e611 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Thu, 5 Nov 2026 10:13:55 +0100 Subject: [PATCH 3/5] add test --- test/testsimplifyusing.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/testsimplifyusing.cpp b/test/testsimplifyusing.cpp index 239407f5c2f..82a123c6ac8 100644 --- a/test/testsimplifyusing.cpp +++ b/test/testsimplifyusing.cpp @@ -102,6 +102,7 @@ class TestSimplifyUsing : public TestFixture { TEST_CASE(simplifyUsing10720); TEST_CASE(simplifyUsing13873); // function declaration TEST_CASE(simplifyUsing14877); + TEST_CASE(simplifyUsingMacroName); TEST_CASE(scopeInfo1); TEST_CASE(scopeInfo2); @@ -1725,6 +1726,16 @@ class TestSimplifyUsing : public TestFixture { ASSERT_EQUALS(expected, tok(code)); } + void simplifyUsingMacroName() { + const char code[] = "#define A int\n" + "using F = void (*)(A x);\n" + "void f(F g);\n"; + const Settings settings = settingsBuilder(settings0).cpp(Standards::CPPLatest).build(); + SimpleTokenizer2 tokenizer(settings, *this, code, "test.cpp"); + ASSERT(tokenizer.simplifyTokens1("")); + ASSERT_EQUALS("void f ( void ( * g ) ( $int ) ) ;", tokenizer.tokens()->stringifyList(false, true, false, false, false)); + } + void scopeInfo1() { const char code[] = "struct A {\n" " enum class Mode { UNKNOWN, ENABLED, NONE, };\n" From 0087f08bd25b6269e057a2b725eb13d7276c10c6 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Fri, 6 Nov 2026 10:13:49 +0100 Subject: [PATCH 4/5] fix nits --- lib/token.cpp | 4 ++-- lib/token.h | 11 +++++++++++ lib/tokenize.cpp | 4 ++-- lib/tokenlist.cpp | 2 +- test/testother.cpp | 18 ++++++++++++++++++ 5 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/token.cpp b/lib/token.cpp index 3f8e2a88d4c..c501d187731 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -1282,7 +1282,7 @@ std::string Token::stringify(const stringifyOptions& options) const ret += "long "; } } - if (options.macro && isExpandedMacro()) + if (options.macro && hasMacroName()) ret += '$'; if (isName() && mStr.find(' ') != std::string::npos) { for (const char i : mStr) { @@ -1698,7 +1698,7 @@ static void indent(std::string &str, const nonneg int indent1, const nonneg int void Token::astStringVerboseRecursive(std::string& ret, const nonneg int indent1, const nonneg int indent2) const { - if (isExpandedMacro()) + if (hasMacroName()) ret += '$'; ret += mStr; if (mImpl->mValueType) diff --git a/lib/token.h b/lib/token.h index 5e200f63b9c..2049e3027d8 100644 --- a/lib/token.h +++ b/lib/token.h @@ -499,7 +499,18 @@ class CPPCHECKLIB Token { void isStandardType(const bool b) { setFlag(fIsStandardType, b); } + /** + * Is this token macro code at this location? Tokens copied from a + * typedef are excluded, see hasMacroName(). + */ bool isExpandedMacro() const { + return mImpl->mMacroName && !isSimplifiedTypedef(); + } + /** + * Did the token text originate from a macro? Also true for tokens copied + * from a typedef. Intended for dump output and copying tokens. + */ + bool hasMacroName() const { return !!mImpl->mMacroName; } bool isCast() const { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index f4b19014844..95b77b67b44 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1033,7 +1033,7 @@ namespace { to = to->next(); to->isSimplifiedTypedef(true); to->isExternC(from->isExternC()); - if (from->isExpandedMacro()) + if (from->hasMacroName()) to->setMacroName(from->getMacroName()); } return to; @@ -6396,7 +6396,7 @@ void Tokenizer::dump(std::ostream &out) const outs += " isCast=\"true\""; if (tok->isExternC()) outs += " externLang=\"C\""; - if (tok->isExpandedMacro()) + if (tok->hasMacroName()) outs += " macroName=\"" + tok->getMacroName() + "\""; if (tok->isTemplateArg()) { outs += " isTemplateArg=\"true\""; diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index 20d0bc1400b..a7304ad0252 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -267,7 +267,7 @@ Token *TokenList::copyTokens(Token *dest, const Token *first, const Token *last, tok2->flags(tok->flags()); tok2->varId(tok->varId()); tok2->setTokenDebug(tok->getTokenDebug()); - if (tok->isExpandedMacro()) + if (tok->hasMacroName()) tok2->setMacroName(tok->getMacroName()); // Check for links and fix them up diff --git a/test/testother.cpp b/test/testother.cpp index 085b39cfffb..7c62680a9e0 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -7565,6 +7565,16 @@ class TestOther : public TestFixture { " {}\n" "}\n"); ASSERT_EQUALS("", errout_str()); + + // typedef of macro, same typedef in both branches + checkP("#define INT int\n" + "typedef INT T;\n" + "void g(T);\n" + "void f(bool c) {\n" + " if (c) { T x = 1; g(x); }\n" + " else { T x = 1; g(x); }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:6:3] -> [test.cpp:5:3]: (style, inconclusive) Found duplicate branches for 'if' and 'else'. [duplicateBranch]\n", errout_str()); } void duplicateBranch2() { @@ -7691,6 +7701,14 @@ class TestOther : public TestFixture { } void duplicateExpression1() { + // typedef of macro is not macro code at the point of use + checkP("#define INT int\n" + "typedef INT T;\n" + "void foo(int a) {\n" + " if ((T)a == (T)a) { }\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:4:14]: (style) Same expression on both sides of '=='. [duplicateExpression]\n", errout_str()); + check("void foo(int a) {\n" " if (a == a) { }\n" "}\n"); From 6332bbf711c89b752c6d14c9617dcaaa64e35f89 Mon Sep 17 00:00:00 2001 From: William Jakobsson <50847546+wjakobsson@users.noreply.github.com> Date: Tue, 24 Nov 2026 11:23:13 +0100 Subject: [PATCH 5/5] use seperate flag for copied macro names --- lib/token.h | 14 +++++++++++--- lib/tokenize.cpp | 3 +-- lib/tokenlist.cpp | 3 +-- test/testtokenize.cpp | 10 ++++++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/token.h b/lib/token.h index 2049e3027d8..1381fcc0a0a 100644 --- a/lib/token.h +++ b/lib/token.h @@ -500,11 +500,12 @@ class CPPCHECKLIB Token { setFlag(fIsStandardType, b); } /** - * Is this token macro code at this location? Tokens copied from a - * typedef are excluded, see hasMacroName(). + * Is this token macro code at this location? Tokens whose macro name was + * copied from another token (e.g. a typedef definition) are excluded, + * see hasMacroName(). */ bool isExpandedMacro() const { - return mImpl->mMacroName && !isSimplifiedTypedef(); + return mImpl->mMacroName && !getFlag(fIsMacroNameCopied); } /** * Did the token text originate from a macro? Also true for tokens copied @@ -877,6 +878,12 @@ class CPPCHECKLIB Token { else *mImpl->mMacroName = std::move(name); } + void copyMacroName(const Token* from) { + if (from->hasMacroName()) { + setMacroName(from->getMacroName()); + setFlag(fIsMacroNameCopied, true); + } + } template static const Token *findsimplematch(const Token * const startTok, const char (&pattern)[count]) { @@ -1533,6 +1540,7 @@ class CPPCHECKLIB Token { fIsInitBracket = (1ULL << 44), // Is this bracket used as a part of variable initialization i.e: int a{5}, b(2); fIsAnonymous = (1ULL << 45), // Is this a token added for an unnamed member fIsInsertedBrace = (1ULL << 46), // brace added when simplifying e.g. if (x) f(); => if (x) { f(); } + fIsMacroNameCopied = (1ULL << 47), // macro name copied from another token, e.g. a typedef definition }; enum : std::uint8_t { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 95b77b67b44..d9b33cf16a2 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -1033,8 +1033,7 @@ namespace { to = to->next(); to->isSimplifiedTypedef(true); to->isExternC(from->isExternC()); - if (from->hasMacroName()) - to->setMacroName(from->getMacroName()); + to->copyMacroName(from); } return to; } diff --git a/lib/tokenlist.cpp b/lib/tokenlist.cpp index a7304ad0252..a750a51d400 100644 --- a/lib/tokenlist.cpp +++ b/lib/tokenlist.cpp @@ -267,8 +267,7 @@ Token *TokenList::copyTokens(Token *dest, const Token *first, const Token *last, tok2->flags(tok->flags()); tok2->varId(tok->varId()); tok2->setTokenDebug(tok->getTokenDebug()); - if (tok->hasMacroName()) - tok2->setMacroName(tok->getMacroName()); + tok2->copyMacroName(tok); // Check for links and fix them up if (Token::Match(tok2, "(|[|{")) diff --git a/test/testtokenize.cpp b/test/testtokenize.cpp index d527b98f62c..ef2759450aa 100644 --- a/test/testtokenize.cpp +++ b/test/testtokenize.cpp @@ -536,6 +536,7 @@ class TestTokenizer : public TestFixture { TEST_CASE(constFunctionPtrTypedef); // #12135 TEST_CASE(simplifyPlatformTypes); + TEST_CASE(platformTypeExpandedMacro); TEST_CASE(dumpFallthrough); TEST_CASE(dumpAttributeUsed); @@ -9390,6 +9391,15 @@ class TestTokenizer : public TestFixture { ASSERT_EQUALS("void ( * const f ) ( ) ;", tokenizeAndStringify("typedef void (*fp_t)(); fp_t const f;\n")); } + void platformTypeExpandedMacro() { + const char code[] = "#define A DWORD\n" + "A x;\n"; + SimpleTokenizer2 tokenizer(settings_win32a, *this, code, "test.c"); + ASSERT(tokenizer.simplifyTokens1("")); + const Token* tok = Token::findsimplematch(tokenizer.tokens(), "x"); + ASSERT(tok && tok->previous()->isExpandedMacro()); + } + void simplifyPlatformTypes() { { const char code[] = "size_t f();\n";