Skip to content

Commit

Permalink
Fix #10660 FP: overridden member functions of unknown abstract base c…
Browse files Browse the repository at this point in the history
…lasses reported as unused
  • Loading branch information
chrchr-github committed Jul 1, 2024
1 parent a8ee10a commit 19ec98f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
9 changes: 9 additions & 0 deletions lib/checkunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ void CheckUnusedFunctions::parseTokens(const Tokenizer &tokenizer, const Setting
if (func->isExtern())
continue;

bool foundAllBaseClasses{};
if (const Function* ofunc = func->getOverriddenFunction(&foundAllBaseClasses)) {
if (!foundAllBaseClasses || ofunc->isPure())
continue;
}
else if (func->isImplicitlyVirtual()) {
continue;
}

mFunctionDecl.emplace_back(func);

FunctionUsage &usage = mFunctions[stripTemplateParameters(func->name())];
Expand Down
4 changes: 1 addition & 3 deletions lib/symboldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4554,9 +4554,7 @@ void Function::addArguments(const SymbolDatabase *symbolDatabase, const Scope *s

bool Function::isImplicitlyVirtual(bool defaultVal) const
{
if (hasVirtualSpecifier()) //If it has the virtual specifier it's definitely virtual
return true;
if (hasOverrideSpecifier()) //If it has the override specifier then it's either virtual or not going to compile
if (hasVirtualSpecifier() || hasOverrideSpecifier() || hasFinalSpecifier())
return true;
bool foundAllBaseClasses = true;
if (getOverriddenFunction(&foundAllBaseClasses)) //If it overrides a base class's method then it's virtual
Expand Down
24 changes: 24 additions & 0 deletions test/testunusedfunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class TestUnusedFunctions : public TestFixture {
TEST_CASE(entrypointsUnix);

TEST_CASE(includes);
TEST_CASE(virtualFunc);
}

#define check(...) check_(__FILE__, __LINE__, __VA_ARGS__)
Expand Down Expand Up @@ -711,6 +712,29 @@ class TestUnusedFunctions : public TestFixture {
check(processed);
TODO_ASSERT_EQUALS("[test.h:3]: (style) The function 'f' is never used.\n", "[test.cpp:3]: (style) The function 'f' is never used.\n", errout_str());
}

void virtualFunc()
{
check("struct D : public B {\n" // #10660
" virtual void f() {}\n"
" void g() override {}\n"
" void h() final {}\n"
"};\n");
ASSERT_EQUALS("", errout_str());

check("struct B {\n"
" virtual void f() = 0;\n"
" void g();\n"
"};\n"
"struct D : B {\n"
" void f() override {}\n"
"};\n"
"int main() {\n"
" D d;\n"
" d.g();\n"
"}\n");
ASSERT_EQUALS("", errout_str());
}
};

REGISTER_TEST(TestUnusedFunctions)

0 comments on commit 19ec98f

Please sign in to comment.