https://github.com/AaronBallman created 
https://github.com/llvm/llvm-project/pull/140890

WG14 N3469 changed _Lengthof to _Countof but it also introduced the 
<stdcountof.h> header to expose a macro with a non-ugly identifier. GCC vends 
this header as part of the compiler implementation, so Clang should do the same.

>From 10cb78bd9f361dd442c40702dad3c7809f466615 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <aa...@aaronballman.com>
Date: Wed, 21 May 2025 08:59:47 -0400
Subject: [PATCH] [C2y] Add stdcountof.h

WG14 N3469 changed _Lengthof to _Countof but it also introduced the
<stdcountof.h> header to expose a macro with a non-ugly identifier.
GCC vends this header as part of the compiler implementation, so Clang
should do the same.
---
 clang/docs/ReleaseNotes.rst    |  4 +++-
 clang/lib/Headers/stdcountof.h | 15 +++++++++++++++
 clang/test/C/C2y/n3469.c       | 13 +++++++++++--
 3 files changed, 29 insertions(+), 3 deletions(-)
 create mode 100644 clang/lib/Headers/stdcountof.h

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index b466f3758e0b6..7b2777f711b8d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -249,7 +249,9 @@ C2y Feature Support
   a conforming extension in earlier C language modes, but not in C++ language
   modes (``std::extent`` and ``std::size`` already provide the same
   functionality but with more granularity). The feature can be tested via
-  ``__has_feature(c_countof)`` or ``__has_extension(c_countof)``.
+  ``__has_feature(c_countof)`` or ``__has_extension(c_countof)``. This also
+  adds the ``<stdcountof.h>`` header file which exposes the ``countof`` macro
+  which expands to ``_Countof``.
 
 C23 Feature Support
 ^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Headers/stdcountof.h b/clang/lib/Headers/stdcountof.h
new file mode 100644
index 0000000000000..5714e6d6ff860
--- /dev/null
+++ b/clang/lib/Headers/stdcountof.h
@@ -0,0 +1,15 @@
+/*===---- stdcountof.h - Standard header for countof -----------------------===
+ *
+ * Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+ * See https://llvm.org/LICENSE.txt for license information.
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+ *
+ *===-----------------------------------------------------------------------===
+ */
+
+#ifndef __STDCOUNTOF_H
+#define __STDCOUNTOF_H
+
+#define countof _Countof
+
+#endif /* __STDCOUNTOF_H */
diff --git a/clang/test/C/C2y/n3469.c b/clang/test/C/C2y/n3469.c
index 3d9ac8e6411e9..4660596614075 100644
--- a/clang/test/C/C2y/n3469.c
+++ b/clang/test/C/C2y/n3469.c
@@ -1,9 +1,9 @@
-// RUN: %clang_cc1 -fsyntax-only -std=c2y -verify %s
+// RUN: %clang_cc1 -fsyntax-only -std=c2y -verify -ffreestanding %s
 
 /* WG14 N3469: Clang 21
  * The Big Array Size Survey
  *
- * This renames _Lengthof to _Countof.
+ * This renames _Lengthof to _Countof and introduces the stdcountof.h header.
  */
 
 void test() {
@@ -12,3 +12,12 @@ void test() {
                                expected-error {{expected expression}}
 }
 
+#ifdef countof
+#error "why is countof defined as a macro?"
+#endif
+
+#include <stdcountof.h>
+
+#ifndef countof
+#error "why is countof not defined as a macro?"
+#endif

_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to