Skip to content

Commit

Permalink
Fix #12538 valueFlowBailoutIncompleteVar with nested enum (#6220)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrchr-github authored Apr 3, 2024
1 parent f722f08 commit d98efaf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5209,6 +5209,21 @@ const Token * Scope::addEnum(const Token * tok)
return tok2;
}

static const Scope* findEnumScopeInBase(const Scope* scope, const std::string& tokStr)
{
if (scope->definedType) {
const std::vector<Type::BaseInfo>& derivedFrom = scope->definedType->derivedFrom;
for (const Type::BaseInfo& i : derivedFrom) {
const Type *derivedFromType = i.type;
if (derivedFromType && derivedFromType->classScope) {
if (const Scope* enumScope = derivedFromType->classScope->findRecordInNestedList(tokStr))
return enumScope;
}
}
}
return nullptr;
}

const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::set<std::string>& tokensThatAreNotEnumeratorValues) const
{
if (tok->isKeyword())
Expand Down Expand Up @@ -5242,6 +5257,8 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::set<st
temp = scope->nestedIn->findRecordInNestedList(tok1->str());
if (!temp && scope->functionOf)
temp = scope->functionOf->findRecordInNestedList(tok1->str());
if (!temp)
temp = findEnumScopeInBase(scope, tok1->str());
if (temp) {
scope = temp;
break;
Expand Down
25 changes: 24 additions & 1 deletion test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(enum13);
TEST_CASE(enum14);
TEST_CASE(enum15);
TEST_CASE(enum16);

TEST_CASE(sizeOfType);

Expand Down Expand Up @@ -6220,7 +6221,6 @@ class TestSymbolDatabase : public TestFixture {
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());
Expand Down Expand Up @@ -6289,6 +6289,29 @@ class TestSymbolDatabase : public TestFixture {
}
}

void enum16() {
{
GET_SYMBOL_DB("struct B {\n" // #12538
" struct S {\n"
" enum E { E0 = 0 };\n"
" };\n"
"};\n"
"struct D : B {\n"
" S::E f() const {\n"
" return S::E0;\n"
" }\n"
"};\n");
ASSERT(db != nullptr);
auto it = db->scopeList.begin();
std::advance(it, 3);
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"
Expand Down

0 comments on commit d98efaf

Please sign in to comment.