diff options
author | Fariborz Jahanian <fjahanian@apple.com> | 2009-11-05 18:03:03 +0000 |
---|---|---|
committer | Fariborz Jahanian <fjahanian@apple.com> | 2009-11-05 18:03:03 +0000 |
commit | 393c247fe025ccb5f914e37e948192ea86faef8c (patch) | |
tree | ccd2ca710411f416b3dd51bd6e4765245133aa2c | |
parent | 9c4b838782132ec670fd7e48d1a7a7fd433fed06 (diff) |
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
-rw-r--r-- | include/clang/AST/Expr.h | 2 | ||||
-rw-r--r-- | lib/AST/ExprConstant.cpp | 15 | ||||
-rw-r--r-- | lib/CodeGen/CodeGenModule.cpp | 13 | ||||
-rw-r--r-- | lib/CodeGen/CodeGenModule.h | 2 | ||||
-rw-r--r-- | test/CodeGenCXX/static-init-1.cpp | 23 |
5 files changed, 42 insertions, 13 deletions
diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 2a87f58835..665639128e 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -250,6 +250,8 @@ public: /// 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 7862c57c2d..c036640b71 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -204,13 +204,6 @@ public: 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 @@ bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) { } } - 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 @@ bool Expr::isEvaluatable(ASTContext &Ctx) const { 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 db609f6245..5b372faef7 100644 --- a/lib/CodeGen/CodeGenModule.cpp +++ b/lib/CodeGen/CodeGenModule.cpp @@ -543,10 +543,15 @@ bool CodeGenModule::MayDeferGeneration(const ValueDecl *Global) { // 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 2e58337ee5..b09f52cf2b 100644 --- a/lib/CodeGen/CodeGenModule.h +++ b/lib/CodeGen/CodeGenModule.h @@ -499,7 +499,7 @@ private: /// 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 0000000000..d485d4c957 --- /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); + |