Skip to content

Commit 2b88ca8

Browse files
Fix #12564 using type for enum class causes false positives (#6227)
1 parent e61ca25 commit 2b88ca8

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

lib/symboldatabase.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
164164
((Token::Match(tok, "class|struct|union|namespace ::| %name% final| {|:|::|<") &&
165165
!Token::Match(tok->previous(), "new|friend|const|enum|typedef|mutable|volatile|using|)|(|<")) ||
166166
(Token::Match(tok, "enum class| %name% {") ||
167-
Token::Match(tok, "enum class| %name% : %name% {"))))
167+
Token::Match(tok, "enum class| %name% : %name% ::|{"))))
168168
|| (tok->isC() && tok->isKeyword() && Token::Match(tok, "struct|union|enum %name% {"))) {
169169
const Token *tok2 = tok->tokAt(2);
170170

@@ -301,8 +301,11 @@ void SymbolDatabase::createSymbolDatabaseFindAllScopes()
301301
mTokenizer.syntaxError(tok);
302302
}
303303
} else if (new_scope->type == Scope::eEnum) {
304-
if (tok2->str() == ":")
304+
if (tok2->str() == ":") {
305305
tok2 = tok2->tokAt(2);
306+
while (Token::Match(tok2, "%name%|::"))
307+
tok2 = tok2->next();
308+
}
306309
}
307310

308311
new_scope->setBodyStartEnd(tok2);
@@ -5152,7 +5155,8 @@ const Token * Scope::addEnum(const Token * tok)
51525155
tok2 = tok2->next();
51535156

51545157
enumType = tok2;
5155-
tok2 = tok2->next();
5158+
while (Token::Match(tok2, "%name%|::"))
5159+
tok2 = tok2->next();
51565160
}
51575161

51585162
// add enumerators
@@ -6377,7 +6381,7 @@ const Type* SymbolDatabase::findTypeInNested(const Token *startTok, const Scope
63776381
startTok = startTok->next();
63786382

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

63836387
bool hasPath = false;

test/testother.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,6 +2262,15 @@ class TestOther : public TestFixture {
22622262
ASSERT_EQUALS("[test.cpp:2]: (performance) Function parameter 's' should be passed by const reference.\n",
22632263
errout_str());
22642264

2265+
check("struct S {\n"
2266+
" enum class E : std::uint8_t { E0 };\n"
2267+
" static void f(S::E e) {\n"
2268+
" if (e == S::E::E0) {}\n"
2269+
" }\n"
2270+
" char a[20];\n"
2271+
"};\n");
2272+
ASSERT_EQUALS("", errout_str());
2273+
22652274
/*const*/ Settings settings1 = settingsBuilder().platform(Platform::Type::Win64).build();
22662275
check("using ui64 = unsigned __int64;\n"
22672276
"ui64 Test(ui64 one, ui64 two) { return one + two; }\n",

test/testsymboldatabase.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ class TestSymbolDatabase : public TestFixture {
448448
TEST_CASE(enum14);
449449
TEST_CASE(enum15);
450450
TEST_CASE(enum16);
451+
TEST_CASE(enum17);
451452

452453
TEST_CASE(sizeOfType);
453454

@@ -6368,6 +6369,25 @@ class TestSymbolDatabase : public TestFixture {
63686369
}
63696370
}
63706371

6372+
void enum17() {
6373+
{
6374+
GET_SYMBOL_DB("struct S {\n" // #12564
6375+
" enum class E : std::uint8_t { E0 };\n"
6376+
" static void f(S::E e) {\n"
6377+
" if (e == S::E::E0) {}\n"
6378+
" }\n"
6379+
"};\n");
6380+
ASSERT(db != nullptr);
6381+
auto it = db->scopeList.begin();
6382+
std::advance(it, 2);
6383+
const Enumerator* E0 = it->findEnumerator("E0");
6384+
ASSERT(E0 && E0->value_known && E0->value == 0);
6385+
const Token* const e = Token::findsimplematch(tokenizer.tokens(), "E0 )");
6386+
ASSERT(e && e->enumerator());
6387+
ASSERT_EQUALS(E0, e->enumerator());
6388+
}
6389+
}
6390+
63716391
void sizeOfType() {
63726392
// #7615 - crash in Symboldatabase::sizeOfType()
63736393
GET_SYMBOL_DB("enum e;\n"

0 commit comments

Comments
 (0)