Fixed #8858 (FP: identicalConditionAfterEarlyExit when there is #if)

This commit is contained in:
Daniel Marjamäki 2020-07-01 07:48:32 +02:00
parent ddd21a260f
commit f56a17bf3d
2 changed files with 28 additions and 14 deletions

View File

@ -702,22 +702,19 @@ void CheckCondition::multiCondition2()
}
}
} else {
std::stack<const Token *> tokens2;
tokens2.push(cond2);
while (!tokens2.empty()) {
const Token *secondCondition = tokens2.top();
tokens2.pop();
if (!secondCondition)
continue;
if (secondCondition->str() == "||" || secondCondition->str() == "&&") {
tokens2.push(secondCondition->astOperand1());
tokens2.push(secondCondition->astOperand2());
} else if ((!cond1->hasKnownIntValue() || !secondCondition->hasKnownIntValue()) &&
isSameExpression(mTokenizer->isCPP(), true, cond1, secondCondition, mSettings->library, true, true, &errorPath)) {
if (!isAliased(vars))
visitAstNodes(cond2, [&](const Token *secondCondition) {
if (secondCondition->str() == "||" || secondCondition->str() == "&&")
return ChildrenToVisit::op1_and_op2;
if ((!cond1->hasKnownIntValue() || !secondCondition->hasKnownIntValue()) &&
isSameExpression(mTokenizer->isCPP(), true, cond1, secondCondition, mSettings->library, true, true, &errorPath)) {
if (!isAliased(vars) && !mTokenizer->hasIfdef(cond1, secondCondition)) {
identicalConditionAfterEarlyExitError(cond1, secondCondition, errorPath);
return ChildrenToVisit::done;
}
}
}
return ChildrenToVisit::none;
});
}
}
if (Token::Match(tok, "%name% (") && isVariablesChanged(tok, tok->linkAt(1), true, varsInCond, mSettings, mTokenizer->isCPP())) {

View File

@ -18,6 +18,7 @@
#include "checkcondition.h"
#include "library.h"
#include "preprocessor.h"
#include "settings.h"
#include "testsuite.h"
#include "tokenize.h"
@ -136,10 +137,14 @@ private:
std::map<std::string, simplecpp::TokenList*> filedata;
simplecpp::preprocess(tokens2, tokens1, files, filedata, simplecpp::DUI());
Preprocessor preprocessor(settings0, nullptr);
preprocessor.setDirectives(tokens1);
// Tokenizer..
Tokenizer tokenizer(&settings0, this);
tokenizer.createTokens(std::move(tokens2));
tokenizer.simplifyTokens1("");
tokenizer.setPreprocessor(&preprocessor);
// Run checks..
CheckCondition checkCondition;
@ -2570,6 +2575,18 @@ private:
" int FileIndex; \n"
"};\n");
ASSERT_EQUALS("", errout.str());
// #8858 - #if
check("short Do() {\n"
" short ret = bar1();\n"
" if ( ret )\n"
" return ret;\n"
"#ifdef FEATURE\n"
" ret = bar2();\n"
"#endif\n"
" return ret;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void innerConditionModified() {