Skip to content

Commit a9797d7

Browse files
committed
[C2x] Implement the unreachable macro for WG14 N2826
This exposes __builtin_unreachable as the expansion for the unreachable macro in C2x. I added this definition under __need_STDDEF_H_misc on the assumption there is no need for a separate need macro to control adding this. Differential Revision: https://reviews.llvm.org/D143430
1 parent d6ff080 commit a9797d7

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ C Language Changes in Clang
124124

125125
C2x Feature Support
126126
-------------------
127+
- Implemented the ``unreachable`` macro in freestanding ``<stddef.h>`` for
128+
`WG14 N2826 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2826.pdf>`_
127129

128130
C++ Language Changes in Clang
129131
-----------------------------

clang/lib/Headers/stddef.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ using ::std::nullptr_t;
103103
typedef typeof(nullptr) nullptr_t;
104104
#endif /* defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L */
105105

106+
#if defined(__need_STDDEF_H_misc) && defined(__STDC_VERSION__) && \
107+
__STDC_VERSION__ >= 202000L
108+
#define unreachable() __builtin_unreachable()
109+
#endif /* defined(__need_STDDEF_H_misc) && >= C23 */
110+
106111
#if defined(__need_STDDEF_H_misc)
107112
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \
108113
(defined(__cplusplus) && __cplusplus >= 201103L)

clang/test/C/C2x/n2826.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_cc1 -ffreestanding -emit-llvm -o - -std=c2x %s | FileCheck %s
2+
// RUN: %clang_cc1 -ffreestanding -std=c17 -verify %s
3+
4+
/* WG14 N2826: Clang 17
5+
* Add annotations for unreachable control flow v2
6+
*/
7+
#include <stddef.h>
8+
9+
enum E {
10+
Zero,
11+
One,
12+
Two,
13+
};
14+
15+
int test(enum E e) {
16+
switch (e) {
17+
case Zero: return 0;
18+
case One: return 1;
19+
case Two: return 2;
20+
}
21+
unreachable(); // expected-error {{call to undeclared function 'unreachable'}}
22+
}
23+
24+
// CHECK: switch i32 %0, label %[[EPILOG:.+]] [
25+
// CHECK: [[EPILOG]]:
26+
// CHECK-NEXT: unreachable

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,7 +1028,7 @@ <h2 id="c2x">C2x implementation status</h2>
10281028
<tr>
10291029
<td>Add annotations for unreachable control flow v2</td>
10301030
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2826.pdf">N2826</a></td>
1031-
<td class="none" align="center">No</td>
1031+
<td class="unreleased" align="center">Clang 17</td>
10321032
</tr>
10331033
<tr>
10341034
<td>Unicode Sequences More Than 21 Bits are a Constraint Violation r0</td>

0 commit comments

Comments
 (0)