]> granicus.if.org Git - clang/commitdiff
Enforce access control for the destructor in a new[] expression and mark
authorJohn McCall <rjmccall@apple.com>
Wed, 13 Jul 2011 20:12:57 +0000 (20:12 +0000)
committerJohn McCall <rjmccall@apple.com>
Wed, 13 Jul 2011 20:12:57 +0000 (20:12 +0000)
it as used.  Otherwise, we can fail to instantiate or validate the destructor,
which can lead to crashes in IR gen like PR10351.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@135073 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExprCXX.cpp
test/CodeGenCXX/partial-destruction.cpp
test/SemaCXX/new-delete.cpp

index 7ee9c1bec2041af5411d029ad9ab65e2e455f9ce..e804fcd60c74e9c1c2412be42673e93a6fcae129 100644 (file)
@@ -1148,7 +1148,17 @@ Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal,
   if (OperatorDelete)
     MarkDeclarationReferenced(StartLoc, OperatorDelete);
 
-  // FIXME: Also check that the destructor is accessible. (C++ 5.3.4p16)
+  // C++0x [expr.new]p17:
+  //   If the new expression creates an array of objects of class type,
+  //   access and ambiguity control are done for the destructor.
+  if (ArraySize && Constructor) {
+    if (CXXDestructorDecl *dtor = LookupDestructor(Constructor->getParent())) {
+      MarkDeclarationReferenced(StartLoc, dtor);
+      CheckDestructorAccess(StartLoc, dtor, 
+                            PDiag(diag::err_access_dtor)
+                              << Context.getBaseElementType(AllocType));
+    }
+  }
 
   PlacementArgs.release();
   ConstructorArgs.release();
index f929d2d75e7ff8c3eaf900d0530dac6937d75407..82deca06cf80854c2fe9ff6ae47504d1d1f96641 100644 (file)
@@ -153,3 +153,17 @@ namespace test2 {
   }
 
 }
+
+// PR10351
+namespace test3 {
+  struct A { A(); ~A(); void *p; };
+  struct B {
+    B() {}
+    A a;
+  };
+
+  B *test() {
+    return new B[10];
+    // invoke void @_ZN5test31BD1Ev(
+  }
+}
index 13ef461e7c0d8b20ce8478eac5940060f03c50f3..efdfa0f066dbf818b289cd7500011e0a0e9bfcb7 100644 (file)
@@ -389,3 +389,11 @@ namespace PR7702 {
     new DoesNotExist; // expected-error {{expected a type}}
   }
 }
+
+namespace ArrayNewNeedsDtor {
+  struct A { A(); private: ~A(); }; // expected-note {{declared private here}}
+  struct B { B(); A a; }; // expected-error {{field of type 'ArrayNewNeedsDtor::A' has private destructor}}
+  B *test9() {
+    return new B[5]; // expected-note {{implicit default destructor for 'ArrayNewNeedsDtor::B' first required here}}
+  }
+}