Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/token.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2255,6 +2255,7 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token* tok, bool poi
const Variable *var = tok->variable();
if (!var->typeStartToken() || !var->typeEndToken())
return {};
std::pair<const Token*, const Token*> result;
if (Token::simpleMatch(var->typeStartToken(), "auto")) {
const Token * tok2 = var->declEndToken();
if (Token::Match(tok2, "; %varid% =", var->declarationId()))
Expand All @@ -2272,6 +2273,17 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token* tok, bool poi
declEnd = declEnd->link()->next();
return { tok2->next(), declEnd };
}
const Token *typeBeg{}, *typeEnd{};
if (tok2->str() == "::" && Token::simpleMatch(tok2->astOperand2(), "{")) { // empty initlist
typeBeg = previousBeforeAstLeftmostLeaf(tok2);
typeEnd = tok2->astOperand2();
}
else if (tok2->str() == "{") {
typeBeg = previousBeforeAstLeftmostLeaf(tok2);
typeEnd = tok2;
}
if (typeBeg)
result = { typeBeg->next(), typeEnd }; // handle smart pointers/iterators first
}
if (astIsRangeBasedForDecl(var->nameToken()) && astIsContainer(var->nameToken()->astParent()->astOperand2())) { // range-based for
const ValueType* vt = var->nameToken()->astParent()->astOperand2()->valueType();
Expand All @@ -2288,6 +2300,8 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token* tok, bool poi
if (vt && vt->containerTypeToken)
return { vt->containerTypeToken, vt->containerTypeToken->linkAt(-1) };
}
if (result.first)
return result;
}
return {var->typeStartToken(), var->typeEndToken()->next()};
} else if (Token::simpleMatch(tok, "return")) {
Expand Down
23 changes: 23 additions & 0 deletions test/testfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1928,6 +1928,29 @@ class TestFunctions : public TestFixture {
"}\n");
ASSERT_EQUALS("", errout.str());

check("void f() {\n"
" auto v = std::vector<int>{};\n"
" v.push_back(1);\n"
" auto w = std::vector<int>{ 1, 2, 3 };\n"
" w.push_back(1);\n"
" auto x = std::vector<int>(1);\n"
" x.push_back(1);\n"
"}\n");
TODO_ASSERT_EQUALS("",
"[test.cpp:7]: (information) --check-library: There is no matching configuration for function auto::push_back()\n",
errout.str());

check("void f() {\n"
" auto p(std::make_shared<std::vector<int>>());\n"
" p->push_back(1);\n"
" auto q{ std::make_shared<std::vector<int>>{} };\n"
" q->push_back(1);\n"
"}\n");
TODO_ASSERT_EQUALS("",
"[test.cpp:3]: (information) --check-library: There is no matching configuration for function auto::push_back()\n"
"[test.cpp:5]: (information) --check-library: There is no matching configuration for function auto::push_back()\n",
errout.str());

settings.severity = severity_old;
settings.checkLibrary = false;
}
Expand Down