Skip to content

Commit ff63c57

Browse files
Partial fix for #13330 FN knownConditionTrueFalse with comma operator (#8965)
1 parent 71d3932 commit ff63c57

3 files changed

Lines changed: 38 additions & 3 deletions

File tree

‎lib/vf_settokenvalue.cpp‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,25 @@ namespace ValueFlow
216216
return false;
217217
}
218218

219+
static bool isFunctionCall(const Token* tok, const Settings& settings)
220+
{
221+
if (!tok)
222+
return false;
223+
if (tok->str() == "{")
224+
return true;
225+
if (tok->str() == "(") {
226+
if (tok->astOperand1() && tok->astOperand1()->isControlFlowKeyword())
227+
return false;
228+
if (Token::Match(tok->next(), "%name%") && !settings.library.isNotLibraryFunction(tok->next()))
229+
return true;
230+
if (Token::Match(tok->previous(), "%name%|>"))
231+
return true;
232+
if (Token::Match(tok->astOperand1(), "%name%|(|[|*|."))
233+
return true;
234+
}
235+
return false;
236+
}
237+
219238
/** set ValueFlow value and perform calculations if possible */
220239
void setTokenValue(Token* tok,
221240
Value value,
@@ -254,9 +273,7 @@ namespace ValueFlow
254273
return !Token::simpleMatch(p, ",");
255274
});
256275
// Ensure that the comma isn't a function call
257-
if (!callParent || (!Token::Match(callParent->previous(), "%name%|> (") && !Token::simpleMatch(callParent, "{") &&
258-
(!Token::Match(callParent, "( %name%") || settings.library.isNotLibraryFunction(callParent->next())) &&
259-
!(callParent->str() == "(" && Token::Match(callParent->astOperand1(), "%name%|(|[|*|.")))) {
276+
if (!isFunctionCall(callParent, settings)) {
260277
setTokenValue(parent, std::move(value), settings);
261278
return;
262279
}

‎test/testcondition.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5011,6 +5011,14 @@ class TestCondition : public TestFixture {
50115011
"[test.cpp:9:9]: (style) Condition '0b101010' is always true [knownConditionTrueFalse]\n"
50125012
"[test.cpp:10:9]: (style) Condition '!42' is always false [knownConditionTrueFalse]\n",
50135013
errout_str());
5014+
5015+
check("bool g(int = 1) __attribute__((pure));\n"
5016+
"void f() {\n"
5017+
" int n = 0;\n"
5018+
" if (g(), n) {}\n"
5019+
"}\n");
5020+
ASSERT_EQUALS("[test.cpp:4:12]: (style) Condition 'g(),n' is always false [knownConditionTrueFalse]\n",
5021+
errout_str());
50145022
}
50155023

50165024
void alwaysTrueSymbolic()

‎test/testvalueflow.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1887,6 +1887,16 @@ class TestValueFlow : public TestFixture {
18871887
"}\n";
18881888
values = tokenValues(code, ",");
18891889
ASSERT_EQUALS(0U, values.size());
1890+
1891+
code = "void f() {\n"
1892+
" int a = 4;\n"
1893+
" if (g(), a) {}\n"
1894+
"}\n";
1895+
values = tokenValues(code, ",");
1896+
ASSERT_EQUALS(1U, values.size());
1897+
ASSERT(values.front().isKnown());
1898+
ASSERT(values.front().isIntValue());
1899+
ASSERT_EQUALS(4, values.front().intvalue);
18901900
}
18911901

18921902
void valueFlowErrorPath() {

0 commit comments

Comments
 (0)