Skip to content

Commit 200b098

Browse files
Fix #10516 FP for unused private function if address of function is taken (#3901)
1 parent 6376bac commit 200b098

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

lib/checkclass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,7 @@ static bool checkFunctionUsage(const Function *privfunc, const Scope* scope)
11771177

11781178
for (const Variable &var : scope->varlist) {
11791179
if (var.isStatic()) {
1180-
const Token* tok = Token::findmatch(scope->bodyEnd, "%varid% =|(|{", var.declarationId());
1180+
const Token* tok = Token::findmatch(scope->bodyStart, "%varid% =|(|{", var.declarationId());
11811181
if (tok)
11821182
tok = tok->tokAt(2);
11831183
while (tok && tok->str() != ";") {

lib/symboldatabase.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,15 +1081,15 @@ void SymbolDatabase::createSymbolDatabaseSetFunctionPointers(bool firstPass)
10811081

10821082
// Set function call pointers
10831083
for (const Token* tok = mTokenizer->list.front(); tok != mTokenizer->list.back(); tok = tok->next()) {
1084-
if (tok->isName() && !tok->function() && tok->varId() == 0 && Token::Match(tok, "%name% [(,)>]") && !isReservedName(tok->str())) {
1084+
if (tok->isName() && !tok->function() && tok->varId() == 0 && Token::Match(tok, "%name% [(,)>;]") && !isReservedName(tok->str())) {
10851085
if (tok->next()->str() == ">" && !tok->next()->link())
10861086
continue;
10871087

10881088
if (tok->next()->str() != "(") {
10891089
const Token *start = tok;
10901090
while (Token::Match(start->tokAt(-2), "%name% ::"))
10911091
start = start->tokAt(-2);
1092-
if (!Token::Match(start->previous(), "[(,<]") && !Token::Match(start->tokAt(-2), "[(,<] &"))
1092+
if (!Token::Match(start->previous(), "[(,<=]") && !Token::Match(start->tokAt(-2), "[(,<=] &") && !Token::Match(start, "%name% ;"))
10931093
continue;
10941094
}
10951095

@@ -5352,8 +5352,13 @@ const Function* SymbolDatabase::findFunction(const Token *tok) const
53525352
else
53535353
tok1 = nullptr;
53545354

5355-
if (tok1)
5355+
if (tok1) {
5356+
const Function* func = currScope->findFunction(tok1);
5357+
if (func)
5358+
return func;
5359+
53565360
currScope = currScope->findRecordInNestedList(tok1->str());
5361+
}
53575362
}
53585363

53595364
if (tok1)

test/testunusedprivfunc.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class TestUnusedPrivateFunction : public TestFixture {
5555
TEST_CASE(func_pointer4); // ticket #2807
5656
TEST_CASE(func_pointer5); // ticket #2233
5757
TEST_CASE(func_pointer6); // ticket #4787
58+
TEST_CASE(func_pointer7); // ticket #10516
5859

5960
TEST_CASE(ctor);
6061
TEST_CASE(ctor2);
@@ -349,6 +350,32 @@ class TestUnusedPrivateFunction : public TestFixture {
349350
ASSERT_EQUALS("", errout.str());
350351
}
351352

353+
void func_pointer7() { // #10516
354+
check("class C {\n"
355+
" static void f() {}\n"
356+
" static constexpr void(*p)() = f;\n"
357+
"};\n");
358+
ASSERT_EQUALS("", errout.str());
359+
360+
check("class C {\n"
361+
" static void f() {}\n"
362+
" static constexpr void(*p)() = &f;\n"
363+
"};\n");
364+
ASSERT_EQUALS("", errout.str());
365+
366+
check("class C {\n"
367+
" static void f() {}\n"
368+
" static constexpr void(*p)() = C::f;\n"
369+
"};\n");
370+
ASSERT_EQUALS("", errout.str());
371+
372+
check("class C {\n"
373+
" static void f() {}\n"
374+
" static constexpr void(*p)() = &C::f;\n"
375+
"};\n");
376+
ASSERT_EQUALS("", errout.str());
377+
}
378+
352379

353380
void ctor() {
354381
check("class PrivateCtor\n"

0 commit comments

Comments
 (0)