[cfe-commits] r73321 - in /cfe/trunk: lib/AST/ExprConstant.cpp test/Sema/const-eval.c

Eli Friedman eli.friedman at gmail.com
Sat Jun 13 19:17:33 PDT 2009


Author: efriedma
Date: Sat Jun 13 21:17:33 2009
New Revision: 73321

URL: http://llvm.org/viewvc/llvm-project?rev=73321&view=rev
Log:
PR4351: Add constant evaluation for constructs like "foo == NULL", where 
foo has a constant address.


Modified:
    cfe/trunk/lib/AST/ExprConstant.cpp
    cfe/trunk/test/Sema/const-eval.c

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

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Sat Jun 13 21:17:33 2009
@@ -62,6 +62,13 @@
 // Misc utilities
 //===----------------------------------------------------------------------===//
 
+static bool EvalPointerValueAsBool(APValue& Value, bool& Result) {
+  // FIXME: Is this accurate for all kinds of bases?  If not, what would
+  // the check look like?
+  Result = Value.getLValueBase() || Value.getLValueOffset();
+  return true;
+}
+
 static bool HandleConversionToBool(Expr* E, bool& Result, EvalInfo &Info) {
   if (E->getType()->isIntegralType()) {
     APSInt IntResult;
@@ -79,10 +86,7 @@
     APValue PointerResult;
     if (!EvaluatePointer(E, PointerResult, Info))
       return false;
-    // FIXME: Is this accurate for all kinds of bases?  If not, what would
-    // the check look like?
-    Result = PointerResult.getLValueBase() || PointerResult.getLValueOffset();
-    return true;
+    return EvalPointerValueAsBool(PointerResult, Result);
   } else if (E->getType()->isAnyComplexType()) {
     APValue ComplexResult;
     if (!EvaluateComplex(E, ComplexResult, Info))
@@ -937,10 +941,27 @@
       if (!EvaluatePointer(E->getRHS(), RHSValue, Info))
         return false;
 
-      // Reject any bases; this is conservative, but good enough for
-      // common uses
-      if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
-        return false;
+      // Reject any bases from the normal codepath; we special-case comparisons
+      // to null.
+      if (LHSValue.getLValueBase()) {
+        if (!E->isEqualityOp())
+          return false;
+        if (RHSValue.getLValueBase() || RHSValue.getLValueOffset())
+          return false;
+        bool bres;
+        if (!EvalPointerValueAsBool(LHSValue, bres))
+          return false;
+        return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
+      } else if (RHSValue.getLValueBase()) {
+        if (!E->isEqualityOp())
+          return false;
+        if (LHSValue.getLValueBase() || LHSValue.getLValueOffset())
+          return false;
+        bool bres;
+        if (!EvalPointerValueAsBool(RHSValue, bres))
+          return false;
+        return Success(bres ^ (E->getOpcode() == BinaryOperator::EQ), E);
+      }
 
       if (E->getOpcode() == BinaryOperator::Sub) {
         const QualType Type = E->getLHS()->getType();

Modified: cfe/trunk/test/Sema/const-eval.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/const-eval.c?rev=73321&r1=73320&r2=73321&view=diff

==============================================================================
--- cfe/trunk/test/Sema/const-eval.c (original)
+++ cfe/trunk/test/Sema/const-eval.c Sat Jun 13 21:17:33 2009
@@ -66,3 +66,5 @@
 EVAL_EXPR(31, (int*)0 == (int*)0 ? 1 : -1)
 EVAL_EXPR(32, (int*)0 != (int*)0 ? -1 : 1)
 EVAL_EXPR(33, (void*)0 - (void*)0 == 0 ? 1 : -1)
+void foo(void) {}
+EVAL_EXPR(34, (foo == (void *)0) ? -1 : 1)





More information about the cfe-commits mailing list