[PATCH] Fix PR19253: Do not emit global destructor warning for trivial explicitly defaulted destructors

Stephan Tolksdorf st at quanttec.com
Wed Mar 26 07:51:30 PDT 2014


Hi rsmith,

This patch fixes the logic for setting CXXRecordDecl::DefinitionData::HasIrrelevantDestructor.

http://llvm-reviews.chandlerc.com/D3190

Files:
  lib/AST/DeclCXX.cpp
  test/SemaCXX/warn-global-constructors.cpp

Index: lib/AST/DeclCXX.cpp
===================================================================
--- lib/AST/DeclCXX.cpp
+++ lib/AST/DeclCXX.cpp
@@ -527,8 +527,11 @@
   if (CXXDestructorDecl *DD = dyn_cast<CXXDestructorDecl>(D)) {
     SMKind |= SMF_Destructor;
 
-    if (!DD->isImplicit())
+    if (DD->isUserProvided())
       data().HasIrrelevantDestructor = false;
+    // If the destructor is explicitly defaulted and not trivial or not public
+    // or if the destructor is explicitly deleted, we clear
+    // HasIrrelevantDestructor in finishedDefaultedOrDeletedMember.
 
     // C++11 [class.dtor]p5:
     //   A destructor is trivial if [...] the destructor is not virtual.
@@ -936,9 +939,11 @@
     else if (Constructor->isConstexpr())
       // We may now know that the constructor is constexpr.
       data().HasConstexprNonCopyMoveConstructor = true;
-  } else if (isa<CXXDestructorDecl>(D))
+  } else if (isa<CXXDestructorDecl>(D)) {
     SMKind |= SMF_Destructor;
-  else if (D->isCopyAssignmentOperator())
+    data().HasIrrelevantDestructor =
+        D->isTrivial() && D->getAccess() == AS_public && !D->isDeleted();
+  } else if (D->isCopyAssignmentOperator())
     SMKind |= SMF_CopyAssignment;
   else if (D->isMoveAssignmentOperator())
     SMKind |= SMF_MoveAssignment;
Index: test/SemaCXX/warn-global-constructors.cpp
===================================================================
--- test/SemaCXX/warn-global-constructors.cpp
+++ test/SemaCXX/warn-global-constructors.cpp
@@ -96,6 +96,39 @@
   }
 }
 
+namespace pr19253 {
+  struct A {
+    ~A() = default; // expected-warning{{C++11 extension}}
+  };
+
+  struct B {
+  private:
+    friend struct C;
+    ~B() = default; // expected-warning{{C++11 extension}}
+  };
+
+  struct C : B {
+    ~C() = default; // expected-warning{{C++11 extension}}
+    B b;
+  };
+
+  struct D {
+    ~D();
+  };
+
+  struct E {
+    D d;
+    ~E() = default; // expected-warning{{C++11 extension}}
+  };
+
+  // These variables do not require global destructors.
+  A a;
+  C c;
+
+  D d; // expected-warning {{global destructor}}
+  E e; // expected-warning {{global destructor}}
+}
+
 namespace referencemember {
   struct A { int &a; };
   int a;
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D3190.1.patch
Type: text/x-patch
Size: 2216 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20140326/373c4a11/attachment.bin>


More information about the cfe-commits mailing list