[cfe-commits] r57159 - in /cfe/trunk: include/clang/AST/Expr.h lib/AST/Expr.cpp lib/AST/ExprConstant.cpp lib/Sema/SemaDecl.cpp

Chris Lattner sabre at nondot.org
Sun Oct 5 23:49:02 PDT 2008


Author: lattner
Date: Mon Oct  6 01:49:02 2008
New Revision: 57159

URL: http://llvm.org/viewvc/llvm-project?rev=57159&view=rev
Log:
Add a Expr::isEvaluatable method, eliminate isBuiltinConstantExpr
which is checking for something that can be inconsistent with
what we can constant fold.


Modified:
    cfe/trunk/include/clang/AST/Expr.h
    cfe/trunk/lib/AST/Expr.cpp
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp

Modified: cfe/trunk/include/clang/AST/Expr.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/AST/Expr.h?rev=57159&r1=57158&r2=57159&view=diff

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Mon Oct  6 01:49:02 2008
@@ -124,6 +124,10 @@
   /// 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 @@
   /// 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 { 

Modified: cfe/trunk/lib/AST/Expr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Expr.cpp?rev=57159&r1=57158&r2=57159&view=diff

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Mon Oct  6 01:49:02 2008
@@ -162,12 +162,6 @@
 }
 
 
-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 @@
     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;
   }

Modified: cfe/trunk/lib/AST/ExprConstant.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ExprConstant.cpp?rev=57159&r1=57158&r2=57159&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Mon Oct  6 01:49:02 2008
@@ -693,3 +693,10 @@
       
   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);
+}

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=57159&r1=57158&r2=57159&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Oct  6 01:49:02 2008
@@ -879,14 +879,6 @@
   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 @@
     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 @@
     // 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());





More information about the cfe-commits mailing list