Skip to content

Commit f56a17b

Browse files
committed
Fixed #8858 (FP: identicalConditionAfterEarlyExit when there is #if)
1 parent ddd21a2 commit f56a17b

2 files changed

Lines changed: 28 additions & 14 deletions

File tree

lib/checkcondition.cpp

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -702,22 +702,19 @@ void CheckCondition::multiCondition2()
702702
}
703703
}
704704
} else {
705-
std::stack<const Token *> tokens2;
706-
tokens2.push(cond2);
707-
while (!tokens2.empty()) {
708-
const Token *secondCondition = tokens2.top();
709-
tokens2.pop();
710-
if (!secondCondition)
711-
continue;
712-
if (secondCondition->str() == "||" || secondCondition->str() == "&&") {
713-
tokens2.push(secondCondition->astOperand1());
714-
tokens2.push(secondCondition->astOperand2());
715-
} else if ((!cond1->hasKnownIntValue() || !secondCondition->hasKnownIntValue()) &&
716-
isSameExpression(mTokenizer->isCPP(), true, cond1, secondCondition, mSettings->library, true, true, &errorPath)) {
717-
if (!isAliased(vars))
705+
visitAstNodes(cond2, [&](const Token *secondCondition) {
706+
if (secondCondition->str() == "||" || secondCondition->str() == "&&")
707+
return ChildrenToVisit::op1_and_op2;
708+
709+
if ((!cond1->hasKnownIntValue() || !secondCondition->hasKnownIntValue()) &&
710+
isSameExpression(mTokenizer->isCPP(), true, cond1, secondCondition, mSettings->library, true, true, &errorPath)) {
711+
if (!isAliased(vars) && !mTokenizer->hasIfdef(cond1, secondCondition)) {
718712
identicalConditionAfterEarlyExitError(cond1, secondCondition, errorPath);
713+
return ChildrenToVisit::done;
714+
}
719715
}
720-
}
716+
return ChildrenToVisit::none;
717+
});
721718
}
722719
}
723720
if (Token::Match(tok, "%name% (") && isVariablesChanged(tok, tok->linkAt(1), true, varsInCond, mSettings, mTokenizer->isCPP())) {

test/testcondition.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "checkcondition.h"
2020
#include "library.h"
21+
#include "preprocessor.h"
2122
#include "settings.h"
2223
#include "testsuite.h"
2324
#include "tokenize.h"
@@ -136,10 +137,14 @@ class TestCondition : public TestFixture {
136137
std::map<std::string, simplecpp::TokenList*> filedata;
137138
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
138139

140+
Preprocessor preprocessor(settings0, nullptr);
141+
preprocessor.setDirectives(tokens1);
142+
139143
// Tokenizer..
140144
Tokenizer tokenizer(&settings0, this);
141145
tokenizer.createTokens(std::move(tokens2));
142146
tokenizer.simplifyTokens1("");
147+
tokenizer.setPreprocessor(&preprocessor);
143148

144149
// Run checks..
145150
CheckCondition checkCondition;
@@ -2570,6 +2575,18 @@ class TestCondition : public TestFixture {
25702575
" int FileIndex; \n"
25712576
"};\n");
25722577
ASSERT_EQUALS("", errout.str());
2578+
2579+
// #8858 - #if
2580+
check("short Do() {\n"
2581+
" short ret = bar1();\n"
2582+
" if ( ret )\n"
2583+
" return ret;\n"
2584+
"#ifdef FEATURE\n"
2585+
" ret = bar2();\n"
2586+
"#endif\n"
2587+
" return ret;\n"
2588+
"}");
2589+
ASSERT_EQUALS("", errout.str());
25732590
}
25742591

25752592
void innerConditionModified() {

0 commit comments

Comments
 (0)