Fix issue 9277: FP: Dont warn for knwon conditions in if constexpr (#2085)

This commit is contained in:
Paul Fultz II 2019-08-16 00:56:39 -05:00 committed by Daniel Marjamäki
parent 3aef0c9bd3
commit 3e0d1141d3
4 changed files with 21 additions and 2 deletions

View File

@ -1345,7 +1345,7 @@ void CheckCondition::alwaysTrueFalse()
continue;
const bool constIfWhileExpression =
tok->astParent() && Token::Match(tok->astTop()->astOperand1(), "if|while") &&
tok->astParent() && Token::Match(tok->astTop()->astOperand1(), "if|while") && !tok->astTop()->astOperand1()->isConstexpr() &&
(Token::Match(tok->astParent(), "%oror%|&&") || Token::Match(tok->astParent()->astOperand1(), "if|while"));
const bool constValExpr = tok->isNumber() && Token::Match(tok->astParent(),"%oror%|&&|?"); // just one number in boolean expression
const bool compExpr = Token::Match(tok, "%comp%|!"); // a compare expression

View File

@ -566,6 +566,13 @@ public:
setFlag(fIncompleteVar, b);
}
bool isConstexpr() const {
return getFlag(fConstexpr);
}
void isConstexpr(bool b) {
setFlag(fConstexpr, b);
}
bool isBitfield() const {
return mImpl->mBits > 0;
@ -1098,6 +1105,7 @@ private:
fIsAttributeNodiscard = (1 << 23), // __attribute__ ((warn_unused_result)), [[nodiscard]]
fAtAddress = (1 << 24), // @ 0x4000
fIncompleteVar = (1 << 25),
fConstexpr = (1 << 26),
};
Token::Type mTokType;

View File

@ -4233,6 +4233,7 @@ bool Tokenizer::simplifyTokenList1(const char FileName[])
tok->deleteNext();
} else if (tok->strAt(1) == "constexpr") {
tok->deleteNext();
tok->isConstexpr(true);
} else {
syntaxError(tok);
}

View File

@ -3197,7 +3197,7 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Condition 'array' is always true\n", errout.str());
check("void f(int *array, int size ) {\n"
" for(int i = 0; i < size; ++i) {\n"
" if(array == 0)\n"
@ -3206,6 +3206,16 @@ private:
" }\n"
"}\n");
ASSERT_EQUALS("[test.cpp:3] -> [test.cpp:5]: (style) Condition 'array' is always true\n", errout.str());
// #9277
check("int f() {\n"
" constexpr bool x = true;\n"
" if constexpr (x)\n"
" return 0;\n"
" else\n"
" return 1;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
}
void multiConditionAlwaysTrue() {