]> granicus.if.org Git - clang/commitdiff
Support initalisers for more than just int-typed static variables.
authorOliver Hunt <oliver@apple.com>
Sun, 2 Dec 2007 00:11:25 +0000 (00:11 +0000)
committerOliver Hunt <oliver@apple.com>
Sun, 2 Dec 2007 00:11:25 +0000 (00:11 +0000)
We now use the CodeGenModule logic for generating the constant
initialiser expression, so happily further initialiser fixes should
automatically work for statics as well.

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

CodeGen/CGDecl.cpp
CodeGen/CodeGenModule.cpp
CodeGen/CodeGenModule.h
test/CodeGen/staticinit.c

index 09a47b9e5e6b2da33e21acbed6181e51eaf08050..b9f072b65fa7ab14781c6001a9eaddd22faaa876 100644 (file)
@@ -75,14 +75,11 @@ void CodeGenFunction::EmitStaticBlockVarDecl(const BlockVarDecl &D) {
   llvm::Constant *Init = 0;
   if (D.getInit() == 0) {
     Init = llvm::Constant::getNullValue(LTy);
-  } else if (D.getType()->isIntegerType()) {
-    llvm::APSInt Value(static_cast<uint32_t>(
-      getContext().getTypeSize(D.getInit()->getType(), SourceLocation())));
-    if (D.getInit()->isIntegerConstantExpr(Value, getContext()))
-      Init = llvm::ConstantInt::get(Value);
+  } else {
+    Init = CGM.EmitGlobalInit(D.getInit());
   }
 
-  assert(Init && "FIXME: Support initializers");
+  assert(Init && "Unable to create initialiser for static decl");
   
   DMEntry = 
     new llvm::GlobalVariable(LTy, false, 
index e4d0f47b0c3fa8c17f980450bc4506472f05ed0b..171a70c5a0cc65c1a00eb1ef323d87678cf50b9d 100644 (file)
@@ -275,9 +275,8 @@ static llvm::Constant *GenerateConstantExpr(const Expr* Expression,
   return 0;
 }
 
-llvm::Constant *CodeGenModule::EmitGlobalInit(const FileVarDecl *D,
-                                              llvm::GlobalVariable *GV) {
-  return GenerateConstantExpr(D->getInit(), *this);
+llvm::Constant *CodeGenModule::EmitGlobalInit(const Expr *Expression) {
+  return GenerateConstantExpr(Expression, *this);
 }
 
 void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
@@ -300,7 +299,7 @@ void CodeGenModule::EmitGlobalVar(const FileVarDecl *D) {
   }
 
   if (!Init)
-    Init = EmitGlobalInit(D, GV);
+    Init = EmitGlobalInit(D->getInit());
 
   assert(Init && "FIXME: Global variable initializers unimp!");
 
index 1d7c30e487293ed2aa5762dcd22afa09873698e7..9278e68641042c4bd596ae9edbd14ba606deffc1 100644 (file)
@@ -30,6 +30,7 @@ namespace clang {
   class ASTContext;
   class FunctionDecl;
   class Decl;
+  class Expr;
   class ValueDecl;
   class FileVarDecl;
   struct LangOptions;
@@ -76,8 +77,7 @@ public:
   void EmitFunction(const FunctionDecl *FD);
   void EmitGlobalVar(const FileVarDecl *D);
   void EmitGlobalVarDeclarator(const FileVarDecl *D);
-  llvm::Constant *EmitGlobalInit(const FileVarDecl *D, 
-                                 llvm::GlobalVariable *GV);
+  llvm::Constant *EmitGlobalInit(const Expr *Expression);
   
   void PrintStats() {}
 };
index 7411be32202c56c071027ac15649ca3cc56a1338..e226179768caf96d0544cdaa1177b146bdefa4ed 100644 (file)
@@ -1,5 +1,15 @@
 // RUN: clang -emit-llvm %s
 
+struct AStruct { 
+       int i;
+       char *s;
+       double d;
+};
+
 void f() {
   static int i = 42;
+  static int is[] = { 1, 2, 3, 4 };
+  static char* str = "forty-two";
+  static char* strs[] = { "one", "two", "three", "four" };
+  static struct AStruct myStruct = { 1, "two", 3.0 };
 }