Skip to content

Commit 7955bde

Browse files
committed
[C11] Add test coverage for N1310 and claim conformance
This is about the best I could do for testing that `signed char` does not have any padding bits.
1 parent 2e817bf commit 7955bde

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

clang/test/C/C11/n1310.c

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+

clang/www/c_status.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ <h2 id="c11">C11 implementation status</h2>
411411
<tr>
412412
<td>Requiring signed char to have no padding bits</td>
413413
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1310.htm">N1310</a></td>
414-
<td class="unknown" align="center">Unknown</td>
414+
<td class="full" align="center">Yes</td>
415415
</tr>
416416
<tr>
417417
<td>Initializing static or external variables</td>

0 commit comments

Comments
 (0)