diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 9e216a355..ffce78c68 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1373,11 +1373,15 @@ void SymbolDatabase::createSymbolDatabaseEnums() // find enumerators for (const Token* tok = mTokenizer->list.front(); tok != mTokenizer->list.back(); tok = tok->next()) { - if (tok->tokType() != Token::eName) + const bool isVariable = (tok->tokType() == Token::eVariable && !tok->variable()); + if (tok->tokType() != Token::eName && !isVariable) continue; const Enumerator * enumerator = findEnumerator(tok, tokensThatAreNotEnumeratorValues); - if (enumerator) - const_cast(tok)->enumerator(enumerator); + if (enumerator) { + if (isVariable) + const_cast(tok)->varId(0); + const_cast(tok)->enumerator(enumerator); + } } } @@ -4919,6 +4923,12 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::setscope && enumerator->scope->enumClass)) return enumerator; + if (Token::simpleMatch(tok->astParent(), ".")) { + const Token* varTok = tok->astParent()->astOperand1(); + if (varTok && varTok->variable() && varTok->variable()->type() && varTok->variable()->type()->classScope) + scope = varTok->variable()->type()->classScope; + } + for (std::vector::const_iterator s = scope->nestedList.cbegin(); s != scope->nestedList.cend(); ++s) { enumerator = (*s)->findEnumerator(tokStr); @@ -5433,6 +5443,8 @@ const Function* Scope::findFunction(const Token *tok, bool requireConst) const if (rml) valuetok = rml->previous(); } + if (vartok->isEnumerator()) + valuetok = vartok; const ValueType::MatchResult res = ValueType::matchParameter(valuetok->valueType(), var, funcarg); if (res == ValueType::MatchResult::SAME) ++same; diff --git a/test/testplatform.cpp b/test/testplatform.cpp index 409b636cb..6b474abab 100644 --- a/test/testplatform.cpp +++ b/test/testplatform.cpp @@ -182,7 +182,7 @@ private: ASSERT_EQUALS(64, platform.long_long_bit); } - void valid_config_file_1() { + void valid_config_file_1() const { // Valid platform configuration with all possible values specified. // Similar to the avr8 platform file. const char xmldata[] = "\n" @@ -226,7 +226,7 @@ private: ASSERT_EQUALS(64, platform.long_long_bit); } - void valid_config_file_2() { + void valid_config_file_2() const { // Valid platform configuration with all possible values specified and // char_bit > 8. const char xmldata[] = "\n" @@ -296,7 +296,7 @@ private: TODO_ASSERT(!readPlatform(platform, xmldata)); } - void valid_config_file_4() { + void valid_config_file_4() const { // Valid platform configuration with all possible values specified and // set to 0. const char xmldata[] = "\n" diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index d55819ab2..2d79649a7 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -387,6 +387,7 @@ private: TEST_CASE(enum9); TEST_CASE(enum10); // #11001 TEST_CASE(enum11); + TEST_CASE(enum12); TEST_CASE(sizeOfType); @@ -5532,6 +5533,22 @@ private: ASSERT_EQUALS("", errout.str()); } + void enum12() { + GET_SYMBOL_DB_C("struct { enum E { E0 }; } t;\n" + "void f() {\n" + " if (t.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); + ASSERT_EQUALS(E0->value, 0); + const Token* const e = Token::findsimplematch(tokenizer.tokens(), "E0 )"); + ASSERT(e && e->enumerator()); + ASSERT_EQUALS(e->enumerator(), E0); + } + void sizeOfType() { // #7615 - crash in Symboldatabase::sizeOfType() GET_SYMBOL_DB("enum e;\n" @@ -7011,6 +7028,20 @@ private: ASSERT(functok->function()->name() == "f"); ASSERT_EQUALS(3, functok->function()->tokenDef->linenr()); } + { + GET_SYMBOL_DB("struct T { enum E { E0 }; } t; \n" // #11559 + "void f(const void*, T::E) {}\n" + "void f(const int&, T::E) {}\n" + "void g() {\n" + " f(nullptr, t.E0);\n" + "}\n"); + ASSERT_EQUALS("", errout.str()); + const Token *functok = Token::findsimplematch(tokenizer.tokens(), "f ( nullptr"); + ASSERT(functok); + ASSERT(functok->function()); + ASSERT(functok->function()->name() == "f"); + ASSERT_EQUALS(2, functok->function()->tokenDef->linenr()); + } } void findFunction45() {