Fixed false negative #5815

This commit is contained in:
PKEuS 2015-08-31 14:12:02 +02:00
parent 0dd0f2a97d
commit dc6c278d83
4 changed files with 27 additions and 3 deletions

View File

@ -251,7 +251,7 @@ void CheckSizeof::sizeofCalculation()
for (const Token *tok = _tokenizer->tokens(); tok; tok = tok->next()) {
if (Token::simpleMatch(tok, "sizeof (")) {
const Token *argument = tok->next()->astOperand2();
if (argument && argument->isCalculation() && (!argument->isExpandedMacro() || printInconclusive))
if (argument && argument->isCalculation(true) && (!argument->isExpandedMacro() || printInconclusive))
sizeofCalculationError(argument, argument->isExpandedMacro());
}
}

View File

@ -1136,8 +1136,19 @@ void Token::astOperand2(Token *tok)
_astOperand2 = tok;
}
bool Token::isCalculation() const
bool Token::isCalculation(bool goDownwards) const
{
if (goDownwards && Token::Match(this, "[|(|,")) {
bool ret = false;
if (this->astOperand1())
ret = this->astOperand1()->isCalculation(true);
if (ret)
return true;
if (this->astOperand2())
ret = this->astOperand2()->isCalculation(true);
return ret;
}
if (!Token::Match(this, "%cop%|++|--"))
return false;

View File

@ -872,9 +872,10 @@ public:
* For '*' and '&' tokens it is looked up if this is a
* dereference or address-of. A dereference or address-of is not
* counted as a calculation.
* @param goDownwards the function will look for calculations in all children of the tree
* @return returns true if current token is a calculation
*/
bool isCalculation() const;
bool isCalculation(bool goDownwards = false) const;
void clearAst() {
_astOperand1 = _astOperand2 = _astParent = NULL;

View File

@ -108,6 +108,18 @@ private:
check("sizeof(--foo)");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof().\n", errout.str());
check("sizeof(bar(1, 2, --foo, 3, 4))");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof().\n", errout.str());
check("sizeof( int32_t[ i++ ] );");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof().\n", errout.str());
check("sizeof(a[b + 2])");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof().\n", errout.str());
check("sizeof((2 + a)[b])");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof().\n", errout.str());
}
void sizeofForArrayParameter() {