Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #12567 valueFlowBailoutIncompleteVar with nested enum #6224

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5246,17 +5246,18 @@ const Enumerator * SymbolDatabase::findEnumerator(const Token * tok, std::set<st
if (tok1->strAt(-1) == "::")
scope = &scopeList.front();
else {
// FIXME search base class here

const Scope* temp = nullptr;
if (scope)
temp = scope->findRecordInNestedList(tok1->str());
// find first scope
while (scope && scope->nestedIn) {
if (!temp)
temp = scope->nestedIn->findRecordInNestedList(tok1->str());
if (!temp && scope->functionOf)
if (!temp && scope->functionOf) {
temp = scope->functionOf->findRecordInNestedList(tok1->str());
if (!temp && scope->functionOf->nestedIn)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be added upon the deeper the enum is placed? If yes this should probably be a loop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an example? I'd rather not add something without a test.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wrap both of them within another struct and/or namespace and see if it is found.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

namespace N {
    struct S1 {
        enum E { E1 } e;
    };
	namespace O {
		struct S2 {
			static void f();
		};
	}
}
void N::O::S2::f() {
    S1 s;
    s.e = S1::E1;
}
if (!temp && scope->functionOf) {
                    temp = scope->functionOf->findRecordInNestedList(tok1->str());
                    const Scope* nested = scope->functionOf->nestedIn;
                    while (!temp && nested) {
                        temp = nested->findRecordInNestedList(tok1->str());
                        nested = nested->nestedIn;
                    }
                }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's exactly what I was thinking of.

temp = scope->functionOf->nestedIn->findRecordInNestedList(tok1->str());
}
if (!temp)
temp = findEnumScopeInBase(scope, tok1->str());
if (temp) {
Expand Down
22 changes: 22 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6310,6 +6310,28 @@ class TestSymbolDatabase : public TestFixture {
ASSERT(e && e->enumerator());
ASSERT_EQUALS(E0, e->enumerator());
}
{
GET_SYMBOL_DB("namespace ns {\n" // #12567
" struct S1 {\n"
" enum E { E1 } e;\n"
" };\n"
" struct S2 {\n"
" static void f();\n"
" };\n"
"}\n"
"void ns::S2::f() {\n"
" S1 s;\n"
" s.e = S1::E1;\n"
"}\n");
ASSERT(db != nullptr);
auto it = db->scopeList.begin();
std::advance(it, 3);
const Enumerator* E1 = it->findEnumerator("E1");
ASSERT(E1 && E1->value_known && E1->value == 0);
const Token* const e = Token::findsimplematch(tokenizer.tokens(), "E1 ;");
ASSERT(e && e->enumerator());
ASSERT_EQUALS(E1, e->enumerator());
}
}

void sizeOfType() {
Expand Down
Loading