[cfe-commits] r115790 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaExprCXX.cpp test/SemaCXX/c99-variable-length-array.cpp

Douglas Gregor dgregor at apple.com
Wed Oct 6 09:00:31 PDT 2010


Author: dgregor
Date: Wed Oct  6 11:00:31 2010
New Revision: 115790

URL: http://llvm.org/viewvc/llvm-project?rev=115790&view=rev
Log:
Reject the allocation of variably-modified types in C++ 'new'
expressions. Fixes PR8209 in the narrowest way possible. I'm still
considering whether I want to implement the extension that permits the
use of VLA types in a 'new' expression.

Modified:
    cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
    cfe/trunk/lib/Sema/SemaExprCXX.cpp
    cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=115790&r1=115789&r2=115790&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Oct  6 11:00:31 2010
@@ -46,12 +46,14 @@
   "variable length array declaration can not have 'static' storage duration">;
 def err_vla_decl_has_extern_linkage : Error<
   "variable length array declaration can not have 'extern' linkage">;
-  
+
 // C99 variably modified types
 def err_variably_modified_template_arg : Error<
   "variably modified type %0 cannot be used as a template argument">;
 def err_variably_modified_nontype_template_param : Error<
   "non-type template parameter of variably modified type %0">;
+def err_variably_modified_new_type : Error<
+  "'new' cannot allocate object of variably modified type %0">;
 
 // C99 Designated Initializers
 def err_array_designator_negative : Error<

Modified: cfe/trunk/lib/Sema/SemaExprCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExprCXX.cpp?rev=115790&r1=115789&r2=115790&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExprCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExprCXX.cpp Wed Oct  6 11:00:31 2010
@@ -711,8 +711,6 @@
                   MultiExprArg ConstructorArgs,
                   SourceLocation ConstructorRParen) {
   SourceRange TypeRange = AllocTypeInfo->getTypeLoc().getSourceRange();
-  if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange))
-    return ExprError();
 
   // Per C++0x [expr.new]p5, the type being constructed may be a
   // typedef of an array type.
@@ -726,6 +724,9 @@
     }
   }
 
+  if (CheckAllocatedType(AllocType, TypeRange.getBegin(), TypeRange))
+    return ExprError();
+
   QualType ResultType = Context.getPointerType(AllocType);
 
   // C++ 5.3.4p6: "The expression in a direct-new-declarator shall have integral
@@ -930,7 +931,10 @@
   else if (RequireNonAbstractType(Loc, AllocType,
                                   diag::err_allocation_of_abstract_type))
     return true;
-
+  else if (AllocType->isVariablyModifiedType())
+    return Diag(Loc, diag::err_variably_modified_new_type)
+             << AllocType;
+  
   return false;
 }
 

Modified: cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp?rev=115790&r1=115789&r2=115790&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp (original)
+++ cfe/trunk/test/SemaCXX/c99-variable-length-array.cpp Wed Oct  6 11:00:31 2010
@@ -114,3 +114,10 @@
   };
   B<A> a;
 }
+
+namespace PR8209 {
+  void f(int n) {
+    typedef int vla_type[n]; // expected-warning{{variable length arrays are a C99 feature, accepted as an extension}}
+    (void)new vla_type; // expected-error{{variably}}
+  }
+}





More information about the cfe-commits mailing list