-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[clang][headers] Including stddef.h always redefines NULL #99727
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// RUN: rm -rf %t | ||
// RUN: split-file %s %t | ||
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/no-lsv -I%t %t/stddef.cpp -verify | ||
// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-local-submodule-visibility -fmodules-cache-path=%t/lsv -I%t %t/stddef.cpp -verify | ||
|
||
//--- stddef.cpp | ||
#include <b.h> | ||
|
||
void *pointer = NULL; | ||
size_t size = 0; | ||
|
||
// When building with modules, a pcm is never re-imported, so re-including | ||
// stddef.h will not re-import _Builtin_stddef.null to restore the definition of | ||
// NULL, even though stddef.h will unconditionally include __stddef_null.h when | ||
// building with modules. | ||
#undef NULL | ||
#include <stddef.h> | ||
|
||
void *anotherPointer = NULL; // expected-error{{use of undeclared identifier 'NULL'}} | ||
|
||
// stddef.h needs to be a `textual` header to support clients doing things like | ||
// this. | ||
// | ||
// #define __need_NULL | ||
// #include <stddef.h> | ||
// | ||
// As a textual header designed to be included multiple times, it can't directly | ||
// declare anything, or those declarations would go into every module that | ||
// included it. e.g. if stddef.h contained all of its declarations, and modules | ||
// A and B included stddef.h, they would both have the declaration for size_t. | ||
// That breaks Swift, which uses the module name as part of the type name, i.e. | ||
// A.size_t and B.size_t are treated as completely different types in Swift and | ||
// cannot be interchanged. To fix that, stddef.h (and stdarg.h) are split out | ||
// into a separate file per __need macro that can be normal headers in explicit | ||
// submodules. That runs into yet another wrinkle though. When modules build, | ||
// declarations from previous submodules leak into subsequent ones when not | ||
// using local submodule visibility. Consider if stddef.h did the normal thing. | ||
// | ||
// #ifndef __STDDEF_H | ||
// #define __STDDEF_H | ||
// // include all of the sub-headers | ||
// #endif | ||
// | ||
// When SM builds without local submodule visibility, it will precompile a.h | ||
// first. When it gets to b.h, the __STDDEF_H declaration from precompiling a.h | ||
// will leak, and so when b.h includes stddef.h, it won't include any of its | ||
// sub-headers, and SM.B will thus not import _Builtin_stddef or make any of its | ||
// submodules visible. Precompiling b.h will be fine since it sees all of the | ||
// declarations from a.h including stddef.h, but clients that only include b.h | ||
// will not see any of the stddef.h types. stddef.h thus has to make sure to | ||
// always include the necessary sub-headers, even if they've been included | ||
// already. They all have their own header guards to allow this. | ||
// __stddef_null.h is extra special, so this test makes sure to cover NULL plus | ||
// one of the normal stddef.h types. | ||
|
||
//--- module.modulemap | ||
module SM { | ||
module A { | ||
header "a.h" | ||
export * | ||
} | ||
|
||
module B { | ||
header "b.h" | ||
export * | ||
} | ||
} | ||
|
||
//--- a.h | ||
#include <stddef.h> | ||
|
||
//--- b.h | ||
#include <stddef.h> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.