xref: /llvm-project/lldb/test/API/lang/cpp/bitfields/main.cpp (revision 3c30f224005e3749c89e00592bd2a43aba2aaf75)
1 #include <stdint.h>
2 
3 struct LargeBitsA {
4   unsigned int : 30, a : 20;
5 } lba;
6 
7 struct LargeBitsB {
8   unsigned int a : 1, : 11, : 12, b : 20;
9 } lbb;
10 
11 struct LargeBitsC {
12   unsigned int : 13, : 9, a : 1, b : 1, c : 5, d : 1, e : 20;
13 } lbc;
14 
15 struct LargeBitsD {
16   char arr[3];
17   unsigned int : 30, a : 20;
18 } lbd;
19 
20 struct BitfieldsInStructInUnion {
21   class fields {
22     uint64_t : 13;
23     uint64_t : 9;
24 
25     uint64_t a : 1;
26     uint64_t b : 1;
27     uint64_t c : 1;
28     uint64_t d : 1;
29     uint64_t e : 1;
30     uint64_t f : 1;
31     uint64_t g : 1;
32     uint64_t h : 1;
33     uint64_t i : 1;
34     uint64_t j : 1;
35     uint64_t k : 1;
36 
37     // In order to reproduce the crash for this case we need the
38     // members of fields to stay private :-(
39     friend struct BitfieldsInStructInUnion;
40   };
41 
42   union {
43     struct fields f;
44   };
45 
BitfieldsInStructInUnionBitfieldsInStructInUnion46   BitfieldsInStructInUnion() {
47     f.a = 1;
48     f.b = 0;
49     f.c = 1;
50     f.d = 0;
51     f.e = 1;
52     f.f = 0;
53     f.g = 1;
54     f.h = 0;
55     f.i = 1;
56     f.j = 0;
57     f.k = 1;
58   }
59 } bitfields_in_struct_in_union;
60 
61 class Base {
62 public:
63   uint32_t b_a;
64 };
65 
66 class Derived : public Base {
67 public:
68   uint32_t d_a : 1;
69 } derived;
70 
71 union UnionWithBitfields {
72   unsigned int a : 8;
73   unsigned int b : 16;
74   unsigned int c : 32;
75   unsigned int x;
76 } uwbf;
77 
78 union UnionWithUnnamedBitfield {
79   unsigned int : 16, a : 24;
80   unsigned int x;
81 } uwubf;
82 
83 struct BoolBits {
84   bool a : 1;
85   bool b : 1;
86   bool c : 2;
87   bool d : 2;
88 };
89 
90 struct WithVTable {
~WithVTableWithVTable91   virtual ~WithVTable() {}
92   unsigned a : 4;
93   unsigned b : 4;
94   unsigned c : 4;
95 };
96 WithVTable with_vtable;
97 
98 struct WithVTableAndUnnamed {
~WithVTableAndUnnamedWithVTableAndUnnamed99   virtual ~WithVTableAndUnnamed() {}
100   unsigned : 4;
101   unsigned b : 4;
102   unsigned c : 4;
103 };
104 WithVTableAndUnnamed with_vtable_and_unnamed;
105 
106 struct BaseWithVTable {
~BaseWithVTableBaseWithVTable107   virtual ~BaseWithVTable() {}
108 };
109 struct HasBaseWithVTable : BaseWithVTable {
110   unsigned a : 4;
111   unsigned b : 4;
112   unsigned c : 4;
113 };
114 HasBaseWithVTable base_with_vtable;
115 
116 struct DerivedWithVTable : public Base {
~DerivedWithVTableDerivedWithVTable117   virtual ~DerivedWithVTable() {}
118   unsigned a : 1;
119 };
120 DerivedWithVTable derived_with_vtable;
121 
main(int argc,char const * argv[])122 int main(int argc, char const *argv[]) {
123   lba.a = 2;
124 
125   lbb.a = 1;
126   lbb.b = 3;
127 
128   lbc.a = 1;
129   lbc.b = 0;
130   lbc.c = 4;
131   lbc.d = 1;
132   lbc.e = 20;
133 
134   lbd.arr[0] = 'a';
135   lbd.arr[1] = 'b';
136   lbd.arr[2] = '\0';
137   lbd.a = 5;
138 
139   derived.b_a = 2;
140   derived.d_a = 1;
141 
142   uwbf.x = 0xFFFFFFFF;
143   uwubf.x = 0xFFFFFFFF;
144 
145   BoolBits bb;
146   bb.a = 0b1;
147   bb.b = 0b0;
148   bb.c = 0b11;
149   bb.d = 0b01;
150 
151   with_vtable.a = 5;
152   with_vtable.b = 0;
153   with_vtable.c = 5;
154 
155   with_vtable_and_unnamed.b = 0;
156   with_vtable_and_unnamed.c = 5;
157 
158   base_with_vtable.a = 5;
159   base_with_vtable.b = 0;
160   base_with_vtable.c = 5;
161 
162   derived_with_vtable.b_a = 2;
163   derived_with_vtable.a = 1;
164 
165   return 0; // break here
166 }
167