Skip to content

Commit

Permalink
Fix #12564 using type for enum class causes false positives (danmar#6227
Browse files Browse the repository at this point in the history
)
  • Loading branch information
chrchr-github committed Apr 4, 2024
1 parent e61ca25 commit 2b88ca8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
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 @@ -5152,7 +5155,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 @@ -6377,7 +6381,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 @@ -6368,6 +6369,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

0 comments on commit 2b88ca8

Please sign in to comment.