From 0a964c75b786e68b8fe9584904ea1bafdc7c1a72 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 14 Oct 2024 22:12:29 +0200 Subject: [PATCH 1/3] Fix #13218 FP duplicateExpression with nested defines --- lib/astutils.cpp | 8 ++++++-- test/testother.cpp | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/astutils.cpp b/lib/astutils.cpp index 2eb998fb8e3..0ebdd33aeee 100644 --- a/lib/astutils.cpp +++ b/lib/astutils.cpp @@ -1547,8 +1547,12 @@ bool compareTokenFlags(const Token* tok1, const Token* tok2, bool macro) { if (macro) { if (tok1->isExpandedMacro() != tok2->isExpandedMacro()) return false; - if (tok1->isExpandedMacro() && tok1->getMacroName() != tok2->getMacroName()) - return false; + if (tok1->isExpandedMacro()) { // both are macros + if (tok1->getMacroName() != tok2->getMacroName()) + return false; + if (tok1->astParent() && tok2->astParent() && tok1->astParent()->isExpandedMacro() && tok1->astParent()->getMacroName() == tok2->astParent()->getMacroName()) + return false; + } if (tok1->isTemplateArg() || tok2->isTemplateArg()) return false; } diff --git a/test/testother.cpp b/test/testother.cpp index 8f7fce676ec..591e90899d9 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -177,6 +177,7 @@ class TestOther : public TestFixture { TEST_CASE(duplicateExpression15); // #10650 TEST_CASE(duplicateExpression16); // #10569 TEST_CASE(duplicateExpression17); // #12036 + TEST_CASE(duplicateExpression18); TEST_CASE(duplicateExpressionLoop); TEST_CASE(duplicateValueTernary); TEST_CASE(duplicateExpressionTernary); // #6391 @@ -7485,6 +7486,24 @@ class TestOther : public TestFixture { ASSERT_EQUALS("", errout_str()); } + void duplicateExpression18() { + checkP("#if defined(ABC)\n" // #13218 + "#define MACRO1 (0x1)\n" + "#else\n" + "#define MACRO1 (0)\n" + "#endif\n" + "#if defined(XYZ)\n" + "#define MACRO2 (0x2)\n" + "#else\n" + "#define MACRO2 (0)\n" + "#endif\n" + "#define MACRO_ALL (MACRO1 | MACRO2)\n" + "void f() {\n" + " if (MACRO_ALL == 0) {}\n" + "}\n"); + ASSERT_EQUALS("", errout_str()); + } + void duplicateExpressionLoop() { check("void f() {\n" " int a = 1;\n" From 3c2ff4fe35834bad81d26fe2b4482a59ca1ff30f Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Mon, 14 Oct 2024 22:18:01 +0200 Subject: [PATCH 2/3] Update testother.cpp --- test/testother.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/testother.cpp b/test/testother.cpp index 591e90899d9..53210355aee 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -7500,7 +7500,7 @@ class TestOther : public TestFixture { "#define MACRO_ALL (MACRO1 | MACRO2)\n" "void f() {\n" " if (MACRO_ALL == 0) {}\n" - "}\n"); + "}\n"); ASSERT_EQUALS("", errout_str()); } From d302b61d5487c659bf59938b634a7ee6c19a0a8f Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Mon, 14 Oct 2024 22:40:02 +0200 Subject: [PATCH 3/3] TODO --- test/cfg/bsd.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/cfg/bsd.c b/test/cfg/bsd.c index 4727ea6f542..6bc65d641cb 100644 --- a/test/cfg/bsd.c +++ b/test/cfg/bsd.c @@ -32,15 +32,15 @@ void nullPointer_setlinebuf(FILE *stream) // #9323, #9331 void verify_timercmp(struct timeval t) { - // cppcheck-suppress duplicateExpression + // TODO cppcheck-suppress duplicateExpression (void)timercmp(&t, &t, <); - // cppcheck-suppress duplicateExpression + // TODO cppcheck-suppress duplicateExpression (void)timercmp(&t, &t, <=); (void)timercmp(&t, &t, ==); (void)timercmp(&t, &t, !=); - // cppcheck-suppress duplicateExpression + // TODO cppcheck-suppress duplicateExpression (void)timercmp(&t, &t, >=); - // cppcheck-suppress duplicateExpression + // TODO cppcheck-suppress duplicateExpression (void)timercmp(&t, &t, >); }