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

John McCall rjmccall at apple.com
Tue Jan 5 15:42:57 PST 2010


Author: rjmccall
Date: Tue Jan  5 17:42:56 2010
New Revision: 92805

URL: http://llvm.org/viewvc/llvm-project?rev=92805&view=rev
Log:
Add Expr::EvaluateAsBooleanCondition(), which does unprincipled folding to
try to evaluate an expression as a constant boolean condition.  This has
the same intended semantics as used in folding conditional operators.


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=92805&r1=92804&r2=92805&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Tue Jan  5 17:42:56 2010
@@ -241,6 +241,11 @@
   /// stack based objects.
   bool EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const;
 
+  /// EvaluateAsBooleanCondition - Return true if this is a constant
+  /// which we we can fold and convert to a boolean condition using
+  /// any crazy technique that we want to.
+  bool EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const;
+
   /// isEvaluatable - Call Evaluate to see if this expression can be constant
   /// folded, but discard the result.
   bool isEvaluatable(ASTContext &Ctx) const;

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

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Tue Jan  5 17:42:56 2010
@@ -73,7 +73,8 @@
   return true;
 }
 
-static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
+static bool HandleConversionToBool(const Expr* E, bool& Result,
+                                   EvalInfo &Info) {
   if (E->getType()->isIntegralType()) {
     APSInt IntResult;
     if (!EvaluateInteger(E, IntResult, Info))
@@ -1978,6 +1979,13 @@
   return true;
 }
 
+bool Expr::EvaluateAsBooleanCondition(bool &Result, ASTContext &Ctx) const {
+  EvalResult Scratch;
+  EvalInfo Info(Ctx, Scratch);
+
+  return HandleConversionToBool(this, Result, Info);
+}
+
 bool Expr::EvaluateAsLValue(EvalResult &Result, ASTContext &Ctx) const {
   EvalInfo Info(Ctx, Result);
 





More information about the cfe-commits mailing list