Skip to content

[clang][modules] stdarg.h and stddef.h shouldn't directly declare anything #90676

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

Merged
merged 1 commit into from
May 6, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ const HeaderMapCollector::RegexHeaderMap *getSTLPostfixHeaderMap() {
static const HeaderMapCollector::RegexHeaderMap STLPostfixHeaderMap = {
{"include/__stdarg___gnuc_va_list.h$", "<cstdarg>"},
{"include/__stdarg___va_copy.h$", "<cstdarg>"},
{"include/__stdarg_header_macro.h$", "<cstdarg>"},
{"include/__stdarg_va_arg.h$", "<cstdarg>"},
{"include/__stdarg_va_copy.h$", "<cstdarg>"},
{"include/__stdarg_va_list.h$", "<cstdarg>"},
{"include/__stddef_header_macro.h$", "<cstddef>"},
{"include/__stddef_max_align_t.h$", "<cstddef>"},
{"include/__stddef_null.h$", "<cstddef>"},
{"include/__stddef_nullptr_t.h$", "<cstddef>"},
Expand Down
2 changes: 2 additions & 0 deletions clang-tools-extra/clangd/index/CanonicalIncludes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ namespace {
const std::pair<llvm::StringRef, llvm::StringRef> IncludeMappings[] = {
{"include/__stdarg___gnuc_va_list.h", "<cstdarg>"},
{"include/__stdarg___va_copy.h", "<cstdarg>"},
{"include/__stdarg_header_macro.h", "<cstdarg>"},
{"include/__stdarg_va_arg.h", "<cstdarg>"},
{"include/__stdarg_va_copy.h", "<cstdarg>"},
{"include/__stdarg_va_list.h", "<cstdarg>"},
{"include/__stddef_header_macro.h", "<cstddef>"},
{"include/__stddef_max_align_t.h", "<cstddef>"},
{"include/__stddef_null.h", "<cstddef>"},
{"include/__stddef_nullptr_t.h", "<cstddef>"},
Expand Down
2 changes: 2 additions & 0 deletions clang/lib/Headers/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ set(core_files
stdarg.h
__stdarg___gnuc_va_list.h
__stdarg___va_copy.h
__stdarg_header_macro.h
__stdarg_va_arg.h
__stdarg_va_copy.h
__stdarg_va_list.h
stdatomic.h
stdbool.h
stdckdint.h
stddef.h
__stddef_header_macro.h
__stddef_max_align_t.h
__stddef_null.h
__stddef_nullptr_t.h
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Headers/__stdarg_header_macro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*===---- __stdarg_header_macro.h ------------------------------------------===
*
* 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 __STDARG_H
#define __STDARG_H
#endif
12 changes: 12 additions & 0 deletions clang/lib/Headers/__stddef_header_macro.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*===---- __stddef_header_macro.h ------------------------------------------===
*
* 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 __STDDEF_H
#define __STDDEF_H
#endif
9 changes: 9 additions & 0 deletions clang/lib/Headers/module.modulemap
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ module _Builtin_stdarg [system] {
export *
}

explicit module header_macro {
header "__stdarg_header_macro.h"
export *
}

explicit module va_arg {
header "__stdarg_va_arg.h"
export *
Expand Down Expand Up @@ -232,6 +237,10 @@ module _Builtin_stdbool [system] {
module _Builtin_stddef [system] {
textual header "stddef.h"

explicit module header_macro {
header "__stddef_header_macro.h"
export *
}
// __stddef_max_align_t.h is always in this module, even if
// -fbuiltin-headers-in-system-modules is passed.
explicit module max_align_t {
Expand Down
28 changes: 6 additions & 22 deletions clang/lib/Headers/stdarg.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,27 +14,13 @@
* need to use some of its interfaces. Otherwise this header provides all of
* the expected interfaces.
*
* When clang modules are enabled, this header is a textual header. It ignores
* its header guard so that multiple submodules can export its interfaces.
* Take module SM with submodules A and B, whose headers both include stdarg.h
* When SM.A builds, __STDARG_H will be defined. When SM.B builds, the
* definition from SM.A will leak when building without local submodule
* visibility. stdarg.h wouldn't include any of its implementation headers, and
* SM.B wouldn't import any of the stdarg modules, and SM.B's `export *`
* wouldn't export any stdarg interfaces as expected. However, since stdarg.h
* ignores its header guard when building with modules, it all works as
* expected.
*
* When clang modules are not enabled, the header guards can function in the
* normal simple fashion.
* When clang modules are enabled, this header is a textual header to support
* the multiple include behavior. As such, it doesn't directly declare anything
* so that it doesn't add duplicate declarations to all of its includers'
* modules.
*/
#if !defined(__STDARG_H) || __has_feature(modules) || \
defined(__need___va_list) || defined(__need_va_list) || \
defined(__need_va_arg) || defined(__need___va_copy) || \
defined(__need_va_copy)

#if defined(__MVS__) && __has_include_next(<stdarg.h>)
#define __STDARG_H
#include <__stdarg_header_macro.h>
#undef __need___va_list
#undef __need_va_list
#undef __need_va_arg
Expand All @@ -46,7 +32,7 @@
#if !defined(__need___va_list) && !defined(__need_va_list) && \
!defined(__need_va_arg) && !defined(__need___va_copy) && \
!defined(__need_va_copy)
#define __STDARG_H
#include <__stdarg_header_macro.h>
#define __need___va_list
#define __need_va_list
#define __need_va_arg
Expand Down Expand Up @@ -87,5 +73,3 @@
#endif /* defined(__need_va_copy) */

#endif /* __MVS__ */

#endif
30 changes: 6 additions & 24 deletions clang/lib/Headers/stddef.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,13 @@
* need to use some of its interfaces. Otherwise this header provides all of
* the expected interfaces.
*
* When clang modules are enabled, this header is a textual header. It ignores
* its header guard so that multiple submodules can export its interfaces.
* Take module SM with submodules A and B, whose headers both include stddef.h
* When SM.A builds, __STDDEF_H will be defined. When SM.B builds, the
* definition from SM.A will leak when building without local submodule
* visibility. stddef.h wouldn't include any of its implementation headers, and
* SM.B wouldn't import any of the stddef modules, and SM.B's `export *`
* wouldn't export any stddef interfaces as expected. However, since stddef.h
* ignores its header guard when building with modules, it all works as
* expected.
*
* When clang modules are not enabled, the header guards can function in the
* normal simple fashion.
* When clang modules are enabled, this header is a textual header to support
* the multiple include behavior. As such, it doesn't directly declare anything
* so that it doesn't add duplicate declarations to all of its includers'
* modules.
*/
#if !defined(__STDDEF_H) || __has_feature(modules) || \
(defined(__STDC_WANT_LIB_EXT1__) && __STDC_WANT_LIB_EXT1__ >= 1) || \
defined(__need_ptrdiff_t) || defined(__need_size_t) || \
defined(__need_rsize_t) || defined(__need_wchar_t) || \
defined(__need_NULL) || defined(__need_nullptr_t) || \
defined(__need_unreachable) || defined(__need_max_align_t) || \
defined(__need_offsetof) || defined(__need_wint_t)

#if defined(__MVS__) && __has_include_next(<stddef.h>)
#define __STDDEF_H
#include <__stddef_header_macro.h>
#undef __need_ptrdiff_t
#undef __need_size_t
#undef __need_rsize_t
Expand All @@ -57,7 +40,7 @@
!defined(__need_NULL) && !defined(__need_nullptr_t) && \
!defined(__need_unreachable) && !defined(__need_max_align_t) && \
!defined(__need_offsetof) && !defined(__need_wint_t)
#define __STDDEF_H
#include <__stddef_header_macro.h>
#define __need_ptrdiff_t
#define __need_size_t
/* ISO9899:2011 7.20 (C11 Annex K): Define rsize_t if __STDC_WANT_LIB_EXT1__ is
Expand Down Expand Up @@ -137,4 +120,3 @@ __WINT_TYPE__ directly; accommodate both by requiring __need_wint_t */
#endif /* __need_wint_t */

#endif /* __MVS__ */
#endif
2 changes: 2 additions & 0 deletions llvm/utils/gn/secondary/clang/lib/Headers/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ copy("Headers") {
"__clang_hip_stdlib.h",
"__stdarg___gnuc_va_list.h",
"__stdarg___va_copy.h",
"__stdarg_header_macro.h",
"__stdarg_va_arg.h",
"__stdarg_va_copy.h",
"__stdarg_va_list.h",
"__stddef_header_macro.h",
"__stddef_max_align_t.h",
"__stddef_null.h",
"__stddef_nullptr_t.h",
Expand Down
Loading