Improve knownArgument to check arguments to any nary function (#5348)

This commit is contained in:
Paul Fultz II 2023-08-19 12:59:15 -05:00 committed by GitHub
parent a92b10ca3b
commit d691450443
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 10 deletions

View File

@ -3647,17 +3647,23 @@ void CheckOther::checkKnownArgument()
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase(); const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
for (const Scope *functionScope : symbolDatabase->functionScopes) { for (const Scope *functionScope : symbolDatabase->functionScopes) {
for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) { for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) {
if (!Token::simpleMatch(tok->astParent(), "("))
continue;
if (!Token::Match(tok->astParent()->previous(), "%name%"))
continue;
if (Token::Match(tok->astParent()->previous(), "if|while|switch|sizeof"))
continue;
if (tok == tok->astParent()->previous())
continue;
if (!tok->hasKnownIntValue()) if (!tok->hasKnownIntValue())
continue; continue;
if (tok->tokType() == Token::eIncDecOp) if (Token::Match(tok, "++|--|%assign%"))
continue;
if (!Token::Match(tok->astParent(), "(|{|,"))
continue;
if (tok->astParent()->isCast())
continue;
int argn = -1;
const Token* ftok = getTokenArgumentFunction(tok, argn);
if (!ftok)
continue;
if (ftok->isCast())
continue;
if (Token::Match(ftok, "if|while|switch|sizeof"))
continue;
if (tok == tok->astParent()->previous())
continue; continue;
if (isConstVarExpression(tok)) if (isConstVarExpression(tok))
continue; continue;
@ -3668,6 +3674,10 @@ void CheckOther::checkKnownArgument()
tok2 = tok2->astOperand2(); tok2 = tok2->astOperand2();
if (isVariableExpression(tok2)) if (isVariableExpression(tok2))
continue; 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 // ensure that there is a integer variable in expression with unknown value
std::string varexpr; std::string varexpr;
bool isVariableExprHidden = false; // Is variable expression explicitly hidden bool isVariableExprHidden = false; // Is variable expression explicitly hidden
@ -3706,7 +3716,7 @@ void CheckOther::checkKnownArgument()
strTolower(funcname); strTolower(funcname);
if (funcname.find("assert") != std::string::npos) if (funcname.find("assert") != std::string::npos)
continue; continue;
knownArgumentError(tok, tok->astParent()->previous(), &tok->values().front(), varexpr, isVariableExprHidden); knownArgumentError(tok, ftok, &tok->values().front(), varexpr, isVariableExprHidden);
} }
} }
} }

View File

@ -10905,6 +10905,14 @@ private:
"}"); "}");
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()); 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" check("void g(int);\n"
"void f(int x) {\n" "void f(int x) {\n"
" g(0);\n" " g(0);\n"