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,26 +3598,31 @@ 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 (tok->valueType()->container->getYield(tok->strAt(2)) == Library::Container::Yield::SIZE) { if (Token::Match(tok, "%name% . %name% (")) {
const Token *parent = tok->tokAt(3)->astParent(); if (tok->valueType()->container->getYield(tok->strAt(2)) == Library::Container::Yield::SIZE) {
if (!parent || !parent->isComparisonOp() || !parent->astOperand2()) const Token *parent = tok->tokAt(3)->astParent();
if (!parent || !parent->isComparisonOp() || !parent->astOperand2())
continue;
if (parent->astOperand1()->hasKnownIntValue())
intval = parent->astOperand1()->values().front().intvalue;
else if (parent->astOperand2()->hasKnownIntValue())
intval = parent->astOperand2()->values().front().intvalue;
else
continue;
conditionToken = parent;
} else if (tok->valueType()->container->getYield(tok->strAt(2)) == Library::Container::Yield::EMPTY) {
conditionToken = tok->tokAt(3);
intval = 0;
} else {
continue; continue;
if (parent->astOperand1()->hasKnownIntValue()) }
intval = parent->astOperand1()->values().front().intvalue; } else if (tok->valueType()->container->stdStringLike && Token::Match(tok, "%name% ==|!= %str%") && tok->next()->astOperand2() == tok->tokAt(2)) {
else if (parent->astOperand2()->hasKnownIntValue()) intval = Token::getStrLength(tok->tokAt(2));
intval = parent->astOperand2()->values().front().intvalue; conditionToken = tok->next();
else
continue;
conditionToken = parent;
} else if (tok->valueType()->container->getYield(tok->strAt(2)) == Library::Container::Yield::EMPTY) {
conditionToken = tok->tokAt(3);
intval = 0;
} else { } else {
continue; continue;
} }

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"