if (CME->getQualifier())
callIsNonVirtual = true;
- // Elide analyzing the call entirely if the base pointer is not 'this'.
- if (Expr *base = CME->getBase()->IgnoreImpCasts())
+ if (Expr *base = CME->getBase()->IgnoreImpCasts()) {
+ // Elide analyzing the call entirely if the base pointer is not 'this'.
if (!isa<CXXThisExpr>(base))
return;
+
+ // If the most derived class is marked final, we know that now subclass
+ // can override this member.
+ if (base->getBestDynamicClassType()->hasAttr<FinalAttr>())
+ callIsNonVirtual = true;
+ }
}
// Get the callee.
const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(CE->getDirectCallee());
- if (MD && MD->isVirtual() && !callIsNonVirtual)
+ if (MD && MD->isVirtual() && !callIsNonVirtual && !MD->hasAttr<FinalAttr>() &&
+ !MD->getParent()->hasAttr<FinalAttr>())
ReportVirtualCall(CE, MD->isPure());
Enqueue(CE);
-// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.cplusplus.VirtualCall -analyzer-store region -verify %s
+// RUN: %clang_cc1 -analyze -analyzer-checker=alpha.cplusplus.VirtualCall -analyzer-store region -verify -std=c++11 %s
class A {
public:
f(foo()); // expected-warning{{Call virtual functions during construction or destruction will never go to a more derived class}}
}
+class D : public B {
+public:
+ D() {
+ foo(); // no-warning
+ }
+ ~D() { bar(); }
+ int foo() final;
+ void bar() final { foo(); } // no-warning
+};
+
+class E final : public B {
+public:
+ E() {
+ foo(); // no-warning
+ }
+ ~E() { bar(); }
+ int foo() override;
+};
+
int main() {
A *a;
B *b;
C *c;
+ D *d;
+ E *e;
}
#include "virtualcall.h"