[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@kernel.org>
This commit is contained in:
Aaron Ballman
2025-05-28 06:41:01 -04:00
committed by GitHub
parent 0291f495db
commit 5ab944a8c6
9 changed files with 44 additions and 5 deletions

View File

@@ -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
^^^^^^^^^^^^^^^^^^^

View File

@@ -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

View File

@@ -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"

View File

@@ -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 */

View File

@@ -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)

View File

@@ -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)

View File

@@ -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

View File

@@ -49,6 +49,11 @@ module cstd [system] [no_undeclared_includes] {
export *
}
module stdcountof {
header "stdcountof.h"
export *
}
module stddef {
header "stddef.h"
export *

View File

@@ -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;