Skip to content

Commit d691450

Browse files
authored
Improve knownArgument to check arguments to any nary function (#5348)
1 parent a92b10c commit d691450

2 files changed

Lines changed: 25 additions & 7 deletions

File tree

lib/checkother.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3647,17 +3647,23 @@ void CheckOther::checkKnownArgument()
36473647
const SymbolDatabase *symbolDatabase = mTokenizer->getSymbolDatabase();
36483648
for (const Scope *functionScope : symbolDatabase->functionScopes) {
36493649
for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) {
3650-
if (!Token::simpleMatch(tok->astParent(), "("))
3650+
if (!tok->hasKnownIntValue())
36513651
continue;
3652-
if (!Token::Match(tok->astParent()->previous(), "%name%"))
3652+
if (Token::Match(tok, "++|--|%assign%"))
36533653
continue;
3654-
if (Token::Match(tok->astParent()->previous(), "if|while|switch|sizeof"))
3654+
if (!Token::Match(tok->astParent(), "(|{|,"))
36553655
continue;
3656-
if (tok == tok->astParent()->previous())
3656+
if (tok->astParent()->isCast())
36573657
continue;
3658-
if (!tok->hasKnownIntValue())
3658+
int argn = -1;
3659+
const Token* ftok = getTokenArgumentFunction(tok, argn);
3660+
if (!ftok)
36593661
continue;
3660-
if (tok->tokType() == Token::eIncDecOp)
3662+
if (ftok->isCast())
3663+
continue;
3664+
if (Token::Match(ftok, "if|while|switch|sizeof"))
3665+
continue;
3666+
if (tok == tok->astParent()->previous())
36613667
continue;
36623668
if (isConstVarExpression(tok))
36633669
continue;
@@ -3668,6 +3674,10 @@ void CheckOther::checkKnownArgument()
36683674
tok2 = tok2->astOperand2();
36693675
if (isVariableExpression(tok2))
36703676
continue;
3677+
if (tok->isComparisonOp() &&
3678+
isSameExpression(
3679+
mTokenizer->isCPP(), true, tok->astOperand1(), tok->astOperand2(), mSettings->library, true, true))
3680+
continue;
36713681
// ensure that there is a integer variable in expression with unknown value
36723682
std::string varexpr;
36733683
bool isVariableExprHidden = false; // Is variable expression explicitly hidden
@@ -3706,7 +3716,7 @@ void CheckOther::checkKnownArgument()
37063716
strTolower(funcname);
37073717
if (funcname.find("assert") != std::string::npos)
37083718
continue;
3709-
knownArgumentError(tok, tok->astParent()->previous(), &tok->values().front(), varexpr, isVariableExprHidden);
3719+
knownArgumentError(tok, ftok, &tok->values().front(), varexpr, isVariableExprHidden);
37103720
}
37113721
}
37123722
}

test/testother.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10905,6 +10905,14 @@ class TestOther : public TestFixture {
1090510905
"}");
1090610906
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());
1090710907

10908+
check("void g(int, int);\n"
10909+
"void f(int x) {\n"
10910+
" g(x, (x & 0x01) >> 7);\n"
10911+
"}");
10912+
ASSERT_EQUALS(
10913+
"[test.cpp:3]: (style) Argument '(x&0x01)>>7' to function g is always 0. It does not matter what value 'x' has.\n",
10914+
errout.str());
10915+
1090810916
check("void g(int);\n"
1090910917
"void f(int x) {\n"
1091010918
" g(0);\n"

0 commit comments

Comments
 (0)