https://github.com/AaronBallman updated https://github.com/llvm/llvm-project/pull/140890
>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 1/3] [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 >From 7d94ea7a3d0b663ffca61f89bca2ca56ef585f7d Mon Sep 17 00:00:00 2001 From: Aaron Ballman <aa...@aaronballman.com> Date: Wed, 21 May 2025 09:33:56 -0400 Subject: [PATCH 2/3] Update other places where the builtin header needs to be mentioned --- clang/lib/Headers/CMakeLists.txt | 1 + clang/lib/Headers/module.modulemap | 5 +++++ clang/lib/Lex/ModuleMap.cpp | 1 + clang/lib/Lex/PPDirectives.cpp | 4 ++-- clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc | 1 + .../Modules/Inputs/builtin-headers/system-modules.modulemap | 5 +++++ 6 files changed, 15 insertions(+), 2 deletions(-) diff --git a/clang/lib/Headers/CMakeLists.txt b/clang/lib/Headers/CMakeLists.txt index 449feb012481f..013bd13d1d276 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/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp index 4175959d8f55b..be9cab8afb9b3 100644 --- a/clang/lib/Lex/ModuleMap.cpp +++ b/clang/lib/Lex/ModuleMap.cpp @@ -260,6 +260,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/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc b/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc index 463ce921f0672..cc54bc9446a01 100644 --- a/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc +++ b/clang/lib/Tooling/Inclusions/Stdlib/CSymbolMap.inc @@ -389,6 +389,7 @@ SYMBOL(cosh, None, <math.h>) SYMBOL(coshf, None, <math.h>) SYMBOL(coshl, None, <math.h>) SYMBOL(cosl, None, <math.h>) +SYMBOL(countof, None, <stdcountof.h>) SYMBOL(cpow, None, <complex.h>) SYMBOL(cpowf, None, <complex.h>) SYMBOL(cpowl, None, <complex.h>) 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 * >From 3f36d58552b6c67867b0730391b00ad43da5e56c Mon Sep 17 00:00:00 2001 From: Aaron Ballman <aa...@aaronballman.com> Date: Thu, 22 May 2025 08:40:50 -0400 Subject: [PATCH 3/3] Actually test the module header --- clang/test/Modules/builtin-headers.mm | 1 + 1 file changed, 1 insertion(+) 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; +@import _Builtin_stdcountof; @import _Builtin_stddef; @import _Builtin_stdint; @import _Builtin_stdnoreturn; _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits