diff options
author | Chris Lattner <sabre@nondot.org> | 2008-10-06 06:49:02 +0000 |
---|---|---|
committer | Chris Lattner <sabre@nondot.org> | 2008-10-06 06:49:02 +0000 |
commit | cb13691a6292023d9f97381ea5fbfca0e6b13855 (patch) | |
tree | 6212d0a4ab826d7ba48bec07af85f2bc5c5bc1ff | |
parent | 86ee286b09d21ed845e62f1b8fb8119122bcdbaf (diff) | |
download | bcm5719-llvm-cb13691a6292023d9f97381ea5fbfca0e6b13855.tar.gz bcm5719-llvm-cb13691a6292023d9f97381ea5fbfca0e6b13855.zip |
Add a Expr::isEvaluatable method, eliminate isBuiltinConstantExpr
which is checking for something that can be inconsistent with
what we can constant fold.
llvm-svn: 57159
-rw-r--r-- | clang/include/clang/AST/Expr.h | 8 | ||||
-rw-r--r-- | clang/lib/AST/Expr.cpp | 11 | ||||
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 7 | ||||
-rw-r--r-- | clang/lib/Sema/SemaDecl.cpp | 15 |
4 files changed, 20 insertions, 21 deletions
diff --git a/clang/include/clang/AST/Expr.h b/clang/include/clang/AST/Expr.h index df7a83bcbfb..2b42424803d 100644 --- a/clang/include/clang/AST/Expr.h +++ b/clang/include/clang/AST/Expr.h @@ -124,6 +124,10 @@ public: /// we want to. If this function returns true, it returns the folded constant /// in Result. bool tryEvaluate(APValue& Result, ASTContext &Ctx) const; + + /// isEvaluatable - Call tryEvaluate to see if this expression can be constant + /// folded, but discard the result. + bool isEvaluatable(ASTContext &Ctx) const; /// hasGlobalStorage - Return true if this expression has static storage /// duration. This means that the address of this expression is a link-time @@ -709,10 +713,6 @@ public: /// not, return 0. unsigned isBuiltinCall() const; - - /// isBuiltinConstantExpr - Return true if this built-in call is constant. - bool isBuiltinConstantExpr(ASTContext &Ctx) const; - SourceLocation getRParenLoc() const { return RParenLoc; } virtual SourceRange getSourceRange() const { diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp index 45363d69925..6cdaacd6897 100644 --- a/clang/lib/AST/Expr.cpp +++ b/clang/lib/AST/Expr.cpp @@ -162,12 +162,6 @@ unsigned CallExpr::isBuiltinCall() const { } -bool CallExpr::isBuiltinConstantExpr(ASTContext &Ctx) const { - unsigned BID = isBuiltinCall(); - if (!BID) return false; - return Ctx.BuiltinInfo.isConstantExpr(BID); -} - /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it /// corresponds to, e.g. "<<=". const char *BinaryOperator::getOpcodeStr(Opcode Op) { @@ -519,8 +513,11 @@ bool Expr::isConstantExpr(ASTContext &Ctx, SourceLocation *Loc) const { return true; case CallExprClass: { const CallExpr *CE = cast<CallExpr>(this); - if (CE->isBuiltinConstantExpr(Ctx)) + + // Allow any constant foldable calls to builtins. + if (CE->isBuiltinCall() && CE->isEvaluatable(Ctx)) return true; + if (Loc) *Loc = getLocStart(); return false; } diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 1506f448aa0..af30ceb96c3 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -693,3 +693,10 @@ bool Expr::tryEvaluate(APValue &Result, ASTContext &Ctx) const { return false; } + +/// isEvaluatable - Call tryEvaluate to see if this expression can be constant +/// folded, but discard the result. +bool Expr::isEvaluatable(ASTContext &Ctx) const { + APValue V; + return tryEvaluate(V, Ctx); +} diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 74ce855add5..125d91cdbaf 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -879,14 +879,6 @@ bool Sema::CheckAddressConstantExpression(const Expr* Init) { case Expr::StringLiteralClass: case Expr::ObjCStringLiteralClass: return false; - case Expr::CallExprClass: { - const CallExpr *CE = cast<CallExpr>(Init); - if (CE->isBuiltinConstantExpr(Context)) - return false; - Diag(Init->getExprLoc(), - diag::err_init_element_not_constant, Init->getSourceRange()); - return true; - } case Expr::UnaryOperatorClass: { const UnaryOperator *Exp = cast<UnaryOperator>(Init); @@ -1080,8 +1072,11 @@ bool Sema::CheckArithmeticConstantExpression(const Expr* Init) { return false; case Expr::CallExprClass: { const CallExpr *CE = cast<CallExpr>(Init); - if (CE->isBuiltinConstantExpr(Context)) + + // Allow any constant foldable calls to builtins. + if (CE->isBuiltinCall() && CE->isEvaluatable(Context)) return false; + Diag(Init->getExprLoc(), diag::err_init_element_not_constant, Init->getSourceRange()); return true; @@ -1226,7 +1221,7 @@ bool Sema::CheckArithmeticConstantExpression(const Expr* Init) { // Okay, the evaluated side evaluates to a constant, so we accept this. // Check to see if the other side is obviously not a constant. If so, // emit a warning that this is a GNU extension. - if (FalseSide && !FalseSide->tryEvaluate(Val, Context)) + if (FalseSide && !FalseSide->isEvaluatable(Context)) Diag(Init->getExprLoc(), diag::ext_typecheck_expression_not_constant_but_accepted, FalseSide->getSourceRange()); |