74 lines · cpp
1// RUN: rm -rf %t2// RUN: split-file %s %t3// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t/no-lsv -I%t %t/stddef.cpp -verify4// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-local-submodule-visibility -fmodules-cache-path=%t/lsv -I%t %t/stddef.cpp -verify5 6//--- stddef.cpp7#include <b.h>8 9void *pointer = NULL;10size_t size = 0;11 12// When building with modules, a pcm is never re-imported, so re-including13// stddef.h will not re-import _Builtin_stddef.null to restore the definition of14// NULL, even though stddef.h will unconditionally include __stddef_null.h when15// building with modules.16#undef NULL17#include <stddef.h>18 19void *anotherPointer = NULL; // expected-error{{use of undeclared identifier 'NULL'}}20 21// stddef.h needs to be a `textual` header to support clients doing things like22// this.23//24// #define __need_NULL25// #include <stddef.h>26//27// As a textual header designed to be included multiple times, it can't directly28// declare anything, or those declarations would go into every module that29// included it. e.g. if stddef.h contained all of its declarations, and modules30// A and B included stddef.h, they would both have the declaration for size_t.31// That breaks Swift, which uses the module name as part of the type name, i.e.32// A.size_t and B.size_t are treated as completely different types in Swift and33// cannot be interchanged. To fix that, stddef.h (and stdarg.h) are split out34// into a separate file per __need macro that can be normal headers in explicit35// submodules. That runs into yet another wrinkle though. When modules build,36// declarations from previous submodules leak into subsequent ones when not37// using local submodule visibility. Consider if stddef.h did the normal thing.38//39// #ifndef __STDDEF_H40// #define __STDDEF_H41// // include all of the sub-headers42// #endif43//44// When SM builds without local submodule visibility, it will precompile a.h45// first. When it gets to b.h, the __STDDEF_H declaration from precompiling a.h46// will leak, and so when b.h includes stddef.h, it won't include any of its47// sub-headers, and SM.B will thus not import _Builtin_stddef or make any of its48// submodules visible. Precompiling b.h will be fine since it sees all of the49// declarations from a.h including stddef.h, but clients that only include b.h50// will not see any of the stddef.h types. stddef.h thus has to make sure to51// always include the necessary sub-headers, even if they've been included52// already. They all have their own header guards to allow this.53// __stddef_null.h is extra special, so this test makes sure to cover NULL plus54// one of the normal stddef.h types.55 56//--- module.modulemap57module SM {58 module A {59 header "a.h"60 export *61 }62 63 module B {64 header "b.h"65 export *66 }67}68 69//--- a.h70#include <stddef.h>71 72//--- b.h73#include <stddef.h>74