]> granicus.if.org Git - clang/commitdiff
Don't try to fold comparisons between the address of an object and an arbitrary integ...
authorEli Friedman <eli.friedman@gmail.com>
Mon, 31 Oct 2011 22:28:05 +0000 (22:28 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Mon, 31 Oct 2011 22:28:05 +0000 (22:28 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@143374 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/ExprConstant.cpp
test/SemaCXX/constant-expression-cxx11.cpp

index b34b59d90798c578d6136e3b9a1b3e19ba5d5078..5a65038a5f435916d4f144d6fadb1fb4cfb4ebc2 100644 (file)
@@ -1981,6 +1981,12 @@ bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
         // unspecified or undefined behavior.
         if (!E->isEqualityOp())
           return false;
+        // A constant address may compare equal to the address of a symbol.
+        // The one exception is that address of an object cannot compare equal
+        // to the null pointer.
+        if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
+            (!RHSValue.Base && !RHSValue.Offset.isZero()))
+          return false;
         // It's implementation-defined whether distinct literals will have
         // distinct addresses. We define it to be unspecified.
         if (IsLiteralLValue(LHSValue) || IsLiteralLValue(RHSValue))
index 8c845a7bd98c5852a37f9bd89abef3e5dbd7716d..f706e150b4a529c46f93cc8285034ce2fc4ea0d5 100644 (file)
@@ -200,6 +200,9 @@ constexpr bool s6 = &x > &x;
 constexpr S* sptr = &s;
 constexpr bool dyncast = sptr == dynamic_cast<S*>(sptr);
 
+extern char externalvar[];
+constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL;  // expected-error {{must be initialized by a constant expression}}
+
 using check = int[m1 + (m2<<1) + (m3<<2) + (m4<<3) + (m5<<4) + (m6<<5) +
                   (n1<<6) + (n2<<7) + (n7<<8) + (n8<<9) + (g1<<10) + (g2<<11) +
                (s1<<12) + (s2<<13) + (s3<<14) + (s4<<15) + (s5<<16) + (s6<<17)];