Skip to content

Commit c3e3387

Browse files
Fix #13202 FP oppositeInnerCondition for string defines (danmar#6889)
1 parent 45d34fb commit c3e3387

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

lib/astutils.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1544,8 +1544,14 @@ bool isUsedAsBool(const Token* const tok, const Settings& settings)
15441544
}
15451545

15461546
bool compareTokenFlags(const Token* tok1, const Token* tok2, bool macro) {
1547-
if (macro && (tok1->isExpandedMacro() || tok2->isExpandedMacro() || tok1->isTemplateArg() || tok2->isTemplateArg()))
1548-
return false;
1547+
if (macro) {
1548+
if (tok1->isExpandedMacro() != tok2->isExpandedMacro())
1549+
return false;
1550+
if (tok1->isExpandedMacro() && tok1->getMacroName() != tok2->getMacroName())
1551+
return false;
1552+
if (tok1->isTemplateArg() || tok2->isTemplateArg())
1553+
return false;
1554+
}
15491555
if (tok1->isComplex() != tok2->isComplex())
15501556
return false;
15511557
if (tok1->isLong() != tok2->isLong())

test/cfg/bsd.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ void nullPointer_setlinebuf(FILE *stream)
3232
// #9323, #9331
3333
void verify_timercmp(struct timeval t)
3434
{
35+
// cppcheck-suppress duplicateExpression
3536
(void)timercmp(&t, &t, <);
37+
// cppcheck-suppress duplicateExpression
3638
(void)timercmp(&t, &t, <=);
3739
(void)timercmp(&t, &t, ==);
3840
(void)timercmp(&t, &t, !=);
41+
// cppcheck-suppress duplicateExpression
3942
(void)timercmp(&t, &t, >=);
43+
// cppcheck-suppress duplicateExpression
4044
(void)timercmp(&t, &t, >);
4145
}
4246

test/testcondition.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ class TestCondition : public TestFixture {
144144
check_(file, line, code, settings, filename);
145145
}
146146

147+
#define checkP(...) checkP_(__FILE__, __LINE__, __VA_ARGS__)
148+
void checkP_(const char* file, int line, const char code[], const char* filename = "test.cpp")
149+
{
150+
const Settings settings = settingsBuilder(settings0).severity(Severity::performance).certainty(Certainty::inconclusive).build();
151+
152+
std::vector<std::string> files(1, filename);
153+
Tokenizer tokenizer(settings, *this);
154+
PreprocessorHelper::preprocess(code, files, tokenizer, *this);
155+
156+
// Tokenizer..
157+
ASSERT_LOC(tokenizer.simplifyTokens1(""), file, line);
158+
159+
// Run checks..
160+
runChecks<CheckCondition>(tokenizer, this);
161+
}
162+
147163
void assignAndCompare() {
148164
// &
149165
check("void foo(int x)\n"
@@ -2754,6 +2770,18 @@ class TestCondition : public TestFixture {
27542770
" }\n"
27552771
"}");
27562772
ASSERT_EQUALS("", errout_str());
2773+
2774+
checkP("#define TYPE_1 \"a\"\n" // #13202
2775+
"#define TYPE_2 \"b\"\n"
2776+
"#define TYPE_3 \"c\"\n"
2777+
"void f(const std::string& s) {\n"
2778+
" if (s == TYPE_1) {}\n"
2779+
" else if (s == TYPE_2 || s == TYPE_3) {\n"
2780+
" if (s == TYPE_2) {}\n"
2781+
" else if (s == TYPE_3) {}\n"
2782+
" }\n"
2783+
"}");
2784+
ASSERT_EQUALS("", errout_str());
27572785
}
27582786

27592787
void identicalConditionAfterEarlyExit() {

test/testother.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7074,6 +7074,21 @@ class TestOther : public TestFixture {
70747074
" if ($a == $a) { }\n"
70757075
"}");
70767076
ASSERT_EQUALS("", errout_str());
7077+
7078+
checkP("#define X 1\n"
7079+
"#define Y 1\n"
7080+
"void f() {\n"
7081+
" if (X == X) {}\n"
7082+
" if (X == Y) {}\n"
7083+
"}\n");
7084+
ASSERT_EQUALS("[test.cpp:4]: (style) Same expression on both sides of '=='.\n", errout_str());
7085+
7086+
checkP("#define X 1\n"
7087+
"#define Y X\n"
7088+
"void f() {\n"
7089+
" if (X == Y) {}\n"
7090+
"}\n");
7091+
ASSERT_EQUALS("", errout_str());
70777092
}
70787093

70797094
void duplicateExpression6() { // #4639

0 commit comments

Comments
 (0)