From: David Bolvansky Date: Sat, 31 Aug 2019 18:52:44 +0000 (+0000) Subject: [clang] Devirtualization for classes with destructors marked as 'final' X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=bbf5599083233ce072a33769c19c22e6e4ae1671;p=clang [clang] Devirtualization for classes with destructors marked as 'final' A class with a destructor marked final cannot be derived from, so it should afford the same devirtualization opportunities as marking the entire class final. Patch by logan-5 (Logan Smith) Reviewed by rsmith Differential Revision: https://reviews.llvm.org/D66621 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@370597 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp index 59710a5549..4939c37edc 100644 --- a/lib/AST/DeclCXX.cpp +++ b/lib/AST/DeclCXX.cpp @@ -2067,10 +2067,15 @@ CXXMethodDecl *CXXMethodDecl::getDevirtualizedMethod(const Expr *Base, if (DevirtualizedMethod->hasAttr()) return DevirtualizedMethod; - // Similarly, if the class itself is marked 'final' it can't be overridden - // and we can therefore devirtualize the member function call. + // Similarly, if the class itself or its destructor is marked 'final', + // the class can't be derived from and we can therefore devirtualize the + // member function call. if (BestDynamicDecl->hasAttr()) return DevirtualizedMethod; + if (const auto *dtor = BestDynamicDecl->getDestructor()) { + if (dtor->hasAttr()) + return DevirtualizedMethod; + } if (const auto *DRE = dyn_cast(Base)) { if (const auto *VD = dyn_cast(DRE->getDecl())) diff --git a/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp b/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp index 2ab2f759cf..130103de97 100644 --- a/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp +++ b/test/CodeGenCXX/devirtualize-virtual-function-calls-final.cpp @@ -24,11 +24,24 @@ namespace Test2 { } } -namespace Test3 { +namespace Test2a { struct A { + virtual ~A() final {} virtual int f(); }; + // CHECK-LABEL: define i32 @_ZN6Test2a1fEPNS_1AE + int f(A *a) { + // CHECK: call i32 @_ZN6Test2a1A1fEv + return a->f(); + } +} + + +namespace Test3 { + struct A { + virtual int f(); }; + struct B final : A { }; // CHECK-LABEL: define i32 @_ZN5Test31fEPNS_1BE