Skip to content

fix #13967: Template simplifier: put Tokens into proper scope #7634

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be a static function?

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
13 changes: 13 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,18 @@ class TestSymbolDatabase : public TestFixture {
ASSERT_EQUALS(2U, fredAType->classDef->linenr());
}

void namespaces5() { // #13967
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is still about the TemplateSimplifier, but would a test there make sense?

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());
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I feel this test is a bit weird. Variable ids are just unique numbers. If the variable "x" would get varid 1002 in the future nothing is wrong about that but this test will fail. But on the other hand we have lots of similar tests already in testsymboldatabase so you just followed the common pattern!
So we can keep it..

const Scope *scope = x->scope();
ASSERT_EQUALS("test", scope->nestedIn->className);
Copy link
Owner

@danmar danmar Jun 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test will not work well if scope->nestedIn is nullptr I want an extra ASSERT to ensure a clear error report is written..

}

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