| 1 |
// RUN: %clang_cc1 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify %s |
| 2 |
// RUN: %clang_cc1 -verify=ref -Wno-bitfield-constant-conversion %s |
| 3 |
// RUN: %clang_cc1 -std=c++20 -fexperimental-new-constant-interpreter -Wno-bitfield-constant-conversion -verify %s |
| 4 |
// RUN: %clang_cc1 -std=c++20 -verify=ref -Wno-bitfield-constant-conversion %s |
| 5 |
|
| 6 |
// expected-no-diagnostics |
| 7 |
// ref-no-diagnostics |
| 8 |
|
| 9 |
namespace Basic { |
| 10 |
struct A { |
| 11 |
unsigned int a : 2; |
| 12 |
constexpr A() : a(0) {} |
| 13 |
constexpr A(int a) : a(a) {} |
| 14 |
}; |
| 15 |
|
| 16 |
constexpr A a{1}; |
| 17 |
static_assert(a.a == 1, ""); |
| 18 |
|
| 19 |
constexpr A a2{10}; |
| 20 |
static_assert(a2.a == 2, ""); |
| 21 |
|
| 22 |
|
| 23 |
constexpr int storeA() { |
| 24 |
A a; |
| 25 |
a.a = 10; |
| 26 |
|
| 27 |
return a.a; |
| 28 |
} |
| 29 |
static_assert(storeA() == 2, ""); |
| 30 |
|
| 31 |
constexpr int storeA2() { |
| 32 |
A a; |
| 33 |
return a.a = 10; |
| 34 |
} |
| 35 |
static_assert(storeA2() == 2, ""); |
| 36 |
|
| 37 |
#if __cplusplus >= 202002 |
| 38 |
struct Init1 { |
| 39 |
unsigned a : 2 = 1; |
| 40 |
}; |
| 41 |
constexpr Init1 I1{}; |
| 42 |
static_assert(I1.a == 1, ""); |
| 43 |
|
| 44 |
struct Init2 { |
| 45 |
unsigned a : 2 = 100; |
| 46 |
}; |
| 47 |
constexpr Init2 I2{}; |
| 48 |
static_assert(I2.a == 0, ""); |
| 49 |
#endif |
| 50 |
|
| 51 |
struct Init3 { |
| 52 |
unsigned a : 2; |
| 53 |
constexpr Init3() : a(100) {} |
| 54 |
}; |
| 55 |
constexpr Init3 I3{}; |
| 56 |
static_assert(I3.a == 0, ""); |
| 57 |
} |
| 58 |
|
| 59 |
namespace Overflow { |
| 60 |
struct A {int c:3;}; |
| 61 |
|
| 62 |
constexpr int f() { |
| 63 |
A a1{3}; |
| 64 |
return a1.c++; |
| 65 |
} |
| 66 |
|
| 67 |
static_assert(f() == 3, ""); |
| 68 |
} |
| 69 |
|
| 70 |
namespace Compound { |
| 71 |
struct A { |
| 72 |
unsigned int a : 2; |
| 73 |
constexpr A() : a(0) {} |
| 74 |
constexpr A(int a) : a(a) {} |
| 75 |
}; |
| 76 |
|
| 77 |
constexpr unsigned add() { |
| 78 |
A a; |
| 79 |
a.a += 10; |
| 80 |
return a.a; |
| 81 |
} |
| 82 |
static_assert(add() == 2, ""); |
| 83 |
|
| 84 |
constexpr unsigned sub() { |
| 85 |
A a; |
| 86 |
a.a -= 10; |
| 87 |
return a.a; |
| 88 |
} |
| 89 |
static_assert(sub() == 2, ""); |
| 90 |
|
| 91 |
constexpr unsigned mul() { |
| 92 |
A a(1); |
| 93 |
a.a *= 5; |
| 94 |
return a.a; |
| 95 |
} |
| 96 |
static_assert(mul() == 1, ""); |
| 97 |
|
| 98 |
constexpr unsigned div() { |
| 99 |
A a(2); |
| 100 |
a.a /= 2; |
| 101 |
return a.a; |
| 102 |
} |
| 103 |
static_assert(div() == 1, ""); |
| 104 |
} |
| 105 |
|
| 106 |
namespace test0 { |
| 107 |
extern int int_source(); |
| 108 |
struct A { |
| 109 |
int aField; |
| 110 |
int bField; |
| 111 |
}; |
| 112 |
|
| 113 |
struct B { |
| 114 |
int onebit : 2; |
| 115 |
int twobit : 6; |
| 116 |
int intField; |
| 117 |
}; |
| 118 |
|
| 119 |
struct C : A, B { |
| 120 |
}; |
| 121 |
|
| 122 |
void b(C &c) { |
| 123 |
c.onebit = int_source(); |
| 124 |
} |
| 125 |
} |