diff options
author | John McCall <rjmccall@apple.com> | 2010-01-05 23:42:56 +0000 |
---|---|---|
committer | John McCall <rjmccall@apple.com> | 2010-01-05 23:42:56 +0000 |
commit | cd7a445c6b46c5585580dfb652300c8483c0cb6b (patch) | |
tree | 1330a8434ff577b5dd5d839fa099e4dba5d6af02 | |
parent | 377adb6cffb3fdfcf659303d6f901adef4d61971 (diff) |
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
-rw-r--r-- | include/clang/AST/Expr.h | 5 | ||||
-rw-r--r-- | lib/AST/ExprConstant.cpp | 10 |
2 files changed, 14 insertions, 1 deletions
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); |