From a9ad7084bf7c29519270972fcc6e8e8e8716a375 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:01:41 +0200 Subject: [PATCH 1/6] Update symboldatabase.cpp --- lib/symboldatabase.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 32c30f9521f..92d67d9aa81 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5319,6 +5319,17 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::setscope && enumerator->scope->enumClass)) return enumerator; + + if (tok->isCpp() && (*s)->type == Scope::eNamespace && Token::simpleMatch((*s)->classDef, "namespace {")) { + for (const Scope* nested : (*s)->nestedList) { + if (nested->type != Scope::eEnum) + continue; + enumerator = nested->findEnumerator(tokStr); + + if (enumerator && !(enumerator->scope && enumerator->scope->enumClass)) + return enumerator; + } + } } } } From 9cf704bed6133638fb0c22d275f21ab29ed67287 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:03:08 +0200 Subject: [PATCH 2/6] Update testsymboldatabase.cpp --- test/testsymboldatabase.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 082eda73141..e07f3623066 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -451,6 +451,7 @@ class TestSymbolDatabase : public TestFixture { TEST_CASE(enum15); TEST_CASE(enum16); TEST_CASE(enum17); + TEST_CASE(enum18); TEST_CASE(sizeOfType); @@ -6452,6 +6453,25 @@ class TestSymbolDatabase : public TestFixture { } } + void enum18() { + { + GET_SYMBOL_DB("namespace {\n" + " enum { E0 };\n" + "}\n" + "void f() {\n" + " if (0 > E0) {}\n" + "}\n"); + ASSERT(db != nullptr); + auto it = db->scopeList.begin(); + std::advance(it, 2); + const Enumerator* E0 = it->findEnumerator("E0"); + ASSERT(E0 && E0->value_known && E0->value == 0); + const Token* const e = Token::findsimplematch(tokenizer.tokens(), "E0 )"); + ASSERT(e && e->enumerator()); + ASSERT_EQUALS(E0, e->enumerator()); + } + } + void sizeOfType() { // #7615 - crash in Symboldatabase::sizeOfType() GET_SYMBOL_DB("enum e;\n" From 5238090824c09868089c72e496c97bccc7b099da Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 18 Jul 2024 11:06:45 +0200 Subject: [PATCH 3/6] Redundant check --- lib/symboldatabase.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 92d67d9aa81..a7c7e4a83b0 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5320,7 +5320,7 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::setscope && enumerator->scope->enumClass)) return enumerator; - if (tok->isCpp() && (*s)->type == Scope::eNamespace && Token::simpleMatch((*s)->classDef, "namespace {")) { + if ((*s)->type == Scope::eNamespace && Token::simpleMatch((*s)->classDef, "namespace {")) { for (const Scope* nested : (*s)->nestedList) { if (nested->type != Scope::eEnum) continue; From aa7e25a695bb37f45d3703f61bdd31b55a14e3c9 Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 18 Jul 2024 19:44:54 +0200 Subject: [PATCH 4/6] Update symboldatabase.cpp --- lib/symboldatabase.cpp | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index a7c7e4a83b0..f266664f1b1 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5190,6 +5190,22 @@ static const Scope* findEnumScopeInBase(const Scope* scope, const std::string& t return nullptr; } +static const Enumerator* findEnumeratorInUsingList(const Scope* scope, const std::string& name) +{ + for (const auto& u : scope->usingList) { + if (!u.scope) + continue; + for (const Scope* nested : u.scope->nestedList) { + if (nested->type != Scope::eEnum) + continue; + const Enumerator* e = nested->findEnumerator(name); + if (e && !(e->scope && e->scope->enumClass)) + return e; + } + } + return nullptr; +} + const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::set& tokensThatAreNotEnumeratorValues) const { if (tok->isKeyword()) @@ -5268,6 +5284,8 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::setfindEnumerator(tokStr); + if (!enumerator) + enumerator = findEnumeratorInUsingList(scope, tokStr); if (enumerator && !(enumerator->scope && enumerator->scope->enumClass)) return enumerator; @@ -5310,6 +5328,8 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::setnestedIn; enumerator = scope->findEnumerator(tokStr); + if (!enumerator) + enumerator = findEnumeratorInUsingList(scope, tokStr); if (enumerator && !(enumerator->scope && enumerator->scope->enumClass)) return enumerator; @@ -5320,7 +5340,7 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::setscope && enumerator->scope->enumClass)) return enumerator; - if ((*s)->type == Scope::eNamespace && Token::simpleMatch((*s)->classDef, "namespace {")) { + if (tok->isCpp() && (*s)->type == Scope::eNamespace && Token::simpleMatch((*s)->classDef, "namespace {")) { for (const Scope* nested : (*s)->nestedList) { if (nested->type != Scope::eEnum) continue; @@ -6108,6 +6128,14 @@ S* findRecordInNestedListImpl(S& thisScope, const std::string & name, bool isC) } } + for (const auto& u : thisScope.usingList) { + if (!u.scope) + continue; + S* nestedScope = const_cast(u.scope)->findRecordInNestedList(name, false); + if (nestedScope) + return nestedScope; + } + T * nested_type = thisScope.findType(name); if (nested_type) { From b4b2021f6e0d2e4a77a4a69d731755b1e4da027e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Thu, 18 Jul 2024 19:45:23 +0200 Subject: [PATCH 5/6] Update testsymboldatabase.cpp --- test/testsymboldatabase.cpp | 54 +++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index e07f3623066..ebc7c70f9ca 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -6470,6 +6470,60 @@ class TestSymbolDatabase : public TestFixture { ASSERT(e && e->enumerator()); ASSERT_EQUALS(E0, e->enumerator()); } + { + GET_SYMBOL_DB("namespace ns {\n" + " enum { V1 };\n" + " struct C1 {\n" + " enum { V2 };\n" + " };\n" + "}\n" + "using namespace ns;\n" + "void f() {\n" + " if (0 > V1) {}\n" + " if (0 > C1::V2) {}\n" + "}\n"); + ASSERT(db != nullptr); + auto it = db->scopeList.begin(); + std::advance(it, 2); + const Enumerator* V1 = it->findEnumerator("V1"); + ASSERT(V1 && V1->value_known && V1->value == 0); + const Token* const e1 = Token::findsimplematch(tokenizer.tokens(), "V1 )"); + ASSERT(e1 && e1->enumerator()); + ASSERT_EQUALS(V1, e1->enumerator()); + std::advance(it, 2); + const Enumerator* V2 = it->findEnumerator("V2"); + ASSERT(V2 && V2->value_known && V2->value == 0); + const Token* const e2 = Token::findsimplematch(tokenizer.tokens(), "V2 )"); + ASSERT(e2 && e2->enumerator()); + ASSERT_EQUALS(V2, e2->enumerator()); + } + { + GET_SYMBOL_DB("namespace ns {\n" + " enum { V1 };\n" + " struct C1 {\n" + " enum { V2 };\n" + " };\n" + "}\n" + "void f() {\n" + " using namespace ns;\n" + " if (0 > V1) {}\n" + " if (0 > C1::V2) {}\n" + "}\n"); + ASSERT(db != nullptr); + auto it = db->scopeList.begin(); + std::advance(it, 2); + const Enumerator* V1 = it->findEnumerator("V1"); + ASSERT(V1 && V1->value_known && V1->value == 0); + const Token* const e1 = Token::findsimplematch(tokenizer.tokens(), "V1 )"); + ASSERT(e1 && e1->enumerator()); + ASSERT_EQUALS(V1, e1->enumerator()); + std::advance(it, 2); + const Enumerator* V2 = it->findEnumerator("V2"); + ASSERT(V2 && V2->value_known && V2->value == 0); + const Token* const e2 = Token::findsimplematch(tokenizer.tokens(), "V2 )"); + ASSERT(e2 && e2->enumerator()); + ASSERT_EQUALS(V2, e2->enumerator()); + } } void sizeOfType() { From 19548cd497a470f4ada02285da5e17d3c4cae018 Mon Sep 17 00:00:00 2001 From: chrchr-github Date: Thu, 18 Jul 2024 20:39:59 +0200 Subject: [PATCH 6/6] Format --- lib/symboldatabase.cpp | 2 +- test/testsymboldatabase.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index f266664f1b1..4881ebb6871 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -6130,7 +6130,7 @@ S* findRecordInNestedListImpl(S& thisScope, const std::string & name, bool isC) for (const auto& u : thisScope.usingList) { if (!u.scope) - continue; + continue; S* nestedScope = const_cast(u.scope)->findRecordInNestedList(name, false); if (nestedScope) return nestedScope; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index ebc7c70f9ca..94c6030364a 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -6471,7 +6471,7 @@ class TestSymbolDatabase : public TestFixture { ASSERT_EQUALS(E0, e->enumerator()); } { - GET_SYMBOL_DB("namespace ns {\n" + GET_SYMBOL_DB("namespace ns {\n" // #12114 " enum { V1 };\n" " struct C1 {\n" " enum { V2 };\n"