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