Skip to content

[C2y] Add stdcountof.h #140890

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/Headers/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
15 changes: 15 additions & 0 deletions clang/lib/Headers/stdcountof.h
Original file line number Diff line number Diff line change
@@ -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 */
1 change: 1 addition & 0 deletions clang/lib/Lex/ModuleMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These too!

.Case("stddef.h", true)
.Case("stdint.h", true)
.Case("tgmath.h", true)
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ static bool warnByDefaultOnWrongCase(StringRef Include) {
.Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that these aren't tablegened somewhere is frightening!

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

Expand Down
13 changes: 11 additions & 2 deletions clang/test/C/C2y/n3469.c
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ module cstd [system] [no_undeclared_includes] {
export *
}

module stdcountof {
header "stdcountof.h"
export *
}

module stddef {
header "stddef.h"
export *
Expand Down
1 change: 1 addition & 0 deletions clang/test/Modules/builtin-headers.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading