[cfe-commits] r86145 - in /cfe/trunk: include/clang/AST/Expr.h lib/AST/ExprConstant.cpp lib/CodeGen/CodeGenModule.cpp lib/CodeGen/CodeGenModule.h test/CodeGenCXX/static-init-1.cpp

Chris Lattner clattner at apple.com
Thu Nov 5 10:10:07 PST 2009


On Nov 5, 2009, at 10:03 AM, Fariborz Jahanian wrote:
> URL: http://llvm.org/viewvc/llvm-project?rev=86145&view=rev
> Log:
> Added support for static variables which require
> initialization before main. Fixes pr5396.

Thanks Fariborz.  Please add a big doxygen comment above the Expr::  
HasSideEffects method in the header explaining exactly what kind of  
"side effects" it returns true for.

-Chris

>
>
> Added:
>    cfe/trunk/test/CodeGenCXX/static-init-1.cpp
> Modified:
>    cfe/trunk/include/clang/AST/Expr.h
>    cfe/trunk/lib/AST/ExprConstant.cpp
>    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
>    cfe/trunk/lib/CodeGen/CodeGenModule.h
>
> Modified: cfe/trunk/include/clang/AST/Expr.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=86145&r1=86144&r2=86145&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/include/clang/AST/Expr.h (original)
> +++ cfe/trunk/include/clang/AST/Expr.h Thu Nov  5 12:03:03 2009
> @@ -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;
>
> Modified: cfe/trunk/lib/AST/ExprConstant.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=86145&r1=86144&r2=86145&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/lib/AST/ExprConstant.cpp (original)
> +++ cfe/trunk/lib/AST/ExprConstant.cpp Thu Nov  5 12:03:03 2009
> @@ -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);
>
> Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=86145&r1=86144&r2=86145&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Thu Nov  5 12:03:03 2009
> @@ -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) {
>
> Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=86145&r1=86144&r2=86145&view=diff
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
> +++ cfe/trunk/lib/CodeGen/CodeGenModule.h Thu Nov  5 12:03:03 2009
> @@ -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);
>
> Added: cfe/trunk/test/CodeGenCXX/static-init-1.cpp
> URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/static-init-1.cpp?rev=86145&view=auto
>
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> = 
> ======================================================================
> --- cfe/trunk/test/CodeGenCXX/static-init-1.cpp (added)
> +++ cfe/trunk/test/CodeGenCXX/static-init-1.cpp Thu Nov  5 12:03:03  
> 2009
> @@ -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);
> +
>
>
> _______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits




More information about the cfe-commits mailing list