Skip to content

Commit

Permalink
Improve knownArgument to check arguments to any nary function (danmar…
Browse files Browse the repository at this point in the history
  • Loading branch information
pfultz2 authored Aug 19, 2023
1 parent a92b10c commit d691450
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/checkother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3647,17 +3647,23 @@ void CheckOther::checkKnownArgument()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope *functionScope : symbolDatabase->functionScopes) {
for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok->astParent(), "("))
if (!tok->hasKnownIntValue())
continue;
if (!Token::Match(tok->astParent()->previous(), "%name%"))
if (Token::Match(tok, "++|--|%assign%"))
continue;
if (Token::Match(tok->astParent()->previous(), "if|while|switch|sizeof"))
if (!Token::Match(tok->astParent(), "(|{|,"))
continue;
if (tok == tok->astParent()->previous())
if (tok->astParent()->isCast())
continue;
if (!tok->hasKnownIntValue())
int argn = -1;
const Token* ftok = getTokenArgumentFunction(tok, argn);
if (!ftok)
continue;
if (tok->tokType() == Token::eIncDecOp)
if (ftok->isCast())
continue;
if (Token::Match(ftok, "if|while|switch|sizeof"))
continue;
if (tok == tok->astParent()->previous())
continue;
if (isConstVarExpression(tok))
continue;
Expand All @@ -3668,6 +3674,10 @@ void CheckOther::checkKnownArgument()
tok2 = tok2->astOperand2();
if (isVariableExpression(tok2))
continue;
if (tok->isComparisonOp() &&
isSameExpression(
mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, true, true))
continue;
// ensure that there is a integer variable in expression with unknown value
std::string varexpr;
bool isVariableExprHidden = false; // Is variable expression explicitly hidden
Expand Down Expand Up @@ -3706,7 +3716,7 @@ void CheckOther::checkKnownArgument()
strTolower(funcname);
if (funcname.find("assert") != std::string::npos)
continue;
knownArgumentError(tok, tok->astParent()->previous(), &tok->values().front(), varexpr, isVariableExprHidden);
knownArgumentError(tok, ftok, &tok->values().front(), varexpr, isVariableExprHidden);
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10905,6 +10905,14 @@ class TestOther : public TestFixture {
"}");
ASSERT_EQUALS("[test.cpp:3]: (style) Argument '(int)((x&0x01)>>7)' to function g is always 0. It does not matter what value 'x' has.\n", errout.str());

check("void g(int, int);\n"
"void f(int x) {\n"
" g(x, (x & 0x01) >> 7);\n"
"}");
ASSERT_EQUALS(
"[test.cpp:3]: (style) Argument '(x&0x01)>>7' to function g is always 0. It does not matter what value 'x' has.\n",
errout.str());

check("void g(int);\n"
"void f(int x) {\n"
" g(0);\n"
Expand Down

0 comments on commit d691450

Please sign in to comment.