Fix FP with lifetime containers (#1865)

This commit is contained in:
Paul Fultz II 2019-06-02 03:14:48 -05:00 committed by Daniel Marjamäki
parent 9e457b6136
commit 6ae7be0f53
3 changed files with 17 additions and 1 deletions

View File

@ -1827,6 +1827,12 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token * tok)
}
std::string Token::typeStr(const Token* tok)
{
if (tok->valueType()) {
const ValueType * vt = tok->valueType();
std::string ret = vt->str();
if (!ret.empty())
return ret;
}
std::pair<const Token*, const Token*> r = Token::typeDecl(tok);
if (!r.first || !r.second)
return "";

View File

@ -3161,7 +3161,7 @@ static void valueFlowLifetimeFunction(Token *tok, TokenList *tokenlist, ErrorLog
Token *vartok = tok->tokAt(-2);
std::vector<const Token *> args = getArguments(tok);
std::size_t n = args.size();
if (n > 1 && astCanonicalType(args[n - 2]) == astCanonicalType(args[n - 1]) &&
if (n > 1 && Token::typeStr(args[n - 2]) == Token::typeStr(args[n - 1]) &&
(((astIsIterator(args[n - 2]) && astIsIterator(args[n - 1])) ||
(astIsPointer(args[n - 2]) && astIsPointer(args[n - 1]))))) {
LifetimeStore{args.back(), "Added to container '" + vartok->str() + "'.", ValueFlow::Value::Object} .byDerefCopy(

View File

@ -1701,6 +1701,16 @@ private:
"};\n");
ASSERT_EQUALS("", errout.str());
check("void f(bool b) {\n"
" std::vector<int> v = {1};\n"
" if (b) {\n"
" int a[] = {0};\n"
" v.insert(a, a+1);\n"
" }\n"
" return v.back() == 0;\n"
"}\n");
ASSERT_EQUALS("", errout.str());
check("class A {\n"
" int f( P p ) {\n"
" std::vector< S > maps;\n"