Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #13202 FP oppositeInnerCondition for string defines #6889

Merged
merged 10 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
3 changes: 3 additions & 0 deletions test/cfg/bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ 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, >=);
(void)timercmp(&t, &t, >);
chrchr-github marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down
2 changes: 1 addition & 1 deletion test/cfg/windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ void uninitvar_towupper(_locale_t l)
void oppositeInnerCondition_SUCCEEDED_FAILED(HRESULT hr)
{
if (SUCCEEDED(hr)) {
// TODO ticket #8596 cppcheck-suppress oppositeInnerCondition
// cppcheck-suppress oppositeInnerCondition
if (FAILED(hr)) {}
}
}
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
8 changes: 8 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7066,6 +7066,14 @@ class TestOther : public TestFixture {
" if ($a == $a) { }\n"
"}");
ASSERT_EQUALS("", errout_str());

checkP("#define X 1\n"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what macroName there will be if there are nested macros. Can you try this:

#define X 1
#define Y X
void f() {
     if (X == Y) {}
}

I think that ideally we don't warn about that because Y could possibly have different definitions..

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it strikes me that it might be within reach to identify if a macro can have different possible values in different configurations.. if the code is something like:

#ifdef __GNUC__
#define X 1
#else
#define X 2
#endif

I think it would not be impossible to mark "X" as a macro that has different values using the info from simplecpp that I think is available. But the info must be propagated to the checkers somehow..

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also don't know if a macro was a simple define or a functional-style macro, which should be handled differently (see the failing test in windows.cpp).

"#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());
}

void duplicateExpression6() { // #4639
Expand Down
Loading