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

Eli Friedman eli.friedman at gmail.com
Tue Apr 28 12:17:36 PDT 2009


Author: efriedma
Date: Tue Apr 28 14:17:36 2009
New Revision: 70317

URL: http://llvm.org/viewvc/llvm-project?rev=70317&view=rev
Log:
PR4097: add logic to Evaluate to handle pointer equality comparisons.


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=70317&r1=70316&r2=70317&view=diff

==============================================================================
--- cfe/trunk/lib/AST/ExprConstant.cpp (original)
+++ cfe/trunk/lib/AST/ExprConstant.cpp Tue Apr 28 14:17:36 2009
@@ -900,8 +900,8 @@
     }
   }
   
-  if (E->getOpcode() == BinaryOperator::Sub) {
-    if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
+  if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
+    if (E->getOpcode() == BinaryOperator::Sub || E->isEqualityOp()) {
       APValue LHSValue;
       if (!EvaluatePointer(E->getLHS(), LHSValue, Info))
         return false;
@@ -915,13 +915,22 @@
       if (LHSValue.getLValueBase() || RHSValue.getLValueBase())
         return false;
 
-      const QualType Type = E->getLHS()->getType();
-      const QualType ElementType = Type->getAsPointerType()->getPointeeType();
+      if (E->getOpcode() == BinaryOperator::Sub) {
+        const QualType Type = E->getLHS()->getType();
+        const QualType ElementType = Type->getAsPointerType()->getPointeeType();
 
-      uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
-      D /= Info.Ctx.getTypeSize(ElementType) / 8;
+        uint64_t D = LHSValue.getLValueOffset() - RHSValue.getLValueOffset();
+        D /= Info.Ctx.getTypeSize(ElementType) / 8;
 
-      return Success(D, E);
+        return Success(D, E);
+      }
+      bool Result;
+      if (E->getOpcode() == BinaryOperator::EQ) {
+        Result = LHSValue.getLValueOffset() == RHSValue.getLValueOffset();
+      } else if (E->getOpcode() == BinaryOperator::NE) {
+        Result = LHSValue.getLValueOffset() != RHSValue.getLValueOffset();
+      }
+      return Success(Result, E);
     }
   }
   if (!LHSTy->isIntegralType() ||

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

==============================================================================
--- cfe/trunk/test/Sema/const-eval.c (original)
+++ cfe/trunk/test/Sema/const-eval.c Tue Apr 28 14:17:36 2009
@@ -63,3 +63,5 @@
 static const struct a V1 = (struct a){ 1, 2};
 
 EVAL_EXPR(30, (int)(_Complex float)((1<<30)-1) == (1<<30) ? 1 : -1)
+EVAL_EXPR(31, (int*)0 == (int*)0 ? 1 : -1)
+EVAL_EXPR(32, (int*)0 != (int*)0 ? -1 : 1)





More information about the cfe-commits mailing list