diff options
author | Anders Carlsson <andersca@mac.com> | 2008-11-22 21:04:56 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2008-11-22 21:04:56 +0000 |
commit | 59689ed7644e816eee05330426ee041183501935 (patch) | |
tree | ba4072257fdc4c0b38c0f87af378bea04c122b24 /clang/lib/AST/ExprConstant.cpp | |
parent | 2fedf0e8f722e64cf8b0c878c9346dc5a998919b (diff) | |
download | bcm5719-llvm-59689ed7644e816eee05330426ee041183501935.tar.gz bcm5719-llvm-59689ed7644e816eee05330426ee041183501935.zip |
Use Expr::Evaluate for case statements. Fixes PR2525
llvm-svn: 59881
Diffstat (limited to 'clang/lib/AST/ExprConstant.cpp')
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 36 |
1 files changed, 27 insertions, 9 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 6b71d110043..35cec0f3442 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -537,19 +537,28 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) { // These need to be handled specially because the operands aren't // necessarily integral bool bres; - if (!HandleConversionToBool(E->getLHS(), bres, Info)) { + + if (HandleConversionToBool(E->getLHS(), bres, Info)) { + // We were able to evaluate the LHS, see if we can get away with not + // evaluating the RHS: 0 && X -> 0, 1 || X -> 1 + } else { // We can't evaluate the LHS; however, sometimes the result // is determined by the RHS: X && 0 -> 0, X || 1 -> 1. - if (HandleConversionToBool(E->getRHS(), bres, Info) && - bres == (E->getOpcode() == BinaryOperator::LOr)) { - Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); - Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); - Result = bres; - return true; + if (!HandleConversionToBool(E->getRHS(), bres, Info)) { + // We can't evaluate. + return false; } + } - // Really can't evaluate - return false; + // FIXME: If we evaluate the RHS, we need to check if the LHS has + // any side effects. + + if (bres == (E->getOpcode() == BinaryOperator::LOr) || + !bres == (E->getOpcode() == BinaryOperator::LAnd)) { + Result.zextOrTrunc(getIntTypeSizeInBits(E->getType())); + Result.setIsUnsigned(E->getType()->isUnsignedIntegerType()); + Result = bres; + return true; } bool bres2; @@ -1176,3 +1185,12 @@ bool Expr::isEvaluatable(ASTContext &Ctx) const { APValue V; return Evaluate(V, Ctx); } + +APSInt Expr::EvaluateAsInt(ASTContext &Ctx) const { + APValue V; + bool Result = Evaluate(V, Ctx); + assert(Result && "Could not evaluate expression"); + assert(V.isInt() && "Expression did not evaluate to integer"); + + return V.getInt(); +} |