Skip to content

Commit 83ec29f

Browse files
authored
Fix #15305 (SymbolDatabase: enum/struct tag name gets Token::function() when a function with the same name exists) (#9111)
1 parent b03a347 commit 83ec29f

2 files changed

Lines changed: 79 additions & 1 deletion

File tree

‎lib/symboldatabase.cpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,14 +1201,21 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass)
12011201
}
12021202
}
12031203

1204+
// Is tok the name in an elaborated type specifier, i.e. "struct S" or "struct N::S"?
1205+
auto isElaboratedTypeName = [](const Token* tok) {
1206+
while (Token::Match(tok->tokAt(-2), "%name% ::"))
1207+
tok = tok->tokAt(-2);
1208+
return Token::Match(tok, "class|struct|union|enum") || Token::Match(tok->previous(), "class|struct|union|enum");
1209+
};
1210+
12041211
// Set function call pointers
12051212
const Token* inTemplateArg = nullptr;
12061213
for (Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) {
12071214
if (inTemplateArg == nullptr && tok->link() && tok->str() == "<")
12081215
inTemplateArg = tok->link();
12091216
if (inTemplateArg == tok)
12101217
inTemplateArg = nullptr;
1211-
if (tok->isName() && !tok->function() && tok->varId() == 0 && ((tok->astParent() && tok->astParent()->isComparisonOp()) || Token::Match(tok, "%name% [{}(,)>;:]]")) && !isReservedName(tok)) {
1218+
if (tok->isName() && !tok->function() && tok->varId() == 0 && !isElaboratedTypeName(tok) && ((tok->astParent() && tok->astParent()->isComparisonOp()) || Token::Match(tok, "%name% [{}(,)>;:]]")) && !isReservedName(tok)) {
12121219
if (tok->strAt(1) == ">" && !tok->linkAt(1))
12131220
continue;
12141221

‎test/testsymboldatabase.cpp‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ class TestSymbolDatabase : public TestFixture {
549549
TEST_CASE(findFunction64); // overloaded operator()
550550
TEST_CASE(findFunction65);
551551
TEST_CASE(findFunction66); // #15104 - object passed for pointer to its class
552+
TEST_CASE(findFunction67); // #15305 - tag name same as function name
552553
TEST_CASE(findFunctionRef1);
553554
TEST_CASE(findFunctionRef2); // #13328
554555
TEST_CASE(findFunctionContainer);
@@ -9243,6 +9244,76 @@ class TestSymbolDatabase : public TestFixture {
92439244
}
92449245
}
92459246

9247+
void findFunction67() { // #15305 - tag name same as function name
9248+
{
9249+
GET_SYMBOL_DB_C("enum E { A };\n"
9250+
"void E(void);\n"
9251+
"int f(int x) {\n"
9252+
" return (enum E)x;\n"
9253+
"}\n");
9254+
ASSERT(db);
9255+
const Token* tok = Token::findsimplematch(tokenizer.tokens(), "E {");
9256+
ASSERT(tok && !tok->function());
9257+
tok = Token::findsimplematch(tokenizer.tokens(), "E )");
9258+
ASSERT(tok && !tok->function());
9259+
tok = Token::findsimplematch(tokenizer.tokens(), "E ( ) ;");
9260+
ASSERT(tok && tok->function());
9261+
}
9262+
{
9263+
GET_SYMBOL_DB_C("struct S { int a; int b; };\n"
9264+
"void S(void);\n"
9265+
"int f(void *p) {\n"
9266+
" return ((struct S *)p)->a + sizeof(struct S);\n"
9267+
"}\n"
9268+
"int g(void) {\n"
9269+
" return (struct S){1, 2}.a;\n"
9270+
"}\n");
9271+
ASSERT(db);
9272+
const Token* tok = Token::findsimplematch(tokenizer.tokens(), "S {");
9273+
ASSERT(tok && !tok->function());
9274+
tok = Token::findsimplematch(tokenizer.tokens(), "S *");
9275+
ASSERT(tok && !tok->function());
9276+
tok = Token::findsimplematch(tokenizer.tokens(), "S )");
9277+
ASSERT(tok && !tok->function());
9278+
tok = Token::findsimplematch(tokenizer.tokens(), "S ) {");
9279+
ASSERT(tok && !tok->function());
9280+
tok = Token::findsimplematch(tokenizer.tokens(), "S ( ) ;");
9281+
ASSERT(tok && tok->function());
9282+
}
9283+
{
9284+
GET_SYMBOL_DB_C("union U { int a; float b; };\n"
9285+
"void U(void);\n"
9286+
"int f(void *p) {\n"
9287+
" return ((union U *)p)->a + sizeof(union U);\n"
9288+
"}\n"
9289+
"int g(void) {\n"
9290+
" return (union U){3}.a;\n"
9291+
"}\n");
9292+
ASSERT(db);
9293+
const Token* tok = Token::findsimplematch(tokenizer.tokens(), "U {");
9294+
ASSERT(tok && !tok->function());
9295+
tok = Token::findsimplematch(tokenizer.tokens(), "U *");
9296+
ASSERT(tok && !tok->function());
9297+
tok = Token::findsimplematch(tokenizer.tokens(), "U )");
9298+
ASSERT(tok && !tok->function());
9299+
tok = Token::findsimplematch(tokenizer.tokens(), "U ) {");
9300+
ASSERT(tok && !tok->function());
9301+
tok = Token::findsimplematch(tokenizer.tokens(), "U ( ) ;");
9302+
ASSERT(tok && tok->function());
9303+
}
9304+
{
9305+
GET_SYMBOL_DB("namespace N { struct S { int a; }; void S(int); }\n"
9306+
"int f() {\n"
9307+
" return sizeof(struct N::S) + sizeof(struct ::N::S);\n"
9308+
"}\n");
9309+
ASSERT(db);
9310+
const Token* tok = Token::findsimplematch(tokenizer.tokens(), "N :: S ) +");
9311+
ASSERT(tok && !tok->tokAt(2)->function());
9312+
tok = Token::findsimplematch(tokenizer.tokens(), ":: N :: S ) ;");
9313+
ASSERT(tok && !tok->tokAt(3)->function());
9314+
}
9315+
}
9316+
92469317
void findFunctionRef1() {
92479318
GET_SYMBOL_DB("struct X {\n"
92489319
" const std::vector<int> getInts() const & { return mInts; }\n"

0 commit comments

Comments
 (0)