brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.8 KiB · 9d8d103 Raw
231 lines · cpp
1// RUN: %clang_cc1 -fsyntax-only -verify %s2// RUN: %clang_cc1 -fsyntax-only -verify -fexperimental-new-constant-interpreter %s3 4void test_basic_type_checks() {5  static_assert(__is_same(char, decltype(__builtin_bswapg((char)0))), "");6  static_assert(__is_same(unsigned char, decltype(__builtin_bswapg((unsigned char)0))), "");7  static_assert(__is_same(short, decltype(__builtin_bswapg((short)0))), "");8  static_assert(__is_same(unsigned short, decltype(__builtin_bswapg((unsigned short)0))), "");9  static_assert(__is_same(int, decltype(__builtin_bswapg((int)0))), "");10  static_assert(__is_same(unsigned int, decltype(__builtin_bswapg((unsigned int)0))), "");11  static_assert(__is_same(long, decltype(__builtin_bswapg((long)0))), "");12  static_assert(__is_same(unsigned long, decltype(__builtin_bswapg((unsigned long)0))), "");13	static_assert(__is_same(_BitInt(8), decltype(__builtin_bswapg((_BitInt(8))0))), "");14	static_assert(__is_same(_BitInt(16), decltype(__builtin_bswapg((_BitInt(16))0))), "");15	static_assert(__is_same(_BitInt(32), decltype(__builtin_bswapg((_BitInt(32))0))), "");16	static_assert(__is_same(_BitInt(64), decltype(__builtin_bswapg((_BitInt(64))0))), "");17	static_assert(__is_same(_BitInt(128), decltype(__builtin_bswapg((_BitInt(128))0))), "");18}19 20template<typename T>21void test_template_type_check() {22  static_assert(__is_same(T, decltype(__builtin_bswapg(T{}))), 23                "bswapg should return the same type as its argument");24  constexpr T zero{};25  constexpr T max = ~T{};26  constexpr T one = T{1};27    28  static_assert(__is_same(T, decltype(__builtin_bswapg(zero))), "");29  static_assert(__is_same(T, decltype(__builtin_bswapg(max))), "");30  static_assert(__is_same(T, decltype(__builtin_bswapg(one))), "");31}32template void test_template_type_check<char>();33template void test_template_type_check<unsigned char>();34template void test_template_type_check<short>();35template void test_template_type_check<unsigned short>();36template void test_template_type_check<int>();37template void test_template_type_check<unsigned int>();38template void test_template_type_check<long>();39template void test_template_type_check<_BitInt(8)>();40template void test_template_type_check<_BitInt(16)>();41template void test_template_type_check<_BitInt(32)>();42template void test_template_type_check<_BitInt(64)>();43template void test_template_type_check<_BitInt(128)>();44 45void test_lambda_type_checks() {46  auto lambda = [](auto x) {47    static_assert(__is_same(decltype(x), decltype(__builtin_bswapg(x))), 48                  "bswapg in lambda should preserve type");49    return __builtin_bswapg(x);50  };51  auto result_long = lambda(42UL);52  static_assert(__is_same(unsigned long, decltype(result_long)), "");53    54  auto result_int = lambda(42);55  static_assert(__is_same(int, decltype(result_int)), "");56    57  auto result_short = lambda(static_cast<short>(42));58  static_assert(__is_same(short, decltype(result_short)), "");59 60  auto result_char = lambda(static_cast<char>(42));61  static_assert(__is_same(char, decltype(result_char)), "");62}63 64decltype(auto) test_decltype_auto(int x) {65  return __builtin_bswapg(x);66}67 68void test_decltype_auto_check() {69  int x = 42;70  auto result = test_decltype_auto(x);71  static_assert(__is_same(int, decltype(result)), "");72}73 74template<auto Value>75struct ValueTemplateTypeTest {76  using value_type = decltype(Value);77  using result_type = decltype(__builtin_bswapg(Value));78    79  static constexpr bool type_matches = __is_same(value_type, result_type);80  static_assert(type_matches, "Value template bswapg should preserve type");81    82  static constexpr auto swapped_value = __builtin_bswapg(Value);83};84 85template<auto... Values>86void test_template_pack_types() {87  static_assert((__is_same(decltype(Values), decltype(__builtin_bswapg(Values))) && ...), "All pack elements should preserve type");88}89 90template struct ValueTemplateTypeTest<0x1234>;91template struct ValueTemplateTypeTest<0x12345678UL>;92template struct ValueTemplateTypeTest<(short)0x1234>;93template struct ValueTemplateTypeTest<(char)0x12>;94 95template<typename T>96void test_invalid_type() {97  __builtin_bswapg(T{}); // #invalid_type_use98}99 100void test_basic_errors() {101  test_invalid_type<float>();102  // expected-note@-1 {{in instantiation of function template specialization 'test_invalid_type<float>' requested here}}103  // expected-error@#invalid_type_use {{1st argument must be a scalar integer type (was 'float')}}104  105  test_invalid_type<double>(); 106  // expected-note@-1 {{in instantiation of function template specialization 'test_invalid_type<double>' requested here}}107  // expected-error@#invalid_type_use {{1st argument must be a scalar integer type (was 'double')}}108 109  test_invalid_type<void*>(); 110  // expected-note@-1 {{in instantiation of function template specialization 'test_invalid_type<void *>' requested here}}111  // expected-error@#invalid_type_use {{1st argument must be a scalar integer type (was 'void *')}}112}113 114template<typename T>115auto test_dependent_context(T value) -> decltype(__builtin_bswapg(value)) { // #dependent_use116  return __builtin_bswapg(value); 117}118 119void test_dependent_errors() {120  test_dependent_context(1.0f); 121  // expected-error@-1 {{no matching function for call to 'test_dependent_context'}}122  // expected-note@#dependent_use {{candidate template ignored: substitution failure [with T = float]: 1st argument must be a scalar integer type (was 'float')}}123  test_dependent_context(1.0l); 124  // expected-error@-1 {{no matching function for call to 'test_dependent_context'}}125  // expected-note@#dependent_use {{candidate template ignored: substitution failure [with T = long double]: 1st argument must be a scalar integer type (was 'long double')}}126  test_dependent_context("hello"); 127  // expected-error@-1 {{no matching function for call to 'test_dependent_context'}}128  // expected-note@#dependent_use {{candidate template ignored: substitution failure [with T = const char *]: 1st argument must be a scalar integer type (was 'const char *')}}129}130 131void test_lambda_errors() {132  auto lambda = [](auto x) {133    return __builtin_bswapg(x); // #lambda_use134  };135  136  lambda(1.0f);137  // expected-error@#lambda_use {{1st argument must be a scalar integer type (was 'float')}}138  // expected-note@-2 {{in instantiation of function template specialization 'test_lambda_errors()::(anonymous class)::operator()<float>' requested here}}139  lambda(1.0l);140  // expected-error@#lambda_use {{1st argument must be a scalar integer type (was 'long double')}}141  // expected-note@-2 {{in instantiation of function template specialization 'test_lambda_errors()::(anonymous class)::operator()<long double>' requested here}}142  lambda("hello");143  // expected-error@#lambda_use {{1st argument must be a scalar integer type (was 'const char *')}}144  // expected-note@-2 {{in instantiation of function template specialization 'test_lambda_errors()::(anonymous class)::operator()<const char *>' requested here}}145}146 147template <class... Args> void test_variadic_template_argument_count(Args... args) {148   int result = __builtin_bswapg(args...); // #arg_use149}150void test_variadic_template_args() {151  test_variadic_template_argument_count();152  // expected-error@#arg_use {{too few arguments to function call, expected 1, have 0}}153	// expected-note@-2 {{in instantiation of function template specialization 'test_variadic_template_argument_count<>' requested here}}154  test_variadic_template_argument_count(1);155  test_variadic_template_argument_count(1, 2);156  // expected-error@#arg_use {{too many arguments to function call, expected 1, have 2}}157	// expected-note@-2 {{in instantiation of function template specialization 'test_variadic_template_argument_count<int, int>' requested here}}158}159 160void test_lvalue_reference(int& a) {161  auto result = __builtin_bswapg(a);162  static_assert(__is_same(int, decltype(result)), "Should decay reference to value type");163}164 165void test_const_lvalue_reference(const int& a) {166  auto result = __builtin_bswapg(a);167  static_assert(__is_same(int, decltype(result)), "Should decay const reference to value type");168}169 170void test_rvalue_reference(int&& a) {171  auto result = __builtin_bswapg(a);172  static_assert(__is_same(int, decltype(result)), "Should decay rvalue reference to value type");173}174 175void test_const_rvalue_reference(const int&& a) {176	auto result = __builtin_bswapg(a);177  static_assert(__is_same(int, decltype(result)), "Should decay const rvalue reference to value type");178}179 180void test_array() {181  int arr[4] = {0x12, 0x34, 0x56, 0x78};182	__builtin_bswapg(arr);183	// expected-error@-1 {{1st argument must be a scalar integer type (was 'int[4]')}}184}185 186void test_pointer() {187  int x = 0x12345678;188	int *ptr = &x;189	__builtin_bswapg(ptr);190	// expected-error@-1 {{1st argument must be a scalar integer type (was 'int *')}}191}192 193enum BasicEnum {194  ENUM_VALUE1 = 0x1234,195  ENUM_VALUE2 = 0x34120000196};197 198void test_enum() {199	const BasicEnum e = ENUM_VALUE1;200	static_assert(__builtin_bswapg(e) == ENUM_VALUE2, "");201}202 203class testClass {204public:205  int value;206  testClass(int v) : value(v) {}207 208	int getValue() { return value; }209};210 211void test_class() {212	testClass c((int)0x12345678);213	__builtin_bswapg(c);214	// expected-error@-1 {{1st argument must be a scalar integer type (was 'testClass')}}215}216 217void test_nullptr() {218	__builtin_bswapg(nullptr);219	// expected-error@-1 {{1st argument must be a scalar integer type (was 'std::nullptr_t')}}220}221 222void test_bitint() {223  static_assert(__builtin_bswapg((_BitInt(8))0x12) == (_BitInt(8))0x12, "");224  static_assert(__builtin_bswapg((_BitInt(16))0x1234) == (_BitInt(16))0x3412, "");225  static_assert(__builtin_bswapg((_BitInt(32))0x00001234) == (_BitInt(32))0x34120000, "");226  static_assert(__builtin_bswapg((_BitInt(64))0x0000000000001234) == (_BitInt(64))0x3412000000000000, "");227  static_assert(__builtin_bswapg(~(_BitInt(128))0) == (~(_BitInt(128))0), "");228  static_assert(__builtin_bswapg((_BitInt(24))0x1234) == (_BitInt(24))0x3412, "");229  // expected-error@-1 {{_BitInt type '_BitInt(24)' (24 bits) must be a multiple of 16 bits for byte swapping}}230}231