valueFlowContainerSize: handling of 'v.size() == 10'

This commit is contained in:
Daniel Marjamäki 2018-08-10 21:42:13 +02:00
parent 3947c23290
commit 2b10e38eec
2 changed files with 18 additions and 2 deletions

View File

@ -3460,21 +3460,27 @@ static void valueFlowContainerSize(TokenList * /*tokenlist*/, SymbolDatabase* sy
continue; continue;
const Token *conditionToken; const Token *conditionToken;
MathLib::bigint intval;
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())
continue; continue;
if (parent->astOperand1()->str() != "0" && parent->astOperand2()->str() != "0") if (parent->astOperand1()->hasKnownIntValue())
intval = parent->astOperand1()->values().front().intvalue;
else if (parent->astOperand2()->hasKnownIntValue())
intval = parent->astOperand2()->values().front().intvalue;
else
continue; continue;
conditionToken = parent; conditionToken = parent;
} else if (tok->valueType()->container->getYield(tok->strAt(2)) == Library::Container::Yield::EMPTY) { } else if (tok->valueType()->container->getYield(tok->strAt(2)) == Library::Container::Yield::EMPTY) {
conditionToken = tok->tokAt(3); conditionToken = tok->tokAt(3);
intval = 0;
} else { } else {
continue; continue;
} }
ValueFlow::Value value(conditionToken, 0LL); ValueFlow::Value value(conditionToken, intval);
value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE; value.valueType = ValueFlow::Value::ValueType::CONTAINER_SIZE;
valueFlowContainerReverse(scope.classDef, tok->varId(), value, settings); valueFlowContainerReverse(scope.classDef, tok->varId(), value, settings);
} }

View File

@ -3277,6 +3277,16 @@ private:
"}"; "}";
values = tokenValues(code, "ints . front"); values = tokenValues(code, "ints . front");
ASSERT_EQUALS(true, values.empty()); ASSERT_EQUALS(true, values.empty());
code = "void f(std::vector<int> v) {\n"
" v[10] = 0;\n"
" if (v.size() == 10) {}\n"
"}";
values = tokenValues(code, "v [");
ASSERT_EQUALS(1, values.size());
ASSERT_EQUALS(true, values.empty() ? true : values.front().isContainerSizeValue());
ASSERT_EQUALS(10, values.empty() ? 10 : values.front().intvalue);
} }
}; };