[clang] 5ab944a - [C2y] Add stdcountof.h (#140890)

via cfe-commits cfe-commits at lists.llvm.org
Wed May 28 03:41:04 PDT 2025


Author: Aaron Ballman
Date: 2025-05-28T06:41:01-04:00
New Revision: 5ab944a8c6a213beb96f3747a441b02e497732e4

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

LOG: [C2y] Add stdcountof.h (#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.

Suggested-by: Alejandro Colomar <alx at kernel.org>

Added: 
    clang/lib/Headers/stdcountof.h

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Headers/CMakeLists.txt
    clang/lib/Headers/module.modulemap
    clang/lib/Lex/ModuleMap.cpp
    clang/lib/Lex/PPDirectives.cpp
    clang/test/C/C2y/n3469.c
    clang/test/Modules/Inputs/builtin-headers/system-modules.modulemap
    clang/test/Modules/builtin-headers.mm

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index ee74431cf33a7..c83f135da541c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -251,7 +251,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/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt
index 53219dc932587..24f5327b07dda 100644
--- a/clang/lib/Headers/CMakeLists.txt
+++ b/clang/lib/Headers/CMakeLists.txt
@@ -18,6 +18,7 @@ set(core_files
   __stdarg_va_list.h
   stdatomic.h
   stdbool.h
+  stdcountof.h
   stdckdint.h
   stddef.h
   __stddef_header_macro.h

diff  --git a/clang/lib/Headers/module.modulemap b/clang/lib/Headers/module.modulemap
index dcaf09e8f2c55..35897a3ed0e79 100644
--- a/clang/lib/Headers/module.modulemap
+++ b/clang/lib/Headers/module.modulemap
@@ -231,6 +231,11 @@ module _Builtin_stdbool [system] {
   export *
 }
 
+module _Builtin_stdcountof [system] {
+  header "stdcountof.h"
+  export *
+}
+
 module _Builtin_stddef [system] {
   textual header "stddef.h"
 

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/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp
index c088e4eecb3fb..637a08fe4dcdb 100644
--- a/clang/lib/Lex/ModuleMap.cpp
+++ b/clang/lib/Lex/ModuleMap.cpp
@@ -258,6 +258,7 @@ static bool isBuiltinHeaderName(StringRef FileName) {
            .Case("stdarg.h", true)
            .Case("stdatomic.h", true)
            .Case("stdbool.h", true)
+           .Case("stdcountof.h", true)
            .Case("stddef.h", true)
            .Case("stdint.h", true)
            .Case("tgmath.h", true)

diff  --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index b2a8459d6b9cc..68f9ca9cb4d40 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -252,8 +252,8 @@ static bool warnByDefaultOnWrongCase(StringRef Include) {
     .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
     .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
     .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
-    .Cases("stdatomic.h", "stdbool.h", "stdckdint.h", "stddef.h", true)
-    .Cases("stdint.h", "stdio.h", "stdlib.h", "stdnoreturn.h", true)
+    .Cases("stdatomic.h", "stdbool.h", "stdckdint.h", "stdcountof.h", true)
+    .Cases("stddef.h", "stdint.h", "stdio.h", "stdlib.h", "stdnoreturn.h", true)
     .Cases("string.h", "tgmath.h", "threads.h", "time.h", "uchar.h", true)
     .Cases("wchar.h", "wctype.h", true)
 

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

diff  --git a/clang/test/Modules/Inputs/builtin-headers/system-modules.modulemap b/clang/test/Modules/Inputs/builtin-headers/system-modules.modulemap
index 0161ff80fe618..186965177caaf 100644
--- a/clang/test/Modules/Inputs/builtin-headers/system-modules.modulemap
+++ b/clang/test/Modules/Inputs/builtin-headers/system-modules.modulemap
@@ -49,6 +49,11 @@ module cstd [system] [no_undeclared_includes] {
     export *
   }
 
+  module stdcountof {
+    header "stdcountof.h"
+    export *
+  }
+
   module stddef {
     header "stddef.h"
     export *

diff  --git a/clang/test/Modules/builtin-headers.mm b/clang/test/Modules/builtin-headers.mm
index 4c9ddec4e99c2..ad2d66ae38dfd 100644
--- a/clang/test/Modules/builtin-headers.mm
+++ b/clang/test/Modules/builtin-headers.mm
@@ -17,6 +17,7 @@
 @import _Builtin_stdarg;
 @import _Builtin_stdatomic;
 @import _Builtin_stdbool;
+ at import _Builtin_stdcountof;
 @import _Builtin_stddef;
 @import _Builtin_stdint;
 @import _Builtin_stdnoreturn;


        


More information about the cfe-commits mailing list