[clang] a9797d7 - [C2x] Implement the `unreachable` macro for WG14 N2826

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 15 11:43:10 PST 2023


Author: Aaron Ballman
Date: 2023-02-15T14:43:01-05:00
New Revision: a9797d7d2d7878464868312e0c147b4e747a31c6

URL: https://github.com/llvm/llvm-project/commit/a9797d7d2d7878464868312e0c147b4e747a31c6
DIFF: https://github.com/llvm/llvm-project/commit/a9797d7d2d7878464868312e0c147b4e747a31c6.diff

LOG: [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

Added: 
    clang/test/C/C2x/n2826.c

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Headers/stddef.h
    clang/www/c_status.html

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 0dcd9b9f04b1d..ab7518cc2dc6b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -124,6 +124,8 @@ C Language Changes in Clang
 
 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
 -----------------------------

diff  --git a/clang/lib/Headers/stddef.h b/clang/lib/Headers/stddef.h
index 42815176dcd0f..539541f0ed41a 100644
--- a/clang/lib/Headers/stddef.h
+++ b/clang/lib/Headers/stddef.h
@@ -103,6 +103,11 @@ using ::std::nullptr_t;
 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)

diff  --git a/clang/test/C/C2x/n2826.c b/clang/test/C/C2x/n2826.c
new file mode 100644
index 0000000000000..f108985c13ddd
--- /dev/null
+++ b/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

diff  --git a/clang/www/c_status.html b/clang/www/c_status.html
index 741d8c5b1ad9c..ee0f116bc2908 100644
--- a/clang/www/c_status.html
+++ b/clang/www/c_status.html
@@ -1028,7 +1028,7 @@ <h2 id="c2x">C2x implementation status</h2>
     <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>


        


More information about the cfe-commits mailing list