Skip to content

Commit

Permalink
Fix FP incorrectStringBooleanError with macro (#5358)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github committed Aug 22, 2023
1 parent 05a2d88 commit af46c68
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion lib/checkstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down
25 changes: 21 additions & 4 deletions test/teststring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "fixture.h"
#include "tokenize.h"

#include <simplecpp.h>

#include <sstream> // IWYU pragma: keep


Expand Down Expand Up @@ -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<std::string> files(1, filename);
std::istringstream istr(code);
const simplecpp::TokenList tokens1(istr, files, files[0]);

// Preprocess..
simplecpp::TokenList tokens2(files);
std::map<std::string, simplecpp::TokenList*> 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<CheckString>(tokenizer, this);
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit af46c68

Please sign in to comment.