Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions lib/vf_settokenvalue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,25 @@ namespace ValueFlow
return false;
}

static bool isFunctionCall(const Token* tok, const Settings& settings)
{
if (!tok)
return false;
if (tok->str() == "{")
return true;
if (tok->str() == "(") {
if (tok->astOperand1() && tok->astOperand1()->isControlFlowKeyword())
return false;
if (Token::Match(tok->next(), "%name%") && !settings.library.isNotLibraryFunction(tok->next()))
return true;
if (Token::Match(tok->previous(), "%name%|>"))
return true;
if (Token::Match(tok->astOperand1(), "%name%|(|[|*|."))
return true;
}
return false;
}

/** set ValueFlow value and perform calculations if possible */
void setTokenValue(Token* tok,
Value value,
Expand Down Expand Up @@ -254,9 +273,7 @@ namespace ValueFlow
return !Token::simpleMatch(p, ",");
});
// Ensure that the comma isn't a function call
if (!callParent || (!Token::Match(callParent->previous(), "%name%|> (") && !Token::simpleMatch(callParent, "{") &&
(!Token::Match(callParent, "( %name%") || settings.library.isNotLibraryFunction(callParent->next())) &&
!(callParent->str() == "(" && Token::Match(callParent->astOperand1(), "%name%|(|[|*|.")))) {
if (!isFunctionCall(callParent, settings)) {
setTokenValue(parent, std::move(value), settings);
return;
}
Expand Down
8 changes: 8 additions & 0 deletions test/testcondition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5011,6 +5011,14 @@ class TestCondition : public TestFixture {
"[test.cpp:9:9]: (style) Condition '0b101010' is always true [knownConditionTrueFalse]\n"
"[test.cpp:10:9]: (style) Condition '!42' is always false [knownConditionTrueFalse]\n",
errout_str());

check("bool g(int = 1) __attribute__((pure));\n"
"void f() {\n"
" int n = 0;\n"
" if (g(), n) {}\n"
"}\n");
ASSERT_EQUALS("[test.cpp:4:12]: (style) Condition 'g(),n' is always false [knownConditionTrueFalse]\n",
errout_str());
}

void alwaysTrueSymbolic()
Expand Down
10 changes: 10 additions & 0 deletions test/testvalueflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1887,6 +1887,16 @@ class TestValueFlow : public TestFixture {
"}\n";
values = tokenValues(code, ",");
ASSERT_EQUALS(0U, values.size());

code = "void f() {\n"
" int a = 4;\n"
" if (g(), a) {}\n"
"}\n";
values = tokenValues(code, ",");
ASSERT_EQUALS(1U, values.size());
ASSERT(values.front().isKnown());
ASSERT(values.front().isIntValue());
ASSERT_EQUALS(4, values.front().intvalue);
}

void valueFlowErrorPath() {
Expand Down
Loading