ilyakuteev updated this revision to Diff 372563. CHANGES SINCE LAST ACTION https://reviews.llvm.org/D106876/new/
https://reviews.llvm.org/D106876 Files: clang/include/clang/Serialization/ASTWriter.h clang/lib/Serialization/ASTWriter.cpp clang/test/Modules/Inputs/AddRemoveIrrelevantModuleMap/a.modulemap clang/test/Modules/Inputs/AddRemoveIrrelevantModuleMap/b.modulemap clang/test/Modules/add-remove-irrelevant-mobile-map.m
Index: clang/test/Modules/add-remove-irrelevant-mobile-map.m =================================================================== --- /dev/null +++ clang/test/Modules/add-remove-irrelevant-mobile-map.m @@ -0,0 +1,16 @@ +// RUN: rm -rf %t +// RUN: rm -rf %t.mcp +// RUN: mkdir -p %t + +// Build with a.modulemap +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t.mcp -fdisable-module-hash -fmodule-map-file=%S/Inputs/AddRemoveIrrelevantModuleMap/a.modulemap %s -verify +// RUN: cp %t.mcp/a.pcm %t/a.pcm + +// Build without b.modulemap +// RUN: rm -rf %t.mcp +// RUN: %clang_cc1 -fmodules -fimplicit-module-maps -fmodules-cache-path=%t.mcp -fdisable-module-hash -fmodule-map-file=%S/Inputs/AddRemoveIrrelevantModuleMap/a.modulemap -fmodule-map-file=%S/Inputs/AddRemoveIrrelevantModuleMap/b.modulemap %s -verify +// RUN: not diff %t.mcp/a.pcm %t/a.pcm + +// expected-no-diagnostics + +@import a; Index: clang/test/Modules/Inputs/AddRemoveIrrelevantModuleMap/b.modulemap =================================================================== --- /dev/null +++ clang/test/Modules/Inputs/AddRemoveIrrelevantModuleMap/b.modulemap @@ -0,0 +1 @@ +module b { } Index: clang/test/Modules/Inputs/AddRemoveIrrelevantModuleMap/a.modulemap =================================================================== --- /dev/null +++ clang/test/Modules/Inputs/AddRemoveIrrelevantModuleMap/a.modulemap @@ -0,0 +1 @@ +module a { } Index: clang/lib/Serialization/ASTWriter.cpp =================================================================== --- clang/lib/Serialization/ASTWriter.cpp +++ clang/lib/Serialization/ASTWriter.cpp @@ -149,6 +149,59 @@ namespace { +std::set<const FileEntry *> GetAllModuleMaps(const HeaderSearch &HS, + Module *RootModule) { + std::set<const FileEntry *> ModuleMaps{}; + std::set<Module *> ProcessedModules; + SmallVector<Module *> ModulesToProcess{RootModule}; + + SmallVector<const FileEntry *, 16> FilesByUID; + HS.getFileMgr().GetUniqueIDMapping(FilesByUID); + + if (FilesByUID.size() > HS.header_file_size()) + FilesByUID.resize(HS.header_file_size()); + + for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) { + const FileEntry *File = FilesByUID[UID]; + if (!File) + continue; + + const HeaderFileInfo *HFI = + HS.getExistingFileInfo(File, /*WantExternal*/ false); + if (!HFI) + continue; + + const auto KnownHeaders = HS.findAllModulesForHeader(File); + for (const auto& KH: KnownHeaders) { + if (!KH.getModule()) + continue; + ModulesToProcess.push_back(KH.getModule()); + } + } + + while (!ModulesToProcess.empty()) { + auto CurrentModule = ModulesToProcess.pop_back_val(); + ProcessedModules.insert(CurrentModule); + + auto *ModuleMapFile = + HS.getModuleMap().getModuleMapFileForUniquing(CurrentModule); + if (!ModuleMapFile) { + continue; + } + + ModuleMaps.insert(ModuleMapFile); + + for (auto *ImportedModule : (CurrentModule)->Imports) { + if (!ImportedModule || + ProcessedModules.find(ImportedModule) != ProcessedModules.end()) { + continue; + } + ModulesToProcess.push_back(ImportedModule); + } + } + return ModuleMaps; +} + class ASTTypeWriter { ASTWriter &Writer; ASTWriter::RecordData Record; @@ -1396,9 +1449,15 @@ Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir); } + std::set<const FileEntry *> AffectingModuleMaps; + if (WritingModule) { + AffectingModuleMaps = + GetAllModuleMaps(PP.getHeaderSearchInfo(), WritingModule); + } + WriteInputFiles(Context.SourceMgr, PP.getHeaderSearchInfo().getHeaderSearchOpts(), - PP.getLangOpts().Modules); + AffectingModuleMaps); Stream.ExitBlock(); } @@ -1416,9 +1475,10 @@ } // namespace -void ASTWriter::WriteInputFiles(SourceManager &SourceMgr, - HeaderSearchOptions &HSOpts, - bool Modules) { +void ASTWriter::WriteInputFiles( + SourceManager &SourceMgr, + HeaderSearchOptions &HSOpts, + std::set<const FileEntry *> &AffectingModuleMaps) { using namespace llvm; Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4); @@ -1458,6 +1518,16 @@ if (!Cache->OrigEntry) continue; + if (isModuleMap(File.getFileCharacteristic()) && + !isSystem(File.getFileCharacteristic()) && + !AffectingModuleMaps.empty() && + AffectingModuleMaps.find(Cache->OrigEntry) == + AffectingModuleMaps.end()) { + SkippedModuleMaps.insert(Cache->OrigEntry); + // Do not emit modulemaps that do not affect current module. + continue; + } + InputFileEntry Entry; Entry.File = Cache->OrigEntry; Entry.IsSystemFile = isSystem(File.getFileCharacteristic()); @@ -1971,11 +2041,17 @@ Record.push_back(SLoc->getOffset() - 2); if (SLoc->isFile()) { const SrcMgr::FileInfo &File = SLoc->getFile(); + const SrcMgr::ContentCache *Content = &File.getContentCache(); + if (Content->OrigEntry && + SkippedModuleMaps.find(Content->OrigEntry) != + SkippedModuleMaps.end()) { + // 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 && Index: clang/include/clang/Serialization/ASTWriter.h =================================================================== --- clang/include/clang/Serialization/ASTWriter.h +++ clang/include/clang/Serialization/ASTWriter.h @@ -456,6 +456,9 @@ std::vector<std::unique_ptr<ModuleFileExtensionWriter>> ModuleFileExtensionWriters; + /// User ModuleMaps skipped when writing control block. + std::set<const FileEntry *> SkippedModuleMaps; + /// Retrieve or create a submodule ID for this module. unsigned getSubmoduleID(Module *Mod); @@ -475,7 +478,7 @@ createSignature(StringRef AllBytes, StringRef ASTBlockBytes); void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts, - bool Modules); + std::set<const FileEntry *> &AffectingModuleMaps); void WriteSourceManagerBlock(SourceManager &SourceMgr, const Preprocessor &PP); void WritePreprocessor(const Preprocessor &PP, bool IsModule);
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits