CheckOther::checkTooBigBitwiseShift: improving error messages

This commit is contained in:
Daniel Marjamäki 2014-09-09 17:06:45 +02:00
parent a5b863796c
commit a36b0e55be
2 changed files with 18 additions and 7 deletions

View File

@ -2702,17 +2702,28 @@ void CheckOther::checkTooBigBitwiseShift()
// Get biggest rhs value. preferably a value which doesn't have 'condition'. // Get biggest rhs value. preferably a value which doesn't have 'condition'.
const ValueFlow::Value *value = tok->astOperand2()->getValueGE(lhsbits, _settings); const ValueFlow::Value *value = tok->astOperand2()->getValueGE(lhsbits, _settings);
if (value) if (!value)
tooBigBitwiseShiftError(tok, lhsbits, value->intvalue); continue;
if (value->condition && !_settings->isEnabled("warning"))
continue;
if (value->inconclusive && !_settings->inconclusive)
continue;
tooBigBitwiseShiftError(tok, lhsbits, *value);
} }
} }
} }
void CheckOther::tooBigBitwiseShiftError(const Token *tok, int lhsbits, MathLib::bigint rhsbits) void CheckOther::tooBigBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits)
{ {
std::list<const Token*> callstack;
callstack.push_back(tok);
if (rhsbits.condition)
callstack.push_back(rhsbits.condition);
std::ostringstream errmsg; std::ostringstream errmsg;
errmsg << "Shifting " << lhsbits << "-bit value by " << rhsbits << " bits is undefined behaviour"; errmsg << "Shifting " << lhsbits << "-bit value by " << rhsbits.intvalue << " bits is undefined behaviour";
reportError(tok, Severity::error, "shiftTooManyBits", errmsg.str()); if (rhsbits.condition)
errmsg << ". See condition at line " << rhsbits.condition->linenr() << ".";
reportError(callstack, rhsbits.condition ? Severity::warning : Severity::error, "shiftTooManyBits", errmsg.str(), rhsbits.inconclusive);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@ -290,7 +290,7 @@ private:
void SuspiciousSemicolonError(const Token *tok); void SuspiciousSemicolonError(const Token *tok);
void doubleCloseDirError(const Token *tok, const std::string &varname); void doubleCloseDirError(const Token *tok, const std::string &varname);
void negativeBitwiseShiftError(const Token *tok); void negativeBitwiseShiftError(const Token *tok);
void tooBigBitwiseShiftError(const Token *tok, int lhsbits, MathLib::bigint rhsbits); void tooBigBitwiseShiftError(const Token *tok, int lhsbits, const ValueFlow::Value &rhsbits);
void redundantCopyError(const Token *tok, const std::string &varname); void redundantCopyError(const Token *tok, const std::string &varname);
void incompleteArrayFillError(const Token* tok, const std::string& buffer, const std::string& function, bool boolean); void incompleteArrayFillError(const Token* tok, const std::string& buffer, const std::string& function, bool boolean);
void varFuncNullUBError(const Token *tok); void varFuncNullUBError(const Token *tok);
@ -309,7 +309,7 @@ private:
c.doubleFreeError(0, "varname"); c.doubleFreeError(0, "varname");
c.invalidPointerCastError(0, "float", "double", false); c.invalidPointerCastError(0, "float", "double", false);
c.negativeBitwiseShiftError(0); c.negativeBitwiseShiftError(0);
c.tooBigBitwiseShiftError(0, 32, 64); c.tooBigBitwiseShiftError(0, 32, ValueFlow::Value(64));
c.checkPipeParameterSizeError(0, "varname", "dimension"); c.checkPipeParameterSizeError(0, "varname", "dimension");
//performance //performance