CheckCondition: Fix FPs for aliased variables (daca)

This commit is contained in:
Daniel Marjamäki 2017-09-04 09:34:03 +02:00
parent a02fd54a69
commit 9465dca583
3 changed files with 31 additions and 5 deletions

View File

@ -51,6 +51,13 @@ namespace {
CheckCondition instance;
}
bool CheckCondition::isAliased(const std::set<unsigned int> &vars) const {
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::Match(tok, "= & %var% ;") && vars.find(tok->tokAt(2)->varId()) != vars.end())
return true;
}
return false;
}
void CheckCondition::assignIf()
{
@ -516,11 +523,15 @@ void CheckCondition::multiCondition2()
const Token *cond2 = tok->next()->astOperand2();
if (type == MULTICONDITIONTYPE::INNER) {
if (isOppositeCond(false, _tokenizer->isCPP(), cond1, cond2, _settings->library, true))
oppositeInnerConditionError(cond1, cond2);
if (isOppositeCond(false, _tokenizer->isCPP(), cond1, cond2, _settings->library, true)) {
if (!isAliased(vars))
oppositeInnerConditionError(cond1, cond2);
}
} else {
if (isSameExpression(_tokenizer->isCPP(), true, cond1, cond2, _settings->library, true))
sameConditionAfterEarlyExitError(cond1, cond2);
if (isSameExpression(_tokenizer->isCPP(), true, cond1, cond2, _settings->library, true)) {
if (!isAliased(vars))
sameConditionAfterEarlyExitError(cond1, cond2);
}
}
}

View File

@ -114,7 +114,7 @@ public:
void checkInvalidTestForOverflow();
private:
bool isAliased(const std::set<unsigned int> &vars) const;
bool isOverlappingCond(const Token * const cond1, const Token * const cond2, bool pure) const;
void assignIfError(const Token *tok1, const Token *tok2, const std::string &condition, bool result);
void mismatchingBitAndError(const Token *tok1, const MathLib::bigint num1, const Token *tok2, const MathLib::bigint num2);

View File

@ -76,6 +76,7 @@ private:
TEST_CASE(modulo);
TEST_CASE(oppositeInnerCondition);
TEST_CASE(oppositeInnerConditionAlias);
TEST_CASE(oppositeInnerCondition2);
TEST_CASE(oppositeInnerConditionAnd);
@ -1597,6 +1598,20 @@ private:
ASSERT_EQUALS("[test.cpp:2] -> [test.cpp:3]: (warning) Opposite inner 'if' condition leads to a dead code block.\n", errout.str());
}
void oppositeInnerConditionAlias() {
check("void f() {\n"
" struct S s;\n"
" bool hasFailed = false;\n"
" s.status = &hasFailed;\n"
"\n"
" if (! hasFailed) {\n"
" doStuff(&s);\n"
" if (hasFailed) {}\n"
" }\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void oppositeInnerCondition2() {
// first comparison: <
check("void f(int x) {\n"