[cfe-commits] r60318 - in /cfe/trunk: include/clang/AST/Expr.h lib/AST/Expr.cpp

Anders Carlsson andersca at mac.com
Sun Nov 30 18:13:57 PST 2008


Author: andersca
Date: Sun Nov 30 20:13:57 2008
New Revision: 60318

URL: http://llvm.org/viewvc/llvm-project?rev=60318&view=rev
Log:
Add a new variant of isNullConstantExpr that returns an EvalResult.

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

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

==============================================================================
--- cfe/trunk/include/clang/AST/Expr.h (original)
+++ cfe/trunk/include/clang/AST/Expr.h Sun Nov 30 20:13:57 2008
@@ -110,7 +110,6 @@
   };
   isModifiableLvalueResult isModifiableLvalue(ASTContext &Ctx) const;
   
-  bool isNullPointerConstant(ASTContext &Ctx) const;
   bool isBitField();
 
   /// getIntegerConstantExprValue() - Return the value of an integer
@@ -179,6 +178,12 @@
   /// must be called on an expression that constant folds to an integer.
   llvm::APSInt EvaluateAsInt(ASTContext &Ctx) const;
 
+  /// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
+  /// integer constant expression with the value zero, or if this is one that is
+  /// cast to void*.
+  bool isNullPointerConstant(EvalResult &Result, ASTContext &Ctx) const;
+  bool isNullPointerConstant(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
   /// constant.

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

==============================================================================
--- cfe/trunk/lib/AST/Expr.cpp (original)
+++ cfe/trunk/lib/AST/Expr.cpp Sun Nov 30 20:13:57 2008
@@ -1007,7 +1007,14 @@
 /// isNullPointerConstant - C99 6.3.2.3p3 -  Return true if this is either an
 /// integer constant expression with the value zero, or if this is one that is
 /// cast to void*.
-bool Expr::isNullPointerConstant(ASTContext &Ctx) const {
+bool Expr::isNullPointerConstant(ASTContext &Ctx) const
+{
+  EvalResult EvalResult;
+  
+  return isNullPointerConstant(EvalResult, Ctx);
+}
+
+bool Expr::isNullPointerConstant(EvalResult &Result, ASTContext &Ctx) const {
   // Strip off a cast to void*, if it exists. Except in C++.
   if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) {
     if (!Ctx.getLangOptions().CPlusPlus) {
@@ -1017,20 +1024,20 @@
         if (Pointee.getCVRQualifiers() == 0 && 
             Pointee->isVoidType() &&                              // to void*
             CE->getSubExpr()->getType()->isIntegerType())         // from int.
-          return CE->getSubExpr()->isNullPointerConstant(Ctx);
+          return CE->getSubExpr()->isNullPointerConstant(Result, Ctx);
       }
     }
   } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) {
     // Ignore the ImplicitCastExpr type entirely.
-    return ICE->getSubExpr()->isNullPointerConstant(Ctx);
+    return ICE->getSubExpr()->isNullPointerConstant(Result, Ctx);
   } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) {
     // Accept ((void*)0) as a null pointer constant, as many other
     // implementations do.
-    return PE->getSubExpr()->isNullPointerConstant(Ctx);
+    return PE->getSubExpr()->isNullPointerConstant(Result, Ctx);
   } else if (const CXXDefaultArgExpr *DefaultArg 
                = dyn_cast<CXXDefaultArgExpr>(this)) {
     // See through default argument expressions
-    return DefaultArg->getExpr()->isNullPointerConstant(Ctx);
+    return DefaultArg->getExpr()->isNullPointerConstant(Result, Ctx);
   } else if (isa<GNUNullExpr>(this)) {
     // The GNU __null extension is always a null pointer constant.
     return true;
@@ -1042,6 +1049,9 @@
   
   // If we have an integer constant expression, we need to *evaluate* it and
   // test for the value 0.
+  return Evaluate(Result, Ctx) && !Result.HasSideEffects &&
+      Result.Val.isInt() && Result.Val.getInt() == 0;
+
   llvm::APSInt Val(32);
   return isIntegerConstantExpr(Val, Ctx, 0, true) && Val == 0;
 }





More information about the cfe-commits mailing list