virtual call in constructor; warn when function is explicitly virtual
This commit is contained in:
parent
f7b0f44186
commit
d9dacc97e4
|
@ -2421,6 +2421,8 @@ void CheckClass::checkVirtualFunctionCallInConstructor()
|
|||
getFirstVirtualFunctionCallStack(virtualFunctionCallsMap, callToken, callstack);
|
||||
if (callstack.empty())
|
||||
continue;
|
||||
if (!(callstack.back()->function()->hasVirtualSpecifier() || callstack.back()->function()->hasOverrideSpecifier()))
|
||||
continue;
|
||||
if (callstack.back()->function()->isPure())
|
||||
pureVirtualFunctionCallInConstructorError(scope->function, callstack, callstack.back()->str());
|
||||
else
|
||||
|
|
|
@ -70,8 +70,7 @@ public:
|
|||
checkClass.virtualDestructor();
|
||||
checkClass.checkConst();
|
||||
checkClass.copyconstructors();
|
||||
// FIXME: Only report warnings for inherited classes
|
||||
// checkClass.checkVirtualFunctionCallInConstructor();
|
||||
checkClass.checkVirtualFunctionCallInConstructor();
|
||||
checkClass.checkDuplInheritedMembers();
|
||||
checkClass.checkExplicitConstructors();
|
||||
checkClass.checkCopyCtorAndEqOperator();
|
||||
|
|
|
@ -6951,11 +6951,11 @@ private:
|
|||
" virtual int f() = 0;\n"
|
||||
"};\n"
|
||||
"class A : B {\n"
|
||||
" int f();\n"
|
||||
" int f();\n" // <- not explicitly virtual
|
||||
" A() {f();}\n"
|
||||
"};\n"
|
||||
"int A::f() { return 1; }");
|
||||
ASSERT_EQUALS("[test.cpp:6] -> [test.cpp:5]: (style) Virtual function 'f' is called from constructor 'A()' at line 6. Dynamic binding is not used.\n", errout.str());
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
checkVirtualFunctionCall("class A\n"
|
||||
"{\n"
|
||||
|
@ -6963,6 +6963,12 @@ private:
|
|||
" virtual void f() {}\n"
|
||||
"};");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
|
||||
checkVirtualFunctionCall("class A : B {\n"
|
||||
" int f() final { return 1; }\n"
|
||||
" A() { f(); }\n"
|
||||
"};\n");
|
||||
ASSERT_EQUALS("", errout.str());
|
||||
}
|
||||
|
||||
void pureVirtualFunctionCall() {
|
||||
|
|
Loading…
Reference in New Issue