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 #pragma GCC diagnostic ignored "-Wextra"
89 // Suppress warning: ordered comparison of pointer with integer zero
90 
91 constexpr bool b0  = p1;        // { dg-error "not a constant expression" }
92 constexpr bool b1  = p1 == 0;   // { dg-error "not a constant expression" }
93 constexpr bool b2  = p1 != 0;   // { dg-error "not a constant expression" }
94 constexpr bool b4  = p1 <= 0;   // { dg-error "ordered comparison" }
95 constexpr bool b5  = p1 >  0;   // { dg-error "ordered comparison" }
96 
97 constexpr bool b7  = !p1;       // { dg-error "not a constant expression" }
98 constexpr bool b8  = 0 == p1;   // { dg-error "not a constant expression" }
99 constexpr bool b9  = 0 != p1;   // { dg-error "not a constant expression" }
100 constexpr bool b10 = 0 <  p1;   // { dg-error "ordered comparison" }
101 constexpr bool b13 = 0 >= p1;   // { dg-error "ordered comparison" }
102 
103 // The following are accepted as constant expressions due to bug c++/70196.
104 // constexpr bool b3  = p1 <  0;
105 // constexpr bool b6  = p1 >= 0;
106 // constexpr bool b11 = 0 <= p1;
107 // constexpr bool b12 = 0 >  p1;
108 
109 #pragma GCC diagnostic pop
110 
111 }
112