Skip to content

Commit 5465250

Browse files
[clang][modules] stdarg.h and stddef.h shouldn't directly declare anything
stdarg.h and especially stddef.h are textual and so everything they declare gets precompiled into all of their clients' pcm files. They shouldn't directly declare anything though, their purpose is to select what submodules get imported, and not to add duplicate declarations to all of their clients. Make it so that they always ignore their header guards, even without modules, and declare them in separate header files so that they only go into the stdarg/stddef pcms. Still declare them in case clients rely on them.
1 parent 7111304 commit 5465250

File tree

9 files changed

+53
-46
lines changed

9 files changed

+53
-46
lines changed

clang-tools-extra/clang-include-fixer/find-all-symbols/STLPostfixHeaderMap.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ const HeaderMapCollector::RegexHeaderMap *getSTLPostfixHeaderMap() {
1515
static const HeaderMapCollector::RegexHeaderMap STLPostfixHeaderMap = {
1616
{"include/__stdarg___gnuc_va_list.h$", "<cstdarg>"},
1717
{"include/__stdarg___va_copy.h$", "<cstdarg>"},
18+
{"include/__stdarg_macro.h$", "<cstdarg"},
1819
{"include/__stdarg_va_arg.h$", "<cstdarg>"},
1920
{"include/__stdarg_va_copy.h$", "<cstdarg>"},
2021
{"include/__stdarg_va_list.h$", "<cstdarg>"},
22+
{"include/__stddef_macro.h$", "<cstddef"},
2123
{"include/__stddef_max_align_t.h$", "<cstddef>"},
2224
{"include/__stddef_null.h$", "<cstddef>"},
2325
{"include/__stddef_nullptr_t.h$", "<cstddef>"},

clang-tools-extra/clangd/index/CanonicalIncludes.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ namespace {
1818
const std::pair<llvm::StringRef, llvm::StringRef> IncludeMappings[] = {
1919
{"include/__stdarg___gnuc_va_list.h", "<cstdarg>"},
2020
{"include/__stdarg___va_copy.h", "<cstdarg>"},
21+
{"include/__stdarg_macro.h", "<cstdarg>"},
2122
{"include/__stdarg_va_arg.h", "<cstdarg>"},
2223
{"include/__stdarg_va_copy.h", "<cstdarg>"},
2324
{"include/__stdarg_va_list.h", "<cstdarg>"},
25+
{"include/__stddef_macro.h", "<cstddef>"},
2426
{"include/__stddef_max_align_t.h", "<cstddef>"},
2527
{"include/__stddef_null.h", "<cstddef>"},
2628
{"include/__stddef_nullptr_t.h", "<cstddef>"},

clang/lib/Headers/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ set(core_files
1212
stdarg.h
1313
__stdarg___gnuc_va_list.h
1414
__stdarg___va_copy.h
15+
__stdarg_macro.h
1516
__stdarg_va_arg.h
1617
__stdarg_va_copy.h
1718
__stdarg_va_list.h
1819
stdatomic.h
1920
stdbool.h
2021
stdckdint.h
2122
stddef.h
23+
__stddef_macro.h
2224
__stddef_max_align_t.h
2325
__stddef_null.h
2426
__stddef_nullptr_t.h

clang/lib/Headers/__stdarg_macro.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*===---- __stdarg_macro.h -------------------------------------------------===
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 __STDARG_H
11+
#define __STDARG_H
12+
#endif

clang/lib/Headers/__stddef_macro.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*===---- __stddef_macro.h -------------------------------------------------===
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 __STDDEF_H
11+
#define __STDDEF_H
12+
#endif

clang/lib/Headers/module.modulemap

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,11 @@ module _Builtin_stdarg [system] {
203203
export *
204204
}
205205

206+
explicit module macro {
207+
header "__stdarg_macro.h"
208+
export *
209+
}
210+
206211
explicit module va_arg {
207212
header "__stdarg_va_arg.h"
208213
export *
@@ -232,6 +237,10 @@ module _Builtin_stdbool [system] {
232237
module _Builtin_stddef [system] {
233238
textual header "stddef.h"
234239

240+
explicit module macro {
241+
header "__stddef_macro.h"
242+
export *
243+
}
235244
// __stddef_max_align_t.h is always in this module, even if
236245
// -fbuiltin-headers-in-system-modules is passed.
237246
explicit module max_align_t {

clang/lib/Headers/stdarg.h

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,13 @@
1414
* need to use some of its interfaces. Otherwise this header provides all of
1515
* the expected interfaces.
1616
*
17-
* When clang modules are enabled, this header is a textual header. It ignores
18-
* its header guard so that multiple submodules can export its interfaces.
19-
* Take module SM with submodules A and B, whose headers both include stdarg.h
20-
* When SM.A builds, __STDARG_H will be defined. When SM.B builds, the
21-
* definition from SM.A will leak when building without local submodule
22-
* visibility. stdarg.h wouldn't include any of its implementation headers, and
23-
* SM.B wouldn't import any of the stdarg modules, and SM.B's `export *`
24-
* wouldn't export any stdarg interfaces as expected. However, since stdarg.h
25-
* ignores its header guard when building with modules, it all works as
26-
* expected.
27-
*
28-
* When clang modules are not enabled, the header guards can function in the
29-
* normal simple fashion.
17+
* When clang modules are enabled, this header is a textual header to support
18+
* the multiple include behavior. As such, it doesn't directly declare anything
19+
* so that it doesn't add duplicate declarations to all of its includers'
20+
* modules.
3021
*/
31-
#if !defined(__STDARG_H) || __has_feature(modules) || \
32-
defined(__need___va_list) || defined(__need_va_list) || \
33-
defined(__need_va_arg) || defined(__need___va_copy) || \
34-
defined(__need_va_copy)
35-
3622
#if defined(__MVS__) && __has_include_next(<stdarg.h>)
37-
#define __STDARG_H
23+
#include <__stdarg_macro.h>
3824
#undef __need___va_list
3925
#undef __need_va_list
4026
#undef __need_va_arg
@@ -46,7 +32,7 @@
4632
#if !defined(__need___va_list) && !defined(__need_va_list) && \
4733
!defined(__need_va_arg) && !defined(__need___va_copy) && \
4834
!defined(__need_va_copy)
49-
#define __STDARG_H
35+
#include <__stdarg_macro.h>
5036
#define __need___va_list
5137
#define __need_va_list
5238
#define __need_va_arg
@@ -87,5 +73,3 @@
8773
#endif /* defined(__need_va_copy) */
8874

8975
#endif /* __MVS__ */
90-
91-
#endif

clang/lib/Headers/stddef.h

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,13 @@
1414
* need to use some of its interfaces. Otherwise this header provides all of
1515
* the expected interfaces.
1616
*
17-
* When clang modules are enabled, this header is a textual header. It ignores
18-
* its header guard so that multiple submodules can export its interfaces.
19-
* Take module SM with submodules A and B, whose headers both include stddef.h
20-
* When SM.A builds, __STDDEF_H will be defined. When SM.B builds, the
21-
* definition from SM.A will leak when building without local submodule
22-
* visibility. stddef.h wouldn't include any of its implementation headers, and
23-
* SM.B wouldn't import any of the stddef modules, and SM.B's `export *`
24-
* wouldn't export any stddef interfaces as expected. However, since stddef.h
25-
* ignores its header guard when building with modules, it all works as
26-
* expected.
27-
*
28-
* When clang modules are not enabled, the header guards can function in the
29-
* normal simple fashion.
17+
* When clang modules are enabled, this header is a textual header to support
18+
* the multiple include behavior. As such, it doesn't directly declare anything
19+
* so that it doesn't add duplicate declarations to all of its includers'
20+
* modules.
3021
*/
31-
#if !defined(__STDDEF_H) || __has_feature(modules) || \
32-
(defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1) || \
33-
defined(__need_ptrdiff_t) || defined(__need_size_t) || \
34-
defined(__need_rsize_t) || defined(__need_wchar_t) || \
35-
defined(__need_NULL) || defined(__need_nullptr_t) || \
36-
defined(__need_unreachable) || defined(__need_max_align_t) || \
37-
defined(__need_offsetof) || defined(__need_wint_t)
38-
3922
#if defined(__MVS__) && __has_include_next(<stddef.h>)
40-
#define __STDDEF_H
23+
#include <__stddef_macro.h>
4124
#undef __need_ptrdiff_t
4225
#undef __need_size_t
4326
#undef __need_rsize_t
@@ -57,7 +40,7 @@
5740
!defined(__need_NULL) && !defined(__need_nullptr_t) && \
5841
!defined(__need_unreachable) && !defined(__need_max_align_t) && \
5942
!defined(__need_offsetof) && !defined(__need_wint_t)
60-
#define __STDDEF_H
43+
#include <__stddef_macro.h>
6144
#define __need_ptrdiff_t
6245
#define __need_size_t
6346
/* ISO9899:2011 7.20 (C11 Annex K): Define rsize_t if __STDC_WANT_LIB_EXT1__ is
@@ -137,4 +120,3 @@ __WINT_TYPE__ directly; accommodate both by requiring __need_wint_t */
137120
#endif /* __need_wint_t */
138121

139122
#endif /* __MVS__ */
140-
#endif

llvm/utils/gn/secondary/clang/lib/Headers/BUILD.gn

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,11 @@ copy("Headers") {
105105
"__clang_hip_stdlib.h",
106106
"__stdarg___gnuc_va_list.h",
107107
"__stdarg___va_copy.h",
108+
"__stdarg_macro.h",
108109
"__stdarg_va_arg.h",
109110
"__stdarg_va_copy.h",
110111
"__stdarg_va_list.h",
112+
"__stddef_macro.h",
111113
"__stddef_max_align_t.h",
112114
"__stddef_null.h",
113115
"__stddef_nullptr_t.h",

0 commit comments

Comments
 (0)