From c6b3c56174e2fa3328e623b552aad68ee44f1b5d Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 19 Sep 2023 11:45:59 +0200 Subject: [PATCH] Improve findEnumerator() (refs #10045) (#5459) --- lib/symboldatabase.cpp | 21 +++++++-------- test/testsymboldatabase.cpp | 53 ++++++++++++++++++++++++++----------- 2 files changed, 46 insertions(+), 28 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 3763f3115..0fc607663 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -5050,19 +5050,16 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::setfunctionOf && (temp = scope->functionOf->findRecordInNestedList(tok1->str()))) { - scope = temp; - } else { - // find first scope - while (scope && scope->nestedIn) { - temp = scope->nestedIn->findRecordInNestedList(tok1->str()); - if (temp) { - scope = temp; - break; - } - scope = scope->nestedIn; + // find first scope + while (scope && scope->nestedIn) { + const Scope* temp = scope->nestedIn->findRecordInNestedList(tok1->str()); + if (!temp && scope->functionOf) + temp = scope->functionOf->findRecordInNestedList(tok1->str()); + if (temp) { + scope = temp; + break; } + scope = scope->nestedIn; } } diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index cd691b38b..46167ec54 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -5827,22 +5827,43 @@ private: } void enum15() { - GET_SYMBOL_DB("struct S {\n" - " S();\n" - " enum E { E0 };\n" - "};\n" - "S::S() {\n" - " E e = E::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); - std::advance(it, 1); - const Token* const e = Token::findsimplematch(tokenizer.tokens(), "E0 ;"); - ASSERT(e && e->enumerator()); - ASSERT_EQUALS(E0, e->enumerator()); + { + GET_SYMBOL_DB("struct S {\n" + " S();\n" + " enum E { E0 };\n" + "};\n" + "S::S() {\n" + " E e = E::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); + std::advance(it, 1); + const Token* const e = Token::findsimplematch(tokenizer.tokens(), "E0 ;"); + ASSERT(e && e->enumerator()); + ASSERT_EQUALS(E0, e->enumerator()); + } + { + GET_SYMBOL_DB("struct S {\n" + " S(bool x);\n" + " enum E { E0 };\n" + "};\n" + "S::S(bool x) {\n" + " if (x)\n" + " E e = E::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); + std::advance(it, 1); + const Token* const e = Token::findsimplematch(tokenizer.tokens(), "E0 ;"); + ASSERT(e && e->enumerator()); + ASSERT_EQUALS(E0, e->enumerator()); + } } void sizeOfType() {