From 5984ea216d2acbe5613b0af06ea27ef395ca9904 Mon Sep 17 00:00:00 2001 From: Ben Langmuir Date: Mon, 8 May 2023 15:59:51 -0700 Subject: [PATCH] [clang] Prevent creation of new submodules in ASTWriter Avoid inferring new submodules for headers in ASTWriter's collection of affecting modulemap files, since we don't want to pick up dependencies that didn't actually exist during parsing. rdar://109112624 Differential Revision: https://reviews.llvm.org/D150151 --- clang/include/clang/Lex/HeaderSearch.h | 6 +++++- clang/include/clang/Lex/ModuleMap.h | 6 +++++- clang/lib/Lex/HeaderSearch.cpp | 5 +++-- clang/lib/Lex/ModuleMap.cpp | 4 ++-- clang/lib/Serialization/ASTWriter.cpp | 3 ++- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/clang/include/clang/Lex/HeaderSearch.h b/clang/include/clang/Lex/HeaderSearch.h index 76e3e786ff07..49fb99c1483c 100644 --- a/clang/include/clang/Lex/HeaderSearch.h +++ b/clang/include/clang/Lex/HeaderSearch.h @@ -665,9 +665,13 @@ public: /// Retrieve all the modules corresponding to the given file. /// + /// \param AllowCreation Whether to allow inference of a new submodule, or to + /// only return existing known modules. + /// /// \ref findModuleForHeader should typically be used instead of this. ArrayRef - findAllModulesForHeader(const FileEntry *File) const; + findAllModulesForHeader(const FileEntry *File, + bool AllowCreation = true) const; /// Read the contents of the given module map file. /// diff --git a/clang/include/clang/Lex/ModuleMap.h b/clang/include/clang/Lex/ModuleMap.h index f155c609b06c..8f8580fd1149 100644 --- a/clang/include/clang/Lex/ModuleMap.h +++ b/clang/include/clang/Lex/ModuleMap.h @@ -448,9 +448,13 @@ public: /// and does not consult the external source. (Those checks are the /// responsibility of \ref HeaderSearch.) /// + /// \param AllowCreation Whether to allow inference of a new submodule, or to + /// only return existing known modules. + /// /// Typically, \ref findModuleForHeader should be used instead, as it picks /// the preferred module for the header. - ArrayRef findAllModulesForHeader(const FileEntry *File); + ArrayRef findAllModulesForHeader(const FileEntry *File, + bool AllowCreation = true); /// Like \ref findAllModulesForHeader, but do not attempt to infer module /// ownership from umbrella headers if we've not already done so. diff --git a/clang/lib/Lex/HeaderSearch.cpp b/clang/lib/Lex/HeaderSearch.cpp index 5a7357a5ada4..a650bbea1e48 100644 --- a/clang/lib/Lex/HeaderSearch.cpp +++ b/clang/lib/Lex/HeaderSearch.cpp @@ -1565,13 +1565,14 @@ HeaderSearch::findModuleForHeader(const FileEntry *File, bool AllowTextual, } ArrayRef -HeaderSearch::findAllModulesForHeader(const FileEntry *File) const { +HeaderSearch::findAllModulesForHeader(const FileEntry *File, + bool AllowCreation) const { if (ExternalSource) { // Make sure the external source has handled header info about this file, // which includes whether the file is part of a module. (void)getExistingFileInfo(File); } - return ModMap.findAllModulesForHeader(File); + return ModMap.findAllModulesForHeader(File, AllowCreation); } static bool suggestModule(HeaderSearch &HS, const FileEntry *File, diff --git a/clang/lib/Lex/ModuleMap.cpp b/clang/lib/Lex/ModuleMap.cpp index 0e148a43dc92..66e9a7e1dbea 100644 --- a/clang/lib/Lex/ModuleMap.cpp +++ b/clang/lib/Lex/ModuleMap.cpp @@ -683,12 +683,12 @@ ModuleMap::findOrCreateModuleForHeaderInUmbrellaDir(const FileEntry *File) { } ArrayRef -ModuleMap::findAllModulesForHeader(const FileEntry *File) { +ModuleMap::findAllModulesForHeader(const FileEntry *File, bool AllowCreation) { HeadersMap::iterator Known = findKnownHeader(File); if (Known != Headers.end()) return Known->second; - if (findOrCreateModuleForHeaderInUmbrellaDir(File)) + if (AllowCreation && findOrCreateModuleForHeaderInUmbrellaDir(File)) return Headers.find(File)->second; return std::nullopt; diff --git a/clang/lib/Serialization/ASTWriter.cpp b/clang/lib/Serialization/ASTWriter.cpp index e124cb4c5f81..06d758b36c59 100644 --- a/clang/lib/Serialization/ASTWriter.cpp +++ b/clang/lib/Serialization/ASTWriter.cpp @@ -185,7 +185,8 @@ std::set GetAffectingModuleMaps(const Preprocessor &PP, if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader)) continue; - for (const auto &KH : HS.findAllModulesForHeader(File)) { + for (const auto &KH : + HS.findAllModulesForHeader(File, /*AllowCreation=*/false)) { if (!KH.getModule()) continue; ModulesToProcess.push_back(KH.getModule());