xref: /llvm-project/clang/test/SemaCXX/has_unique_object_reps_bitint.cpp (revision 872f74440f3c8b749d90384a23caa894068504f9)
1 // RUN: %clang_cc1 -triple x86_64-unknown-unknown -fsyntax-only -verify -std=c++17 -Wno-bitfield-width %s
2 //  expected-no-diagnostics
3 
4 static_assert(__has_unique_object_representations(_BitInt(8)));
5 static_assert(__has_unique_object_representations(unsigned _BitInt(8)));
6 static_assert(__has_unique_object_representations(_BitInt(sizeof(int) * 8u)));
7 // sizeof(_BitInt(24)) may be 4 to align it to the next greater integer type, in which case it would have 8 padding bits.
8 static_assert(__has_unique_object_representations(_BitInt(24)) == (sizeof(_BitInt(24)) == 3));
9 
10 static_assert(!__has_unique_object_representations(_BitInt(7)));
11 static_assert(!__has_unique_object_representations(unsigned _BitInt(7)));
12 static_assert(!__has_unique_object_representations(_BitInt(2)));
13 static_assert(!__has_unique_object_representations(unsigned _BitInt(1)));
14 
15 template <unsigned N>
check()16 constexpr bool check() {
17   if constexpr (N <= __BITINT_MAXWIDTH__) {
18     static_assert(__has_unique_object_representations(_BitInt(N)) == (sizeof(_BitInt(N)) * 8u == N));
19     static_assert(__has_unique_object_representations(unsigned _BitInt(N)) == (sizeof(unsigned _BitInt(N)) * 8u == N));
20   }
21   return true;
22 }
23 
24 template <unsigned... N>
25 constexpr bool do_check = (check<N>() && ...);
26 
27 static_assert(do_check<2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18>);
28 static_assert(do_check<15, 16, 17, 23, 24, 25, 31, 32, 33>);
29 static_assert(do_check<39, 40, 41, 47, 48, 49>);
30 static_assert(do_check<127, 128, 129, 255, 256, 257, 383, 384, 385>);
31 
32 template <unsigned N>
33 struct in_struct {
34   _BitInt(N) x;
checkin_struct35   static constexpr bool check() {
36     return __has_unique_object_representations(in_struct<N>) == __has_unique_object_representations(_BitInt(N));
37   }
38 };
39 
40 static_assert(in_struct<8>::check());
41 static_assert(in_struct<7>::check());
42 
43 struct bit_fields_1 {
44   _BitInt(7) x : 7;
45   unsigned _BitInt(1) y : 1;
46 };
47 
48 static_assert(__has_unique_object_representations(bit_fields_1) == (sizeof(bit_fields_1) == 1));
49 
50 struct bit_fields_2 {
51   _BitInt(8) x : 7;
52 };
53 
54 static_assert(!__has_unique_object_representations(bit_fields_2));
55 
56 struct bit_fields_3 {
57   _BitInt(15) x : 8;
58 };
59 
60 static_assert(__has_unique_object_representations(bit_fields_3) == (sizeof(bit_fields_3) == 1));
61 
62 #if __BITINT_MAXWIDTH__ >= 129
63 struct bit_fields_4 {
64   _BitInt(129) x : 128;
65 };
66 
67 static_assert(__has_unique_object_representations(bit_fields_4) == (sizeof(bit_fields_4) == 128 / 8));
68 #endif
69 
70 struct bit_fields_5 {
71   _BitInt(2) x : 8;
72 };
73 
74 static_assert(!__has_unique_object_representations(bit_fields_5));
75 
76 template <unsigned N>
77 struct ref_member {
78   _BitInt(N) & x;
79 };
80 
81 struct int_ref_member {
82   int &x;
83 };
84 
85 static_assert(__has_unique_object_representations(ref_member<7>) == __has_unique_object_representations(ref_member<8>));
86 static_assert(__has_unique_object_representations(ref_member<8>) == __has_unique_object_representations(int_ref_member));
87