valueFlowContainerSize: add simple forward analysis

This commit is contained in:
Daniel Marjamäki 2018-08-10 22:06:23 +02:00
parent 2b10e38eec
commit 4a502a7f6b
2 changed files with 42 additions and 18 deletions

View File

@ -1832,7 +1832,7 @@ static bool valueFlowForward(Token * const startToken,
}
// Forward known values in the else branch
if(Token::simpleMatch(end, "} else {")) {
if (Token::simpleMatch(end, "} else {")) {
std::list<ValueFlow::Value> knownValues;
std::copy_if(values.begin(), values.end(), std::back_inserter(knownValues), std::mem_fn(&ValueFlow::Value::isKnown));
valueFlowForward(end->tokAt(2),
@ -3441,7 +3441,24 @@ static void valueFlowContainerReverse(const Token *tok, unsigned int containerId
if (Token::Match(tok, "%name% ="))
break;
if (!tok->valueType() || !tok->valueType()->container)
break;
if (Token::Match(tok, "%name% . %name% (") && tok->valueType()->container->getAction(tok->strAt(2)) != Library::Container::Action::NO_ACTION)
break;
setTokenValue(const_cast<Token *>(tok), value, settings);
}
}
static void valueFlowContainerForward(const Token *tok, unsigned int containerId, const ValueFlow::Value &value, const Settings *settings)
{
while (nullptr != (tok = tok->next())) {
if (Token::Match(tok, "[{}]"))
break;
if (tok->varId() != containerId)
continue;
if (Token::Match(tok, "%name% ="))
break;
if (!tok->valueType() || !tok->valueType()->container)
break;
if (Token::Match(tok, "%name% . %name% (") && tok->valueType()->container->getAction(tok->strAt(2)) != Library::Container::Action::NO_ACTION)
break;
setTokenValue(const_cast<Token *>(tok), value, settings);
@ -3483,6 +3500,10 @@ static void valueFlowContainerSize(TokenList * /*tokenlist*/, SymbolDatabase* sy
ValueFlow::Value value(conditionToken, intval);
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
valueFlowContainerReverse(scope.classDef, tok->varId(), value, settings);
const Token *after = scope.bodyEnd;
if (Token::simpleMatch(after, "} else {"))
after = after->linkAt(2);
valueFlowContainerForward(after, tok->varId(), value, settings);
}
}
}

View File

@ -116,8 +116,8 @@ private:
for (const Token *tok = tokenizer.tokens(); tok; tok = tok->next()) {
if (tok->str() == "x" && tok->linenr() == linenr) {
for(const ValueFlow::Value& val:tok->values()) {
if(val.isKnown() && val.intvalue == value)
for (const ValueFlow::Value& val:tok->values()) {
if (val.isKnown() && val.intvalue == value)
return true;
}
}
@ -3245,9 +3245,16 @@ private:
ASSERT_EQUALS(true, values.front().intvalue == 0 || values.back().intvalue == 0);
}
static bool isPossibleContainerSizeValue(const std::list<ValueFlow::Value> &values, MathLib::bigint i) {
return values.size() == 1 &&
values.front().isContainerSizeValue() &&
values.front().isPossible() &&
values.front().intvalue == i;
}
void valueFlowContainerSize() {
const char *code;
std::list<ValueFlow::Value> values;
LOAD_LIB_2(settings.library, "std.cfg");
@ -3256,37 +3263,33 @@ private:
" ints.front();\n"
" if (ints.empty()) {}\n"
"}";
values = tokenValues(code, "ints . front");
ASSERT_EQUALS(1, values.size());
ASSERT_EQUALS(true, values.empty() ? true : values.front().isContainerSizeValue());
ASSERT_EQUALS(0, values.empty() ? 0 : values.front().intvalue);
ASSERT(isPossibleContainerSizeValue(tokenValues(code, "ints . front"), 0));
code = "void f(const std::list<int> &ints) {\n"
" ints.front();\n"
" if (ints.size()==0) {}\n"
"}";
values = tokenValues(code, "ints . front");
ASSERT_EQUALS(1, values.size());
ASSERT_EQUALS(true, values.empty() ? true : values.front().isContainerSizeValue());
ASSERT_EQUALS(0, values.empty() ? 0 : values.front().intvalue);
ASSERT(isPossibleContainerSizeValue(tokenValues(code, "ints . front"), 0));
code = "void f(std::list<int> ints) {\n"
" ints.front();\n"
" ints.pop_back();\n"
" if (ints.empty()) {}\n"
"}";
values = tokenValues(code, "ints . front");
ASSERT_EQUALS(true, values.empty());
ASSERT(tokenValues(code, "ints . front").empty());
code = "void f(std::vector<int> v) {\n"
" v[10] = 0;\n"
" if (v.size() == 10) {}\n"
"}";
values = tokenValues(code, "v [");
ASSERT_EQUALS(1, values.size());
ASSERT_EQUALS(true, values.empty() ? true : values.front().isContainerSizeValue());
ASSERT_EQUALS(10, values.empty() ? 10 : values.front().intvalue);
ASSERT(isPossibleContainerSizeValue(tokenValues(code, "v ["), 10));
// valueFlowContainerForward
code = "void f(const std::list<int> &ints) {\n"
" if (ints.empty()) {}\n"
" ints.front();\n"
"}";
ASSERT(isPossibleContainerSizeValue(tokenValues(code, "ints . front"), 0));
}
};