| 1 |
// PR sanitizer/63956 |
| 2 |
// { dg-do compile } |
| 3 |
// { dg-options "-std=c++14 -fsanitize=undefined,float-divide-by-zero" } |
| 4 |
|
| 5 |
#define SA(X) static_assert((X),#X) |
| 6 |
#define INT_MIN (-__INT_MAX__ - 1) |
| 7 |
|
| 8 |
constexpr int |
| 9 |
fn1 (int a, int b) |
| 10 |
{ |
| 11 |
if (b != 2) |
| 12 |
a <<= b; |
| 13 |
// { dg-error "5 << -2.. is negative" "" { target *-*-* } .-1 } |
| 14 |
// { dg-error "is greater than or equal to the precision .. of the left operand" "" { target *-*-* } .-2 } |
| 15 |
// { dg-error "-2 << 4.. is negative" "" { target *-*-* } .-3 } |
| 16 |
return a; |
| 17 |
} |
| 18 |
|
| 19 |
constexpr int i1 = fn1 (5, 3); |
| 20 |
constexpr int i2 = fn1 (5, -2); // { dg-message "in .constexpr. expansion" } |
| 21 |
constexpr int i3 = fn1 (5, sizeof (int) * __CHAR_BIT__); // { dg-message "in .constexpr. expansion" } |
| 22 |
constexpr int i4 = fn1 (5, 256); // { dg-message "in .constexpr. expansion" } |
| 23 |
constexpr int i5 = fn1 (5, 2); |
| 24 |
constexpr int i6 = fn1 (-2, 4); // { dg-message "in .constexpr. expansion" } |
| 25 |
constexpr int i7 = fn1 (0, 2); |
| 26 |
|
| 27 |
SA (i1 == 40); |
| 28 |
SA (i5 == 5); |
| 29 |
SA (i7 == 0); |
| 30 |
|
| 31 |
constexpr int |
| 32 |
fn2 (int a, int b) |
| 33 |
{ |
| 34 |
if (b != 2) |
| 35 |
a >>= b; |
| 36 |
// { dg-error "4 >> -1.. is negative" "" { target *-*-* } .-1 } |
| 37 |
// { dg-error "is greater than or equal to the precision .. of the left operand" "" { target *-*-* } .-2 } |
| 38 |
|
| 39 |
return a; |
| 40 |
} |
| 41 |
|
| 42 |
constexpr int j1 = fn2 (4, 1); |
| 43 |
constexpr int j2 = fn2 (4, -1); // { dg-message "in .constexpr. expansion" } |
| 44 |
constexpr int j3 = fn2 (10, sizeof (int) * __CHAR_BIT__); // { dg-message "in .constexpr. expansion" } |
| 45 |
constexpr int j4 = fn2 (1, 256); // { dg-message "in .constexpr. expansion" } |
| 46 |
constexpr int j5 = fn2 (5, 2); |
| 47 |
constexpr int j6 = fn2 (-2, 4); |
| 48 |
constexpr int j7 = fn2 (0, 4); |
| 49 |
|
| 50 |
SA (j1 == 2); |
| 51 |
SA (j5 == 5); |
| 52 |
SA (j7 == 0); |
| 53 |
|
| 54 |
constexpr int |
| 55 |
fn3 (int a, int b) |
| 56 |
{ |
| 57 |
if (b != 2) |
| 58 |
a = a / b; // { dg-error "..7 / 0.. is not a constant expression" } |
| 59 |
// { dg-error "overflow in constant expression" "" { target *-*-* } .-1 } |
| 60 |
return a; |
| 61 |
} |
| 62 |
|
| 63 |
constexpr int k1 = fn3 (8, 4); |
| 64 |
constexpr int k2 = fn3 (7, 0); // { dg-message "in .constexpr. expansion" } |
| 65 |
constexpr int k3 = fn3 (INT_MIN, -1); // { dg-message "in .constexpr. expansion of " } |
| 66 |
|
| 67 |
SA (k1 == 2); |
| 68 |
|
| 69 |
constexpr float |
| 70 |
fn4 (float a, float b) |
| 71 |
{ |
| 72 |
if (b != 2.0) |
| 73 |
a = a / b; // { dg-error "is not a constant expression" } |
| 74 |
return a; |
| 75 |
} |
| 76 |
|
| 77 |
constexpr float l1 = fn4 (5.0, 3.0); |
| 78 |
constexpr float l2 = fn4 (7.0, 0.0); // { dg-message "in .constexpr. expansion" } |
| 79 |
|
| 80 |
constexpr int |
| 81 |
fn5 (const int *a, int b) |
| 82 |
{ |
| 83 |
if (b != 2) |
| 84 |
b = a[b]; // { dg-error "array subscript" } |
| 85 |
return b; |
| 86 |
} |
| 87 |
|
| 88 |
constexpr int m1[4] = { 1, 2, 3, 4 }; |
| 89 |
constexpr int m2 = fn5 (m1, 3); |
| 90 |
constexpr int m3 = fn5 (m1, 4); // { dg-message "in .constexpr. expansion of " } |
| 91 |
|
| 92 |
constexpr int |
| 93 |
fn6 (const int &a, int b) |
| 94 |
{ |
| 95 |
if (b != 2) |
| 96 |
b = a; |
| 97 |
return b; |
| 98 |
} |
| 99 |
|
| 100 |
constexpr int |
| 101 |
fn7 (const int *a, int b) |
| 102 |
{ |
| 103 |
if (b != 3) |
| 104 |
return fn6 (*a, b); // { dg-error "null pointer" } |
| 105 |
return 7; |
| 106 |
} |
| 107 |
|
| 108 |
constexpr int n1 = 7; |
| 109 |
constexpr int n2 = fn7 (&n1, 5); |
| 110 |
constexpr int n3 = fn7 ((const int *) 0, 8); // { dg-message "in .constexpr. expansion of " } |
| 111 |
|
| 112 |
constexpr int |
| 113 |
fn8 (int i) |
| 114 |
{ |
| 115 |
constexpr int g[10] = { }; |
| 116 |
return g[i]; // { dg-error "array subscript" } |
| 117 |
} |
| 118 |
|
| 119 |
constexpr int o1 = fn8 (9); |
| 120 |
constexpr int o2 = fn8 (10); // { dg-message "in .constexpr. expansion of " } |
| 121 |
|
| 122 |
constexpr int |
| 123 |
fn9 (int a, int b) |
| 124 |
{ |
| 125 |
if (b != 0) |
| 126 |
return a + b; // { dg-error "overflow in constant expression" } |
| 127 |
return a; |
| 128 |
} |
| 129 |
|
| 130 |
constexpr int p1 = fn9 (42, 7); |
| 131 |
constexpr int p2 = fn9 (__INT_MAX__, 1); // { dg-message "in .constexpr. expansion of " } |
| 132 |
constexpr int p3 = fn9 (__INT_MAX__, -1); |
| 133 |
constexpr int p4 = fn9 (INT_MIN, 1); |
| 134 |
constexpr int p5 = fn9 (INT_MIN, -1); // { dg-message "in .constexpr. expansion of " } |
| 135 |
|
| 136 |
SA (p1 == 49); |
| 137 |
SA (p3 == __INT_MAX__ - 1); |
| 138 |
SA (p4 == INT_MIN + 1); |
| 139 |
|
| 140 |
constexpr int |
| 141 |
fn10 (int a, int b) |
| 142 |
{ |
| 143 |
if (b != 0) |
| 144 |
return a * b; // { dg-error "overflow in constant expression" } |
| 145 |
return a; |
| 146 |
} |
| 147 |
|
| 148 |
constexpr int q1 = fn10 (10, 10); |
| 149 |
constexpr int q2 = fn10 (__INT_MAX__, 2); // { dg-message "in .constexpr. expansion of " } |
| 150 |
constexpr int q3 = fn10 (INT_MIN, 2); // { dg-message "in .constexpr. expansion of " } |
| 151 |
constexpr int q4 = fn10 (-1, -1); |
| 152 |
|
| 153 |
SA (q1 == 100); |
| 154 |
SA (q4 == 1); |
| 155 |
|
| 156 |
constexpr int |
| 157 |
fn11 (double d) |
| 158 |
{ |
| 159 |
int i = d; // { dg-error "overflow in constant expression" } |
| 160 |
if (i != 0) |
| 161 |
return i; |
| 162 |
return i * 2; |
| 163 |
} |
| 164 |
|
| 165 |
void |
| 166 |
__attribute__((optimize("no-trapping-math"))) |
| 167 |
foo () |
| 168 |
{ |
| 169 |
constexpr int r1 = fn11 (3.4); |
| 170 |
constexpr int r2 = fn11 (__builtin_inf ()); // { dg-message "in .constexpr. expansion of " } |
| 171 |
} |
| 172 |
|
| 173 |
constexpr int |
| 174 |
fn12 (int i) |
| 175 |
{ |
| 176 |
if (i == 42) |
| 177 |
__builtin_unreachable (); // { dg-error "is not a constant expression" } |
| 178 |
return i + 10; |
| 179 |
} |
| 180 |
|
| 181 |
constexpr int s1 = fn12 (1); |
| 182 |
constexpr int s2 = fn12 (42); // { dg-message "in .constexpr. expansion of " } |
| 183 |
|
| 184 |
SA (s1 == 11); |