Skip to content

Commit

Permalink
Fix #12761 (SymbolDatabase: two structs with same name, constructor a…
Browse files Browse the repository at this point in the history
…dded to the wrong struct in the SymbolDatabase) (#6424)
  • Loading branch information
danmar committed May 23, 2024
1 parent 7c095f2 commit f066778
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
7 changes: 3 additions & 4 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6194,10 +6194,9 @@ const Scope *SymbolDatabase::findScope(const Token *tok, const Scope *startScope
if (tok->strAt(1) == "::") {
scope = scope->findRecordInNestedList(tok->str());
tok = tok->tokAt(2);
} else if (tok->strAt(1) == "<" && Token::simpleMatch(tok->linkAt(1), "> ::")) {
scope = scope->findRecordInNestedList(tok->str());
tok = tok->linkAt(1)->tokAt(2);
} else
} else if (tok->strAt(1) == "<")
return nullptr;
else
return scope->findRecordInNestedList(tok->str());
}

Expand Down
42 changes: 38 additions & 4 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(createSymbolDatabaseFindAllScopes5);
TEST_CASE(createSymbolDatabaseFindAllScopes6);
TEST_CASE(createSymbolDatabaseFindAllScopes7);
TEST_CASE(createSymbolDatabaseFindAllScopes8); // #12761

TEST_CASE(createSymbolDatabaseIncompleteVars);

Expand Down Expand Up @@ -5611,7 +5612,7 @@ class TestSymbolDatabase : public TestFixture {
"static const int foo = 8;\n"
"}\n");
ASSERT(db);
ASSERT_EQUALS(6, db->scopeList.size());
ASSERT_EQUALS(9, db->scopeList.size());
ASSERT_EQUALS(1, db->scopeList.front().varlist.size());
auto list = db->scopeList;
list.pop_front();
Expand Down Expand Up @@ -5658,12 +5659,12 @@ class TestSymbolDatabase : public TestFixture {
"};\n"
"const S S::IN(1);\n");
ASSERT(db);
ASSERT_EQUALS(6, db->scopeList.size());
ASSERT_EQUALS(7, db->scopeList.size());
const Token* const var = Token::findsimplematch(tokenizer.tokens(), "IN (");
ASSERT(var && var->variable());
ASSERT_EQUALS(var->variable()->name(), "IN");
auto it = db->scopeList.begin();
std::advance(it, 4);
auto it = db->scopeList.cbegin();
std::advance(it, 5);
ASSERT_EQUALS(it->className, "S");
ASSERT_EQUALS(var->variable()->scope(), &*it);
}
Expand Down Expand Up @@ -5754,6 +5755,39 @@ class TestSymbolDatabase : public TestFixture {
}
}

void createSymbolDatabaseFindAllScopes8() // #12761
{
// There are 2 myst constructors. They should belong to different scopes.
GET_SYMBOL_DB("template <class A = void, class B = void, class C = void>\n"
"class Test {\n"
"private:\n"
" template <class T>\n"
" struct myst {\n"
" T* x;\n"
" myst(T* y) : x(y){};\n" // <- myst constructor
" };\n"
"};\n"
"\n"
"template <class A, class B> class Test<A, B, void> {};\n"
"\n"
"template <>\n"
"class Test<void, void, void> {\n"
"private:\n"
" template <class T>\n"
" struct myst {\n"
" T* x;\n"
" myst(T* y) : x(y){};\n" // <- myst constructor
" };\n"
"};");
ASSERT(db);

const Token* myst1 = Token::findsimplematch(tokenizer.tokens(), "myst ( T * y )");
const Token* myst2 = Token::findsimplematch(myst1->next(), "myst ( T * y )");
ASSERT(myst1);
ASSERT(myst2);
ASSERT(myst1->scope() != myst2->scope());
}

void createSymbolDatabaseIncompleteVars()
{
{
Expand Down

0 comments on commit f066778

Please sign in to comment.