Skip to content

Commit 078a6d7

Browse files
Fix #11238 FP knownConditionTrueFalse with constexpr? (#4341)
1 parent fc6b79a commit 078a6d7

4 files changed

Lines changed: 23 additions & 11 deletions

File tree

‎lib/astutils.cpp‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2768,35 +2768,37 @@ bool isCPPCast(const Token* tok)
27682768
return tok && Token::simpleMatch(tok->previous(), "> (") && tok->astOperand2() && tok->astOperand1() && isCPPCastKeyword(tok->astOperand1());
27692769
}
27702770

2771-
bool isConstVarExpression(const Token *tok, const char* skipMatch)
2771+
bool isConstVarExpression(const Token *tok, std::function<bool(const Token*)> skipPredicate)
27722772
{
27732773
if (!tok)
27742774
return false;
27752775
if (tok->str() == "?" && tok->astOperand2() && tok->astOperand2()->str() == ":") // ternary operator
27762776
return isConstVarExpression(tok->astOperand2()->astOperand1()) && isConstVarExpression(tok->astOperand2()->astOperand2()); // left and right of ":"
2777-
if (skipMatch && Token::Match(tok, skipMatch))
2777+
if (skipPredicate && skipPredicate(tok))
27782778
return false;
27792779
if (Token::simpleMatch(tok->previous(), "sizeof ("))
27802780
return true;
27812781
if (Token::Match(tok->previous(), "%name% (")) {
2782-
if (Token::simpleMatch(tok->astOperand1(), ".") && !isConstVarExpression(tok->astOperand1(), skipMatch))
2782+
if (Token::simpleMatch(tok->astOperand1(), ".") && !isConstVarExpression(tok->astOperand1(), skipPredicate))
27832783
return false;
27842784
std::vector<const Token *> args = getArguments(tok);
2785+
if (args.empty() && tok->previous()->function() && tok->previous()->function()->isConstexpr())
2786+
return true;
27852787
return std::all_of(args.begin(), args.end(), [&](const Token* t) {
2786-
return isConstVarExpression(t, skipMatch);
2788+
return isConstVarExpression(t, skipPredicate);
27872789
});
27882790
}
27892791
if (isCPPCast(tok)) {
2790-
return isConstVarExpression(tok->astOperand2(), skipMatch);
2792+
return isConstVarExpression(tok->astOperand2(), skipPredicate);
27912793
}
27922794
if (Token::Match(tok, "( %type%"))
2793-
return isConstVarExpression(tok->astOperand1(), skipMatch);
2795+
return isConstVarExpression(tok->astOperand1(), skipPredicate);
27942796
if (tok->str() == "::" && tok->hasKnownValue())
2795-
return isConstVarExpression(tok->astOperand2(), skipMatch);
2797+
return isConstVarExpression(tok->astOperand2(), skipPredicate);
27962798
if (Token::Match(tok, "%cop%|[|.")) {
2797-
if (tok->astOperand1() && !isConstVarExpression(tok->astOperand1(), skipMatch))
2799+
if (tok->astOperand1() && !isConstVarExpression(tok->astOperand1(), skipPredicate))
27982800
return false;
2799-
if (tok->astOperand2() && !isConstVarExpression(tok->astOperand2(), skipMatch))
2801+
if (tok->astOperand2() && !isConstVarExpression(tok->astOperand2(), skipPredicate))
28002802
return false;
28012803
return true;
28022804
}

‎lib/astutils.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ bool isLikelyStreamRead(bool cpp, const Token *op);
383383

384384
bool isCPPCast(const Token* tok);
385385

386-
bool isConstVarExpression(const Token *tok, const char * skipMatch = nullptr);
386+
bool isConstVarExpression(const Token* tok, std::function<bool(const Token*)> skipPredicate = nullptr);
387387

388388
const Variable *getLHSVariable(const Token *tok);
389389

‎lib/checkcondition.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1497,7 +1497,9 @@ void CheckCondition::alwaysTrueFalse()
14971497
continue;
14981498
if (tok->isComparisonOp() && isSameExpression(mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, true, true))
14991499
continue;
1500-
if (isConstVarExpression(tok, "[|(|&|+|-|*|/|%|^|>>|<<"))
1500+
if (isConstVarExpression(tok, [](const Token* tok) {
1501+
return Token::Match(tok, "[|(|&|+|-|*|/|%|^|>>|<<") && !Token::simpleMatch(tok, "( )");
1502+
}))
15011503
continue;
15021504

15031505
// there are specific warnings about nonzero expressions (pointer/unsigned)

‎test/testcondition.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4281,6 +4281,14 @@ class TestCondition : public TestFixture {
42814281
" }\n"
42824282
"};\n");
42834283
ASSERT_EQUALS("", errout.str());
4284+
4285+
check("constexpr int f() {\n" // #11238
4286+
" return 1;\n"
4287+
"}\n"
4288+
"constexpr bool g() {\n"
4289+
" return f() == 1;\n"
4290+
"}\n");
4291+
ASSERT_EQUALS("", errout.str());
42844292
}
42854293

42864294
void alwaysTrueSymbolic()

0 commit comments

Comments
 (0)