| 1 |
// RUN: %clang_cc1 -verify -std=c89 %s |
| 2 |
// RUN: %clang_cc1 -verify -std=c99 %s |
| 3 |
// RUN: %clang_cc1 -verify -std=c11 %s |
| 4 |
// RUN: %clang_cc1 -verify -std=c17 %s |
| 5 |
// RUN: %clang_cc1 -verify -std=c23 %s |
| 6 |
// expected-no-diagnostics |
| 7 |
|
| 8 |
/* WG14 N1310: Yes |
| 9 |
* Requiring signed char to have no padding bits |
| 10 |
*/ |
| 11 |
|
| 12 |
/* This is shockingly hard to test, but we're trying our best by checking that |
| 13 |
* setting each bit of an unsigned char, then bit-casting it to signed char, |
| 14 |
* results in a value we expect to see. If we have padding bits, then it's |
| 15 |
* possible (but not mandatory) for the value to not be as we expect, so a |
| 16 |
* failing assertion means the implementation is broken but a passing test does |
| 17 |
* not *prove* there aren't padding bits. |
| 18 |
*/ |
| 19 |
_Static_assert(__CHAR_BIT__ == 8, ""); |
| 20 |
_Static_assert(sizeof(signed char) == 1, ""); |
| 21 |
|
| 22 |
#define TEST(Bit, Expected) __builtin_bit_cast(signed char, (unsigned char)(1 << Bit)) == Expected |
| 23 |
_Static_assert(TEST(0, 1), ""); |
| 24 |
_Static_assert(TEST(1, 2), ""); |
| 25 |
_Static_assert(TEST(2, 4), ""); |
| 26 |
_Static_assert(TEST(3, 8), ""); |
| 27 |
_Static_assert(TEST(4, 16), ""); |
| 28 |
_Static_assert(TEST(5, 32), ""); |
| 29 |
_Static_assert(TEST(6, 64), ""); |
| 30 |
_Static_assert(TEST(7, (signed char)128), ""); |
| 31 |
|