[PATCH] D66711: [clang] Warning for non-final classes with final destructors

Dávid Bolvanský via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 31 11:30:20 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL370594: [clang] Warning for non-final classes with final destructors (authored by xbolva00, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D66711?vs=217049&id=218229#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66711/new/

https://reviews.llvm.org/D66711

Files:
  cfe/trunk/lib/Sema/SemaDeclCXX.cpp
  cfe/trunk/test/SemaCXX/MicrosoftExtensions.cpp
  cfe/trunk/test/SemaCXX/warn-final-dtor-non-final-class.cpp


Index: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp
@@ -6236,6 +6236,19 @@
     }
   }
 
+  // Warn if the class has a final destructor but is not itself marked final.
+  if (!Record->hasAttr<FinalAttr>()) {
+    if (const CXXDestructorDecl *dtor = Record->getDestructor()) {
+      if (const FinalAttr *FA = dtor->getAttr<FinalAttr>()) {
+        Diag(FA->getLocation(), diag::warn_final_dtor_non_final_class)
+          << FA->isSpelledAsSealed();
+        Diag(Record->getLocation(), diag::note_final_dtor_non_final_class_silence)
+          << Context.getRecordType(Record)
+          << FA->isSpelledAsSealed();
+      }
+    }
+  }
+
   // See if trivial_abi has to be dropped.
   if (Record->hasAttr<TrivialABIAttr>())
     checkIllFormedTrivialABIStruct(*Record);
Index: cfe/trunk/test/SemaCXX/MicrosoftExtensions.cpp
===================================================================
--- cfe/trunk/test/SemaCXX/MicrosoftExtensions.cpp
+++ cfe/trunk/test/SemaCXX/MicrosoftExtensions.cpp
@@ -440,6 +440,11 @@
 // expected-error at +1 {{base 'SealedType' is marked 'sealed'}}
 struct InheritFromSealed : SealedType {};
 
+class SealedDestructor { // expected-note {{mark 'SealedDestructor' as 'sealed' to silence this warning}}
+    // expected-warning at +1 {{'sealed' keyword is a Microsoft extension}}
+    virtual ~SealedDestructor() sealed; // expected-warning {{class with destructor marked 'sealed' cannot be inherited from}}
+};
+
 void AfterClassBody() {
   // expected-warning at +1 {{attribute 'deprecated' is ignored, place it after "struct" to apply attribute to type declaration}}
   struct D {} __declspec(deprecated);
Index: cfe/trunk/test/SemaCXX/warn-final-dtor-non-final-class.cpp
===================================================================
--- cfe/trunk/test/SemaCXX/warn-final-dtor-non-final-class.cpp
+++ cfe/trunk/test/SemaCXX/warn-final-dtor-non-final-class.cpp
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s -Wfinal-dtor-non-final-class
+
+class A {
+    ~A();
+};
+
+class B { // expected-note {{mark 'B' as 'final' to silence this warning}}
+    virtual ~B() final; // expected-warning {{class with destructor marked 'final' cannot be inherited from}}
+};
+
+class C final {
+    virtual ~C() final;
+};


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66711.218229.patch
Type: text/x-patch
Size: 2399 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190831/9e1f4371/attachment.bin>


More information about the llvm-commits mailing list