[cfe-commits] r81672 - in /cfe/trunk: include/clang/AST/Expr.h lib/AST/ExprConstant.cpp

Eli Friedman eli.friedman at gmail.com
Sun Sep 13 03:17:45 PDT 2009


Author: efriedma
Date: Sun Sep 13 05:17:44 2009
New Revision: 81672

URL: http://llvm.org/viewvc/llvm-project?rev=81672&view=rev
Log:
Add utility to evaluate lvalues which are an offset relative to a stack 
location.  Patch by Enea Zaffanella.


Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/lib/AST/ExprConstant.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=81672&r1=81671&r2=81672&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Sun Sep 13 05:17:44 2009
@@ -248,9 +248,14 @@
   /// must be called on an expression that constant folds to an integer.
   llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const;
 
-  /// EvaluateAsLValue - Evaluate an expression to see if it's a valid LValue.
+  /// EvaluateAsLValue - Evaluate an expression to see if it's a lvalue
+  /// with link time known address.
   bool EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const;
 
+  /// EvaluateAsAnyLValue - The same as EvaluateAsLValue, except that it
+  /// also succeeds on stack based, immutable address lvalues.
+  bool EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const;
+
   /// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
   /// integer constant expression with the value zero, or if this is one that is
   /// cast to void*.

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=81672&r1=81671&r2=81672&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Sun Sep 13 05:17:44 2009
@@ -46,8 +46,12 @@
   /// EvalResult - Contains information about the evaluation.
   Expr::EvalResult &EvalResult;
 
-  EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult) : Ctx(ctx),
-           EvalResult(evalresult) {}
+  /// AnyLValue - Stack based LValue results are not discarded.
+  bool AnyLValue;
+
+  EvalInfo(ASTContext &ctx, Expr::EvalResult& evalresult,
+           bool anylvalue = false)
+    : Ctx(ctx), EvalResult(evalresult), AnyLValue(anylvalue) {}
 };
 
 
@@ -189,7 +193,7 @@
   if (isa<FunctionDecl>(E->getDecl())) {
     return APValue(E, 0);
   } else if (VarDecl* VD = dyn_cast<VarDecl>(E->getDecl())) {
-    if (!VD->hasGlobalStorage())
+    if (!Info.AnyLValue && !VD->hasGlobalStorage())
       return APValue();
     if (!VD->getType()->isReferenceType())
       return APValue(E, 0);
@@ -209,9 +213,9 @@
 }
 
 APValue LValueExprEvaluator::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) {
-  if (E->isFileScope())
-    return APValue(E, 0);
-  return APValue();
+  if (!Info.AnyLValue && !E->isFileScope())
+    return APValue();
+  return APValue(E, 0);
 }
 
 APValue LValueExprEvaluator::VisitMemberExpr(MemberExpr *E) {
@@ -1786,6 +1790,12 @@
   return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
 }
 
+bool Expr::EvaluateAsAnyLValue(EvalResult &Result, ASTContext &Ctx) const {
+  EvalInfo Info(Ctx, Result, true);
+
+  return EvaluateLValue(this, Result.Val, Info) && !Result.HasSideEffects;
+}
+
 /// isEvaluatable - Call Evaluate to see if this expression can be constant
 /// folded, but discard the result.
 bool Expr::isEvaluatable(ASTContext &Ctx) const {





More information about the cfe-commits mailing list