From 272fb64163129e4ca4a04e8a56868635bd606c64 Mon Sep 17 00:00:00 2001 From: chrchr Date: Tue, 22 Aug 2023 13:09:12 +0200 Subject: [PATCH 1/2] Fix FP incorrectStringBooleanError with macro --- lib/checkstring.cpp | 4 +++- test/teststring.cpp | 25 +++++++++++++++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/checkstring.cpp b/lib/checkstring.cpp index c9965b53e51..e58f065185c 100644 --- a/lib/checkstring.cpp +++ b/lib/checkstring.cpp @@ -288,7 +288,9 @@ void CheckString::checkIncorrectStringCompare() incorrectStringCompareError(tok->next(), "substr", end->strAt(1)); } } - } else if (Token::Match(tok, "%str%|%char%") && isUsedAsBool(tok)) + } else if (Token::Match(tok, "%str%|%char%") && + !(tok->astParent() && tok->astParent()->isExpandedMacro()) && + isUsedAsBool(tok)) incorrectStringBooleanError(tok, tok->str()); } } diff --git a/test/teststring.cpp b/test/teststring.cpp index 87ffcccaaf0..56e886afdc1 100644 --- a/test/teststring.cpp +++ b/test/teststring.cpp @@ -23,6 +23,8 @@ #include "fixture.h" #include "tokenize.h" +#include + #include // IWYU pragma: keep @@ -60,15 +62,24 @@ class TestString : public TestFixture { TEST_CASE(deadStrcmp); } -#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__) - void check_(const char* file, int line, const char code[], const char filename[] = "test.cpp") { + void check(const char code[], const char filename[] = "test.cpp") { // Clear the error buffer.. errout.str(""); + // Raw tokens.. + std::vector files(1, filename); + std::istringstream istr(code); + const simplecpp::TokenList tokens1(istr, files, files[0]); + + // Preprocess.. + simplecpp::TokenList tokens2(files); + std::map filedata; + simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI()); + // Tokenize.. Tokenizer tokenizer(&settings, this); - std::istringstream istr(code); - ASSERT_LOC(tokenizer.tokenize(istr, filename), file, line); + tokenizer.createTokens(std::move(tokens2)); + tokenizer.simplifyTokens1(""); // Check char variable usage.. runChecks(tokenizer, this); @@ -768,6 +779,12 @@ class TestString : public TestFixture { ASSERT_EQUALS("[test.cpp:3]: (warning) Conversion of char literal '\\0' to bool always evaluates to false.\n" "[test.cpp:4]: (warning) Conversion of char literal 'a' to bool always evaluates to true.\n", errout.str()); + + check("#define ERROR(msg) if (msg) printf(\"%s\\n\", msg);\n" + "void f() {\n" + " ERROR(\"abc\")\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); } void deadStrcmp() { From 668f9bc298488f531026577bca543411ba1c94fd Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 22 Aug 2023 15:51:08 +0200 Subject: [PATCH 2/2] Update Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d1256debd8e..1923c9ac4b5 100644 --- a/Makefile +++ b/Makefile @@ -821,7 +821,7 @@ test/testsizeof.o: test/testsizeof.cpp externals/simplecpp/simplecpp.h lib/check test/teststl.o: test/teststl.cpp lib/check.h lib/checkstl.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/teststl.cpp -test/teststring.o: test/teststring.cpp lib/check.h lib/checkstring.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h +test/teststring.o: test/teststring.cpp externals/simplecpp/simplecpp.h lib/check.h lib/checkstring.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h $(CXX) ${INCLUDE_FOR_TEST} $(CPPFLAGS) $(CXXFLAGS) -c -o $@ test/teststring.cpp test/testsummaries.o: test/testsummaries.cpp lib/check.h lib/color.h lib/config.h lib/errorlogger.h lib/errortypes.h lib/importproject.h lib/library.h lib/mathlib.h lib/platform.h lib/settings.h lib/standards.h lib/summaries.h lib/suppressions.h lib/templatesimplifier.h lib/token.h lib/tokenize.h lib/tokenlist.h lib/utils.h lib/vfvalue.h test/fixture.h