valueFlowContainerSize: assignment of string-like containers
This commit is contained in:
parent
189ea5003a
commit
7074eeb869
|
@ -3508,6 +3508,21 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
|
||||||
valueFlowContainerForward(var->nameToken()->next(), var->declarationId(), value, settings);
|
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
|
// conditional conditionSize
|
||||||
for (const Scope &scope : symboldatabase->scopeList) {
|
for (const Scope &scope : symboldatabase->scopeList) {
|
||||||
if (scope.type != Scope::ScopeType::eIf) // TODO: while
|
if (scope.type != Scope::ScopeType::eIf) // TODO: while
|
||||||
|
|
|
@ -3327,10 +3327,16 @@ private:
|
||||||
ASSERT(tokenValues(code, "v [").empty());
|
ASSERT(tokenValues(code, "v [").empty());
|
||||||
|
|
||||||
code = "void f() {\n"
|
code = "void f() {\n"
|
||||||
" std::list<int> ints;\n"
|
" std::list<int> ints;\n" // No value => ints is empty
|
||||||
" ints.front();\n"
|
" ints.front();\n"
|
||||||
"}";
|
"}";
|
||||||
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "ints . front"), 0));
|
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));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue