valueFlowContainerSize: assignment of string-like containers

This commit is contained in:
Daniel Marjamäki 2018-08-12 22:01:58 +02:00
parent 189ea5003a
commit 7074eeb869
2 changed files with 22 additions and 1 deletions

View File

@ -3508,6 +3508,21 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
valueFlowContainerForward(var->nameToken()->next(), var->declarationId(), value, settings);
}
// after assignment
for (const Scope *functionScope : symboldatabase->functionScopes) {
for (const Token *tok = functionScope->bodyStart; tok != functionScope->bodyEnd; tok = tok->next()) {
if (Token::Match(tok, "[;{}] %var% = %str% ;")) {
const Token *containerTok = tok->next();
if (containerTok && containerTok->valueType() && containerTok->valueType()->container && containerTok->valueType()->container->stdStringLike) {
ValueFlow::Value value(Token::getStrLength(containerTok->tokAt(2)));
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
value.setKnown();
valueFlowContainerForward(containerTok->next(), containerTok->varId(), value, settings);
}
}
}
}
// conditional conditionSize
for (const Scope &scope : symboldatabase->scopeList) {
if (scope.type != Scope::ScopeType::eIf) // TODO: while

View File

@ -3327,10 +3327,16 @@ private:
ASSERT(tokenValues(code, "v [").empty());
code = "void f() {\n"
" std::list<int> ints;\n"
" std::list<int> ints;\n" // No value => ints is empty
" ints.front();\n"
"}";
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 0));
code = "void f() {\n"
" std::string s=\"abc\";\n" // size of s is 3
" s.size();\n"
"}";
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "s . size"), 3));
}
};