]> granicus.if.org Git - clang/commitdiff
Make VLAs usable, and make basic usage work correctly. Also, add a
authorEli Friedman <eli.friedman@gmail.com>
Sat, 20 Dec 2008 23:11:59 +0000 (23:11 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sat, 20 Dec 2008 23:11:59 +0000 (23:11 +0000)
simple test that actually does VLA codegen.

Note that despite the fact that the alloca isn't in the entry block, it
should dominate all uses; this is guaranteed by the restrictions on goto
into VLA scope in C99.

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

lib/CodeGen/CGDecl.cpp
lib/CodeGen/CGExprScalar.cpp
test/CodeGen/vla.c [new file with mode: 0644]

index 86bc3475761af418b7fad028c764daea0f1e8c02..db6aae6dfd73bbb34e211ec3172f6e6e42c00bf7 100644 (file)
@@ -192,17 +192,9 @@ void CodeGenFunction::EmitLocalBlockVarDecl(const VarDecl &D) {
 
     // Allocate memory for the array.
     llvm::Value *VLA = Builder.CreateAlloca(llvm::Type::Int8Ty, VLASize, "vla");
-    VLA = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
-
-    llvm::AllocaInst *Alloc = 
-      CreateTempAlloca(LElemPtrTy, D.getIdentifier()->getName());
-    
-    // FIXME: Volatile?
-    Builder.CreateStore(VLA, Alloc);
-    
-    DeclPtr = Alloc;
+    DeclPtr = Builder.CreateBitCast(VLA, LElemPtrTy, "tmp");
   }
-  
+
   llvm::Value *&DMEntry = LocalDeclMap[&D];
   assert(DMEntry == 0 && "Decl already exists in localdeclmap!");
   DMEntry = DeclPtr;
index ffb51647826fd077e12fbc30715b925a0b21792a..4984d0df355e86b95b3cec97473cd3959584b3e4 100644 (file)
@@ -513,16 +513,14 @@ Value *ScalarExprEmitter::VisitImplicitCastExpr(const ImplicitCastExpr *E) {
     // FIXME: For now we assume that all source arrays map to LLVM arrays.  This
     // will not true when we add support for VLAs.
     Value *V = EmitLValue(Op).getAddress();  // Bitfields can't be arrays.
-    
-    if (!(isa<llvm::PointerType>(V->getType()) &&
-          isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
-                               ->getElementType()))) {
-      CGF.ErrorUnsupported(E, "variable-length array cast", true);
-      if (E->getType()->isVoidType())
-        return 0;
-      return llvm::UndefValue::get(CGF.ConvertType(E->getType()));
+
+    if (!Op->getType()->isVariableArrayType()) {
+      assert(isa<llvm::PointerType>(V->getType()) && "Expected pointer");
+      assert(isa<llvm::ArrayType>(cast<llvm::PointerType>(V->getType())
+                                 ->getElementType()) &&
+             "Expected pointer to array");
+      V = Builder.CreateStructGEP(V, 0, "arraydecay");
     }
-    V = Builder.CreateStructGEP(V, 0, "arraydecay");
     
     // The resultant pointer type can be implicitly casted to other pointer
     // types as well (e.g. void*) and can be implicitly converted to integer.
diff --git a/test/CodeGen/vla.c b/test/CodeGen/vla.c
new file mode 100644 (file)
index 0000000..a584020
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: clang %s -emit-llvm -o %t
+
+int b(char* x);
+
+// Extremely basic VLA test
+void a(int x) {
+  char arry[x];
+  arry[0] = 10;
+  b(arry);
+}