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.
10 // { dg-do compile { target c++11 } }
11 // { dg-additional-options "-Wall -Wextra -fdelete-null-pointer-checks" }
17 constexpr int *p0 = &i;
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-warning "ordered comparison" }
23 constexpr bool b4 = p0 <= 0; // { dg-warning "ordered comparison" }
24 constexpr bool b5 = p0 > 0; // { dg-warning "ordered comparison" }
25 constexpr bool b6 = p0 >= 0; // { dg-warning "ordered comparison" }
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-warning "ordered comparison" }
31 constexpr bool b11 = 0 <= p0; // { dg-warning "ordered comparison" }
32 constexpr bool b12 = 0 > p0; // { dg-warning "ordered comparison" }
33 constexpr bool b13 = 0 >= p0; // { dg-warning "ordered comparison" }
39 // PR c++/70172 - incorrect reinterpret_cast from integer to pointer
40 // error on invalid constexpr initialization
42 struct S { int a, b[1]; } s;
46 constexpr int *q0 = p0->b; // { dg-bogus "reinterpret_cast from integer to pointer" }
50 namespace WeakRefTest1 {
52 extern __attribute__ ((weak)) int i;
54 constexpr int *p0 = &i;
56 #pragma GCC diagnostic push
57 #pragma GCC diagnostic ignored "-Wextra"
58 // Suppress warning: ordered comparison of pointer with integer zero
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 "not a constant expression" }
64 constexpr bool b5 = p0 > 0; // { dg-error "not a constant expression" }
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 "not a constant expression" }
70 constexpr bool b13 = 0 >= p0; // { dg-error "not a constant expression" }
72 // The following are accepted as constant expressions due to bug c++/70196.
73 constexpr bool b3 = p0 < 0;
74 constexpr bool b6 = p0 >= 0;
75 constexpr bool b11 = 0 <= p0;
76 constexpr bool b12 = 0 > p0;
78 #pragma GCC diagnostic pop
82 namespace WeakRefTest2 {
84 extern __attribute__ ((weak)) int i;
86 constexpr int *p1 = &i + 1;
88 #pragma GCC diagnostic push
89 #pragma GCC diagnostic ignored "-Wextra"
90 // Suppress warning: ordered comparison of pointer with integer zero
92 constexpr bool b0 = p1; // { dg-error "not a constant expression" }
93 constexpr bool b1 = p1 == 0; // { dg-error "not a constant expression" }
94 constexpr bool b2 = p1 != 0; // { dg-error "not a constant expression" }
95 constexpr bool b4 = p1 <= 0; // { dg-error "not a constant expression" }
96 constexpr bool b5 = p1 > 0; // { dg-error "not a constant expression" }
98 constexpr bool b7 = !p1; // { dg-error "not a constant expression" }
99 constexpr bool b8 = 0 == p1; // { dg-error "not a constant expression" }
100 constexpr bool b9 = 0 != p1; // { dg-error "not a constant expression" }
101 constexpr bool b10 = 0 < p1; // { dg-error "not a constant expression" }
102 constexpr bool b13 = 0 >= p1; // { dg-error "not a constant expression" }
104 // The following are accepted as constant expressions due to bug c++/70196.
105 // constexpr bool b3 = p1 < 0;
106 // constexpr bool b6 = p1 >= 0;
107 // constexpr bool b11 = 0 <= p1;
108 // constexpr bool b12 = 0 > p1;
110 #pragma GCC diagnostic pop