]> granicus.if.org Git - clang/commitdiff
Add Expr::EvaluateAsBooleanCondition(), which does unprincipled folding to
authorJohn McCall <rjmccall@apple.com>
Tue, 5 Jan 2010 23:42:56 +0000 (23:42 +0000)
committerJohn McCall <rjmccall@apple.com>
Tue, 5 Jan 2010 23:42:56 +0000 (23:42 +0000)
try to evaluate an expression as a constant boolean condition.  This has
the same intended semantics as used in folding conditional operators.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92805 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/Expr.h
lib/AST/ExprConstant.cpp

index 0cb22df92ff75d1f74a5cc0cd901cfe03eaea65c..98ecda9b83582e6abe3d1d2c827af8253185d861 100644 (file)
@@ -241,6 +241,11 @@ public:
   /// 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;
index d49a8216f3ac7914af1ef7a1663031d49ecbb680..cc86b243424f89692ff73aae0e82c898d6e7893d 100644 (file)
@@ -73,7 +73,8 @@ static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
   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 @@ bool Expr::EvaluateAsAny(EvalResult &Result, ASTContext &Ctx) const {
   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);