]> granicus.if.org Git - clang/commitdiff
Correctly handle scalars in braces.
authorAnders Carlsson <andersca@mac.com>
Tue, 29 Jan 2008 01:15:48 +0000 (01:15 +0000)
committerAnders Carlsson <andersca@mac.com>
Tue, 29 Jan 2008 01:15:48 +0000 (01:15 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@46480 91177308-0d34-0410-b5e6-96231b3b80d8

CodeGen/CGExprConstant.cpp
CodeGen/CGExprScalar.cpp
test/CodeGen/globalinit.c
test/CodeGen/init.c [new file with mode: 0644]

index 5808da4212defde8f2505b6d3534813add293e8f..da2fef66f03912faf41b71206c4422813e1cf03a 100644 (file)
@@ -68,26 +68,18 @@ public:
   }
   
   llvm::Constant *VisitInitListExpr(InitListExpr *ILE) {
-    if (ILE->getType()->isVoidType()) {
-      // FIXME: Remove this when sema of initializers is finished (and the code
-      // below).
-      CGM.WarnUnsupported(ILE, "initializer");
-      return 0;
+    const llvm::CompositeType *CType = 
+      dyn_cast<llvm::CompositeType>(ConvertType(ILE->getType()));
+
+    if (!CType) {
+        // We have a scalar in braces. Just use the first element.
+        return Visit(ILE->getInit(0));
     }
-    
-    assert((ILE->getType()->isArrayType() || ILE->getType()->isStructureType() ||
-            ILE->getType()->isVectorType()) &&
-           "Bad type for init list!");
-    CodeGenTypes& Types = CGM.getTypes();
-    
+      
     unsigned NumInitElements = ILE->getNumInits();
     unsigned NumInitableElts = NumInitElements;
-    
-    const llvm::CompositeType *CType = 
-    cast<llvm::CompositeType>(Types.ConvertType(ILE->getType()));
-    assert(CType);
     std::vector<llvm::Constant*> Elts;    
-    
+      
     // Initialising an array requires us to automatically initialise any 
     // elements that have not been initialised explicitly
     const llvm::ArrayType *AType = 0; 
index 8ec1a83318c98269f1a07adc0d188626962342db..652729ac27b1e1f1a3516efd2970ddccdf18a436 100644 (file)
@@ -130,7 +130,11 @@ public:
     unsigned NumInitElements = E->getNumInits();
     
     const llvm::VectorType *VType = 
-      cast<llvm::VectorType>(ConvertType(E->getType()));
+      dyn_cast<llvm::VectorType>(ConvertType(E->getType()));
+    
+    // We have a scalar in braces. Just use the first element.
+    if (!VType) 
+      return Visit(E->getInit(0));
     
     unsigned NumVectorElements = VType->getNumElements();
     const llvm::Type *ElementType = VType->getElementType();
index 717b0c1cfba404f12d7646a8c6cd2546e22fd478..13a9e930bd3b4c578bd049f91bb53cb6a19797b4 100644 (file)
@@ -32,3 +32,6 @@ void booltest2() {
   static _Bool booltest3 = 4;
 }
 
+// Braces in a scalar
+int a = { 1 };
+int b = { 1, 2 };
diff --git a/test/CodeGen/init.c b/test/CodeGen/init.c
new file mode 100644 (file)
index 0000000..77a85fa
--- /dev/null
@@ -0,0 +1,7 @@
+// RUN: clang -emit-llvm %s
+void f1()
+{
+       // Braces in a scalar
+       int a = { 1 };
+       int b = { 1, 2 };
+}
\ No newline at end of file