]> granicus.if.org Git - clang/commitdiff
Don't crash emitting an initializer for a static local with union type.
authorEli Friedman <eli.friedman@gmail.com>
Sun, 8 Jun 2008 01:23:18 +0000 (01:23 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Sun, 8 Jun 2008 01:23:18 +0000 (01:23 +0000)
This fix just makes sure to construct the global with the appropriate
type, and fixes up the one user this affects to compensate.

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

lib/CodeGen/CGDecl.cpp
test/CodeGen/static-local-union.c [new file with mode: 0644]

index 5451d4afc02b689dbbdaf676e02a1d4a30fc42c7..e7085fb5e31ac0a6e148f31969728e124884279d 100644 (file)
@@ -93,8 +93,9 @@ CodeGenFunction::GenerateStaticBlockVarDecl(const VarDecl &D,
   else
     assert(0 && "Unknown context for block var decl"); // FIXME Handle objc.
 
-  llvm::GlobalValue *GV = 
-    new llvm::GlobalVariable(LTy, false, llvm::GlobalValue::InternalLinkage,
+  llvm::GlobalValue *GV =
+    new llvm::GlobalVariable(Init->getType(), false,
+                             llvm::GlobalValue::InternalLinkage,
                              Init, ContextName + Separator + D.getName(),
                              &CGM.getModule(), 0, Ty.getAddressSpace());
 
@@ -115,7 +116,10 @@ void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) {
     CGM.AddAnnotation(Ann);
   }
 
-  DMEntry = GV;
+  const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType());
+  const llvm::Type *LPtrTy =
+    llvm::PointerType::get(LTy, D.getType().getAddressSpace());
+  DMEntry = llvm::ConstantExpr::getBitCast(GV, LPtrTy);
 
   // Emit global variable debug descriptor for static vars.
   CGDebugInfo *DI = CGM.getDebugInfo();
diff --git a/test/CodeGen/static-local-union.c b/test/CodeGen/static-local-union.c
new file mode 100644 (file)
index 0000000..9515a31
--- /dev/null
@@ -0,0 +1,4 @@
+// RUN: clang -emit-llvm < %s
+
+int a() {static union{int a;} r[2] = {1,2};return r[1].a;}
+