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
8 changes: 5 additions & 3 deletions lib/checkclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2588,11 +2588,13 @@ void CheckClass::checkVirtualFunctionCallInConstructor()
getFirstVirtualFunctionCallStack(virtualFunctionCallsMap, callToken, callstack);
if (callstack.empty())
continue;
if (!(callstack.back()->function()->hasVirtualSpecifier() || callstack.back()->function()->hasOverrideSpecifier()))
const Function* const func = callstack.back()->function();
if (!(func->hasVirtualSpecifier() || func->hasOverrideSpecifier()))
continue;
if (callstack.back()->function()->isPure())
if (func->isPure())
pureVirtualFunctionCallInConstructorError(scope->function, callstack, callstack.back()->str());
else if (!callstack.back()->function()->hasFinalSpecifier())
else if (!func->hasFinalSpecifier() &&
!(func->nestedIn && func->nestedIn->classDef && func->nestedIn->classDef->isFinalType()))
virtualFunctionCallInConstructorError(scope->function, callstack, callstack.back()->str());
}
}
Expand Down
8 changes: 8 additions & 0 deletions lib/token.h
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,13 @@ class CPPCHECKLIB Token {
setFlag(fIsSimplifedScope, b);
}

bool isFinalType() const {
return getFlag(fIsFinalType);
}
void isFinalType(bool b) {
setFlag(fIsFinalType, b);
}

bool isBitfield() const {
return mImpl->mBits > 0;
}
Expand Down Expand Up @@ -1287,6 +1294,7 @@ class CPPCHECKLIB Token {
fIsIncompleteConstant = (1ULL << 36),
fIsRestrict = (1ULL << 37), // Is this a restrict pointer type
fIsSimplifiedTypedef = (1ULL << 38),
fIsFinalType = (1ULL << 39), // Is this a type with final specifier
};

Token::Type mTokType;
Expand Down
13 changes: 11 additions & 2 deletions lib/tokenize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8426,8 +8426,17 @@ void Tokenizer::simplifyKeyword()

// final:
// 1) struct name final { }; <- struct is final
if (Token::Match(tok->previous(), "struct|class|union %type% final [:{]")) {
tok->deleteNext();
if (Token::Match(tok->previous(), "struct|class|union %type%")) {
Token* finalTok = tok->next();
if (Token::simpleMatch(finalTok, "<")) { // specialization
finalTok = finalTok->findClosingBracket();
if (finalTok)
finalTok = finalTok->next();
}
if (Token::Match(finalTok, "final [:{]")) {
finalTok->deleteThis();
tok->previous()->isFinalType(true);
}
}

// noexcept -> noexcept(true)
Expand Down
8 changes: 8 additions & 0 deletions test/testclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7438,6 +7438,14 @@ class TestClass : public TestFixture {
"}\n"
"S::~S() = default;\n");
ASSERT_EQUALS("", errout.str());

checkVirtualFunctionCall("struct Base: { virtual void wibble() = 0; virtual ~Base() {} };\n" // #11167
"struct D final : public Base {\n"
" void wibble() override;\n"
" D() {}\n"
" virtual ~D() { wibble(); }\n"
"};\n");
ASSERT_EQUALS("", errout.str());
}

void pureVirtualFunctionCall() {
Expand Down
2 changes: 1 addition & 1 deletion test/testsimplifytemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5530,7 +5530,7 @@ class TestSimplifyTemplate : public TestFixture {
"private:\n"
" std::basic_ostream<unsigned char> &outputStream_;\n"
"};";
const char expected[] = "struct OutputU16<unsignedchar> final { "
const char expected[] = "struct OutputU16<unsignedchar> { "
"explicit OutputU16<unsignedchar> ( std :: basic_ostream < unsigned char > & t ) : outputStream_ ( t ) { } "
"void operator() ( unsigned short ) const ; "
"private: "
Expand Down
14 changes: 14 additions & 0 deletions test/testvarid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ class TestVarID : public TestFixture {
TEST_CASE(varid_templateParameter); // #7046 set varid for "X": std::array<int,X> Y;
TEST_CASE(varid_templateParameterFunctionPointer); // #11050
TEST_CASE(varid_templateUsing); // #5781 #7273
TEST_CASE(varid_templateSpecializationFinal);
TEST_CASE(varid_not_template_in_condition); // #7988
TEST_CASE(varid_cppcast); // #6190
TEST_CASE(varid_variadicFunc);
Expand Down Expand Up @@ -2378,6 +2379,19 @@ class TestVarID : public TestFixture {
tokenize(code));
}

void varid_templateSpecializationFinal() {
const char code[] = "template <typename T>\n"
"struct S;\n"
"template <>\n"
"struct S<void> final {};\n";
ASSERT_EQUALS("4: struct S<void> ;\n"
"1: template < typename T >\n"
"2: struct S ;\n"
"3:\n"
"4: struct S<void> { } ;\n",
tokenize(code));
}

void varid_not_template_in_condition() {
const char code1[] = "void f() { if (x<a||x>b); }";
ASSERT_EQUALS("1: void f ( ) { if ( x < a || x > b ) { ; } }\n", tokenize(code1));
Expand Down