@@ -1983,6 +1983,10 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1983
1983
EnterAnnotationToken (SourceRange (HashLoc, EndLoc),
1984
1984
tok::annot_module_begin, Action.ModuleForHeader );
1985
1985
break ;
1986
+ case ImportAction::HeaderUnitImport:
1987
+ EnterAnnotationToken (SourceRange (HashLoc, EndLoc), tok::annot_header_unit,
1988
+ Action.ModuleForHeader );
1989
+ break ;
1986
1990
case ImportAction::ModuleImport:
1987
1991
EnterAnnotationToken (SourceRange (HashLoc, EndLoc),
1988
1992
tok::annot_module_include, Action.ModuleForHeader );
@@ -2191,6 +2195,17 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2191
2195
// known to have no effect beyond its effect on module visibility -- that is,
2192
2196
// if it's got an include guard that is already defined, set to Import if it
2193
2197
// is a modular header we've already built and should import.
2198
+
2199
+ // For C++20 Modules
2200
+ // [cpp.include]/7 If the header identified by the header-name denotes an
2201
+ // importable header, it is implementation-defined whether the #include
2202
+ // preprocessing directive is instead replaced by an import directive.
2203
+ // For this implementation, the translation is permitted when we are parsing
2204
+ // the Global Module Fragment, and not otherwise (the cases where it would be
2205
+ // valid to replace an include with an import are highly constrained once in
2206
+ // named module purview; this choice avoids considerable complexity in
2207
+ // determining valid cases).
2208
+
2194
2209
enum { Enter, Import, Skip, IncludeLimitReached } Action = Enter;
2195
2210
2196
2211
if (PPOpts->SingleFileParseMode )
@@ -2203,13 +2218,34 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2203
2218
alreadyIncluded (*File))
2204
2219
Action = IncludeLimitReached;
2205
2220
2221
+ bool MaybeTranslateInclude = Action == Enter && File && SuggestedModule &&
2222
+ !isForModuleBuilding (SuggestedModule.getModule (),
2223
+ getLangOpts ().CurrentModule ,
2224
+ getLangOpts ().ModuleName );
2225
+
2226
+ // FIXME: We do not have a good way to disambiguate C++ clang modules from
2227
+ // C++ standard modules (other than use/non-use of Header Units).
2228
+ Module *SM = SuggestedModule.getModule ();
2229
+ // Maybe a usable Header Unit
2230
+ bool UsableHeaderUnit = false ;
2231
+ if (getLangOpts ().CPlusPlusModules && SM && SM->isHeaderUnit ()) {
2232
+ if (TrackGMFState.inGMF () || IsImportDecl)
2233
+ UsableHeaderUnit = true ;
2234
+ else if (!IsImportDecl) {
2235
+ // This is a Header Unit that we do not include-translate
2236
+ SuggestedModule = ModuleMap::KnownHeader ();
2237
+ SM = nullptr ;
2238
+ }
2239
+ }
2240
+ // Maybe a usable clang header module.
2241
+ bool UsableHeaderModule =
2242
+ (getLangOpts ().CPlusPlusModules || getLangOpts ().Modules ) && SM &&
2243
+ !SM->isHeaderUnit ();
2244
+
2206
2245
// Determine whether we should try to import the module for this #include, if
2207
2246
// there is one. Don't do so if precompiled module support is disabled or we
2208
2247
// are processing this module textually (because we're building the module).
2209
- if (Action == Enter && File && SuggestedModule && getLangOpts ().Modules &&
2210
- !isForModuleBuilding (SuggestedModule.getModule (),
2211
- getLangOpts ().CurrentModule ,
2212
- getLangOpts ().ModuleName )) {
2248
+ if (MaybeTranslateInclude && (UsableHeaderUnit || UsableHeaderModule)) {
2213
2249
// If this include corresponds to a module but that module is
2214
2250
// unavailable, diagnose the situation and bail out.
2215
2251
// FIXME: Remove this; loadModule does the same check (but produces
@@ -2226,7 +2262,7 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2226
2262
// FIXME: Should we have a second loadModule() overload to avoid this
2227
2263
// extra lookup step?
2228
2264
SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2 > Path;
2229
- for (Module *Mod = SuggestedModule. getModule () ; Mod; Mod = Mod->Parent )
2265
+ for (Module *Mod = SM ; Mod; Mod = Mod->Parent )
2230
2266
Path.push_back (std::make_pair (getIdentifierInfo (Mod->Name ),
2231
2267
FilenameTok.getLocation ()));
2232
2268
std::reverse (Path.begin (), Path.end ());
@@ -2293,17 +2329,23 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2293
2329
// Ask HeaderInfo if we should enter this #include file. If not, #including
2294
2330
// this file will have no effect.
2295
2331
if (Action == Enter && File &&
2296
- !HeaderInfo.ShouldEnterIncludeFile (
2297
- *this , &File->getFileEntry (), EnterOnce, getLangOpts ().Modules ,
2298
- SuggestedModule.getModule (), IsFirstIncludeOfFile)) {
2332
+ !HeaderInfo.ShouldEnterIncludeFile (*this , &File->getFileEntry (),
2333
+ EnterOnce, getLangOpts ().Modules , SM,
2334
+ IsFirstIncludeOfFile)) {
2335
+ // C++ standard modules:
2336
+ // If we are not in the GMF, then we textually include only
2337
+ // clang modules:
2299
2338
// Even if we've already preprocessed this header once and know that we
2300
2339
// don't need to see its contents again, we still need to import it if it's
2301
2340
// modular because we might not have imported it from this submodule before.
2302
2341
//
2303
2342
// FIXME: We don't do this when compiling a PCH because the AST
2304
2343
// serialization layer can't cope with it. This means we get local
2305
2344
// submodule visibility semantics wrong in that case.
2306
- Action = (SuggestedModule && !getLangOpts ().CompilingPCH ) ? Import : Skip;
2345
+ if (UsableHeaderUnit && !getLangOpts ().CompilingPCH )
2346
+ Action = TrackGMFState.inGMF () ? Import : Skip;
2347
+ else
2348
+ Action = (SuggestedModule && !getLangOpts ().CompilingPCH ) ? Import : Skip;
2307
2349
}
2308
2350
2309
2351
// Check for circular inclusion of the main file.
@@ -2440,8 +2482,8 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2440
2482
switch (Action) {
2441
2483
case Skip:
2442
2484
// If we don't need to enter the file, stop now.
2443
- if (Module *M = SuggestedModule. getModule () )
2444
- return {ImportAction::SkippedModuleImport, M };
2485
+ if (SM )
2486
+ return {ImportAction::SkippedModuleImport, SM };
2445
2487
return {ImportAction::None};
2446
2488
2447
2489
case IncludeLimitReached:
@@ -2451,16 +2493,15 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2451
2493
2452
2494
case Import: {
2453
2495
// If this is a module import, make it visible if needed.
2454
- Module *M = SuggestedModule.getModule ();
2455
- assert (M && " no module to import" );
2496
+ assert (SM && " no module to import" );
2456
2497
2457
- makeModuleVisible (M , EndLoc);
2498
+ makeModuleVisible (SM , EndLoc);
2458
2499
2459
2500
if (IncludeTok.getIdentifierInfo ()->getPPKeywordID () ==
2460
2501
tok::pp___include_macros)
2461
2502
return {ImportAction::None};
2462
2503
2463
- return {ImportAction::ModuleImport, M };
2504
+ return {ImportAction::ModuleImport, SM };
2464
2505
}
2465
2506
2466
2507
case Enter:
@@ -2492,13 +2533,14 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2492
2533
return {ImportAction::None};
2493
2534
2494
2535
// Determine if we're switching to building a new submodule, and which one.
2495
- if (auto *M = SuggestedModule.getModule ()) {
2496
- if (M->getTopLevelModule ()->ShadowingModule ) {
2536
+ // This does not apply for C++20 modules header units.
2537
+ if (SM && !SM->isHeaderUnit ()) {
2538
+ if (SM->getTopLevelModule ()->ShadowingModule ) {
2497
2539
// We are building a submodule that belongs to a shadowed module. This
2498
2540
// means we find header files in the shadowed module.
2499
- Diag (M ->DefinitionLoc , diag::err_module_build_shadowed_submodule)
2500
- << M ->getFullModuleName ();
2501
- Diag (M ->getTopLevelModule ()->ShadowingModule ->DefinitionLoc ,
2541
+ Diag (SM ->DefinitionLoc , diag::err_module_build_shadowed_submodule)
2542
+ << SM ->getFullModuleName ();
2543
+ Diag (SM ->getTopLevelModule ()->ShadowingModule ->DefinitionLoc ,
2502
2544
diag::note_previous_definition);
2503
2545
return {ImportAction::None};
2504
2546
}
@@ -2511,22 +2553,22 @@ Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2511
2553
// that PCH, which means we should enter the submodule. We need to teach
2512
2554
// the AST serialization layer to deal with the resulting AST.
2513
2555
if (getLangOpts ().CompilingPCH &&
2514
- isForModuleBuilding (M , getLangOpts ().CurrentModule ,
2556
+ isForModuleBuilding (SM , getLangOpts ().CurrentModule ,
2515
2557
getLangOpts ().ModuleName ))
2516
2558
return {ImportAction::None};
2517
2559
2518
2560
assert (!CurLexerSubmodule && " should not have marked this as a module yet" );
2519
- CurLexerSubmodule = M ;
2561
+ CurLexerSubmodule = SM ;
2520
2562
2521
2563
// Let the macro handling code know that any future macros are within
2522
2564
// the new submodule.
2523
- EnterSubmodule (M , EndLoc, /* ForPragma*/ false );
2565
+ EnterSubmodule (SM , EndLoc, /* ForPragma*/ false );
2524
2566
2525
2567
// Let the parser know that any future declarations are within the new
2526
2568
// submodule.
2527
2569
// FIXME: There's no point doing this if we're handling a #__include_macros
2528
2570
// directive.
2529
- return {ImportAction::ModuleBegin, M };
2571
+ return {ImportAction::ModuleBegin, SM };
2530
2572
}
2531
2573
2532
2574
assert (!IsImportDecl && " failed to diagnose missing module for import decl" );
0 commit comments