Get type from auto with scope (#4822)

This commit is contained in:
chrchr-github 2023-02-24 21:05:26 +01:00 committed by GitHub
parent a030970160
commit d65cc696b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 5 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -2264,9 +2264,16 @@ std::pair<const Token*, const Token*> 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<const Token*, const Token*> 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<const Token*, const Token*> 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() };

View File

@ -1966,6 +1966,15 @@ private:
"}\n");
ASSERT_EQUALS("", errout.str());
check("namespace N {\n"
" struct S { static const std::set<std::string> 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;
}