From cd7a445c6b46c5585580dfb652300c8483c0cb6b Mon Sep 17 00:00:00 2001 From: John McCall Date: Tue, 5 Jan 2010 23:42:56 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@92805 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/AST/Expr.h | 5 +++++ lib/AST/ExprConstant.cpp | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/include/clang/AST/Expr.h b/include/clang/AST/Expr.h index 0cb22df92f..98ecda9b83 100644 --- a/include/clang/AST/Expr.h +++ b/include/clang/AST/Expr.h @@ -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; diff --git a/lib/AST/ExprConstant.cpp b/lib/AST/ExprConstant.cpp index d49a8216f3..cc86b24342 100644 --- a/lib/AST/ExprConstant.cpp +++ b/lib/AST/ExprConstant.cpp @@ -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); -- 2.40.0