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 #12564 using type for enum class causes false positives #6227

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
((Token::Match(tok, "class|struct|union|namespace ::| %name% final| {|:|::|<") &&
!Token::Match(tok->previous(), "new|friend|const|enum|typedef|mutable|volatile|using|)|(|<")) ||
(Token::Match(tok, "enum class| %name% {") ||
Token::Match(tok, "enum class| %name% : %name% {"))))
Token::Match(tok, "enum class| %name% : %name% ::|{"))))
|| (tok->isC() && tok->isKeyword() && Token::Match(tok, "struct|union|enum %name% {"))) {
const Token *tok2 = tok->tokAt(2);

Expand Down Expand Up @@ -301,8 +301,11 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
mTokenizer.syntaxError(tok);
}
} else if (new_scope->type == Scope::eEnum) {
if (tok2->str() == ":")
if (tok2->str() == ":") {
tok2 = tok2->tokAt(2);
while (Token::Match(tok2, "%name%|::"))
tok2 = tok2->next();
}
}

new_scope->setBodyStartEnd(tok2);
Expand Down Expand Up @@ -5149,7 +5152,8 @@ const Token * Scope::addEnum(const Token * tok)
tok2 = tok2->next();

enumType = tok2;
tok2 = tok2->next();
while (Token::Match(tok2, "%name%|::"))
tok2 = tok2->next();
}

// add enumerators
Expand Down Expand Up @@ -6370,7 +6374,7 @@ const Type* SymbolDatabase::findTypeInNested(const Token *startTok, const Scope
startTok = startTok->next();

// type same as scope
if (startTok->str() == startScope->className && startScope->isClassOrStruct())
if (startScope->isClassOrStruct() && startTok->str() == startScope->className && !Token::simpleMatch(startTok->next(), "::"))
return startScope->definedType;

bool hasPath = false;
Expand Down
9 changes: 9 additions & 0 deletions test/testother.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2262,6 +2262,15 @@ class TestOther : public TestFixture {
ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 's' should be passed by const reference.\n",
errout_str());

check("struct S {\n"
" enum class E : std::uint8_t { E0 };\n"
" static void f(S::E e) {\n"
" if (e == S::E::E0) {}\n"
" }\n"
" char a[20];\n"
"};\n");
ASSERT_EQUALS("", errout_str());

/*const*/ Settings settings1 = settingsBuilder().platform(Platform::Type::Win64).build();
check("using ui64 = unsigned __int64;\n"
"ui64 Test(ui64 one, ui64 two) { return one + two; }\n",
Expand Down
20 changes: 20 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ class TestSymbolDatabase : public TestFixture {
TEST_CASE(enum14);
TEST_CASE(enum15);
TEST_CASE(enum16);
TEST_CASE(enum17);

TEST_CASE(sizeOfType);

Expand Down Expand Up @@ -6312,6 +6313,25 @@ class TestSymbolDatabase : public TestFixture {
}
}

void enum17() {
{
GET_SYMBOL_DB("struct S {\n" // #12564
" enum class E : std::uint8_t { E0 };\n"
" static void f(S::E e) {\n"
" if (e == S::E::E0) {}\n"
" }\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"
Expand Down
Loading