Fixed #3793 (improve check: calculation in sizeof)

This commit is contained in:
Ankita Gupta 2012-09-03 15:56:10 +02:00 committed by Daniel Marjamäki
parent 04661fd7d0
commit c3a65dca40
2 changed files with 13 additions and 5 deletions

View File

@ -3007,9 +3007,11 @@ void CheckOther::sizeofCalculation()
if (Token::simpleMatch(tok, "sizeof (")) {
const Token* const end = tok->linkAt(1);
for (const Token *tok2 = tok->tokAt(2); tok2 != end; tok2 = tok2->next()) {
if (tok2->isOp() && (!tok2->isExpandedMacro() || _settings->inconclusive) && !Token::Match(tok2, ">|<|&|*")) {
sizeofCalculationError(tok2, tok2->isExpandedMacro());
break;
if (tok2->isOp() && (!tok2->isExpandedMacro() || _settings->inconclusive) && !Token::Match(tok2, ">|<|&") && (Token::Match(tok2->previous(), "%var%") || !Token::Match(tok2, "*"))) {
if (!(Token::Match(tok2->previous(), "%type%") || Token::Match(tok2->next(), "%type%"))) {
sizeofCalculationError(tok2, tok2->isExpandedMacro());
break;
}
}
}
}

View File

@ -1366,12 +1366,18 @@ private:
}
void sizeofCalculation() {
check("sizeof(a+b)");
check("int a, b; int a,sizeof(a+b)");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof()\n", errout.str());
check("sizeof(-a)");
check("int a, b; sizeof(a*b)");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof()\n", errout.str());
check("int a, b; sizeof(-a)");
ASSERT_EQUALS("[test.cpp:1]: (warning) Found calculation inside sizeof()\n", errout.str());
check("int a, b; sizeof(*a)");
ASSERT_EQUALS("", errout.str());
check("sizeof(void * const)");
ASSERT_EQUALS("", errout.str());
}