Skip to content

Commit 5ab944a

Browse files
authored
[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>
1 parent 0291f49 commit 5ab944a

File tree

9 files changed

+44
-5
lines changed

9 files changed

+44
-5
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ C2y Feature Support
251251
a conforming extension in earlier C language modes, but not in C++ language
252252
modes (``std::extent`` and ``std::size`` already provide the same
253253
functionality but with more granularity). The feature can be tested via
254-
``__has_feature(c_countof)`` or ``__has_extension(c_countof)``.
254+
``__has_feature(c_countof)`` or ``__has_extension(c_countof)``. This also
255+
adds the ``<stdcountof.h>`` header file which exposes the ``countof`` macro
256+
which expands to ``_Countof``.
255257

256258
C23 Feature Support
257259
^^^^^^^^^^^^^^^^^^^

clang/lib/Headers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(core_files
1818
__stdarg_va_list.h
1919
stdatomic.h
2020
stdbool.h
21+
stdcountof.h
2122
stdckdint.h
2223
stddef.h
2324
__stddef_header_macro.h

clang/lib/Headers/module.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ module _Builtin_stdbool [system] {
231231
export *
232232
}
233233

234+
module _Builtin_stdcountof [system] {
235+
header "stdcountof.h"
236+
export *
237+
}
238+
234239
module _Builtin_stddef [system] {
235240
textual header "stddef.h"
236241

clang/lib/Headers/stdcountof.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*===---- stdcountof.h - Standard header for countof -----------------------===
2+
*
3+
* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
* See https://llvm.org/LICENSE.txt for license information.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*
7+
*===-----------------------------------------------------------------------===
8+
*/
9+
10+
#ifndef __STDCOUNTOF_H
11+
#define __STDCOUNTOF_H
12+
13+
#define countof _Countof
14+
15+
#endif /* __STDCOUNTOF_H */

clang/lib/Lex/ModuleMap.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ static bool isBuiltinHeaderName(StringRef FileName) {
258258
.Case("stdarg.h", true)
259259
.Case("stdatomic.h", true)
260260
.Case("stdbool.h", true)
261+
.Case("stdcountof.h", true)
261262
.Case("stddef.h", true)
262263
.Case("stdint.h", true)
263264
.Case("tgmath.h", true)

clang/lib/Lex/PPDirectives.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,8 @@ static bool warnByDefaultOnWrongCase(StringRef Include) {
252252
.Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
253253
.Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
254254
.Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
255-
.Cases("stdatomic.h", "stdbool.h", "stdckdint.h", "stddef.h", true)
256-
.Cases("stdint.h", "stdio.h", "stdlib.h", "stdnoreturn.h", true)
255+
.Cases("stdatomic.h", "stdbool.h", "stdckdint.h", "stdcountof.h", true)
256+
.Cases("stddef.h", "stdint.h", "stdio.h", "stdlib.h", "stdnoreturn.h", true)
257257
.Cases("string.h", "tgmath.h", "threads.h", "time.h", "uchar.h", true)
258258
.Cases("wchar.h", "wctype.h", true)
259259

clang/test/C/C2y/n3469.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
// RUN: %clang_cc1 -fsyntax-only -std=c2y -verify %s
1+
// RUN: %clang_cc1 -fsyntax-only -std=c2y -verify -ffreestanding %s
22

33
/* WG14 N3469: Clang 21
44
* The Big Array Size Survey
55
*
6-
* This renames _Lengthof to _Countof.
6+
* This renames _Lengthof to _Countof and introduces the stdcountof.h header.
77
*/
88

99
void test() {
@@ -12,3 +12,12 @@ void test() {
1212
expected-error {{expected expression}}
1313
}
1414

15+
#ifdef countof
16+
#error "why is countof defined as a macro?"
17+
#endif
18+
19+
#include <stdcountof.h>
20+
21+
#ifndef countof
22+
#error "why is countof not defined as a macro?"
23+
#endif

clang/test/Modules/Inputs/builtin-headers/system-modules.modulemap

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ module cstd [system] [no_undeclared_includes] {
4949
export *
5050
}
5151

52+
module stdcountof {
53+
header "stdcountof.h"
54+
export *
55+
}
56+
5257
module stddef {
5358
header "stddef.h"
5459
export *

clang/test/Modules/builtin-headers.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@import _Builtin_stdarg;
1818
@import _Builtin_stdatomic;
1919
@import _Builtin_stdbool;
20+
@import _Builtin_stdcountof;
2021
@import _Builtin_stddef;
2122
@import _Builtin_stdint;
2223
@import _Builtin_stdnoreturn;

0 commit comments

Comments
 (0)