valueFlowContainerSize: empty container variables

This commit is contained in:
Daniel Marjamäki 2018-08-12 14:39:29 +02:00
parent cfd5460670
commit 189ea5003a
2 changed files with 21 additions and 0 deletions

View File

@ -3494,6 +3494,21 @@ static void valueFlowContainerForward(const Token *tok, unsigned int containerId
static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symboldatabase, ErrorLogger * /*errorLogger*/, const Settings *settings)
{
// declaration
for (const Variable *var : symboldatabase->variableList()) {
if (!var || !var->isLocal() || var->isPointer() || var->isReference())
continue;
if (!var->valueType() || !var->valueType()->container)
continue;
if (!Token::Match(var->nameToken(), "%name% ;"))
continue;
ValueFlow::Value value(0);
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
value.setKnown();
valueFlowContainerForward(var->nameToken()->next(), var->declarationId(), value, settings);
}
// conditional conditionSize
for (const Scope &scope : symboldatabase->scopeList) {
if (scope.type != Scope::ScopeType::eIf) // TODO: while
continue;

View File

@ -3325,6 +3325,12 @@ private:
" if (!v.empty() && v[10]==0) {}\n" // <- no container size for 'v[10]'
"}";
ASSERT(tokenValues(code, "v [").empty());
code = "void f() {\n"
" std::list<int> ints;\n"
" ints.front();\n"
"}";
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 0));
}
};