[PATCH] D143430: [C2x] Implement the `unreachable` macro for WG14 N2826

Aaron Ballman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 6 11:49:44 PST 2023


aaron.ballman created this revision.
aaron.ballman added reviewers: jyknight, clang-language-wg, efriedma.
Herald added a project: All.
aaron.ballman requested review of this revision.
Herald added a project: clang.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143430

Files:
  clang/docs/ReleaseNotes.rst
  clang/lib/Headers/stddef.h
  clang/test/C/C2x/n2826.c
  clang/www/c_status.html


Index: clang/www/c_status.html
===================================================================
--- clang/www/c_status.html
+++ clang/www/c_status.html
@@ -1028,7 +1028,7 @@
     <tr>
       <td>Add annotations for unreachable control flow v2</td>
       <td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2826.pdf">N2826</a></td>
-      <td class="none" align="center">No</td>
+      <td class="unreleased" align="center">Clang 17</td>
     </tr>
     <tr>
       <td>Unicode Sequences More Than 21 Bits are a Constraint Violation r0</td>
Index: clang/test/C/C2x/n2826.c
===================================================================
--- /dev/null
+++ clang/test/C/C2x/n2826.c
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -ffreestanding -emit-llvm -o - -std=c2x %s | FileCheck %s
+// RUN: %clang_cc1 -ffreestanding -std=c17 -verify %s
+
+/* WG14 N2826: Clang 17
+ * Add annotations for unreachable control flow v2
+ */
+#include <stddef.h>
+
+enum E {
+  Zero,
+  One,
+  Two,
+};
+
+int test(enum E e) {
+  switch (e) {
+  case Zero: return 0;
+  case One: return 1;
+  case Two: return 2;
+  }
+  unreachable(); // expected-error {{call to undeclared function 'unreachable'}}
+}
+
+// CHECK: switch i32 %0, label %[[EPILOG:.+]] [
+// CHECK: [[EPILOG]]:
+// CHECK-NEXT: unreachable
Index: clang/lib/Headers/stddef.h
===================================================================
--- clang/lib/Headers/stddef.h
+++ clang/lib/Headers/stddef.h
@@ -103,6 +103,11 @@
 typedef typeof(nullptr) nullptr_t;
 #endif /* defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202000L */
 
+#if defined(__need_STDDEF_H_misc) && defined(__STDC_VERSION__) &&              \
+    __STDC_VERSION__ >= 202000L
+#define unreachable() __builtin_unreachable()
+#endif /* defined(__need_STDDEF_H_misc) && >= C23 */
+
 #if defined(__need_STDDEF_H_misc)
 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) ||              \
     (defined(__cplusplus) && __cplusplus >= 201103L)
Index: clang/docs/ReleaseNotes.rst
===================================================================
--- clang/docs/ReleaseNotes.rst
+++ clang/docs/ReleaseNotes.rst
@@ -117,6 +117,8 @@
 
 C2x Feature Support
 -------------------
+- Implemented the ``unreachable`` macro in freestanding ``<stddef.h>`` for
+  `WG14 N2826 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2826.pdf>`_
 
 C++ Language Changes in Clang
 -----------------------------


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143430.495233.patch
Type: text/x-patch
Size: 2428 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230206/d9fb2943/attachment.bin>


More information about the cfe-commits mailing list