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