]> granicus.if.org Git - clang/commitdiff
The array form of 'new' can never have initializers.
authorAnders Carlsson <andersca@mac.com>
Mon, 3 May 2010 15:45:23 +0000 (15:45 +0000)
committerAnders Carlsson <andersca@mac.com>
Mon, 3 May 2010 15:45:23 +0000 (15:45 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@102917 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaExprCXX.cpp
test/SemaCXX/new-delete.cpp

index 5c5e2c8ebf01214023d6560573eea9bb81063193..1d0270187940ee36ed9e9871951eafc8bb109517 100644 (file)
@@ -2179,6 +2179,8 @@ def err_new_incomplete_type : Error<
   "allocation of incomplete type %0">;
 def err_new_array_nonconst : Error<
   "only the first dimension of an allocated array may have dynamic size">;
+def err_new_array_init_args : Error<
+  "array 'new' cannot have initialization arguments">;
 def err_new_paren_array_nonconst : Error<
   "when type is in parentheses, array cannot have dynamic size">;
 def err_placement_new_non_placement_delete : Error<
index e6b3c4e3a046078bcfb74d4f846869655df4f323..425fc2d15aa5331d9cc648055a1e77726c3212c0 100644 (file)
@@ -738,6 +738,15 @@ Sema::BuildCXXNew(SourceLocation StartLoc, bool UseGlobal,
   unsigned NumConsArgs = ConstructorArgs.size();
   ASTOwningVector<&ActionBase::DeleteExpr> ConvertedConstructorArgs(*this);
 
+  // Array 'new' can't have any initializers.
+  if (NumConsArgs && ArraySize) {
+    SourceRange InitRange(ConsArgs[0]->getLocStart(),
+                          ConsArgs[NumConsArgs - 1]->getLocEnd());
+    
+    Diag(StartLoc, diag::err_new_array_init_args) << InitRange;
+    return ExprError();
+  }
+
   if (!AllocType->isDependentType() &&
       !Expr::hasAnyTypeDependentArguments(ConsArgs, NumConsArgs)) {
     // C++0x [expr.new]p15:
index 50aba47410a4e3657355cd7d3cc7106822aaedf8..763ed2c7b4d6b440e84192b287bd9bc63dc1b27d 100644 (file)
@@ -34,7 +34,6 @@ void good_news()
   S *ps = new S(1, 2, 3.4);
   ps = new (pf) (S)(1, 2, 3.4);
   S *(*paps)[2] = new S*[*pi][2];
-  ps = new (S[3])(1, 2, 3.4);
   typedef int ia4[4];
   ia4 *pai = new (int[3][4]);
   pi = ::new int;
@@ -231,3 +230,23 @@ namespace PR5918 { // Look for template operator new overloads.
     (void)new(0) S;
   }
 }
+
+namespace Test1 {
+
+void f() {
+  (void)new int[10](1, 2); // expected-error {{array 'new' cannot have initialization arguments}}
+}
+
+template<typename T>
+void g(unsigned i) {
+  (void)new T[1](i); // expected-error {{array 'new' cannot have initialization arguments}}
+}
+
+template<typename T>
+void h(unsigned i) {
+  (void)new T(i); // expected-error {{array 'new' cannot have initialization arguments}}
+}
+template void h<unsigned>(unsigned);
+template void h<unsigned[10]>(unsigned); // expected-note {{in instantiation of function template specialization 'Test1::h<unsigned int [10]>' requested here}}
+
+}