Skip to content

Commit 10cb78b

Browse files
committed
[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.
1 parent 651db24 commit 10cb78b

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

clang/docs/ReleaseNotes.rst

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

254256
C23 Feature Support
255257
^^^^^^^^^^^^^^^^^^^

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

0 commit comments

Comments
 (0)