[cfe-commits] r46534 - in /cfe/trunk: CodeGen/CGExprConstant.cpp test/CodeGen/struct.c

Devang Patel dpatel at apple.com
Tue Jan 29 15:23:18 PST 2008


Author: dpatel
Date: Tue Jan 29 17:23:18 2008
New Revision: 46534

URL: http://llvm.org/viewvc/llvm-project?rev=46534&view=rev
Log:
Handle incomplete struct initializer.

Modified:
    cfe/trunk/CodeGen/CGExprConstant.cpp
    cfe/trunk/test/CodeGen/struct.c

Modified: cfe/trunk/CodeGen/CGExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/CodeGen/CGExprConstant.cpp?rev=46534&r1=46533&r2=46534&view=diff

==============================================================================
--- cfe/trunk/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/CodeGen/CGExprConstant.cpp Tue Jan 29 17:23:18 2008
@@ -80,19 +80,26 @@
     unsigned NumInitableElts = NumInitElements;
     std::vector<llvm::Constant*> Elts;    
       
-    // Initialising an array requires us to automatically initialise any 
-    // elements that have not been initialised explicitly
+    // Initialising an array or structure requires us to automatically 
+    // initialise any elements that have not been initialised explicitly
     const llvm::ArrayType *AType = 0; 
-    const llvm::Type *AElemTy = 0;
-    unsigned NumArrayElements = 0;
+    const llvm::StructType *SType = 0; 
+    const llvm::Type *ElemTy = 0;
+    unsigned NumElements = 0;
     
     // If this is an array, we may have to truncate the initializer
     if ((AType = dyn_cast<llvm::ArrayType>(CType))) {
-      NumArrayElements = AType->getNumElements();
-      AElemTy = AType->getElementType();
-      NumInitableElts = std::min(NumInitableElts, NumArrayElements);
+      NumElements = AType->getNumElements();
+      ElemTy = AType->getElementType();
+      NumInitableElts = std::min(NumInitableElts, NumElements);
     }
-    
+
+    // If this is a structure, we may have to truncate the initializer
+    if ((SType = dyn_cast<llvm::StructType>(CType))) {
+      NumElements = SType->getNumElements();
+      NumInitableElts = std::min(NumInitableElts, NumElements);
+    }
+
     // Copy initializer elements.
     unsigned i = 0;
     for (i = 0; i < NumInitableElts; ++i) {
@@ -107,19 +114,25 @@
       Elts.push_back(C);
     }
     
-    if (ILE->getType()->isStructureType())
-      return llvm::ConstantStruct::get(cast<llvm::StructType>(CType), Elts);
+    if (SType) {
+      // Initialize remaining structure elements.
+      for (; i < NumElements; ++i) {
+        ElemTy = SType->getElementType(i);
+        Elts.push_back(llvm::Constant::getNullValue(ElemTy));
+      }
+      return llvm::ConstantStruct::get(SType, Elts);
+    }
     
     if (ILE->getType()->isVectorType())
       return llvm::ConstantVector::get(cast<llvm::VectorType>(CType), Elts);
     
     // Make sure we have an array at this point
     assert(AType);
-    
+
     // Initialize remaining array elements.
-    for (; i < NumArrayElements; ++i)
-      Elts.push_back(llvm::Constant::getNullValue(AElemTy));
-    
+    for (; i < NumElements; ++i)
+      Elts.push_back(llvm::Constant::getNullValue(ElemTy));
+
     return llvm::ConstantArray::get(AType, Elts);    
   }
   

Modified: cfe/trunk/test/CodeGen/struct.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/struct.c?rev=46534&r1=46533&r2=46534&view=diff

==============================================================================
--- cfe/trunk/test/CodeGen/struct.c (original)
+++ cfe/trunk/test/CodeGen/struct.c Tue Jan 29 17:23:18 2008
@@ -135,4 +135,8 @@
 	const struct _a a2;
 	
 	a1 = a2;
-}
\ No newline at end of file
+}
+
+/* struct initialization */
+struct a13 {int b; int c};
+struct a13 c13 = {5};





More information about the cfe-commits mailing list