| 1 |
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify=expected,both %s |
| 2 |
// RUN: %clang_cc1 -verify=ref,both -Wno-bitfield-constant-conversion %s |
| 3 |
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify=expected,both %s |
| 4 |
// RUN: %clang_cc1 -std=c++20 -verify=ref,both -Wno-bitfield-constant-conversion %s |
| 5 |
|
| 6 |
namespace Basic { |
| 7 |
struct A { |
| 8 |
unsigned int a : 2; |
| 9 |
constexpr A() : a(0) {} |
| 10 |
constexpr A(int a) : a(a) {} |
| 11 |
}; |
| 12 |
|
| 13 |
constexpr A a{1}; |
| 14 |
static_assert(a.a == 1, ""); |
| 15 |
|
| 16 |
constexpr A a2{10}; |
| 17 |
static_assert(a2.a == 2, ""); |
| 18 |
|
| 19 |
|
| 20 |
constexpr int storeA() { |
| 21 |
A a; |
| 22 |
a.a = 10; |
| 23 |
|
| 24 |
return a.a; |
| 25 |
} |
| 26 |
static_assert(storeA() == 2, ""); |
| 27 |
|
| 28 |
constexpr int storeA2() { |
| 29 |
A a; |
| 30 |
return a.a = 10; |
| 31 |
} |
| 32 |
static_assert(storeA2() == 2, ""); |
| 33 |
|
| 34 |
#if __cplusplus >= 202002 |
| 35 |
struct Init1 { |
| 36 |
unsigned a : 2 = 1; |
| 37 |
}; |
| 38 |
constexpr Init1 I1{}; |
| 39 |
static_assert(I1.a == 1, ""); |
| 40 |
|
| 41 |
struct Init2 { |
| 42 |
unsigned a : 2 = 100; |
| 43 |
}; |
| 44 |
constexpr Init2 I2{}; |
| 45 |
static_assert(I2.a == 0, ""); |
| 46 |
#endif |
| 47 |
|
| 48 |
struct Init3 { |
| 49 |
unsigned a : 2; |
| 50 |
constexpr Init3() : a(100) {} |
| 51 |
}; |
| 52 |
constexpr Init3 I3{}; |
| 53 |
static_assert(I3.a == 0, ""); |
| 54 |
} |
| 55 |
|
| 56 |
namespace Overflow { |
| 57 |
struct A {int c:3;}; |
| 58 |
|
| 59 |
constexpr int f() { |
| 60 |
A a1{3}; |
| 61 |
return a1.c++; |
| 62 |
} |
| 63 |
|
| 64 |
static_assert(f() == 3, ""); |
| 65 |
} |
| 66 |
|
| 67 |
namespace Compound { |
| 68 |
struct A { |
| 69 |
unsigned int a : 2; |
| 70 |
constexpr A() : a(0) {} |
| 71 |
constexpr A(int a) : a(a) {} |
| 72 |
}; |
| 73 |
|
| 74 |
constexpr unsigned add() { |
| 75 |
A a; |
| 76 |
a.a += 10; |
| 77 |
return a.a; |
| 78 |
} |
| 79 |
static_assert(add() == 2, ""); |
| 80 |
|
| 81 |
constexpr unsigned sub() { |
| 82 |
A a; |
| 83 |
a.a -= 10; |
| 84 |
return a.a; |
| 85 |
} |
| 86 |
static_assert(sub() == 2, ""); |
| 87 |
|
| 88 |
constexpr unsigned mul() { |
| 89 |
A a(1); |
| 90 |
a.a *= 5; |
| 91 |
return a.a; |
| 92 |
} |
| 93 |
static_assert(mul() == 1, ""); |
| 94 |
|
| 95 |
constexpr unsigned div() { |
| 96 |
A a(2); |
| 97 |
a.a /= 2; |
| 98 |
return a.a; |
| 99 |
} |
| 100 |
static_assert(div() == 1, ""); |
| 101 |
} |
| 102 |
|
| 103 |
namespace test0 { |
| 104 |
extern int int_source(); |
| 105 |
struct A { |
| 106 |
int aField; |
| 107 |
int bField; |
| 108 |
}; |
| 109 |
|
| 110 |
struct B { |
| 111 |
int onebit : 2; |
| 112 |
int twobit : 6; |
| 113 |
int intField; |
| 114 |
}; |
| 115 |
|
| 116 |
struct C : A, B { |
| 117 |
}; |
| 118 |
|
| 119 |
void b(C &c) { |
| 120 |
c.onebit = int_source(); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
namespace NonConstBitWidth { |
| 125 |
int n3 = 37; // both-note {{declared here}} |
| 126 |
struct S { |
| 127 |
int l : n3; // both-error {{constant expression}} \ |
| 128 |
// both-note {{read of non-const variable}} |
| 129 |
}; |
| 130 |
} |
| 131 |
|
| 132 |
namespace IncDecOverflow { |
| 133 |
constexpr bool test1() { |
| 134 |
struct {unsigned u: 5; } a {}; |
| 135 |
a.u--; |
| 136 |
return a.u == 31; |
| 137 |
} |
| 138 |
static_assert(test1(), ""); |
| 139 |
|
| 140 |
constexpr bool test2() { |
| 141 |
struct {unsigned u: 5; } a {}; |
| 142 |
--a.u; |
| 143 |
return a.u == 31; |
| 144 |
} |
| 145 |
static_assert(test2(), ""); |
| 146 |
|
| 147 |
constexpr bool test3() { |
| 148 |
int x = 0; |
| 149 |
struct {unsigned u: 5; } a {}; |
| 150 |
x = a.u--; |
| 151 |
return a.u == 31; |
| 152 |
} |
| 153 |
static_assert(test3(), ""); |
| 154 |
|
| 155 |
constexpr bool test4() { |
| 156 |
int x = 0; |
| 157 |
struct {unsigned u: 5; } a {}; |
| 158 |
x = --a.u; |
| 159 |
return a.u == 31; |
| 160 |
} |
| 161 |
static_assert(test4(), ""); |
| 162 |
|
| 163 |
constexpr bool test5() { |
| 164 |
struct {unsigned u: 5; } a {}; |
| 165 |
a.u = 31; |
| 166 |
++a.u; |
| 167 |
|
| 168 |
return a.u == 0; |
| 169 |
} |
| 170 |
static_assert(test5(), ""); |
| 171 |
|
| 172 |
constexpr bool test6() { |
| 173 |
struct {unsigned u: 5; } a {}; |
| 174 |
a.u = 31; |
| 175 |
++a.u; |
| 176 |
|
| 177 |
return a.u == 0; |
| 178 |
} |
| 179 |
static_assert(test6(), ""); |
| 180 |
|
| 181 |
constexpr bool test7() { |
| 182 |
struct {unsigned u: 5; } a {}; |
| 183 |
a.u = 31; |
| 184 |
a.u++; |
| 185 |
|
| 186 |
return a.u == 0; |
| 187 |
} |
| 188 |
static_assert(test7(), ""); |
| 189 |
|
| 190 |
constexpr bool test8() { |
| 191 |
int x = 0; |
| 192 |
struct {unsigned u: 5; } a {}; |
| 193 |
a.u = 31; |
| 194 |
x = a.u++; |
| 195 |
|
| 196 |
return a.u == 0; |
| 197 |
} |
| 198 |
static_assert(test8(), ""); |
| 199 |
|
| 200 |
constexpr bool test9() { |
| 201 |
int x = 0; |
| 202 |
struct {unsigned u: 5; } a {}; |
| 203 |
a.u = 31; |
| 204 |
x = ++a.u; |
| 205 |
|
| 206 |
return a.u == 0; |
| 207 |
} |
| 208 |
static_assert(test9(), ""); |
| 209 |
} |