vbyakovl created this revision.
vbyakovl added a reviewer: aaron.s.wishnick.
vbyakovl added subscribers: DmitryPolukhin, cfe-commits.
This implements GNU C++ extension "Variable length array". This works under 
-std=gnu++98.

http://reviews.llvm.org/D18823

Files:
  llvm/tools/clang/lib/CodeGen/CGClass.cpp
  llvm/tools/clang/lib/CodeGen/CGExprCXX.cpp
  llvm/tools/clang/lib/CodeGen/CodeGenFunction.h
  llvm/tools/clang/lib/Sema/SemaType.cpp
  llvm/tools/clang/test/CodeGenCXX/vla-consruct.cpp
  llvm/tools/clang/test/SemaCXX/c99-variable-length-array.cpp
  llvm/tools/clang/test/SemaCXX/vla.cpp

Index: llvm/tools/clang/lib/CodeGen/CGExprCXX.cpp
===================================================================
--- llvm/tools/clang/lib/CodeGen/CGExprCXX.cpp
+++ llvm/tools/clang/lib/CodeGen/CGExprCXX.cpp
@@ -472,8 +472,8 @@
     }
   }
   
-  if (const ConstantArrayType *arrayType 
-        = getContext().getAsConstantArrayType(E->getType())) {
+  if (const ArrayType *arrayType
+        = getContext().getAsArrayType(E->getType())) {
     EmitCXXAggrConstructorCall(CD, arrayType, Dest.getAddress(), E);
   } else {
     CXXCtorType Type = Ctor_Complete;
Index: llvm/tools/clang/lib/CodeGen/CGClass.cpp
===================================================================
--- llvm/tools/clang/lib/CodeGen/CGClass.cpp
+++ llvm/tools/clang/lib/CodeGen/CGClass.cpp
@@ -1915,7 +1915,7 @@
 /// \param zeroInitialize true if each element should be
 ///   zero-initialized before it is constructed
 void CodeGenFunction::EmitCXXAggrConstructorCall(
-    const CXXConstructorDecl *ctor, const ConstantArrayType *arrayType,
+    const CXXConstructorDecl *ctor, const ArrayType *arrayType,
     Address arrayBegin, const CXXConstructExpr *E, bool zeroInitialize) {
   QualType elementType;
   llvm::Value *numElements =
Index: llvm/tools/clang/lib/CodeGen/CodeGenFunction.h
===================================================================
--- llvm/tools/clang/lib/CodeGen/CodeGenFunction.h
+++ llvm/tools/clang/lib/CodeGen/CodeGenFunction.h
@@ -1872,7 +1872,7 @@
                                       const CXXConstructExpr *E);
 
   void EmitCXXAggrConstructorCall(const CXXConstructorDecl *D,
-                                  const ConstantArrayType *ArrayTy,
+                                  const ArrayType *ArrayTy,
                                   Address ArrayPtr,
                                   const CXXConstructExpr *E,
                                   bool ZeroInitialization = false);
Index: llvm/tools/clang/lib/Sema/SemaType.cpp
===================================================================
--- llvm/tools/clang/lib/Sema/SemaType.cpp
+++ llvm/tools/clang/lib/Sema/SemaType.cpp
@@ -2155,7 +2155,8 @@
   }
   // If this is not C99, extwarn about VLA's and C99 array size modifiers.
   if (!getLangOpts().C99) {
-    if (T->isVariableArrayType()) {
+    if (T->isVariableArrayType() &&
+        !(getLangOpts().CPlusPlus && getLangOpts().GNUMode)) {
       // Prohibit the use of non-POD types in VLAs.
       QualType BaseT = Context.getBaseElementType(T);
       if (!T->isDependentType() && isCompleteType(Loc, BaseT) &&
Index: llvm/tools/clang/test/SemaCXX/vla.cpp
===================================================================
--- llvm/tools/clang/test/SemaCXX/vla.cpp
+++ llvm/tools/clang/test/SemaCXX/vla.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -verify %s
+// RUN: %clang_cc1 -std=c++98 -verify %s
 
 // PR11925
 int n;
Index: llvm/tools/clang/test/SemaCXX/c99-variable-length-array.cpp
===================================================================
--- llvm/tools/clang/test/SemaCXX/c99-variable-length-array.cpp
+++ llvm/tools/clang/test/SemaCXX/c99-variable-length-array.cpp
@@ -1,4 +1,7 @@
-// RUN: %clang_cc1 -fsyntax-only -verify -Wvla-extension %s
+// RUN: %clang_cc1 -fsyntax-only -std=c++98 -DCPP98 -verify -Wvla-extension %s
+// RUN: %clang_cc1 -fsyntax-only -std=gnu++98 -DGNU98 -verify -Wvla-extension %s
+
+#ifdef CPP98
 struct NonPOD {
   NonPOD();
 };
@@ -123,16 +126,20 @@
     (void)new vla_type; // expected-error{{variably}}
   }
 }
+#endif // CPP98
 
+#ifdef GNU98 
 namespace rdar8733881 { // rdar://8733881
 
 static const int k_cVal3 = (int)(1000*0.2f);
   int f() {
     // Ok, fold to a constant size array as an extension.
     char rgch[k_cVal3] = {0};
-  }
+  } // expected-warning{{control reaches end of non-void function}}
 }
+#endif // GNU98
 
+#ifdef CPP98
 namespace PR11744 {
   template<typename T> int f(int n) {
     T arr[3][n]; // expected-warning 3 {{variable length arrays are a C99 feature}}
@@ -161,3 +168,4 @@
     func2<int>();
   }
 }
+#endif // CPP98
Index: llvm/tools/clang/test/CodeGenCXX/vla-consruct.cpp
===================================================================
--- llvm/tools/clang/test/CodeGenCXX/vla-consruct.cpp
+++ llvm/tools/clang/test/CodeGenCXX/vla-consruct.cpp
@@ -0,0 +1,139 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fcxx-exceptions -fexceptions -std=gnu++98 -O0 %s -emit-llvm -o - | FileCheck %s
+
+extern "C" int printf(const char*, ...);
+
+static int N;
+struct S {
+  S() __attribute__ ((nothrow))  { printf("%d: S()\n", ++N); }
+  ~S()  __attribute__ ((nothrow))  { printf("%d: ~S()\n", N--); }
+  int n[17];
+};
+  // CHECK: [[struct_S:%.+]] = type { [17 x i32] }
+void print(int n, int a, int b, int c, int d) {
+  printf("n=%d\n,sizeof(S)=%d\nsizeof(array_t[0][0])=%d\nsizeof(array_t[0])=%d\nsizeof(array_t)=%d\n",
+         n, a, b, c, d);
+  if (n == 2) throw(n);
+}
+
+void test(int n) {
+  // CHECK: define void {{.*test.*}}(i32 [[n:%.+]]) #
+  // CHECK: [[n_addr:%.+]] = alloca
+  // CHECK-NEXT: [[saved_stack:%.+]] = alloca
+  // CHECK-NEXT: [[sizeof_S:%.+]] = alloca
+  // CHECK-NEXT: [[sizeof_array_t_0_0:%.+]] = alloca
+  // CHECK-NEXT: [[sizeof_array_t_0:%.+]] = alloca
+  // CHECK-NEXT: [[sizeof_array_t:%.+]] = alloca
+  // CHECK-NEXT: [[exn_slot:%.+]] = alloca i8*
+  // CHECK-NEXT: [[ehselector_slot:%.+]] = alloca i32
+  // CHECK-NEXT: store i32 [[n]], i32* [[n_addr]]
+  // CHECK-NEXT: [[t0:%.+]] = load i32, i32* [[n_addr]]
+  // CHECK-NEXT: [[t1:%.+]] = zext i32 [[t0]] to i64
+  // CHECK-NEXT: [[t2:%.+]] = load i32, i32* [[n_addr]]
+  // CHECK-NEXT: [[add:%.+]] = add nsw i32 [[t2]], 1
+  // CHECK-NEXT: [[t3:%.+]] = zext i32 [[add]] to i64
+  // CHECK-NEXT: [[t4:%.+]] = call i8* @llvm.stacksave()
+  // CHECK-NEXT: store i8* [[t4]], i8** [[saved_stack]]
+  // CHECK-NEXT: [[t5:%.+]] = mul nuw i64 [[t1]], [[t3]]
+  // CHECK-NEXT: [[vla:%.+]] = alloca [[struct_S]], i64 [[t5]]
+  // CHECK-NEXT: [[t6:%.+]] = mul nuw i64 [[t1]], [[t3]]
+  // CHECK-NEXT: [[isempty:%.+]] = icmp eq i64 [[t6]], 0
+  // CHECK-NEXT: br i1 [[isempty]], label %[[arrayctor_cont:.+]], label %[[new_ctorloop:.+]]
+
+  S array_t[n][n+1];
+ 
+  // CHECK: [[new_ctorloop]]
+  // CHECK-NEXT: [[arrayctor_end:%.+]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[vla]], i64 [[t6]]
+  // CHECK-NEXT: br label %[[arrayctor_loop:.+]]
+
+  // CHECK: [[arrayctor_loop]]
+  // CHECK-NEXT: [[arrayctor_cur:%.+]] = phi [[struct_S]]* [ [[vla]], %[[new_ctorloop]] ], [ [[arrayctor_next:%.+]], %[[arrayctor_loop]] ]
+  // CHECK-NEXT: call void [[ctor:@.+]]([[struct_S]]* [[arrayctor_cur]])
+  // CHECK-NEXT: [[arrayctor_next]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[arrayctor_cur]], i64 1
+  // CHECK-NEXT: [[arrayctor_done:%.+]] = icmp eq [[struct_S]]* [[arrayctor_next]], [[arrayctor_end]]
+  // CHECK-NEXT: br i1 [[arrayctor_done]], label %[[arrayctor_cont]], label %[[arrayctor_loop]]
+
+  int sizeof_S = sizeof(S);
+  int sizeof_array_t_0_0 = sizeof(array_t[0][0]);
+  int sizeof_array_t_0 = sizeof(array_t[0]);
+  int sizeof_array_t = sizeof(array_t);
+  print(n, sizeof_S, sizeof_array_t_0_0, sizeof_array_t_0, sizeof_array_t);
+
+  //  CHECK: [[arrayctor_cont]]
+  //  CHECK-NEXT: store i32 68, i32* [[sizeof_S]]
+  //  CHECK-NEXT: store i32 68, i32* [[sizeof_array_t_0_0]]
+  //  CHECK: [[t8:%.+]] = mul nuw i64 68, [[t3]]
+  //  CHECK-NEXT: [[conv:%.+]] = trunc i64 [[t8]] to i32
+  //  CHECK-NEXT: store i32 [[conv]], i32* [[sizeof_array_t_0]]
+  //  CHECK-NEXT: [[t9:%.+]] = mul nuw i64 [[t1]], [[t3]]
+  //  CHECK-NEXT: [[t10:%.+]] = mul nuw i64 68, [[t9]]
+  //  CHECK-NEXT: [[conv1:%.+]] = trunc i64 [[t10]] to i32
+  //  CHECK-NEXT: store i32 [[conv1]], i32* [[sizeof_array_t]]
+  //  CHECK-NEXT: [[t11:%.+]] = load i32, i32* [[n_addr:%.+]] 
+  //  CHECK-NEXT: [[t12:%.+]] = load i32, i32* [[sizeof_S]]
+  //  CHECK-NEXT: [[t13:%.+]] = load i32, i32* [[sizeof_array_t_0_0]]
+  //  CHECK-NEXT: [[t14:%.+]] = load i32, i32* [[sizeof_array_t_0]]
+  //  CHECK-NEXT: [[t15:%.+]] = load i32, i32* [[sizeof_array_t]]
+  //  CHECK-NEXT: invoke void @{{.*print.*}}(i32 [[t11]], i32 [[t12]], i32 [[t13]], i32 [[t14]], i32 [[t15]])
+  //  CHECK-NEXT: to label %[[invoke_cont:.+]] unwind label %[[lpad:.+]]
+
+  //  CHECK: [[invoke_cont]]
+  //  CHECK-NEXT: [[t16:%.+]] = mul nuw i64 [[t1]], [[t3]]
+  //  CHECK-NEXT: [[t17:%.+]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[vla]], i64 [[t16]]
+  //  CHECK-NEXT: [[arraydestroy_isempty:%.+]] = icmp eq [[struct_S]]* [[vla]], [[t17]]
+  //  CHECK-NEXT: br i1 [[arraydestroy_isempty]], label %[[arraydestroy_done2:.+]], label %[[arraydestroy_body:.+]]
+
+  //  CHECK: [[arraydestroy_body]]
+  //  CHECK-NEXT: [[arraydestroy_elementPast:%.+]] = phi [[struct_S]]* [ [[t17]], %[[invoke_cont]] ], [ [[arraydestroy_element:%.+]], %[[arraydestroy_body]] ]
+  //  CHECK-NEXT: [[arraydestroy_element]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[arraydestroy_elementPast]]
+  //  CHECK-NEXT: call void @[[dtor:.+]]([[struct_S]]* [[arraydestroy_element]])
+  //  CHECK-NEXT: [[arraydestroy_done:%.+]] = icmp eq [[struct_S]]* [[arraydestroy_element]], [[vla]]
+  //  CHECK-NEXT: br i1 [[arraydestroy_done]], label %[[arraydestroy_done2]], label %[[arraydestroy_body]]
+
+  //  CHECK: [[arraydestroy_done2]]
+  //  CHECK-NEXT: [[t17:%.+]] = load i8*, i8** [[saved_stack]]
+  //  CHECK-NEXT: call void @llvm.stackrestore(i8* [[t17]])
+  //  CHECK: ret void
+
+  //  CHECK: [[lpad]]
+  //  CHECK-NEXT: [[t19:%.+]] = landingpad { i8*, i32 }
+  //  CHECK: [[t20:%.+]] = extractvalue { i8*, i32 } [[t19]], 0
+  //  CHECK-NEXT: store i8* [[t20]], i8** [[exn_slot]]
+  //  CHECK-NEXT: [[t21:%.+]] = extractvalue { i8*, i32 } [[t19]], 1
+  //  CHECK-NEXT: store i32 [[t21]], i32* [[ehselector_slot]]
+  //  CHECK-NEXT: [[t22:%.+]] = mul nuw i64 [[t1]], [[t3]]
+  //  CHECK-NEXT: [[t23:%.+]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[vla]], i64 [[t22]]
+  //  CHECK-NEXT: [[arraydestroy_isempty3:%.+]] = icmp eq [[struct_S]]* [[vla]], [[t23]]
+  //  CHECK-NEXT: br i1 [[arraydestroy_isempty3]], label %[[arraydestroy_done8:.+]], label %[[arraydestroy_body4:.+]]
+
+  //  CHECK: [[arraydestroy_body4]]
+  //  CHECK: [[arraydestroy_elementPast5:%.+]] = phi [[struct_S]]* [ [[t23]], %[[lpad]] ], [ [[arraydestroy_element6:.+]], %[[arraydestroy_body4]] ]
+  //  CHECK-NEXT: [[arraydestroy_element6]] = getelementptr inbounds [[struct_S]], [[struct_S]]* [[arraydestroy_elementPast5]], i64 -1
+  //  CHECK-NEXT: call void @[[dtor]]([[struct_S]]* [[arraydestroy_element6]])
+  //  CHECK-NEXT: [[arraydestroy_done7:%.+]] = icmp eq [[struct_S]]* [[arraydestroy_element6]], [[vla]]
+  //  CHECK-NEXT: br i1 [[arraydestroy_done7]], label %[[arraydestroy_done8]], label %[[arraydestroy_body4]]
+
+  //  CHECK: [[arraydestroy_done8]]
+  //  CHECK-NEXT: br label %[[eh_resume:.+]]
+
+  //  CHECK: [[eh_resume]]
+  //  CHECK-NEXT: [[exn:%.+]] = load i8*, i8** [[exn_slot]]
+  //  CHECK-NEXT: [[sel:%.+]] = load i32, i32* [[ehselector_slot]]
+  //  CHECK-NEXT: [[lpad_val:%.+]] = insertvalue { i8*, i32 } undef, i8* [[exn]], 0
+  //  CHECK-NEXT: [[lpad_val9:%.+]] = insertvalue { i8*, i32 } [[lpad_val]], i32 [[sel]], 1
+  //  CHECK-NEXT: resume { i8*, i32 } [[lpad_val9]]
+
+}
+
+int main()
+{
+  try {
+    test(2);
+  } catch(int e) {
+    printf("expeption %d\n", e);
+  }
+  try {
+    test(3);
+  } catch(int e) {
+    printf("expeption %d", e);
+  }
+}
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to