Added support for static variables which require
initialization before main. Fixes pr5396.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@86145 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h
index 2a87f58..6656391 100644
--- a/include/clang/AST/Expr.h
+++ b/include/clang/AST/Expr.h
@@ -250,6 +250,8 @@
   /// folded, but discard the result.
   bool isEvaluatable(ASTContext &Ctx) const;
 
+  bool HasSideEffects(ASTContext &Ctx) const;
+  
   /// EvaluateAsInt - Call Evaluate and return the folded integer. This
   /// must be called on an expression that constant folds to an integer.
   llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const;
diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp
index 7862c57..c036640 100644
--- a/lib/AST/ExprConstant.cpp
+++ b/lib/AST/ExprConstant.cpp
@@ -204,13 +204,6 @@
   bool VisitUnaryOperator(UnaryOperator *E) { return Visit(E->getSubExpr()); }
 };
 
-bool HasSideEffects(const Expr* E, ASTContext &Ctx) {
-  Expr::EvalResult Result;
-  EvalInfo Info(Ctx, Result);
-
-  return HasSideEffect(Info).Visit(const_cast<Expr*>(E));
-}
-
 } // end anonymous namespace
 
 //===----------------------------------------------------------------------===//
@@ -964,7 +957,7 @@
           }
         }
 
-    if (HasSideEffects(E->getArg(0), Info.Ctx)) {
+    if (E->getArg(0)->HasSideEffects(Info.Ctx)) {
       if (E->getArg(1)->EvaluateAsInt(Info.Ctx).getZExtValue() < 2)
         return Success(-1ULL, E);
       return Success(0, E);
@@ -1947,6 +1940,12 @@
   return Evaluate(Result, Ctx) && !Result.HasSideEffects;
 }
 
+bool Expr::HasSideEffects(ASTContext &Ctx) const {
+  Expr::EvalResult Result;
+  EvalInfo Info(Ctx, Result);
+  return HasSideEffect(Info).Visit(const_cast<Expr*>(this));
+}
+
 APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const {
   EvalResult EvalResult;
   bool Result = Evaluate(EvalResult, Ctx);
diff --git a/lib/CodeGen/CodeGenModule.cpp b/lib/CodeGen/CodeGenModule.cpp
index db609f6..5b372fa 100644
--- a/lib/CodeGen/CodeGenModule.cpp
+++ b/lib/CodeGen/CodeGenModule.cpp
@@ -543,10 +543,15 @@
       
   // Static data may be deferred, but out-of-line static data members
   // cannot be.
-  // FIXME: What if the initializer has side effects?
-  return VD->isInAnonymousNamespace() ||
-         (VD->getStorageClass() == VarDecl::Static &&
-          !(VD->isStaticDataMember() && VD->isOutOfLine()));
+  if (VD->isInAnonymousNamespace())
+    return true;
+  if (VD->getStorageClass() == VarDecl::Static) {
+    // Initializer has side effects?
+    if (VD->getInit() && VD->getInit()->HasSideEffects(Context))
+      return false;
+    return !(VD->isStaticDataMember() && VD->isOutOfLine());
+  }
+  return false;
 }
 
 void CodeGenModule::EmitGlobal(GlobalDecl GD) {
diff --git a/lib/CodeGen/CodeGenModule.h b/lib/CodeGen/CodeGenModule.h
index 2e58337..b09f52c 100644
--- a/lib/CodeGen/CodeGenModule.h
+++ b/lib/CodeGen/CodeGenModule.h
@@ -499,7 +499,7 @@
 
   /// EmitCXXGlobalInitFunc - Emit a function that initializes C++ globals.
   void EmitCXXGlobalInitFunc();
-
+  
   // FIXME: Hardcoding priority here is gross.
   void AddGlobalCtor(llvm::Function *Ctor, int Priority=65535);
   void AddGlobalDtor(llvm::Function *Dtor, int Priority=65535);
diff --git a/test/CodeGenCXX/static-init-1.cpp b/test/CodeGenCXX/static-init-1.cpp
new file mode 100644
index 0000000..d485d4c
--- /dev/null
+++ b/test/CodeGenCXX/static-init-1.cpp
@@ -0,0 +1,23 @@
+// RUN: clang-cc -triple=x86_64-apple-darwin9 -emit-llvm %s -o %t &&
+// RUN: grep "call i32 @_Z5func1i" %t | count 3
+
+extern "C" int printf(...);
+
+static int count;
+
+int func2(int c) { return printf("loading the func2(%d)\n", c); };
+int func1(int c) { return printf("loading the func1(%d)\n", c); }
+
+static int loader_1 = func1(++count);
+
+int loader_2 = func2(++count);
+
+static int loader_3 = func1(++count);
+
+
+int main() {}
+
+int loader_4 = func2(++count);
+static int loader_5 = func1(++count);
+int loader_6 = func2(++count);
+