diff --git a/clang/include/clang/Serialization/ASTWriter.h b/clang/include/clang/Serialization/ASTWriter.h --- a/clang/include/clang/Serialization/ASTWriter.h +++ b/clang/include/clang/Serialization/ASTWriter.h @@ -475,7 +475,7 @@ createSignature(StringRef AllBytes, StringRef ASTBlockBytes); void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts, - bool Modules); + std::set &AffectingModuleMaps); void WriteSourceManagerBlock(SourceManager &SourceMgr, const Preprocessor &PP); void WritePreprocessor(const Preprocessor &PP, bool IsModule); diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -149,6 +149,47 @@ namespace { +std::string ModuleMapFilePathForModule(ModuleMap &Map, Module *GivenModule) { + if (GivenModule->PresumedModuleMapFile.empty()) { + auto *ModuleMapFile = Map.getModuleMapFileForUniquing(GivenModule); + if (!ModuleMapFile) { + return std::string(); + } + return std::string(ModuleMapFile->getName()); + } else { + return GivenModule->PresumedModuleMapFile; + } +} + +std::set GetAllModulemaps(ModuleMap &Map, Module *RootModule) { + std::set ModuleMaps{}; + std::set ProcessedModules; + std::set ModulesToProcess{RootModule}; + while (!ModulesToProcess.empty()) { + auto CurrentModule = ModulesToProcess.begin(); + ProcessedModules.insert(*CurrentModule); + + const std::string CurrentModuleMapFile = + ModuleMapFilePathForModule(Map, *CurrentModule); + if (CurrentModuleMapFile.empty()) { + ModulesToProcess.erase(CurrentModule); + continue; + } + + ModuleMaps.insert(CurrentModuleMapFile); + + for (auto *ImportedModule : (*CurrentModule)->Imports) { + if (!ImportedModule || + ProcessedModules.find(ImportedModule) != ProcessedModules.end()) { + continue; + } + ModulesToProcess.insert(ImportedModule); + } + ModulesToProcess.erase(CurrentModule); + } + return ModuleMaps; +} + class ASTTypeWriter { ASTWriter &Writer; ASTWriter::RecordData Record; @@ -1396,9 +1437,15 @@ Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir); } + std::set AffectingModulemaps; + if (WritingModule) { + AffectingModulemaps = GetAllModulemaps( + PP.getHeaderSearchInfo().getModuleMap(), WritingModule); + } + WriteInputFiles(Context.SourceMgr, PP.getHeaderSearchInfo().getHeaderSearchOpts(), - PP.getLangOpts().Modules); + AffectingModulemaps); Stream.ExitBlock(); } @@ -1418,7 +1465,7 @@ void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts, - bool Modules) { + std::set &AffectingModuleMaps) { using namespace llvm; Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4); @@ -1458,6 +1505,15 @@ if (!Cache->OrigEntry) continue; + if (isModuleMap(File.getFileCharacteristic()) && + !isSystem(File.getFileCharacteristic()) && + !AffectingModuleMaps.empty() && + AffectingModuleMaps.find(std::string(Cache->OrigEntry->getName())) == + AffectingModuleMaps.end()) { + // Do not emit modulemaps that do not affect current module. + continue; + } + InputFileEntry Entry; Entry.File = Cache->OrigEntry; Entry.IsSystemFile = isSystem(File.getFileCharacteristic()); @@ -1971,11 +2027,15 @@ Record.push_back(SLoc->getOffset() - 2); if (SLoc->isFile()) { const SrcMgr::FileInfo &File = SLoc->getFile(); + const SrcMgr::ContentCache *Content = &File.getContentCache(); + if (Content->OrigEntry && InputFileIDs[Content->OrigEntry] == 0) { + // Do not emit files that were not listed as inputs. + continue; + } AddSourceLocation(File.getIncludeLoc(), Record); Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding Record.push_back(File.hasLineDirectives()); - const SrcMgr::ContentCache *Content = &File.getContentCache(); bool EmitBlob = false; if (Content->OrigEntry) { assert(Content->OrigEntry == Content->ContentsEntry &&