| 1 |
// PR c++/67376 - [5/6 regression] Comparison with pointer to past-the-end |
| 2 |
// of array fails inside constant expression |
| 3 |
// This test verifies the aspect of the bug raised in comment #10, |
| 4 |
// specifically comparing pointers to null. The basic regression test |
| 5 |
// is in g++.dg/cpp0x/constexpr-67376.C. |
| 6 |
// Note also that while the description of the bug talks about pointers |
| 7 |
// pointing past the end of arrays but the prolem is more general than |
| 8 |
// that and involves all constexpr object pointers. |
| 9 |
|
| 10 |
// { dg-do compile { target c++11 } } |
| 11 |
// { dg-additional-options "-Wall -Wextra -fdelete-null-pointer-checks" } |
| 12 |
|
| 13 |
namespace A { |
| 14 |
|
| 15 |
extern int i; |
| 16 |
|
| 17 |
constexpr int *p0 = &i; |
| 18 |
|
| 19 |
constexpr bool b0 = p0; // { dg-warning "address of .A::i." } |
| 20 |
constexpr bool b1 = p0 == 0; // { dg-warning "address of .A::i." } |
| 21 |
constexpr bool b2 = p0 != 0; // { dg-warning "address of .A::i." } |
| 22 |
constexpr bool b3 = p0 < 0; // { dg-error "25:ordered comparison" } |
| 23 |
constexpr bool b4 = p0 <= 0; // { dg-error "25:ordered comparison" } |
| 24 |
constexpr bool b5 = p0 > 0; // { dg-error "25:ordered comparison" } |
| 25 |
constexpr bool b6 = p0 >= 0; // { dg-error "25:ordered comparison" } |
| 26 |
|
| 27 |
constexpr bool b7 = !p0; // { dg-warning "address of .A::i." } |
| 28 |
constexpr bool b8 = 0 == p0; // { dg-warning "address of .A::i." } |
| 29 |
constexpr bool b9 = 0 != p0; // { dg-warning "address of .A::i." } |
| 30 |
constexpr bool b10 = 0 < p0; // { dg-error "24:ordered comparison" } |
| 31 |
constexpr bool b11 = 0 <= p0; // { dg-error "24:ordered comparison" } |
| 32 |
constexpr bool b12 = 0 > p0; // { dg-error "24:ordered comparison" } |
| 33 |
constexpr bool b13 = 0 >= p0; // { dg-error "24:ordered comparison" } |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
namespace B { |
| 38 |
|
| 39 |
// PR c++/70172 - incorrect reinterpret_cast from integer to pointer |
| 40 |
// error on invalid constexpr initialization |
| 41 |
|
| 42 |
struct S { int a, b[1]; } s; |
| 43 |
|
| 44 |
constexpr S *p0 = &s; |
| 45 |
|
| 46 |
constexpr int *q0 = p0->b; // { dg-bogus "reinterpret_cast from integer to pointer" } |
| 47 |
|
| 48 |
} |
| 49 |
|
| 50 |
namespace WeakRefTest1 { |
| 51 |
|
| 52 |
extern __attribute__ ((weak)) int i; |
| 53 |
|
| 54 |
constexpr int *p0 = &i; |
| 55 |
|
| 56 |
#pragma GCC diagnostic push |
| 57 |
#pragma GCC diagnostic ignored "-Wextra" |
| 58 |
// Suppress warning: ordered comparison of pointer with integer zero |
| 59 |
|
| 60 |
constexpr bool b0 = p0; // { dg-error "not a constant expression" } |
| 61 |
constexpr bool b1 = p0 == 0; // { dg-error "not a constant expression" } |
| 62 |
constexpr bool b2 = p0 != 0; // { dg-error "not a constant expression" } |
| 63 |
constexpr bool b4 = p0 <= 0; // { dg-error "ordered comparison" } |
| 64 |
constexpr bool b5 = p0 > 0; // { dg-error "ordered comparison" } |
| 65 |
|
| 66 |
constexpr bool b7 = !p0; // { dg-error "not a constant expression" } |
| 67 |
constexpr bool b8 = 0 == p0; // { dg-error "not a constant expression" } |
| 68 |
constexpr bool b9 = 0 != p0; // { dg-error "not a constant expression" } |
| 69 |
constexpr bool b10 = 0 < p0; // { dg-error "ordered comparison" } |
| 70 |
constexpr bool b13 = 0 >= p0; // { dg-error "ordered comparison" } |
| 71 |
|
| 72 |
constexpr bool b3 = p0 < 0; // { dg-error "ordered comparison" } |
| 73 |
constexpr bool b6 = p0 >= 0; // { dg-error "ordered comparison" } |
| 74 |
constexpr bool b11 = 0 <= p0; // { dg-error "ordered comparison" } |
| 75 |
constexpr bool b12 = 0 > p0; // { dg-error "ordered comparison" } |
| 76 |
|
| 77 |
#pragma GCC diagnostic pop |
| 78 |
|
| 79 |
} |
| 80 |
|
| 81 |
namespace WeakRefTest2 { |
| 82 |
|
| 83 |
extern __attribute__ ((weak)) int i; |
| 84 |
|
| 85 |
constexpr int *p1 = &i + 1; |
| 86 |
|
| 87 |
#pragma GCC diagnostic push |
| 88 |
// Suppress warning: ordered comparison of pointer with integer zero. |
| 89 |
#pragma GCC diagnostic ignored "-Wextra" |
| 90 |
// Also suppress -Waddress for comparisons of constant addresses to |
| 91 |
// to null. |
| 92 |
#pragma GCC diagnostic ignored "-Waddress" |
| 93 |
|
| 94 |
constexpr bool b0 = p1; // { dg-error "not a constant expression" } |
| 95 |
constexpr bool b1 = p1 == 0; // { dg-error "not a constant expression" } |
| 96 |
constexpr bool b2 = p1 != 0; // { dg-error "not a constant expression" } |
| 97 |
constexpr bool b4 = p1 <= 0; // { dg-error "ordered comparison" } |
| 98 |
constexpr bool b5 = p1 > 0; // { dg-error "ordered comparison" } |
| 99 |
|
| 100 |
constexpr bool b7 = !p1; // { dg-error "not a constant expression" } |
| 101 |
constexpr bool b8 = 0 == p1; // { dg-error "not a constant expression" } |
| 102 |
constexpr bool b9 = 0 != p1; // { dg-error "not a constant expression" } |
| 103 |
constexpr bool b10 = 0 < p1; // { dg-error "ordered comparison" } |
| 104 |
constexpr bool b13 = 0 >= p1; // { dg-error "ordered comparison" } |
| 105 |
|
| 106 |
// The following are accepted as constant expressions due to bug c++/70196. |
| 107 |
// constexpr bool b3 = p1 < 0; |
| 108 |
// constexpr bool b6 = p1 >= 0; |
| 109 |
// constexpr bool b11 = 0 <= p1; |
| 110 |
// constexpr bool b12 = 0 > p1; |
| 111 |
|
| 112 |
#pragma GCC diagnostic pop |
| 113 |
|
| 114 |
} |