diff options
-rw-r--r-- | CodeGen/CGExpr.cpp | 2 | ||||
-rw-r--r-- | CodeGen/CodeGenModule.cpp | 12 | ||||
-rw-r--r-- | CodeGen/CodeGenModule.h | 4 | ||||
-rw-r--r-- | test/CodeGen/globalinit.c | 7 |
4 files changed, 17 insertions, 8 deletions
diff --git a/CodeGen/CGExpr.cpp b/CodeGen/CGExpr.cpp index 8d136bc0b4..c20680bc4e 100644 --- a/CodeGen/CGExpr.cpp +++ b/CodeGen/CGExpr.cpp @@ -273,7 +273,7 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { } else if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { return LValue::MakeAddr(CGM.GetAddrOfFunctionDecl(FD, false)); } else if (const FileVarDecl *FVD = dyn_cast<FileVarDecl>(D)) { - return LValue::MakeAddr(CGM.GetAddrOfFileVarDecl(FVD, false)); + return LValue::MakeAddr(CGM.GetAddrOfGlobalVar(FVD, false)); } assert(0 && "Unimp declref"); //an invalid LValue, but the assert will diff --git a/CodeGen/CodeGenModule.cpp b/CodeGen/CodeGenModule.cpp index a09932e141..d6412a745e 100644 --- a/CodeGen/CodeGenModule.cpp +++ b/CodeGen/CodeGenModule.cpp @@ -113,8 +113,10 @@ llvm::Constant *CodeGenModule::GetAddrOfFunctionDecl(const FunctionDecl *D, return Entry = NewFn; } -llvm::Constant *CodeGenModule::GetAddrOfFileVarDecl(const FileVarDecl *D, - bool isDefinition) { +llvm::Constant *CodeGenModule::GetAddrOfGlobalVar(const VarDecl *D, + bool isDefinition) { + assert(D->hasGlobalStorage() && "Not a global variable"); + // See if it is already in the map. llvm::Constant *&Entry = GlobalDeclMap[D]; if (Entry) return Entry; @@ -433,8 +435,8 @@ static llvm::Constant *GenerateConstantExpr(const Expr *Expression, // The only thing that can have array type like this is a // DeclRefExpr(FileVarDecl)? const DeclRefExpr *DRE = cast<DeclRefExpr>(ICExpr->getSubExpr()); - const FileVarDecl *FVD = cast<FileVarDecl>(DRE->getDecl()); - llvm::Constant *C = CGM.GetAddrOfFileVarDecl(FVD, false); + const VarDecl *VD = cast<VarDecl>(DRE->getDecl()); + llvm::Constant *C = CGM.GetAddrOfGlobalVar(VD, false); assert(isa<llvm::PointerType>(C->getType()) && isa<llvm::ArrayType>(cast<llvm::PointerType>(C->getType()) ->getElementType())); @@ -486,7 +488,7 @@ void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) { // Get the global, forcing it to be a direct reference. llvm::GlobalVariable *GV = - cast<llvm::GlobalVariable>(GetAddrOfFileVarDecl(D, true)); + cast<llvm::GlobalVariable>(GetAddrOfGlobalVar(D, true)); // Convert the initializer, or use zero if appropriate. llvm::Constant *Init = 0; diff --git a/CodeGen/CodeGenModule.h b/CodeGen/CodeGenModule.h index 5c61ad882b..6339f51671 100644 --- a/CodeGen/CodeGenModule.h +++ b/CodeGen/CodeGenModule.h @@ -33,6 +33,7 @@ namespace clang { class Expr; class Stmt; class ValueDecl; + class VarDecl; class FileVarDecl; struct LangOptions; class Diagnostic; @@ -69,8 +70,7 @@ public: llvm::Constant *GetAddrOfFunctionDecl(const FunctionDecl *D, bool isDefinition); - llvm::Constant *GetAddrOfFileVarDecl(const FileVarDecl *D, - bool isDefinition); + llvm::Constant *GetAddrOfGlobalVar(const VarDecl *D, bool isDefinition); /// getBuiltinLibFunction - Given a builtin id for a function like diff --git a/test/CodeGen/globalinit.c b/test/CodeGen/globalinit.c index f47fd5c457..66bffd3dce 100644 --- a/test/CodeGen/globalinit.c +++ b/test/CodeGen/globalinit.c @@ -19,3 +19,10 @@ int (*mb_ptr2len) (char *p) = latin_ptr2len; char string[8] = "string"; // extend init char string2[4] = "string"; // truncate init +char *test(int c) { + static char buf[10]; + static char *bufptr = buf; + + return c ? buf : bufptr; +} + |