Improved check for shifting by negative values and removed false positives

This commit is contained in:
Arpit Chaudhary 2012-09-04 16:06:00 +05:30 committed by PKEuS
parent dcba21dedd
commit 67e40a85e5
2 changed files with 31 additions and 3 deletions

View File

@ -3329,12 +3329,20 @@ void CheckOther::redundantCopyError(const Token *tok,const std::string& varname)
void CheckOther::checkNegativeBitwiseShift()
{
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
for (const Token *tok = _tokenizer->tokens(); tok ; tok = tok->next()) {
if (Token::Match(tok,"%var% >>|<< %num%") || Token::Match(tok,"%num >>|<< %num%")) {
if ((Token::Match(tok,"%var% >>|<< %num%") || Token::Match(tok,"%num% >>|<< %num%")) && !Token::Match(tok->previous(),">>|<<")) {
if (tok->isName()) {
const Variable* var = symbolDatabase->getVariableFromVarId(tok->varId());
if (var && var->typeStartToken()->isStandardType() && (tok->strAt(2))[0] == '-')
negativeBitwiseShiftError(tok);
} else {
if ((tok->strAt(2))[0] == '-')
negativeBitwiseShiftError(tok);
}
}
}
}

View File

@ -6111,6 +6111,26 @@ private:
" a >>= -1;\n"
"}");
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value.\n", errout.str());
check("void foo()\n"
"{\n"
" int a = 123 << -1;\n"
"}");
TODO_ASSERT_EQUALS("[test.cpp:3]: (error) Shifting by a negative value.\n", "", errout.str());
check("void foo()\n"
"{\n"
" std::cout << -1;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << a << -1 ;\n"
"}");
ASSERT_EQUALS("", errout.str());
check("void foo()\n"
"{\n"
" std::cout << 3 << -1 ;\n"
"}");
ASSERT_EQUALS("", errout.str());
}
void incompleteArrayFill() {