diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index d321281b31d..0baaffad663 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1201,6 +1201,13 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass) } } + // Is tok the name in an elaborated type specifier, i.e. "struct S" or "struct N::S"? + auto isElaboratedTypeName = [](const Token* tok) { + while (Token::Match(tok->tokAt(-2), "%name% ::")) + tok = tok->tokAt(-2); + return Token::Match(tok, "class|struct|union|enum") || Token::Match(tok->previous(), "class|struct|union|enum"); + }; + // Set function call pointers const Token* inTemplateArg = nullptr; for (Token* tok = mTokenizer.list.front(); tok != mTokenizer.list.back(); tok = tok->next()) { @@ -1208,7 +1215,7 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass) inTemplateArg = tok->link(); if (inTemplateArg == tok) inTemplateArg = nullptr; - if (tok->isName() && !tok->function() && tok->varId() == 0 && ((tok->astParent() && tok->astParent()->isComparisonOp()) || Token::Match(tok, "%name% [{}(,)>;:]]")) && !isReservedName(tok)) { + if (tok->isName() && !tok->function() && tok->varId() == 0 && !isElaboratedTypeName(tok) && ((tok->astParent() && tok->astParent()->isComparisonOp()) || Token::Match(tok, "%name% [{}(,)>;:]]")) && !isReservedName(tok)) { if (tok->strAt(1) == ">" && !tok->linkAt(1)) continue; diff --git a/test/testsymboldatabase.cpp b/test/testsymboldatabase.cpp index 6175b76e393..767986bbaf5 100644 --- a/test/testsymboldatabase.cpp +++ b/test/testsymboldatabase.cpp @@ -549,6 +549,7 @@ class TestSymbolDatabase : public TestFixture { TEST_CASE(findFunction64); // overloaded operator() TEST_CASE(findFunction65); TEST_CASE(findFunction66); // #15104 - object passed for pointer to its class + TEST_CASE(findFunction67); // #15305 - tag name same as function name TEST_CASE(findFunctionRef1); TEST_CASE(findFunctionRef2); // #13328 TEST_CASE(findFunctionContainer); @@ -9243,6 +9244,76 @@ class TestSymbolDatabase : public TestFixture { } } + void findFunction67() { // #15305 - tag name same as function name + { + GET_SYMBOL_DB_C("enum E { A };\n" + "void E(void);\n" + "int f(int x) {\n" + " return (enum E)x;\n" + "}\n"); + ASSERT(db); + const Token* tok = Token::findsimplematch(tokenizer.tokens(), "E {"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "E )"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "E ( ) ;"); + ASSERT(tok && tok->function()); + } + { + GET_SYMBOL_DB_C("struct S { int a; int b; };\n" + "void S(void);\n" + "int f(void *p) {\n" + " return ((struct S *)p)->a + sizeof(struct S);\n" + "}\n" + "int g(void) {\n" + " return (struct S){1, 2}.a;\n" + "}\n"); + ASSERT(db); + const Token* tok = Token::findsimplematch(tokenizer.tokens(), "S {"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "S *"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "S )"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "S ) {"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "S ( ) ;"); + ASSERT(tok && tok->function()); + } + { + GET_SYMBOL_DB_C("union U { int a; float b; };\n" + "void U(void);\n" + "int f(void *p) {\n" + " return ((union U *)p)->a + sizeof(union U);\n" + "}\n" + "int g(void) {\n" + " return (union U){3}.a;\n" + "}\n"); + ASSERT(db); + const Token* tok = Token::findsimplematch(tokenizer.tokens(), "U {"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "U *"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "U )"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "U ) {"); + ASSERT(tok && !tok->function()); + tok = Token::findsimplematch(tokenizer.tokens(), "U ( ) ;"); + ASSERT(tok && tok->function()); + } + { + GET_SYMBOL_DB("namespace N { struct S { int a; }; void S(int); }\n" + "int f() {\n" + " return sizeof(struct N::S) + sizeof(struct ::N::S);\n" + "}\n"); + ASSERT(db); + const Token* tok = Token::findsimplematch(tokenizer.tokens(), "N :: S ) +"); + ASSERT(tok && !tok->tokAt(2)->function()); + tok = Token::findsimplematch(tokenizer.tokens(), ":: N :: S ) ;"); + ASSERT(tok && !tok->tokAt(3)->function()); + } + } + void findFunctionRef1() { GET_SYMBOL_DB("struct X {\n" " const std::vector getInts() const & { return mInts; }\n"