Skip to content

Commit

Permalink
Fix #13202 FP oppositeInnerCondition for string defines (#6889)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Oct 11, 2024
1 parent 45d34fb commit c3e3387
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/astutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,8 +1544,14 @@ bool isUsedAsBool(const Token* const tok, const Settings& settings)
}

bool compareTokenFlags(const Token* tok1, const Token* tok2, bool macro) {
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
return false;
if (macro) {
if (tok1->isExpandedMacro() != tok2->isExpandedMacro())
return false;
if (tok1->isExpandedMacro() && tok1->getMacroName() != tok2->getMacroName())
return false;
if (tok1->isTemplateArg() || tok2->isTemplateArg())
return false;
}
if (tok1->isComplex() != tok2->isComplex())
return false;
if (tok1->isLong() != tok2->isLong())
Expand Down
4 changes: 4 additions & 0 deletions test/cfg/bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ void nullPointer_setlinebuf(FILE *stream)
// #9323, #9331
void verify_timercmp(struct timeval t)
{
// cppcheck-suppress duplicateExpression
(void)timercmp(&t, &t, <);
// cppcheck-suppress duplicateExpression
(void)timercmp(&t, &t, <=);
(void)timercmp(&t, &t, ==);
(void)timercmp(&t, &t, !=);
// cppcheck-suppress duplicateExpression
(void)timercmp(&t, &t, >=);
// cppcheck-suppress duplicateExpression
(void)timercmp(&t, &t, >);
}

Expand Down
28 changes: 28 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ class TestCondition : public TestFixture {
check_(file, line, code, settings, filename);
}

#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
void checkP_(const char* file, int line, const char code[], const char* filename = "test.cpp")
{
const Settings settings = settingsBuilder(settings0).severity(Severity::performance).certainty(Certainty::inconclusive).build();

std::vector<std::string> files(1, filename);
Tokenizer tokenizer(settings, *this);
PreprocessorHelper::preprocess(code, files, tokenizer, *this);

// Tokenizer..
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);

// Run checks..
runChecks<CheckCondition>(tokenizer, this);
}

void assignAndCompare() {
// &
check("void foo(int x)\n"
Expand Down Expand Up @@ -2754,6 +2770,18 @@ class TestCondition : public TestFixture {
" }\n"
"}");
ASSERT_EQUALS("", errout_str());

checkP("#define TYPE_1 \"a\"\n" // #13202
"#define TYPE_2 \"b\"\n"
"#define TYPE_3 \"c\"\n"
"void f(const std::string& s) {\n"
" if (s == TYPE_1) {}\n"
" else if (s == TYPE_2 || s == TYPE_3) {\n"
" if (s == TYPE_2) {}\n"
" else if (s == TYPE_3) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout_str());
}

void identicalConditionAfterEarlyExit() {
Expand Down
15 changes: 15 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7074,6 +7074,21 @@ class TestOther : public TestFixture {
" if ($a == $a) { }\n"
"}");
ASSERT_EQUALS("", errout_str());

checkP("#define X 1\n"
"#define Y 1\n"
"void f() {\n"
" if (X == X) {}\n"
" if (X == Y) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4]: (style) Same expression on both sides of '=='.\n", errout_str());

checkP("#define X 1\n"
"#define Y X\n"
"void f() {\n"
" if (X == Y) {}\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}

void duplicateExpression6() { // #4639
Expand Down

0 comments on commit c3e3387

Please sign in to comment.