From 7e0ddd3669e3d0f8d76b12176879645623e8d63e Mon Sep 17 00:00:00 2001 From: chrchr-github <78114321+chrchr-github@users.noreply.github.com> Date: Tue, 2 May 2023 22:53:21 +0200 Subject: [PATCH] Set function pointer for ::f (#5028) --- lib/symboldatabase.cpp | 6 +++--- test/testother.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 033447614..d8aa3f58d 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -1105,7 +1105,7 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass) const Token *start = tok; while (Token::Match(start->tokAt(-2), "%name% ::")) start = start->tokAt(-2); - if (!Token::Match(start->previous(), "[(,<=]") && !Token::Match(start->tokAt(-2), "[(,<=] &") && !Token::Match(start, "%name% ;")) + if (!Token::Match(start->previous(), "[(,<=]") && !Token::simpleMatch(start->previous(), "::") && !Token::Match(start->tokAt(-2), "[(,<=] &") && !Token::Match(start, "%name% ;")) continue; isTemplateArg = Token::simpleMatch(start->previous(), "<") || Token::simpleMatch(start->tokAt(-2), "<"); } @@ -5577,8 +5577,8 @@ const Function* SymbolDatabase::findFunction(const Token* const tok) const if (tok1->strAt(-1) == "::") { currScope = &scopeList.front(); - if (Token::Match(tok1, "%name% (")) - return currScope->findFunction(tok); + if (const Function* f = currScope->findFunction(tok)) + return f; currScope = currScope->findRecordInNestedList(tok1->str()); } diff --git a/test/testother.cpp b/test/testother.cpp index 06bcc2203..830041833 100644 --- a/test/testother.cpp +++ b/test/testother.cpp @@ -3522,6 +3522,18 @@ private: ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (style) Parameter 'p' can be declared as pointer to const. " "However it seems that 'cb' is a callback function, if 'p' is declared with const you might also need to cast function pointer(s).\n", errout.str()); + + check("typedef void (*cb_t)(int*);\n" + "void cb(int* p) {\n" + " if (*p) {}\n" + "}\n" + "void g(cb_t);\n" + "void f() {\n" + " g(::cb);\n" + "}\n"); + ASSERT_EQUALS("[test.cpp:7] -> [test.cpp:2]: (style) Parameter 'p' can be declared as pointer to const. " + "However it seems that 'cb' is a callback function, if 'p' is declared with const you might also need to cast function pointer(s).\n", + errout.str()); } void switchRedundantAssignmentTest() {