Refactored checkNegativeBitwiseShift() so it uses ast and valueflow
This commit is contained in:
parent
d1b1699bb0
commit
407c9fdf9d
|
@ -3299,22 +3299,46 @@ void CheckOther::redundantCopyError(const Token *tok,const std::string& varname)
|
||||||
void CheckOther::checkNegativeBitwiseShift()
|
void CheckOther::checkNegativeBitwiseShift()
|
||||||
{
|
{
|
||||||
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
|
||||||
|
|
||||||
const std::size_t functions = symbolDatabase->functionScopes.size();
|
const std::size_t functions = symbolDatabase->functionScopes.size();
|
||||||
for (std::size_t i = 0; i < functions; ++i) {
|
for (std::size_t i = 0; i < functions; ++i) {
|
||||||
const Scope * scope = symbolDatabase->functionScopes[i];
|
const Scope * scope = symbolDatabase->functionScopes[i];
|
||||||
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
|
for (const Token* tok = scope->classStart->next(); tok != scope->classEnd; tok = tok->next()) {
|
||||||
|
if (tok->str() != "<<" && tok->str() != ">>")
|
||||||
|
continue;
|
||||||
|
|
||||||
if ((Token::Match(tok,"%var% >>|<< %num%") || Token::Match(tok,"%num% >>|<< %num%")) && !Token::Match(tok->previous(),">>|<<")) {
|
if (!tok->astOperand1() || !tok->astOperand2())
|
||||||
if (tok->isName()) {
|
continue;
|
||||||
const Variable *var = tok->variable();
|
|
||||||
if (var && var->typeStartToken()->isStandardType() && (tok->strAt(2))[0] == '-')
|
// don't warn if lhs is a class. this is an overloaded operator then
|
||||||
negativeBitwiseShiftError(tok);
|
if (_tokenizer->isCPP()) {
|
||||||
} else {
|
const Token *rhs = tok->astOperand1();
|
||||||
if ((tok->strAt(2))[0] == '-')
|
while (Token::Match(rhs, "::|."))
|
||||||
negativeBitwiseShiftError(tok);
|
rhs = rhs->astOperand2();
|
||||||
|
if (!rhs)
|
||||||
|
continue;
|
||||||
|
if (!rhs->isNumber() && !rhs->variable())
|
||||||
|
continue;
|
||||||
|
if (!rhs->variable()->typeStartToken()->isStandardType())
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get negative rhs value. preferably a value which doesn't have 'condition'.
|
||||||
|
const ValueFlow::Value *value = nullptr;
|
||||||
|
for (std::list<ValueFlow::Value>::const_iterator it = tok->astOperand2()->values.begin();
|
||||||
|
it != tok->astOperand2()->values.end();
|
||||||
|
++it) {
|
||||||
|
if (it->intvalue < 0) {
|
||||||
|
if (value == nullptr || it->condition == nullptr)
|
||||||
|
value = &(*it);
|
||||||
|
if (value->condition == nullptr)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!value)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
negativeBitwiseShiftError(tok);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3322,7 +3346,7 @@ void CheckOther::checkNegativeBitwiseShift()
|
||||||
|
|
||||||
void CheckOther::negativeBitwiseShiftError(const Token *tok)
|
void CheckOther::negativeBitwiseShiftError(const Token *tok)
|
||||||
{
|
{
|
||||||
reportError(tok, Severity::error, "shiftNegative", "Shifting by a negative value.");
|
reportError(tok, Severity::error, "shiftNegative", "Shifting by a negative value is undefined behaviour");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -6177,25 +6177,25 @@ private:
|
||||||
" int a; a = 123;\n"
|
" int a; a = 123;\n"
|
||||||
" a << -1;\n"
|
" a << -1;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value is undefined behaviour\n", errout.str());
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int a; a = 123;\n"
|
" int a; a = 123;\n"
|
||||||
" a >> -1;\n"
|
" a >> -1;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value is undefined behaviour\n", errout.str());
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int a; a = 123;\n"
|
" int a; a = 123;\n"
|
||||||
" a <<= -1;\n"
|
" a <<= -1;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value is undefined behaviour\n", errout.str());
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" int a; a = 123;\n"
|
" int a; a = 123;\n"
|
||||||
" a >>= -1;\n"
|
" a >>= -1;\n"
|
||||||
"}");
|
"}");
|
||||||
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value.\n", errout.str());
|
ASSERT_EQUALS("[test.cpp:4]: (error) Shifting by a negative value is undefined behaviour\n", errout.str());
|
||||||
check("void foo()\n"
|
check("void foo()\n"
|
||||||
"{\n"
|
"{\n"
|
||||||
" std::cout << -1;\n"
|
" std::cout << -1;\n"
|
||||||
|
|
Loading…
Reference in New Issue