Value flow: refactorings, write debug warning if iterating too far

This commit is contained in:
Daniel Marjamäki 2014-01-05 20:06:46 +01:00
parent 86201c13cb
commit afaf1473a8
4 changed files with 43 additions and 18 deletions

View File

@ -2158,8 +2158,10 @@ void CheckOther::checkZeroDivision()
std::list<ValueFlow::Value>::const_iterator it;
for (it = values.begin(); it != values.end(); ++it) {
if (it->intvalue == 0) {
if (!it->link || _settings->isEnabled("warning"))
if (it->condition == NULL)
zerodivError(tok);
else if (_settings->isEnabled("warning"))
zerodivcondError(it->condition,tok);
}
}
}
@ -2176,6 +2178,11 @@ void CheckOther::checkZeroDivisionOrUselessCondition()
{
if (!_settings->isEnabled("warning"))
return;
// Use experimental checking instead based on value flow analysis
if (_settings->valueFlow)
return;
const SymbolDatabase *symbolDatabase = _tokenizer->getSymbolDatabase();
const std::size_t numberOfFunctions = symbolDatabase->functionScopes.size();
for (std::size_t functionIndex = 0; functionIndex < numberOfFunctions; ++functionIndex) {

View File

@ -1605,7 +1605,7 @@ bool Tokenizer::tokenize(std::istream &code,
list.createAst();
if (_settings->valueFlow)
ValueFlow::setValues(list.front());
ValueFlow::setValues(&list, _errorLogger, _settings);
return true;
}

View File

@ -17,20 +17,27 @@
*/
#include "valueflow.h"
#include "token.h"
#include "errorlogger.h"
#include "mathlib.h"
#include "settings.h"
#include "symboldatabase.h"
#include "token.h"
#include "tokenlist.h"
static void valueFlowBeforeCondition(Token *tokens)
static void valueFlowBeforeCondition(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings)
{
for (Token *tok = tokens; tok; tok = tok->next()) {
for (Token *tok = tokenlist->front(); tok; tok = tok->next()) {
unsigned int varid;
MathLib::bigint num;
const Variable *var;
if (Token::Match(tok, "==|!=|>=|<=") && tok->astOperand1() && tok->astOperand2()) {
if (tok->astOperand1()->isName() && tok->astOperand2()->isNumber()) {
varid = tok->astOperand1()->varId();
var = tok->astOperand1()->variable();
num = MathLib::toLongNumber(tok->astOperand2()->str());
} else if (tok->astOperand1()->isNumber() && tok->astOperand2()->isName()) {
varid = tok->astOperand2()->varId();
var = tok->astOperand2()->variable();
num = MathLib::toLongNumber(tok->astOperand1()->str());
} else {
continue;
@ -38,9 +45,11 @@ static void valueFlowBeforeCondition(Token *tokens)
} else if (Token::Match(tok->previous(), "if|while ( %var% %oror%|&&|)") ||
Token::Match(tok, "%oror%|&& %var% %oror%|&&|)")) {
varid = tok->next()->varId();
var = tok->next()->variable();
num = 0;
} else if (tok->str() == "!" && tok->astOperand1() && tok->astOperand1()->isName()) {
varid = tok->astOperand1()->varId();
var = tok->astOperand1()->variable();
num = 0;
} else {
continue;
@ -50,26 +59,33 @@ static void valueFlowBeforeCondition(Token *tokens)
continue;
struct ValueFlow::Value val;
val.link = tok;
val.condition = tok;
val.intvalue = num;
for (Token *tok2 = tok->previous(); tok2; tok2 = tok2->previous()) {
if (tok2->varId() == varid)
tok2->values.push_back(val);
if (tok2->str() == "{") {
if (!Token::simpleMatch(tok2->previous(), ") {"))
for (Token *tok2 = tok->previous(); ; tok2 = tok2->previous()) {
if (!tok2) {
if (settings->debugwarnings) {
std::list<ErrorLogger::ErrorMessage::FileLocation> callstack;
callstack.push_back(ErrorLogger::ErrorMessage::FileLocation(tok,tokenlist));
ErrorLogger::ErrorMessage errmsg(callstack, Severity::debug, "iterated too far", "debugValueFlowBeforeCondition", false);
errorLogger->reportErr(errmsg);
}
break;
if (!Token::simpleMatch(tok2->previous()->link()->previous(), "if ("))
}
if (tok2->varId() == varid) {
tok2->values.push_back(val);
if (var && tok2 == var->nameToken())
break;
}
}
}
}
void ValueFlow::setValues(Token *tokens)
void ValueFlow::setValues(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings)
{
for (Token *tok = tokens; tok; tok = tok->next())
for (Token *tok = tokenlist->front(); tok; tok = tok->next())
tok->values.clear();
valueFlowBeforeCondition(tokens);
valueFlowBeforeCondition(tokenlist, errorLogger, settings);
}

View File

@ -22,15 +22,17 @@
//---------------------------------------------------------------------------
class Token;
class TokenList;
class ErrorLogger;
class Settings;
namespace ValueFlow {
struct Value {
const Token *link;
const Token *condition;
long long intvalue;
};
void setValues(Token *tokens);
void setValues(TokenList *tokenlist, ErrorLogger *errorLogger, const Settings *settings);
}
#endif // valueflowH