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
33 changes: 31 additions & 2 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,30 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()

const bool doProgress = (mSettings.reportProgress != -1);

std::map<Scope *, std::set<std::string>> forwardDecls;

const std::function<Scope *(const Token *, Scope *)> findForwardDeclScope = [&](const Token *tok, Scope *startScope) {
Comment thread
chrchr-github marked this conversation as resolved.
if (tok->str() == "::")
return findForwardDeclScope(tok->next(), &scopeList.front());

if (Token::Match(tok, "%name% :: %name%")) {
auto it = std::find_if(startScope->nestedList.cbegin(), startScope->nestedList.cend(), [&](const Scope *scope) {
return scope->className == tok->str();
});

if (it == startScope->nestedList.cend())
return static_cast<Scope *>(nullptr);

return findForwardDeclScope(tok->tokAt(2), *it);
}

auto it = forwardDecls.find(startScope);
if (it == forwardDecls.cend())
return static_cast<Scope *>(nullptr);

return it->second.count(tok->str()) > 0 ? startScope : nullptr;
};

// find all scopes
for (const Token *tok = mTokenizer.tokens(); tok; tok = tok ? tok->next() : nullptr) {
// #5593 suggested to add here:
Expand Down Expand Up @@ -291,7 +315,11 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
scope = new_scope;
tok = tok2;
} else {
scopeList.emplace_back(*this, tok, scope);

const Scope *forwardDeclScope = findForwardDeclScope(tok->next(), scope);
const Scope *nestedIn = forwardDeclScope ? forwardDeclScope : scope;

scopeList.emplace_back(*this, tok, nestedIn);
new_scope = &scopeList.back();

if (tok->str() == "class")
Expand All @@ -303,7 +331,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
if (new_scope->isClassOrStructOrUnion() || new_scope->type == ScopeType::eEnum) {
Type* new_type = findType(name, scope);
if (!new_type) {
typeList.emplace_back(new_scope->classDef, new_scope, scope);
typeList.emplace_back(new_scope->classDef, new_scope, nestedIn);
new_type = &typeList.back();
scope->definedTypesMap[new_type->name()] = new_type;
} else
Expand Down Expand Up @@ -386,6 +414,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
typeList.emplace_back(tok, nullptr, scope);
Type* new_type = &typeList.back();
scope->definedTypesMap[new_type->name()] = new_type;
forwardDecls[scope].insert(tok->strAt(1));
}
tok = tok->tokAt(2);
}
Expand Down
14 changes: 14 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(namespaces2);
TEST_CASE(namespaces3); // #3854 - unknown macro
TEST_CASE(namespaces4);
TEST_CASE(namespaces5); // #13967
TEST_CASE(needInitialization);

TEST_CASE(tryCatch1);
Expand Down Expand Up @@ -3209,6 +3210,19 @@ class TestSymbolDatabase : public TestFixture {
ASSERT_EQUALS(2U, fredAType->classDef->linenr());
}

void namespaces5() { // #13967
Comment thread
ludviggunne marked this conversation as resolved.
GET_SYMBOL_DB("namespace test {\n"
" template <int S>\n"
" struct Test { int x[S]; };\n"
" const Test<64> test;\n"
"}\n");
const Variable *x = db->getVariableFromVarId(2U);
ASSERT_EQUALS("x", x->name());
Comment thread
ludviggunne marked this conversation as resolved.
const Scope *scope = x->scope();
ASSERT(scope->nestedIn);
ASSERT_EQUALS("test", scope->nestedIn->className);
Comment thread
ludviggunne marked this conversation as resolved.
}

void needInitialization() {
{
GET_SYMBOL_DB_DBG("template <typename T>\n" // #10259
Expand Down
Loading