From d65cc696b03def88c0e95317f4eef1a3e2023f5f Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Fri, 24 Feb 2023 21:05:26 +0100 Subject: [PATCH] Get type from auto with scope (#4822) --- lib/symboldatabase.cpp | 2 +- lib/templatesimplifier.cpp | 6 +++--- lib/token.cpp | 9 ++++++++- test/testfunctions.cpp | 9 +++++++++ 4 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index eb1861d6e..9e216a355 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -972,7 +972,7 @@ void SymbolDatabase::createSymbolDatabaseVariableSymbolTable() { // create variable symbol table mVariableList.resize(mTokenizer->varIdCount() + 1); - std::fill_n(mVariableList.begin(), mVariableList.size(), (const Variable*)nullptr); + std::fill_n(mVariableList.begin(), mVariableList.size(), nullptr); // check all scopes for variables for (Scope& scope : scopeList) { diff --git a/lib/templatesimplifier.cpp b/lib/templatesimplifier.cpp index 45a532842..922411325 100644 --- a/lib/templatesimplifier.cpp +++ b/lib/templatesimplifier.cpp @@ -965,9 +965,9 @@ void TemplateSimplifier::getTemplateInstantiations() // insert using namespace into token stream std::string::size_type offset = 0; std::string::size_type pos = 0; - while ((pos = nameSpace.substr(offset).find(' ')) != std::string::npos) { - qualificationTok->insertToken(nameSpace.substr(offset, pos), emptyString, true); - offset = offset + pos + 1; + while ((pos = nameSpace.find(' ', offset)) != std::string::npos) { + qualificationTok->insertToken(nameSpace.substr(offset, pos - offset), emptyString, true); + offset = pos + 1; } qualificationTok->insertToken(nameSpace.substr(offset), emptyString, true); qualificationTok->insertToken("::", emptyString, true); diff --git a/lib/token.cpp b/lib/token.cpp index 40be28382..d2fe45e4a 100644 --- a/lib/token.cpp +++ b/lib/token.cpp @@ -2264,9 +2264,16 @@ std::pair Token::typeDecl(const Token* tok, bool poi tok2 = tok2->tokAt(2); if (Token::simpleMatch(tok2, "=") && Token::Match(tok2->astOperand2(), "!!=") && tok != tok2->astOperand2()) { tok2 = tok2->astOperand2(); - std::pair r = typeDecl(tok2); + + const Token* varTok = tok2; // try to find a variable + if (Token::Match(varTok, ":: %name%")) + varTok = varTok->next(); + while (Token::Match(varTok, "%name% ::")) + varTok = varTok->tokAt(2); + std::pair r = typeDecl(varTok); if (r.first) return r; + if (pointedToType && tok2->astOperand1() && Token::simpleMatch(tok2, "new")) { if (Token::simpleMatch(tok2->astOperand1(), "(")) return { tok2->next(), tok2->astOperand1() }; diff --git a/test/testfunctions.cpp b/test/testfunctions.cpp index 937d2c57f..50648588b 100644 --- a/test/testfunctions.cpp +++ b/test/testfunctions.cpp @@ -1966,6 +1966,15 @@ private: "}\n"); ASSERT_EQUALS("", errout.str()); + check("namespace N {\n" + " struct S { static const std::set s; };\n" + "}\n" + "void f() {\n" + " const auto& t = N::S::s;\n" + " if (t.find(\"abc\") != t.end()) {}\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + settings = settings_old; }