diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-02-25 20:08:33 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-02-25 20:08:33 +0000 |
commit | e5731f81497d11f95027a500a3ee118f4fc9e1be (patch) | |
tree | 1b941443c359c5b65fd13e4e1d578c931b01f774 | |
parent | ac1afdc58e6ad5c969f45fd2ff6d140d3b4dd862 (diff) |
Allow constant initializers to reference their defining decl.
- PR3662.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65472 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/CodeGen/CGDecl.cpp | 9 | ||||
-rw-r--r-- | test/CodeGen/const-init.c | 11 |
2 files changed, 20 insertions, 0 deletions
diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 349656f58b..ee2516a850 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -106,6 +106,10 @@ void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) { llvm::GlobalVariable *GV = CreateStaticBlockVarDecl(D, ".", llvm::GlobalValue::InternalLinkage); + // Store into LocalDeclMap before generating initializer to handle + // circular references. + DMEntry = GV; + if (D.getInit()) { llvm::Constant *Init = CGM.EmitConstantExpr(D.getInit(), this); @@ -162,6 +166,11 @@ void CodeGenFunction::EmitStaticBlockVarDecl(const VarDecl &D) { if (D.getAttr<UsedAttr>()) CGM.AddUsedGlobal(GV); + // We may have to cast the constant because of the initializer + // mismatch above. + // + // FIXME: It is really dangerous to store this in the map; if anyone + // RAUW's the GV uses of this constant will be invalid. const llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(D.getType()); const llvm::Type *LPtrTy = llvm::PointerType::get(LTy, D.getType().getAddressSpace()); diff --git a/test/CodeGen/const-init.c b/test/CodeGen/const-init.c index f6199815ed..d88add36a9 100644 --- a/test/CodeGen/const-init.c +++ b/test/CodeGen/const-init.c @@ -85,4 +85,15 @@ void g18(void) { static int *p[] = { &g19 }; } +// RUN: grep '@g20.l0 = internal global .struct.g20_s1 <{ .struct.g20_s0\* null, .struct.g20_s0\*\* getelementptr (.struct.g20_s1\* @g20.l0, i32 0, i32 0) }>' %t && + +struct g20_s0; +struct g20_s1 { + struct g20_s0 *f0, **f1; +}; +void *g20(void) { + static struct g20_s1 l0 = { ((void*) 0), &l0.f0 }; + return l0.f1; +} + // RUN: true |