summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDevang Patel <dpatel@apple.com>2008-01-29 23:23:18 +0000
committerDevang Patel <dpatel@apple.com>2008-01-29 23:23:18 +0000
commit45a65d2ee19e349c3f98a4e4b586c96963ed947f (patch)
treedefaa6da727617374a32633670f7f23106540047
parent284011b462938b7b60af89839dec32948f4d4b23 (diff)
downloadbcm5719-llvm-45a65d2ee19e349c3f98a4e4b586c96963ed947f.tar.gz
bcm5719-llvm-45a65d2ee19e349c3f98a4e4b586c96963ed947f.zip
Handle incomplete struct initializer.
llvm-svn: 46534
-rw-r--r--clang/CodeGen/CGExprConstant.cpp41
-rw-r--r--clang/test/CodeGen/struct.c6
2 files changed, 32 insertions, 15 deletions
diff --git a/clang/CodeGen/CGExprConstant.cpp b/clang/CodeGen/CGExprConstant.cpp
index e03db991c45..070ce1d734c 100644
--- a/clang/CodeGen/CGExprConstant.cpp
+++ b/clang/CodeGen/CGExprConstant.cpp
@@ -80,19 +80,26 @@ public:
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 @@ public:
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);
}
diff --git a/clang/test/CodeGen/struct.c b/clang/test/CodeGen/struct.c
index be5917a3da9..919983938f1 100644
--- a/clang/test/CodeGen/struct.c
+++ b/clang/test/CodeGen/struct.c
@@ -135,4 +135,8 @@ void f12()
const struct _a a2;
a1 = a2;
-} \ No newline at end of file
+}
+
+/* struct initialization */
+struct a13 {int b; int c};
+struct a13 c13 = {5};
OpenPOWER on IntegriCloud