ValueFlow: Improve containerSize handling of string like classes

This commit is contained in:
Daniel Marjamäki 2018-10-28 19:14:00 +01:00
parent 8beb42cc90
commit c03d32b429
2 changed files with 44 additions and 15 deletions

View File

@ -3598,12 +3598,11 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
for (const Token *tok = scope.classDef; tok && tok->str() != "{"; tok = tok->next()) { for (const Token *tok = scope.classDef; tok && tok->str() != "{"; tok = tok->next()) {
if (!tok->isName() || !tok->valueType() || tok->valueType()->type != ValueType::CONTAINER || !tok->valueType()->container) if (!tok->isName() || !tok->valueType() || tok->valueType()->type != ValueType::CONTAINER || !tok->valueType()->container)
continue; continue;
if (!Token::Match(tok, "%name% . %name% ("))
continue;
const Token *conditionToken; const Token *conditionToken;
MathLib::bigint intval; MathLib::bigint intval;
if (Token::Match(tok, "%name% . %name% (")) {
if (tok->valueType()->container->getYield(tok->strAt(2)) == Library::Container::Yield::SIZE) { if (tok->valueType()->container->getYield(tok->strAt(2)) == Library::Container::Yield::SIZE) {
const Token *parent = tok->tokAt(3)->astParent(); const Token *parent = tok->tokAt(3)->astParent();
if (!parent || !parent->isComparisonOp() || !parent->astOperand2()) if (!parent || !parent->isComparisonOp() || !parent->astOperand2())
@ -3621,6 +3620,12 @@ static void valueFlowContainerSize(TokenList *tokenlist, SymbolDatabase* symbold
} else { } else {
continue; continue;
} }
} else if (tok->valueType()->container->stdStringLike && Token::Match(tok, "%name% ==|!= %str%") && tok->next()->astOperand2() == tok->tokAt(2)) {
intval = Token::getStrLength(tok->tokAt(2));
conditionToken = tok->next();
} else {
continue;
}
ValueFlow::Value value(conditionToken, intval); ValueFlow::Value value(conditionToken, intval);
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;

View File

@ -3398,6 +3398,30 @@ private:
"}"; "}";
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "s . size"), 6)); ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "s . size"), 6));
code = "void f(std::string s) {\n"
" if (s == \"hello\")\n"
" s[40] = c;\n"
"}";
ASSERT_EQUALS("", isKnownContainerSizeValue(tokenValues(code, "s ["), 5));
code = "void f(std::string s) {\n"
" s[40] = c;\n"
" if (s == \"hello\") {}\n"
"}";
ASSERT_EQUALS("", isPossibleContainerSizeValue(tokenValues(code, "s ["), 5));
code = "void f(std::string s) {\n"
" if (s != \"hello\") {}\n"
" s[40] = c;\n"
"}";
ASSERT_EQUALS("", isPossibleContainerSizeValue(tokenValues(code, "s ["), 5));
code = "void f(std::string s) {\n"
" if (s != \"hello\")\n"
" s[40] = c;\n"
"}";
ASSERT(tokenValues(code, "s [").empty());
// valueFlowContainerForward, function call // valueFlowContainerForward, function call
code = "void f() {\n" code = "void f() {\n"
" std::list<int> x;\n" " std::list<int> x;\n"