comparison tools/clang/lib/Lex/PPLexerChange.cpp @ 126:c4cc77a799c9

fix
author mir3636
date Mon, 04 Dec 2017 21:20:52 +0900
parents 36195a0db682
children 7c836c76d71d
comparison
equal deleted inserted replaced
125:56c5119fbcd2 126:c4cc77a799c9
38 return IncludeMacroStack.empty(); 38 return IncludeMacroStack.empty();
39 39
40 // If there are any stacked lexers, we're in a #include. 40 // If there are any stacked lexers, we're in a #include.
41 assert(IsFileLexer(IncludeMacroStack[0]) && 41 assert(IsFileLexer(IncludeMacroStack[0]) &&
42 "Top level include stack isn't our primary lexer?"); 42 "Top level include stack isn't our primary lexer?");
43 for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i) 43 return std::none_of(
44 if (IsFileLexer(IncludeMacroStack[i])) 44 IncludeMacroStack.begin() + 1, IncludeMacroStack.end(),
45 return false; 45 [&](const IncludeStackInfo &ISI) -> bool { return IsFileLexer(ISI); });
46 return true;
47 } 46 }
48 47
49 /// getCurrentLexer - Return the current file lexer being lexed from. Note 48 /// getCurrentLexer - Return the current file lexer being lexed from. Note
50 /// that this ignores any potentially active macro expansions and _Pragma 49 /// that this ignores any potentially active macro expansions and _Pragma
51 /// expansions going on at the time. 50 /// expansions going on at the time.
52 PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { 51 PreprocessorLexer *Preprocessor::getCurrentFileLexer() const {
53 if (IsFileLexer()) 52 if (IsFileLexer())
54 return CurPPLexer; 53 return CurPPLexer;
55 54
56 // Look for a stacked lexer. 55 // Look for a stacked lexer.
57 for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { 56 for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
58 const IncludeStackInfo& ISI = IncludeMacroStack[i-1];
59 if (IsFileLexer(ISI)) 57 if (IsFileLexer(ISI))
60 return ISI.ThePPLexer; 58 return ISI.ThePPLexer;
61 } 59 }
62 return nullptr; 60 return nullptr;
63 } 61 }
116 PushIncludeMacroStack(); 114 PushIncludeMacroStack();
117 115
118 CurLexer.reset(TheLexer); 116 CurLexer.reset(TheLexer);
119 CurPPLexer = TheLexer; 117 CurPPLexer = TheLexer;
120 CurDirLookup = CurDir; 118 CurDirLookup = CurDir;
121 CurSubmodule = nullptr; 119 CurLexerSubmodule = nullptr;
122 if (CurLexerKind != CLK_LexAfterModuleImport) 120 if (CurLexerKind != CLK_LexAfterModuleImport)
123 CurLexerKind = CLK_Lexer; 121 CurLexerKind = CLK_Lexer;
124 122
125 // Notify the client, if desired, that we are in a new source file. 123 // Notify the client, if desired, that we are in a new source file.
126 if (Callbacks && !CurLexer->Is_PragmaLexer) { 124 if (Callbacks && !CurLexer->Is_PragmaLexer) {
141 PushIncludeMacroStack(); 139 PushIncludeMacroStack();
142 140
143 CurDirLookup = CurDir; 141 CurDirLookup = CurDir;
144 CurPTHLexer.reset(PL); 142 CurPTHLexer.reset(PL);
145 CurPPLexer = CurPTHLexer.get(); 143 CurPPLexer = CurPTHLexer.get();
146 CurSubmodule = nullptr; 144 CurLexerSubmodule = nullptr;
147 if (CurLexerKind != CLK_LexAfterModuleImport) 145 if (CurLexerKind != CLK_LexAfterModuleImport)
148 CurLexerKind = CLK_PTHLexer; 146 CurLexerKind = CLK_PTHLexer;
149 147
150 // Notify the client, if desired, that we are in a new source file. 148 // Notify the client, if desired, that we are in a new source file.
151 if (Callbacks) { 149 if (Callbacks) {
286 } 284 }
287 285
288 return EndPos; 286 return EndPos;
289 } 287 }
290 288
289 static void collectAllSubModulesWithUmbrellaHeader(
290 const Module &Mod, SmallVectorImpl<const Module *> &SubMods) {
291 if (Mod.getUmbrellaHeader())
292 SubMods.push_back(&Mod);
293 for (auto *M : Mod.submodules())
294 collectAllSubModulesWithUmbrellaHeader(*M, SubMods);
295 }
296
297 void Preprocessor::diagnoseMissingHeaderInUmbrellaDir(const Module &Mod) {
298 assert(Mod.getUmbrellaHeader() && "Module must use umbrella header");
299 SourceLocation StartLoc =
300 SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
301 if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header, StartLoc))
302 return;
303
304 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
305 const DirectoryEntry *Dir = Mod.getUmbrellaDir().Entry;
306 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
307 std::error_code EC;
308 for (vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC), End;
309 Entry != End && !EC; Entry.increment(EC)) {
310 using llvm::StringSwitch;
311
312 // Check whether this entry has an extension typically associated with
313 // headers.
314 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->getName()))
315 .Cases(".h", ".H", ".hh", ".hpp", true)
316 .Default(false))
317 continue;
318
319 if (const FileEntry *Header = getFileManager().getFile(Entry->getName()))
320 if (!getSourceManager().hasFileInfo(Header)) {
321 if (!ModMap.isHeaderInUnavailableModule(Header)) {
322 // Find the relative path that would access this header.
323 SmallString<128> RelativePath;
324 computeRelativePath(FileMgr, Dir, Header, RelativePath);
325 Diag(StartLoc, diag::warn_uncovered_module_header)
326 << Mod.getFullModuleName() << RelativePath;
327 }
328 }
329 }
330 }
291 331
292 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of 332 /// HandleEndOfFile - This callback is invoked when the lexer hits the end of
293 /// the current file. This either returns the EOF token or pops a level off 333 /// the current file. This either returns the EOF token or pops a level off
294 /// the include stack and keeps going. 334 /// the include stack and keeps going.
295 bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { 335 bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
296 assert(!CurTokenLexer && 336 assert(!CurTokenLexer &&
297 "Ending a file when currently in a macro!"); 337 "Ending a file when currently in a macro!");
338
339 // If we have an unclosed module region from a pragma at the end of a
340 // module, complain and close it now.
341 // FIXME: This is not correct if we are building a module from PTH.
342 const bool LeavingSubmodule = CurLexer && CurLexerSubmodule;
343 if ((LeavingSubmodule || IncludeMacroStack.empty()) &&
344 !BuildingSubmoduleStack.empty() &&
345 BuildingSubmoduleStack.back().IsPragma) {
346 Diag(BuildingSubmoduleStack.back().ImportLoc,
347 diag::err_pp_module_begin_without_module_end);
348 Module *M = LeaveSubmodule(/*ForPragma*/true);
349
350 Result.startToken();
351 const char *EndPos = getCurLexerEndPos();
352 CurLexer->BufferPtr = EndPos;
353 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
354 Result.setAnnotationEndLoc(Result.getLocation());
355 Result.setAnnotationValue(M);
356 return true;
357 }
298 358
299 // See if this file had a controlling macro. 359 // See if this file had a controlling macro.
300 if (CurPPLexer) { // Not ending a macro, ignore it. 360 if (CurPPLexer) { // Not ending a macro, ignore it.
301 if (const IdentifierInfo *ControllingMacro = 361 if (const IdentifierInfo *ControllingMacro =
302 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) { 362 CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) {
303 // Okay, this has a controlling macro, remember in HeaderFileInfo. 363 // Okay, this has a controlling macro, remember in HeaderFileInfo.
304 if (const FileEntry *FE = CurPPLexer->getFileEntry()) { 364 if (const FileEntry *FE = CurPPLexer->getFileEntry()) {
305 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); 365 HeaderInfo.SetFileControllingMacro(FE, ControllingMacro);
306 if (MacroInfo *MI = 366 if (MacroInfo *MI =
307 getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro))) { 367 getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro)))
308 MI->UsedForHeaderGuard = true; 368 MI->setUsedForHeaderGuard(true);
309 }
310 if (const IdentifierInfo *DefinedMacro = 369 if (const IdentifierInfo *DefinedMacro =
311 CurPPLexer->MIOpt.GetDefinedMacro()) { 370 CurPPLexer->MIOpt.GetDefinedMacro()) {
312 if (!isMacroDefined(ControllingMacro) && 371 if (!isMacroDefined(ControllingMacro) &&
313 DefinedMacro != ControllingMacro && 372 DefinedMacro != ControllingMacro &&
314 HeaderInfo.FirstTimeLexingFile(FE)) { 373 HeaderInfo.FirstTimeLexingFile(FE)) {
396 SourceMgr.local_sloc_entry_size() - 455 SourceMgr.local_sloc_entry_size() -
397 CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/; 456 CurPPLexer->getInitialNumSLocEntries() + 1/*#include'd file*/;
398 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs); 457 SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs);
399 } 458 }
400 459
460 bool ExitedFromPredefinesFile = false;
401 FileID ExitedFID; 461 FileID ExitedFID;
402 if (Callbacks && !isEndOfMacro && CurPPLexer) 462 if (!isEndOfMacro && CurPPLexer) {
403 ExitedFID = CurPPLexer->getFileID(); 463 ExitedFID = CurPPLexer->getFileID();
404 464
405 bool LeavingSubmodule = CurSubmodule && CurLexer; 465 assert(PredefinesFileID.isValid() &&
466 "HandleEndOfFile is called before PredefinesFileId is set");
467 ExitedFromPredefinesFile = (PredefinesFileID == ExitedFID);
468 }
469
406 if (LeavingSubmodule) { 470 if (LeavingSubmodule) {
471 // We're done with this submodule.
472 Module *M = LeaveSubmodule(/*ForPragma*/false);
473
407 // Notify the parser that we've left the module. 474 // Notify the parser that we've left the module.
408 const char *EndPos = getCurLexerEndPos(); 475 const char *EndPos = getCurLexerEndPos();
409 Result.startToken(); 476 Result.startToken();
410 CurLexer->BufferPtr = EndPos; 477 CurLexer->BufferPtr = EndPos;
411 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end); 478 CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end);
412 Result.setAnnotationEndLoc(Result.getLocation()); 479 Result.setAnnotationEndLoc(Result.getLocation());
413 Result.setAnnotationValue(CurSubmodule); 480 Result.setAnnotationValue(M);
414
415 // We're done with this submodule.
416 LeaveSubmodule();
417 } 481 }
418 482
419 // We're done with the #included file. 483 // We're done with the #included file.
420 RemoveTopOfLexerStack(); 484 RemoveTopOfLexerStack();
485
421 // Propagate info about start-of-line/leading white-space/etc. 486 // Propagate info about start-of-line/leading white-space/etc.
422 PropagateLineStartLeadingSpaceInfo(Result); 487 PropagateLineStartLeadingSpaceInfo(Result);
423 488
424 // Notify the client, if desired, that we are in a new source file. 489 // Notify the client, if desired, that we are in a new source file.
425 if (Callbacks && !isEndOfMacro && CurPPLexer) { 490 if (Callbacks && !isEndOfMacro && CurPPLexer) {
426 SrcMgr::CharacteristicKind FileType = 491 SrcMgr::CharacteristicKind FileType =
427 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation()); 492 SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation());
428 Callbacks->FileChanged(CurPPLexer->getSourceLocation(), 493 Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
429 PPCallbacks::ExitFile, FileType, ExitedFID); 494 PPCallbacks::ExitFile, FileType, ExitedFID);
430 } 495 }
496
431 #ifndef noCbC 497 #ifndef noCbC
432 if (SavedTokenFlag && IncludeMacroStack.size() == SavedDepth){ 498 if (SavedTokenFlag && IncludeMacroStack.size() == SavedDepth){
433 Result = SavedToken; 499 Result = SavedToken;
434 SavedTokenFlag = false; 500 SavedTokenFlag = false;
435 if (CurLexer->ParsingPreprocessorDirective) { 501 if (CurLexer->ParsingPreprocessorDirective) {
443 CurLexer->IsAtStartOfLine = true; 509 CurLexer->IsAtStartOfLine = true;
444 CurLexer->IsAtPhysicalStartOfLine = true; 510 CurLexer->IsAtPhysicalStartOfLine = true;
445 } 511 }
446 return true; 512 return true;
447 } 513 }
448 #endif 514 #endif
515
516 // Restore conditional stack from the preamble right after exiting from the
517 // predefines file.
518 if (ExitedFromPredefinesFile)
519 replayPreambleConditionalStack();
520
449 // Client should lex another token unless we generated an EOM. 521 // Client should lex another token unless we generated an EOM.
450 return LeavingSubmodule; 522 return LeavingSubmodule;
451 } 523 }
452 524
453 // If this is the end of the main file, form an EOF token. 525 // If this is the end of the main file, form an EOF token.
489 I!=E; ++I) 561 I!=E; ++I)
490 Diag(*I, diag::pp_macro_not_used); 562 Diag(*I, diag::pp_macro_not_used);
491 } 563 }
492 564
493 // If we are building a module that has an umbrella header, make sure that 565 // If we are building a module that has an umbrella header, make sure that
494 // each of the headers within the directory covered by the umbrella header 566 // each of the headers within the directory, including all submodules, is
495 // was actually included by the umbrella header. 567 // covered by the umbrella header was actually included by the umbrella
568 // header.
496 if (Module *Mod = getCurrentModule()) { 569 if (Module *Mod = getCurrentModule()) {
497 if (Mod->getUmbrellaHeader()) { 570 llvm::SmallVector<const Module *, 4> AllMods;
498 SourceLocation StartLoc 571 collectAllSubModulesWithUmbrellaHeader(*Mod, AllMods);
499 = SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); 572 for (auto *M : AllMods)
500 573 diagnoseMissingHeaderInUmbrellaDir(*M);
501 if (!getDiagnostics().isIgnored(diag::warn_uncovered_module_header,
502 StartLoc)) {
503 ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap();
504 const DirectoryEntry *Dir = Mod->getUmbrellaDir().Entry;
505 vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
506 std::error_code EC;
507 for (vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC), End;
508 Entry != End && !EC; Entry.increment(EC)) {
509 using llvm::StringSwitch;
510
511 // Check whether this entry has an extension typically associated with
512 // headers.
513 if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->getName()))
514 .Cases(".h", ".H", ".hh", ".hpp", true)
515 .Default(false))
516 continue;
517
518 if (const FileEntry *Header =
519 getFileManager().getFile(Entry->getName()))
520 if (!getSourceManager().hasFileInfo(Header)) {
521 if (!ModMap.isHeaderInUnavailableModule(Header)) {
522 // Find the relative path that would access this header.
523 SmallString<128> RelativePath;
524 computeRelativePath(FileMgr, Dir, Header, RelativePath);
525 Diag(StartLoc, diag::warn_uncovered_module_header)
526 << Mod->getFullModuleName() << RelativePath;
527 }
528 }
529 }
530 }
531 }
532 } 574 }
533 575
534 return true; 576 return true;
535 } 577 }
536 578
580 // We handle this by scanning for the closest real lexer, switching it to 622 // We handle this by scanning for the closest real lexer, switching it to
581 // raw mode and preprocessor mode. This will cause it to return \n as an 623 // raw mode and preprocessor mode. This will cause it to return \n as an
582 // explicit EOD token. 624 // explicit EOD token.
583 PreprocessorLexer *FoundLexer = nullptr; 625 PreprocessorLexer *FoundLexer = nullptr;
584 bool LexerWasInPPMode = false; 626 bool LexerWasInPPMode = false;
585 for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) { 627 for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) {
586 IncludeStackInfo &ISI = *(IncludeMacroStack.end()-i-1);
587 if (ISI.ThePPLexer == nullptr) continue; // Scan for a real lexer. 628 if (ISI.ThePPLexer == nullptr) continue; // Scan for a real lexer.
588 629
589 // Once we find a real lexer, mark it as raw mode (disabling macro 630 // Once we find a real lexer, mark it as raw mode (disabling macro
590 // expansions) and preprocessor mode (return EOD). We know that the lexer 631 // expansions) and preprocessor mode (return EOD). We know that the lexer
591 // was *not* in raw mode before, because the macro that the comment came 632 // was *not* in raw mode before, because the macro that the comment came
633 // active (an active lexer would return EOD at EOF if there was no \n in 674 // active (an active lexer would return EOD at EOF if there was no \n in
634 // preprocessor directive mode), so just return EOF as our token. 675 // preprocessor directive mode), so just return EOF as our token.
635 assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode"); 676 assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode");
636 } 677 }
637 678
638 void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc) { 679 void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc,
680 bool ForPragma) {
639 if (!getLangOpts().ModulesLocalVisibility) { 681 if (!getLangOpts().ModulesLocalVisibility) {
640 // Just track that we entered this submodule. 682 // Just track that we entered this submodule.
641 BuildingSubmoduleStack.push_back( 683 BuildingSubmoduleStack.push_back(
642 BuildingSubmoduleInfo(M, ImportLoc, CurSubmoduleState)); 684 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
685 PendingModuleMacroNames.size()));
643 return; 686 return;
644 } 687 }
645 688
646 // Resolve as much of the module definition as we can now, before we enter 689 // Resolve as much of the module definition as we can now, before we enter
647 // one of its headers. 690 // one of its headers.
679 } 722 }
680 } 723 }
681 724
682 // Track that we entered this module. 725 // Track that we entered this module.
683 BuildingSubmoduleStack.push_back( 726 BuildingSubmoduleStack.push_back(
684 BuildingSubmoduleInfo(M, ImportLoc, CurSubmoduleState)); 727 BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState,
728 PendingModuleMacroNames.size()));
685 729
686 // Switch to this submodule as the current submodule. 730 // Switch to this submodule as the current submodule.
687 CurSubmoduleState = &State; 731 CurSubmoduleState = &State;
688 732
689 // This module is visible to itself. 733 // This module is visible to itself.
690 if (FirstTime) 734 if (FirstTime)
691 makeModuleVisible(M, ImportLoc); 735 makeModuleVisible(M, ImportLoc);
692 } 736 }
693 737
694 void Preprocessor::LeaveSubmodule() { 738 bool Preprocessor::needModuleMacros() const {
739 // If we're not within a submodule, we never need to create ModuleMacros.
740 if (BuildingSubmoduleStack.empty())
741 return false;
742 // If we are tracking module macro visibility even for textually-included
743 // headers, we need ModuleMacros.
744 if (getLangOpts().ModulesLocalVisibility)
745 return true;
746 // Otherwise, we only need module macros if we're actually compiling a module
747 // interface.
748 return getLangOpts().isCompilingModule();
749 }
750
751 Module *Preprocessor::LeaveSubmodule(bool ForPragma) {
752 if (BuildingSubmoduleStack.empty() ||
753 BuildingSubmoduleStack.back().IsPragma != ForPragma) {
754 assert(ForPragma && "non-pragma module enter/leave mismatch");
755 return nullptr;
756 }
757
695 auto &Info = BuildingSubmoduleStack.back(); 758 auto &Info = BuildingSubmoduleStack.back();
696 759
697 Module *LeavingMod = Info.M; 760 Module *LeavingMod = Info.M;
698 SourceLocation ImportLoc = Info.ImportLoc; 761 SourceLocation ImportLoc = Info.ImportLoc;
699 762
763 if (!needModuleMacros() ||
764 (!getLangOpts().ModulesLocalVisibility &&
765 LeavingMod->getTopLevelModuleName() != getLangOpts().CurrentModule)) {
766 // If we don't need module macros, or this is not a module for which we
767 // are tracking macro visibility, don't build any, and preserve the list
768 // of pending names for the surrounding submodule.
769 BuildingSubmoduleStack.pop_back();
770 makeModuleVisible(LeavingMod, ImportLoc);
771 return LeavingMod;
772 }
773
700 // Create ModuleMacros for any macros defined in this submodule. 774 // Create ModuleMacros for any macros defined in this submodule.
701 for (auto &Macro : CurSubmoduleState->Macros) { 775 llvm::SmallPtrSet<const IdentifierInfo*, 8> VisitedMacros;
702 auto *II = const_cast<IdentifierInfo*>(Macro.first); 776 for (unsigned I = Info.OuterPendingModuleMacroNames;
777 I != PendingModuleMacroNames.size(); ++I) {
778 auto *II = const_cast<IdentifierInfo*>(PendingModuleMacroNames[I]);
779 if (!VisitedMacros.insert(II).second)
780 continue;
781
782 auto MacroIt = CurSubmoduleState->Macros.find(II);
783 if (MacroIt == CurSubmoduleState->Macros.end())
784 continue;
785 auto &Macro = MacroIt->second;
703 786
704 // Find the starting point for the MacroDirective chain in this submodule. 787 // Find the starting point for the MacroDirective chain in this submodule.
705 MacroDirective *OldMD = nullptr; 788 MacroDirective *OldMD = nullptr;
706 if (getLangOpts().ModulesLocalVisibility) { 789 auto *OldState = Info.OuterSubmoduleState;
790 if (getLangOpts().ModulesLocalVisibility)
791 OldState = &NullSubmoduleState;
792 if (OldState && OldState != CurSubmoduleState) {
707 // FIXME: It'd be better to start at the state from when we most recently 793 // FIXME: It'd be better to start at the state from when we most recently
708 // entered this submodule, but it doesn't really matter. 794 // entered this submodule, but it doesn't really matter.
709 auto &PredefMacros = NullSubmoduleState.Macros; 795 auto &OldMacros = OldState->Macros;
710 auto PredefMacroIt = PredefMacros.find(Macro.first); 796 auto OldMacroIt = OldMacros.find(II);
711 if (PredefMacroIt == PredefMacros.end()) 797 if (OldMacroIt == OldMacros.end())
712 OldMD = nullptr; 798 OldMD = nullptr;
713 else 799 else
714 OldMD = PredefMacroIt->second.getLatest(); 800 OldMD = OldMacroIt->second.getLatest();
715 } 801 }
716 802
717 // This module may have exported a new macro. If so, create a ModuleMacro 803 // This module may have exported a new macro. If so, create a ModuleMacro
718 // representing that fact. 804 // representing that fact.
719 bool ExplicitlyPublic = false; 805 bool ExplicitlyPublic = false;
720 for (auto *MD = Macro.second.getLatest(); MD != OldMD; 806 for (auto *MD = Macro.getLatest(); MD != OldMD; MD = MD->getPrevious()) {
721 MD = MD->getPrevious()) {
722 assert(MD && "broken macro directive chain"); 807 assert(MD && "broken macro directive chain");
723
724 // Stop on macros defined in other submodules we #included along the way.
725 // There's no point doing this if we're tracking local submodule
726 // visibility, since there can be no such directives in our list.
727 if (!getLangOpts().ModulesLocalVisibility) {
728 Module *Mod = getModuleContainingLocation(MD->getLocation());
729 if (Mod != LeavingMod)
730 break;
731 }
732 808
733 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) { 809 if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
734 // The latest visibility directive for a name in a submodule affects 810 // The latest visibility directive for a name in a submodule affects
735 // all the directives that come before it. 811 // all the directives that come before it.
736 if (VisMD->isPublic()) 812 if (VisMD->isPublic())
746 // FIXME: Issue a warning if multiple headers for the same submodule 822 // FIXME: Issue a warning if multiple headers for the same submodule
747 // define a macro, rather than silently ignoring all but the first. 823 // define a macro, rather than silently ignoring all but the first.
748 bool IsNew; 824 bool IsNew;
749 // Don't bother creating a module macro if it would represent a #undef 825 // Don't bother creating a module macro if it would represent a #undef
750 // that doesn't override anything. 826 // that doesn't override anything.
751 if (Def || !Macro.second.getOverriddenMacros().empty()) 827 if (Def || !Macro.getOverriddenMacros().empty())
752 addModuleMacro(LeavingMod, II, Def, 828 addModuleMacro(LeavingMod, II, Def,
753 Macro.second.getOverriddenMacros(), IsNew); 829 Macro.getOverriddenMacros(), IsNew);
830
831 if (!getLangOpts().ModulesLocalVisibility) {
832 // This macro is exposed to the rest of this compilation as a
833 // ModuleMacro; we don't need to track its MacroDirective any more.
834 Macro.setLatest(nullptr);
835 Macro.setOverriddenMacros(*this, {});
836 }
754 break; 837 break;
755 } 838 }
756 } 839 }
757 } 840 }
841 PendingModuleMacroNames.resize(Info.OuterPendingModuleMacroNames);
758 842
759 // FIXME: Before we leave this submodule, we should parse all the other 843 // FIXME: Before we leave this submodule, we should parse all the other
760 // headers within it. Otherwise, we're left with an inconsistent state 844 // headers within it. Otherwise, we're left with an inconsistent state
761 // where we've made the module visible but don't yet have its complete 845 // where we've made the module visible but don't yet have its complete
762 // contents. 846 // contents.
767 851
768 BuildingSubmoduleStack.pop_back(); 852 BuildingSubmoduleStack.pop_back();
769 853
770 // A nested #include makes the included submodule visible. 854 // A nested #include makes the included submodule visible.
771 makeModuleVisible(LeavingMod, ImportLoc); 855 makeModuleVisible(LeavingMod, ImportLoc);
772 } 856 return LeavingMod;
857 }