Fix #10516 FP for unused private function if address of function is taken (#3901)

This commit is contained in:
chrchr-github 2022-03-14 19:15:48 +01:00 committed by GitHub
parent 6376bac5bb
commit 200b098471
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 4 deletions

View File

@ -1177,7 +1177,7 @@ static bool checkFunctionUsage(const Function *privfunc, const Scope* scope)
for (const Variable &var : scope->varlist) { for (const Variable &var : scope->varlist) {
if (var.isStatic()) { if (var.isStatic()) {
const Token* tok = Token::findmatch(scope->bodyEnd, "%varid% =|(|{", var.declarationId()); const Token* tok = Token::findmatch(scope->bodyStart, "%varid% =|(|{", var.declarationId());
if (tok) if (tok)
tok = tok->tokAt(2); tok = tok->tokAt(2);
while (tok && tok->str() != ";") { while (tok && tok->str() != ";") {

View File

@ -1081,7 +1081,7 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass)
// Set function call pointers // Set function call pointers
for (const Token* tok = mTokenizer->list.front(); tok != mTokenizer->list.back(); tok = tok->next()) { for (const Token* tok = mTokenizer->list.front(); tok != mTokenizer->list.back(); tok = tok->next()) {
if (tok->isName() && !tok->function() && tok->varId() == 0 && Token::Match(tok, "%name% [(,)>]") && !isReservedName(tok->str())) { if (tok->isName() && !tok->function() && tok->varId() == 0 && Token::Match(tok, "%name% [(,)>;]") && !isReservedName(tok->str())) {
if (tok->next()->str() == ">" && !tok->next()->link()) if (tok->next()->str() == ">" && !tok->next()->link())
continue; continue;
@ -1089,7 +1089,7 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass)
const Token *start = tok; const Token *start = tok;
while (Token::Match(start->tokAt(-2), "%name% ::")) while (Token::Match(start->tokAt(-2), "%name% ::"))
start = start->tokAt(-2); start = start->tokAt(-2);
if (!Token::Match(start->previous(), "[(,<]") && !Token::Match(start->tokAt(-2), "[(,<] &")) if (!Token::Match(start->previous(), "[(,<=]") && !Token::Match(start->tokAt(-2), "[(,<=] &") && !Token::Match(start, "%name% ;"))
continue; continue;
} }
@ -5352,8 +5352,13 @@ const Function* SymbolDatabase::findFunction(const Token *tok) const
else else
tok1 = nullptr; tok1 = nullptr;
if (tok1) if (tok1) {
const Function* func = currScope->findFunction(tok1);
if (func)
return func;
currScope = currScope->findRecordInNestedList(tok1->str()); currScope = currScope->findRecordInNestedList(tok1->str());
}
} }
if (tok1) if (tok1)

View File

@ -55,6 +55,7 @@ private:
TEST_CASE(func_pointer4); // ticket #2807 TEST_CASE(func_pointer4); // ticket #2807
TEST_CASE(func_pointer5); // ticket #2233 TEST_CASE(func_pointer5); // ticket #2233
TEST_CASE(func_pointer6); // ticket #4787 TEST_CASE(func_pointer6); // ticket #4787
TEST_CASE(func_pointer7); // ticket #10516
TEST_CASE(ctor); TEST_CASE(ctor);
TEST_CASE(ctor2); TEST_CASE(ctor2);
@ -349,6 +350,32 @@ private:
ASSERT_EQUALS("", errout.str()); ASSERT_EQUALS("", errout.str());
} }
void func_pointer7() { // #10516
check("class C {\n"
" static void f() {}\n"
" static constexpr void(*p)() = f;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
check("class C {\n"
" static void f() {}\n"
" static constexpr void(*p)() = &f;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
check("class C {\n"
" static void f() {}\n"
" static constexpr void(*p)() = C::f;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
check("class C {\n"
" static void f() {}\n"
" static constexpr void(*p)() = &C::f;\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}
void ctor() { void ctor() {
check("class PrivateCtor\n" check("class PrivateCtor\n"