Skip to content

Commit d65cc69

Browse files
Get type from auto with scope (#4822)
1 parent a030970 commit d65cc69

4 files changed

Lines changed: 21 additions & 5 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ void SymbolDatabase::createSymbolDatabaseVariableSymbolTable()
972972
{
973973
// create variable symbol table
974974
mVariableList.resize(mTokenizer->varIdCount() + 1);
975-
std::fill_n(mVariableList.begin(), mVariableList.size(), (const Variable*)nullptr);
975+
std::fill_n(mVariableList.begin(), mVariableList.size(), nullptr);
976976

977977
// check all scopes for variables
978978
for (Scope& scope : scopeList) {

lib/templatesimplifier.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -965,9 +965,9 @@ void TemplateSimplifier::getTemplateInstantiations()
965965
// insert using namespace into token stream
966966
std::string::size_type offset = 0;
967967
std::string::size_type pos = 0;
968-
while ((pos = nameSpace.substr(offset).find(' ')) != std::string::npos) {
969-
qualificationTok->insertToken(nameSpace.substr(offset, pos), emptyString, true);
970-
offset = offset + pos + 1;
968+
while ((pos = nameSpace.find(' ', offset)) != std::string::npos) {
969+
qualificationTok->insertToken(nameSpace.substr(offset, pos - offset), emptyString, true);
970+
offset = pos + 1;
971971
}
972972
qualificationTok->insertToken(nameSpace.substr(offset), emptyString, true);
973973
qualificationTok->insertToken("::", emptyString, true);

lib/token.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2264,9 +2264,16 @@ std::pair<const Token*, const Token*> Token::typeDecl(const Token* tok, bool poi
22642264
tok2 = tok2->tokAt(2);
22652265
if (Token::simpleMatch(tok2, "=") && Token::Match(tok2->astOperand2(), "!!=") && tok != tok2->astOperand2()) {
22662266
tok2 = tok2->astOperand2();
2267-
std::pair<const Token*, const Token*> r = typeDecl(tok2);
2267+
2268+
const Token* varTok = tok2; // try to find a variable
2269+
if (Token::Match(varTok, ":: %name%"))
2270+
varTok = varTok->next();
2271+
while (Token::Match(varTok, "%name% ::"))
2272+
varTok = varTok->tokAt(2);
2273+
std::pair<const Token*, const Token*> r = typeDecl(varTok);
22682274
if (r.first)
22692275
return r;
2276+
22702277
if (pointedToType && tok2->astOperand1() && Token::simpleMatch(tok2, "new")) {
22712278
if (Token::simpleMatch(tok2->astOperand1(), "("))
22722279
return { tok2->next(), tok2->astOperand1() };

test/testfunctions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1966,6 +1966,15 @@ class TestFunctions : public TestFixture {
19661966
"}\n");
19671967
ASSERT_EQUALS("", errout.str());
19681968

1969+
check("namespace N {\n"
1970+
" struct S { static const std::set<std::string> s; };\n"
1971+
"}\n"
1972+
"void f() {\n"
1973+
" const auto& t = N::S::s;\n"
1974+
" if (t.find(\"abc\") != t.end()) {}\n"
1975+
"}\n");
1976+
ASSERT_EQUALS("", errout.str());
1977+
19691978
settings = settings_old;
19701979
}
19711980

0 commit comments

Comments
 (0)