constexpr-builtin-bit-cast.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s
  2. // RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -triple x86_64-apple-macosx10.14.0 %s -fno-signed-char
  3. // RUN: %clang_cc1 -verify -std=c++2a -fsyntax-only -triple aarch64_be-linux-gnu %s
  4. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  5. # define LITTLE_END 1
  6. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  7. # define LITTLE_END 0
  8. #else
  9. # error "huh?"
  10. #endif
  11. template <class T, class V> struct is_same {
  12. static constexpr bool value = false;
  13. };
  14. template <class T> struct is_same<T, T> {
  15. static constexpr bool value = true;
  16. };
  17. static_assert(sizeof(int) == 4);
  18. static_assert(sizeof(long long) == 8);
  19. template <class To, class From>
  20. constexpr To bit_cast(const From &from) {
  21. static_assert(sizeof(To) == sizeof(From));
  22. #ifdef __CHAR_UNSIGNED__
  23. // expected-note@+4 2 {{indeterminate value can only initialize an object of type 'unsigned char', 'char', or 'std::byte'; 'signed char' is invalid}}
  24. #else
  25. // expected-note@+2 2 {{indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte'; 'signed char' is invalid}}
  26. #endif
  27. return __builtin_bit_cast(To, from);
  28. }
  29. template <class Intermediate, class Init>
  30. constexpr bool round_trip(const Init &init) {
  31. return bit_cast<Init>(bit_cast<Intermediate>(init)) == init;
  32. }
  33. void test_int() {
  34. static_assert(round_trip<unsigned>((int)-1));
  35. static_assert(round_trip<unsigned>((int)0x12345678));
  36. static_assert(round_trip<unsigned>((int)0x87654321));
  37. static_assert(round_trip<unsigned>((int)0x0C05FEFE));
  38. }
  39. void test_array() {
  40. constexpr unsigned char input[] = {0xCA, 0xFE, 0xBA, 0xBE};
  41. constexpr unsigned expected = LITTLE_END ? 0xBEBAFECA : 0xCAFEBABE;
  42. static_assert(bit_cast<unsigned>(input) == expected);
  43. }
  44. void test_record() {
  45. struct int_splicer {
  46. unsigned x;
  47. unsigned y;
  48. constexpr bool operator==(const int_splicer &other) const {
  49. return other.x == x && other.y == y;
  50. }
  51. };
  52. constexpr int_splicer splice{0x0C05FEFE, 0xCAFEBABE};
  53. static_assert(bit_cast<unsigned long long>(splice) == LITTLE_END
  54. ? 0xCAFEBABE0C05FEFE
  55. : 0x0C05FEFECAFEBABE);
  56. static_assert(bit_cast<int_splicer>(0xCAFEBABE0C05FEFE).x == LITTLE_END
  57. ? 0x0C05FEFE
  58. : 0xCAFEBABE);
  59. static_assert(round_trip<unsigned long long>(splice));
  60. static_assert(round_trip<long long>(splice));
  61. struct base2 {
  62. };
  63. struct base3 {
  64. unsigned z;
  65. };
  66. struct bases : int_splicer, base2, base3 {
  67. unsigned doublez;
  68. };
  69. struct tuple4 {
  70. unsigned x, y, z, doublez;
  71. constexpr bool operator==(tuple4 const &other) const {
  72. return x == other.x && y == other.y &&
  73. z == other.z && doublez == other.doublez;
  74. }
  75. };
  76. constexpr bases b = {{1, 2}, {}, {3}, 4};
  77. constexpr tuple4 t4 = bit_cast<tuple4>(b);
  78. static_assert(t4 == tuple4{1, 2, 3, 4});
  79. static_assert(round_trip<tuple4>(b));
  80. }
  81. void test_partially_initialized() {
  82. struct pad {
  83. signed char x;
  84. int y;
  85. };
  86. struct no_pad {
  87. signed char x;
  88. signed char p1, p2, p3;
  89. int y;
  90. };
  91. static_assert(sizeof(pad) == sizeof(no_pad));
  92. constexpr pad pir{4, 4};
  93. // expected-error@+2 {{constexpr variable 'piw' must be initialized by a constant expression}}
  94. // expected-note@+1 {{in call to 'bit_cast(pir)'}}
  95. constexpr int piw = bit_cast<no_pad>(pir).x;
  96. // expected-error@+2 {{constexpr variable 'bad' must be initialized by a constant expression}}
  97. // expected-note@+1 {{in call to 'bit_cast(pir)'}}
  98. constexpr no_pad bad = bit_cast<no_pad>(pir);
  99. constexpr pad fine = bit_cast<pad>(no_pad{1, 2, 3, 4, 5});
  100. static_assert(fine.x == 1 && fine.y == 5);
  101. }
  102. void no_bitfields() {
  103. // FIXME!
  104. struct S {
  105. unsigned char x : 8;
  106. };
  107. struct G {
  108. unsigned char x : 8;
  109. };
  110. constexpr S s{0};
  111. // expected-error@+2 {{constexpr variable 'g' must be initialized by a constant expression}}
  112. // expected-note@+1 {{constexpr bit_cast involving bit-field is not yet supported}}
  113. constexpr G g = __builtin_bit_cast(G, s);
  114. }
  115. void array_members() {
  116. struct S {
  117. int ar[3];
  118. constexpr bool operator==(const S &rhs) {
  119. return ar[0] == rhs.ar[0] && ar[1] == rhs.ar[1] && ar[2] == rhs.ar[2];
  120. }
  121. };
  122. struct G {
  123. int a, b, c;
  124. constexpr bool operator==(const G &rhs) {
  125. return a == rhs.a && b == rhs.b && c == rhs.c;
  126. }
  127. };
  128. constexpr S s{{1, 2, 3}};
  129. constexpr G g = bit_cast<G>(s);
  130. static_assert(g.a == 1 && g.b == 2 && g.c == 3);
  131. static_assert(round_trip<G>(s));
  132. static_assert(round_trip<S>(g));
  133. }
  134. void bad_types() {
  135. union X {
  136. int x;
  137. };
  138. struct G {
  139. int g;
  140. };
  141. // expected-error@+2 {{constexpr variable 'g' must be initialized by a constant expression}}
  142. // expected-note@+1 {{bit_cast from a union type is not allowed in a constant expression}}
  143. constexpr G g = __builtin_bit_cast(G, X{0});
  144. // expected-error@+2 {{constexpr variable 'x' must be initialized by a constant expression}}
  145. // expected-note@+1 {{bit_cast to a union type is not allowed in a constant expression}}
  146. constexpr X x = __builtin_bit_cast(X, G{0});
  147. struct has_pointer {
  148. // expected-note@+1 2 {{invalid type 'int *' is a member of 'has_pointer'}}
  149. int *ptr;
  150. };
  151. // expected-error@+2 {{constexpr variable 'ptr' must be initialized by a constant expression}}
  152. // expected-note@+1 {{bit_cast from a pointer type is not allowed in a constant expression}}
  153. constexpr unsigned long ptr = __builtin_bit_cast(unsigned long, has_pointer{0});
  154. // expected-error@+2 {{constexpr variable 'hptr' must be initialized by a constant expression}}
  155. // expected-note@+1 {{bit_cast to a pointer type is not allowed in a constant expression}}
  156. constexpr has_pointer hptr = __builtin_bit_cast(has_pointer, 0ul);
  157. }
  158. void backtrace() {
  159. struct A {
  160. // expected-note@+1 {{invalid type 'int *' is a member of 'A'}}
  161. int *ptr;
  162. };
  163. struct B {
  164. // expected-note@+1 {{invalid type 'A [10]' is a member of 'B'}}
  165. A as[10];
  166. };
  167. // expected-note@+1 {{invalid type 'B' is a base of 'C'}}
  168. struct C : B {
  169. };
  170. struct E {
  171. unsigned long ar[10];
  172. };
  173. // expected-error@+2 {{constexpr variable 'e' must be initialized by a constant expression}}
  174. // expected-note@+1 {{bit_cast from a pointer type is not allowed in a constant expression}}
  175. constexpr E e = __builtin_bit_cast(E, C{});
  176. }
  177. void test_array_fill() {
  178. constexpr unsigned char a[4] = {1, 2};
  179. constexpr unsigned int i = bit_cast<unsigned int>(a);
  180. static_assert(i == LITTLE_END ? 0x00000201 : 0x01020000, "");
  181. }
  182. typedef decltype(nullptr) nullptr_t;
  183. #ifdef __CHAR_UNSIGNED__
  184. // expected-note@+5 {{indeterminate value can only initialize an object of type 'unsigned char', 'char', or 'std::byte'; 'unsigned long' is invalid}}
  185. #else
  186. // expected-note@+3 {{indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte'; 'unsigned long' is invalid}}
  187. #endif
  188. // expected-error@+1 {{constexpr variable 'test_from_nullptr' must be initialized by a constant expression}}
  189. constexpr unsigned long test_from_nullptr = __builtin_bit_cast(unsigned long, nullptr);
  190. constexpr int test_from_nullptr_pass = (__builtin_bit_cast(unsigned char[8], nullptr), 0);
  191. constexpr int test_to_nullptr() {
  192. nullptr_t npt = __builtin_bit_cast(nullptr_t, 0ul);
  193. struct indet_mem {
  194. unsigned char data[sizeof(void *)];
  195. };
  196. indet_mem im = __builtin_bit_cast(indet_mem, nullptr);
  197. nullptr_t npt2 = __builtin_bit_cast(nullptr_t, im);
  198. return 0;
  199. }
  200. constexpr int ttn = test_to_nullptr();
  201. // expected-warning@+2 {{returning reference to local temporary object}}
  202. // expected-note@+1 {{temporary created here}}
  203. constexpr const long &returns_local() { return 0L; }
  204. // expected-error@+2 {{constexpr variable 'test_nullptr_bad' must be initialized by a constant expression}}
  205. // expected-note@+1 {{read of temporary whose lifetime has ended}}
  206. constexpr nullptr_t test_nullptr_bad = __builtin_bit_cast(nullptr_t, returns_local());
  207. constexpr int test_indeterminate(bool read_indet) {
  208. struct pad {
  209. char a;
  210. int b;
  211. };
  212. struct no_pad {
  213. char a;
  214. unsigned char p1, p2, p3;
  215. int b;
  216. };
  217. pad p{1, 2};
  218. no_pad np = bit_cast<no_pad>(p);
  219. int tmp = np.a + np.b;
  220. unsigned char& indet_ref = np.p1;
  221. if (read_indet) {
  222. // expected-note@+1 {{read of uninitialized object is not allowed in a constant expression}}
  223. tmp = indet_ref;
  224. }
  225. indet_ref = 0;
  226. return 0;
  227. }
  228. constexpr int run_test_indeterminate = test_indeterminate(false);
  229. // expected-error@+2 {{constexpr variable 'run_test_indeterminate2' must be initialized by a constant expression}}
  230. // expected-note@+1 {{in call to 'test_indeterminate(true)'}}
  231. constexpr int run_test_indeterminate2 = test_indeterminate(true);
  232. struct ref_mem {
  233. const int &rm;
  234. };
  235. constexpr int global_int = 0;
  236. // expected-error@+2 {{constexpr variable 'run_ref_mem' must be initialized by a constant expression}}
  237. // expected-note@+1 {{bit_cast from a type with a reference member is not allowed in a constant expression}}
  238. constexpr unsigned long run_ref_mem = __builtin_bit_cast(
  239. unsigned long, ref_mem{global_int});
  240. union u {
  241. int im;
  242. };
  243. // expected-error@+2 {{constexpr variable 'run_u' must be initialized by a constant expression}}
  244. // expected-note@+1 {{bit_cast from a union type is not allowed in a constant expression}}
  245. constexpr int run_u = __builtin_bit_cast(int, u{32});
  246. struct vol_mem {
  247. volatile int x;
  248. };
  249. // expected-error@+2 {{constexpr variable 'run_vol_mem' must be initialized by a constant expression}}
  250. // expected-note@+1 {{non-literal type 'vol_mem' cannot be used in a constant expression}}
  251. constexpr int run_vol_mem = __builtin_bit_cast(int, vol_mem{43});
  252. struct mem_ptr {
  253. int vol_mem::*x; // expected-note{{invalid type 'int vol_mem::*' is a member of 'mem_ptr'}}
  254. };
  255. // expected-error@+2 {{constexpr variable 'run_mem_ptr' must be initialized by a constant expression}}
  256. // expected-note@+1 {{bit_cast from a member pointer type is not allowed in a constant expression}}
  257. constexpr int run_mem_ptr = __builtin_bit_cast(unsigned long, mem_ptr{nullptr});
  258. struct A { char c; /* char padding : 8; */ short s; };
  259. struct B { unsigned char x[4]; };
  260. constexpr B one() {
  261. A a = {1, 2};
  262. return bit_cast<B>(a);
  263. }
  264. constexpr char good_one = one().x[0] + one().x[2] + one().x[3];
  265. // expected-error@+2 {{constexpr variable 'bad_one' must be initialized by a constant expression}}
  266. // expected-note@+1 {{read of uninitialized object is not allowed in a constant expression}}
  267. constexpr char bad_one = one().x[1];
  268. constexpr A two() {
  269. B b = one(); // b.x[1] is indeterminate.
  270. b.x[0] = 'a';
  271. b.x[2] = 1;
  272. b.x[3] = 2;
  273. return bit_cast<A>(b);
  274. }
  275. constexpr short good_two = two().c + two().s;
  276. namespace std {
  277. enum byte : unsigned char {};
  278. }
  279. enum my_byte : unsigned char {};
  280. struct pad {
  281. char a;
  282. int b;
  283. };
  284. constexpr int ok_byte = (__builtin_bit_cast(std::byte[8], pad{1, 2}), 0);
  285. constexpr int ok_uchar = (__builtin_bit_cast(unsigned char[8], pad{1, 2}), 0);
  286. #ifdef __CHAR_UNSIGNED__
  287. // expected-note@+5 {{indeterminate value can only initialize an object of type 'unsigned char', 'char', or 'std::byte'; 'my_byte' is invalid}}}}
  288. #else
  289. // expected-note@+3 {{indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte'; 'my_byte' is invalid}}
  290. #endif
  291. // expected-error@+1 {{constexpr variable 'bad_my_byte' must be initialized by a constant expression}}
  292. constexpr int bad_my_byte = (__builtin_bit_cast(my_byte[8], pad{1, 2}), 0);
  293. #ifndef __CHAR_UNSIGNED__
  294. // expected-error@+3 {{constexpr variable 'bad_char' must be initialized by a constant expression}}
  295. // expected-note@+2 {{indeterminate value can only initialize an object of type 'unsigned char' or 'std::byte'; 'char' is invalid}}
  296. #endif
  297. constexpr int bad_char = (__builtin_bit_cast(char[8], pad{1, 2}), 0);
  298. struct pad_buffer { unsigned char data[sizeof(pad)]; };
  299. constexpr bool test_pad_buffer() {
  300. pad x = {1, 2};
  301. pad_buffer y = __builtin_bit_cast(pad_buffer, x);
  302. pad z = __builtin_bit_cast(pad, y);
  303. return x.a == z.a && x.b == z.b;
  304. }
  305. static_assert(test_pad_buffer());