[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / AST / ByteCode / bitfields.cpp
blobdf8d5678287d3711e33f137ce63ab85e0c128bff
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
6 namespace Basic {
7 struct A {
8 unsigned int a : 2;
9 constexpr A() : a(0) {}
10 constexpr A(int a) : a(a) {}
13 constexpr A a{1};
14 static_assert(a.a == 1, "");
16 constexpr A a2{10};
17 static_assert(a2.a == 2, "");
20 constexpr int storeA() {
21 A a;
22 a.a = 10;
24 return a.a;
26 static_assert(storeA() == 2, "");
28 constexpr int storeA2() {
29 A a;
30 return a.a = 10;
32 static_assert(storeA2() == 2, "");
34 #if __cplusplus >= 202002
35 struct Init1 {
36 unsigned a : 2 = 1;
38 constexpr Init1 I1{};
39 static_assert(I1.a == 1, "");
41 struct Init2 {
42 unsigned a : 2 = 100;
44 constexpr Init2 I2{};
45 static_assert(I2.a == 0, "");
46 #endif
48 struct Init3 {
49 unsigned a : 2;
50 constexpr Init3() : a(100) {}
52 constexpr Init3 I3{};
53 static_assert(I3.a == 0, "");
56 namespace Overflow {
57 struct A {int c:3;};
59 constexpr int f() {
60 A a1{3};
61 return a1.c++;
64 static_assert(f() == 3, "");
67 namespace Compound {
68 struct A {
69 unsigned int a : 2;
70 constexpr A() : a(0) {}
71 constexpr A(int a) : a(a) {}
74 constexpr unsigned add() {
75 A a;
76 a.a += 10;
77 return a.a;
79 static_assert(add() == 2, "");
81 constexpr unsigned sub() {
82 A a;
83 a.a -= 10;
84 return a.a;
86 static_assert(sub() == 2, "");
88 constexpr unsigned mul() {
89 A a(1);
90 a.a *= 5;
91 return a.a;
93 static_assert(mul() == 1, "");
95 constexpr unsigned div() {
96 A a(2);
97 a.a /= 2;
98 return a.a;
100 static_assert(div() == 1, "");
103 namespace test0 {
104 extern int int_source();
105 struct A {
106 int aField;
107 int bField;
110 struct B {
111 int onebit : 2;
112 int twobit : 6;
113 int intField;
116 struct C : A, B {
119 void b(C &c) {
120 c.onebit = int_source();
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}}