Skip to content
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
9 changes: 8 additions & 1 deletion lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,14 +1201,21 @@ 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()) {
if (inTemplateArg == nullptr && tok->link() && tok->str() == "<")
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;

Expand Down
71 changes: 71 additions & 0 deletions test/testsymboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<int> getInts() const & { return mInts; }\n"
Expand Down
Loading