ValueFlow: Add ErrorPath info after for loop

This commit is contained in:
Daniel Marjamäki 2017-05-19 16:32:58 +02:00
parent 2bb54fef69
commit f92b16706c
3 changed files with 30 additions and 4 deletions

View File

@ -1972,9 +1972,7 @@ static void valueFlowAfterAssign(TokenList *tokenlist, SymbolDatabase* symboldat
std::list<ValueFlow::Value> values = tok->astOperand2()->values(); std::list<ValueFlow::Value> values = tok->astOperand2()->values();
for (std::list<ValueFlow::Value>::iterator it = values.begin(); it != values.end(); ++it) { for (std::list<ValueFlow::Value>::iterator it = values.begin(); it != values.end(); ++it) {
std::string info = "Assignment"; std::string info = "Assignment, " + it->infoString();
if (it->isIntValue())
info += ", integer value " + MathLib::toString(it->intvalue);
it->errorPath.push_back(ErrorPathItem(tok->astOperand2(), info)); it->errorPath.push_back(ErrorPathItem(tok->astOperand2(), info));
} }
const bool constValue = tok->astOperand2()->isNumber(); const bool constValue = tok->astOperand2()->isNumber();
@ -2538,6 +2536,7 @@ static void valueFlowForLoopSimplifyAfter(Token *fortok, unsigned int varid, con
std::list<ValueFlow::Value> values; std::list<ValueFlow::Value> values;
values.push_back(ValueFlow::Value(num)); values.push_back(ValueFlow::Value(num));
values.back().errorPath.push_back(ErrorPathItem(fortok,"After for loop, " + values.back().infoString()));
valueFlowForward(fortok->linkAt(1)->linkAt(1)->next(), valueFlowForward(fortok->linkAt(1)->linkAt(1)->next(),
endToken, endToken,
@ -2768,7 +2767,7 @@ static void valueFlowSubFunction(TokenList *tokenlist, ErrorLogger *errorLogger,
// Error path.. // Error path..
for (std::list<ValueFlow::Value>::iterator it = argvalues.begin(); it != argvalues.end(); ++it) for (std::list<ValueFlow::Value>::iterator it = argvalues.begin(); it != argvalues.end(); ++it)
it->errorPath.push_back(ErrorPathItem(argtok, "Function argument, integer value " + MathLib::toString(it->intvalue))); it->errorPath.push_back(ErrorPathItem(argtok, "Function argument, " + it->infoString()));
// passed values are not "known".. // passed values are not "known"..
for (std::list<ValueFlow::Value>::iterator it = argvalues.begin(); it != argvalues.end(); ++it) { for (std::list<ValueFlow::Value>::iterator it = argvalues.begin(); it != argvalues.end(); ++it) {
@ -2938,6 +2937,23 @@ ValueFlow::Value::Value(const Token *c, long long val)
errorPath.push_back(ErrorPathItem(c, "Condition '" + c->expressionString() + "'")); errorPath.push_back(ErrorPathItem(c, "Condition '" + c->expressionString() + "'"));
} }
std::string ValueFlow::Value::infoString() const
{
switch (valueType) {
case INT:
return "integer value " + MathLib::toString(intvalue);
case TOK:
return "value " + tokvalue->str();
case FLOAT:
return "float value " + MathLib::toString(floatValue);
case MOVED:
return "value <Moved>";
case UNINIT:
return "value <Uninit>";
};
throw InternalError(nullptr, "Invalid ValueFlow Value type");
}
const ValueFlow::Value *ValueFlow::valueFlowConstantFoldAST(const Token *expr, const Settings *settings) const ValueFlow::Value *ValueFlow::valueFlowConstantFoldAST(const Token *expr, const Settings *settings)
{ {
if (expr && expr->values().empty()) { if (expr && expr->values().empty()) {

View File

@ -74,6 +74,8 @@ namespace ValueFlow {
valueKind == rhs.valueKind; valueKind == rhs.valueKind;
} }
std::string infoString() const;
enum ValueType { INT, TOK, FLOAT, MOVED, UNINIT } valueType; enum ValueType { INT, TOK, FLOAT, MOVED, UNINIT } valueType;
bool isIntValue() const { bool isIntValue() const {
return valueType == INT; return valueType == INT;

View File

@ -618,6 +618,14 @@ private:
ASSERT_EQUALS("5,Assignment, integer value 3\n" ASSERT_EQUALS("5,Assignment, integer value 3\n"
"6,Function argument, integer value 4\n", "6,Function argument, integer value 4\n",
getErrorPathForX(code, 2U)); getErrorPathForX(code, 2U));
code = "void f(int a) {\n"
" int x;\n"
" for (x = a; x < 50; x++) {}\n"
" b = x;\n"
"}\n";
ASSERT_EQUALS("3,After for loop, integer value 50\n",
getErrorPathForX(code, 4U));
} }
void valueFlowBeforeCondition() { void valueFlowBeforeCondition() {