]> granicus.if.org Git - clang/commitdiff
local static vars are globals also. This fixes a testcase
authorChris Lattner <sabre@nondot.org>
Tue, 18 Dec 2007 08:16:44 +0000 (08:16 +0000)
committerChris Lattner <sabre@nondot.org>
Tue, 18 Dec 2007 08:16:44 +0000 (08:16 +0000)
reported by Seo.

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

CodeGen/CGExpr.cpp
CodeGen/CodeGenModule.cpp
CodeGen/CodeGenModule.h
test/CodeGen/globalinit.c

index 8d136bc0b4282fe319fcc4549a4b244862617854..c20680bc4ea3008bd46e3591c649991c2dfa4a18 100644 (file)
@@ -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
index a09932e141c6b1d4d5af18fd706b32793868bafd..d6412a745eac1ba1d5be3fd1954a78a6fa4eeeb2 100644 (file)
@@ -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;
index 5c61ad882bc94e8955efe74b71db51a6d28332fc..6339f51671c5e3b74d927600eca0a678368b07bd 100644 (file)
@@ -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
index f47fd5c457754d9ab20aacf9bf7641c4fd7ba5ab..66bffd3dce39b0cbd716edb31d661fc6fa29b8d1 100644 (file)
@@ -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;
+}
+