@@ -184,14 +184,29 @@ GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
184184 const SourceManager &SM = PP.getSourceManager ();
185185 const ModuleMap &MM = HS.getModuleMap ();
186186
187- llvm::DenseSet<FileID> ModuleMaps;
188-
189- llvm::DenseSet<const Module *> ProcessedModules;
190- auto CollectModuleMapsForHierarchy = [&](const Module *M) {
187+ // Module maps used only by textual headers are special. Their FileID is
188+ // non-affecting, but their FileEntry is (i.e. must be written as InputFile).
189+ enum AffectedReason : bool {
190+ ARTextualHeader = 0 ,
191+ ARImportOrTextualHeader = 1 ,
192+ };
193+ auto AssignMostImportant = [](AffectedReason &L, AffectedReason R) {
194+ L = std::max (L, R);
195+ };
196+ llvm::DenseMap<FileID, AffectedReason> ModuleMaps;
197+ llvm::DenseMap<const Module *, AffectedReason> ProcessedModules;
198+ auto CollectModuleMapsForHierarchy = [&](const Module *M, AffectedReason Reason) {
191199 M = M->getTopLevelModule ();
192200
193- if (!ProcessedModules.insert (M).second )
201+ // We need to process the header either when it was not present of when we
202+ // previously flagged module map as textual headers and now we found a
203+ // proper import.
204+ if (auto [It, Inserted] = ProcessedModules.insert ({M, Reason});
205+ !Inserted && Reason <= It->second ) {
194206 return ;
207+ } else {
208+ It->second = Reason;
209+ }
195210
196211 std::queue<const Module *> Q;
197212 Q.push (M);
@@ -202,12 +217,12 @@ GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
202217 // The containing module map is affecting, because it's being pointed
203218 // into by Module::DefinitionLoc.
204219 if (auto F = MM.getContainingModuleMapFileID (Mod); F.isValid ())
205- ModuleMaps. insert (F );
220+ AssignMostImportant (ModuleMaps[F], Reason );
206221 // For inferred modules, the module map that allowed inferring is not
207222 // related to the virtual containing module map file. It did affect the
208223 // compilation, though.
209224 if (auto UniqF = MM.getModuleMapFileIDForUniquing (Mod); UniqF.isValid ())
210- ModuleMaps. insert ( UniqF);
225+ AssignMostImportant (ModuleMaps[ UniqF], Reason );
211226
212227 for (auto *SubM : Mod->submodules ())
213228 Q.push (SubM);
@@ -216,7 +231,7 @@ GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
216231
217232 // Handle all the affecting modules referenced from the root module.
218233
219- CollectModuleMapsForHierarchy (RootModule);
234+ CollectModuleMapsForHierarchy (RootModule, ARImportOrTextualHeader );
220235
221236 std::queue<const Module *> Q;
222237 Q.push (RootModule);
@@ -225,9 +240,9 @@ GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
225240 Q.pop ();
226241
227242 for (const Module *ImportedModule : CurrentModule->Imports )
228- CollectModuleMapsForHierarchy (ImportedModule);
243+ CollectModuleMapsForHierarchy (ImportedModule, ARImportOrTextualHeader );
229244 for (const Module *UndeclaredModule : CurrentModule->UndeclaredUses )
230- CollectModuleMapsForHierarchy (UndeclaredModule);
245+ CollectModuleMapsForHierarchy (UndeclaredModule, ARImportOrTextualHeader );
231246
232247 for (auto *M : CurrentModule->submodules ())
233248 Q.push (M);
@@ -256,7 +271,7 @@ GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
256271
257272 for (const auto &KH : HS.findResolvedModulesForHeader (*File))
258273 if (const Module *M = KH.getModule ())
259- CollectModuleMapsForHierarchy (M);
274+ CollectModuleMapsForHierarchy (M, ARTextualHeader );
260275 }
261276
262277 // FIXME: This algorithm is not correct for module map hierarchies where
@@ -278,13 +293,16 @@ GetAffectingModuleMaps(const Preprocessor &PP, Module *RootModule) {
278293 // includes a module map defining a module that's not a submodule of X.
279294
280295 llvm::DenseSet<const FileEntry *> ModuleFileEntries;
281- for (FileID MM : ModuleMaps) {
282- if (auto *FE = SM.getFileEntryForID (MM))
296+ llvm::DenseSet<FileID> ModuleFileIDs;
297+ for (auto [FID, Reason] : ModuleMaps) {
298+ if (Reason == ARImportOrTextualHeader)
299+ ModuleFileIDs.insert (FID);
300+ if (auto *FE = SM.getFileEntryForID (FID))
283301 ModuleFileEntries.insert (FE);
284302 }
285303
286304 AffectingModuleMaps R;
287- R.DefinitionFileIDs = std::move (ModuleMaps );
305+ R.DefinitionFileIDs = std::move (ModuleFileIDs );
288306 R.DefinitionFiles = std::move (ModuleFileEntries);
289307 return std::move (R);
290308}
0 commit comments