xref: /llvm-project/clang/lib/Basic/Module.cpp (revision 40136ecefc0a542da7d0f1736c51325edc5539b4)
1 //===- Module.cpp - Describe a module -------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the Module class, which describes a module in the source
10 // code.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/Basic/Module.h"
15 #include "clang/Basic/CharInfo.h"
16 #include "clang/Basic/FileManager.h"
17 #include "clang/Basic/LangOptions.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Basic/TargetInfo.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/StringRef.h"
24 #include "llvm/ADT/StringSwitch.h"
25 #include "llvm/Support/Compiler.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include <algorithm>
29 #include <cassert>
30 #include <functional>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 using namespace clang;
36 
37 Module::Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
38                bool IsFramework, bool IsExplicit, unsigned VisibilityID)
39     : Name(Name), DefinitionLoc(DefinitionLoc), Parent(Parent),
40       VisibilityID(VisibilityID), IsUnimportable(false),
41       HasIncompatibleModuleFile(false), IsAvailable(true),
42       IsFromModuleFile(false), IsFramework(IsFramework), IsExplicit(IsExplicit),
43       IsSystem(false), IsExternC(false), IsInferred(false),
44       InferSubmodules(false), InferExplicitSubmodules(false),
45       InferExportWildcard(false), ConfigMacrosExhaustive(false),
46       NoUndeclaredIncludes(false), ModuleMapIsPrivate(false),
47       NameVisibility(Hidden) {
48   if (Parent) {
49     IsAvailable = Parent->isAvailable();
50     IsUnimportable = Parent->isUnimportable();
51     IsSystem = Parent->IsSystem;
52     IsExternC = Parent->IsExternC;
53     NoUndeclaredIncludes = Parent->NoUndeclaredIncludes;
54     ModuleMapIsPrivate = Parent->ModuleMapIsPrivate;
55 
56     Parent->SubModuleIndex[Name] = Parent->SubModules.size();
57     Parent->SubModules.push_back(this);
58   }
59 }
60 
61 Module::~Module() {
62   for (auto *Submodule : SubModules) {
63     delete Submodule;
64   }
65 }
66 
67 static bool isPlatformEnvironment(const TargetInfo &Target, StringRef Feature) {
68   StringRef Platform = Target.getPlatformName();
69   StringRef Env = Target.getTriple().getEnvironmentName();
70 
71   // Attempt to match platform and environment.
72   if (Platform == Feature || Target.getTriple().getOSName() == Feature ||
73       Env == Feature)
74     return true;
75 
76   auto CmpPlatformEnv = [](StringRef LHS, StringRef RHS) {
77     auto Pos = LHS.find('-');
78     if (Pos == StringRef::npos)
79       return false;
80     SmallString<128> NewLHS = LHS.slice(0, Pos);
81     NewLHS += LHS.slice(Pos+1, LHS.size());
82     return NewLHS == RHS;
83   };
84 
85   SmallString<128> PlatformEnv = Target.getTriple().getOSAndEnvironmentName();
86   // Darwin has different but equivalent variants for simulators, example:
87   //   1. x86_64-apple-ios-simulator
88   //   2. x86_64-apple-iossimulator
89   // where both are valid examples of the same platform+environment but in the
90   // variant (2) the simulator is hardcoded as part of the platform name. Both
91   // forms above should match for "iossimulator" requirement.
92   if (Target.getTriple().isOSDarwin() && PlatformEnv.endswith("simulator"))
93     return PlatformEnv == Feature || CmpPlatformEnv(PlatformEnv, Feature);
94 
95   return PlatformEnv == Feature;
96 }
97 
98 /// Determine whether a translation unit built using the current
99 /// language options has the given feature.
100 static bool hasFeature(StringRef Feature, const LangOptions &LangOpts,
101                        const TargetInfo &Target) {
102   bool HasFeature = llvm::StringSwitch<bool>(Feature)
103                         .Case("altivec", LangOpts.AltiVec)
104                         .Case("blocks", LangOpts.Blocks)
105                         .Case("coroutines", LangOpts.Coroutines)
106                         .Case("cplusplus", LangOpts.CPlusPlus)
107                         .Case("cplusplus11", LangOpts.CPlusPlus11)
108                         .Case("cplusplus14", LangOpts.CPlusPlus14)
109                         .Case("cplusplus17", LangOpts.CPlusPlus17)
110                         .Case("c99", LangOpts.C99)
111                         .Case("c11", LangOpts.C11)
112                         .Case("c17", LangOpts.C17)
113                         .Case("freestanding", LangOpts.Freestanding)
114                         .Case("gnuinlineasm", LangOpts.GNUAsm)
115                         .Case("objc", LangOpts.ObjC)
116                         .Case("objc_arc", LangOpts.ObjCAutoRefCount)
117                         .Case("opencl", LangOpts.OpenCL)
118                         .Case("tls", Target.isTLSSupported())
119                         .Case("zvector", LangOpts.ZVector)
120                         .Default(Target.hasFeature(Feature) ||
121                                  isPlatformEnvironment(Target, Feature));
122   if (!HasFeature)
123     HasFeature = llvm::is_contained(LangOpts.ModuleFeatures, Feature);
124   return HasFeature;
125 }
126 
127 bool Module::isUnimportable(const LangOptions &LangOpts,
128                             const TargetInfo &Target, Requirement &Req,
129                             Module *&ShadowingModule) const {
130   if (!IsUnimportable)
131     return false;
132 
133   for (const Module *Current = this; Current; Current = Current->Parent) {
134     if (Current->ShadowingModule) {
135       ShadowingModule = Current->ShadowingModule;
136       return true;
137     }
138     for (unsigned I = 0, N = Current->Requirements.size(); I != N; ++I) {
139       if (hasFeature(Current->Requirements[I].first, LangOpts, Target) !=
140               Current->Requirements[I].second) {
141         Req = Current->Requirements[I];
142         return true;
143       }
144     }
145   }
146 
147   llvm_unreachable("could not find a reason why module is unimportable");
148 }
149 
150 // The -fmodule-name option tells the compiler to textually include headers in
151 // the specified module, meaning Clang won't build the specified module. This
152 // is useful in a number of situations, for instance, when building a library
153 // that vends a module map, one might want to avoid hitting intermediate build
154 // products containing the module map or avoid finding the system installed
155 // modulemap for that library.
156 bool Module::isForBuilding(const LangOptions &LangOpts) const {
157   StringRef TopLevelName = getTopLevelModuleName();
158   StringRef CurrentModule = LangOpts.CurrentModule;
159 
160   // When building framework Foo, we want to make sure that Foo *and*
161   // Foo_Private are textually included and no modules are built for both.
162   if (getTopLevelModule()->IsFramework &&
163       CurrentModule == LangOpts.ModuleName &&
164       !CurrentModule.endswith("_Private") && TopLevelName.endswith("_Private"))
165     TopLevelName = TopLevelName.drop_back(8);
166 
167   return TopLevelName == CurrentModule;
168 }
169 
170 bool Module::isAvailable(const LangOptions &LangOpts, const TargetInfo &Target,
171                          Requirement &Req,
172                          UnresolvedHeaderDirective &MissingHeader,
173                          Module *&ShadowingModule) const {
174   if (IsAvailable)
175     return true;
176 
177   if (isUnimportable(LangOpts, Target, Req, ShadowingModule))
178     return false;
179 
180   // FIXME: All missing headers are listed on the top-level module. Should we
181   // just look there?
182   for (const Module *Current = this; Current; Current = Current->Parent) {
183     if (!Current->MissingHeaders.empty()) {
184       MissingHeader = Current->MissingHeaders.front();
185       return false;
186     }
187   }
188 
189   llvm_unreachable("could not find a reason why module is unavailable");
190 }
191 
192 bool Module::isSubModuleOf(const Module *Other) const {
193   for (auto *Parent = this; Parent; Parent = Parent->Parent) {
194     if (Parent == Other)
195       return true;
196   }
197   return false;
198 }
199 
200 const Module *Module::getTopLevelModule() const {
201   const Module *Result = this;
202   while (Result->Parent)
203     Result = Result->Parent;
204 
205   return Result;
206 }
207 
208 static StringRef getModuleNameFromComponent(
209     const std::pair<std::string, SourceLocation> &IdComponent) {
210   return IdComponent.first;
211 }
212 
213 static StringRef getModuleNameFromComponent(StringRef R) { return R; }
214 
215 template<typename InputIter>
216 static void printModuleId(raw_ostream &OS, InputIter Begin, InputIter End,
217                           bool AllowStringLiterals = true) {
218   for (InputIter It = Begin; It != End; ++It) {
219     if (It != Begin)
220       OS << ".";
221 
222     StringRef Name = getModuleNameFromComponent(*It);
223     if (!AllowStringLiterals || isValidAsciiIdentifier(Name))
224       OS << Name;
225     else {
226       OS << '"';
227       OS.write_escaped(Name);
228       OS << '"';
229     }
230   }
231 }
232 
233 template<typename Container>
234 static void printModuleId(raw_ostream &OS, const Container &C) {
235   return printModuleId(OS, C.begin(), C.end());
236 }
237 
238 std::string Module::getFullModuleName(bool AllowStringLiterals) const {
239   SmallVector<StringRef, 2> Names;
240 
241   // Build up the set of module names (from innermost to outermost).
242   for (const Module *M = this; M; M = M->Parent)
243     Names.push_back(M->Name);
244 
245   std::string Result;
246 
247   llvm::raw_string_ostream Out(Result);
248   printModuleId(Out, Names.rbegin(), Names.rend(), AllowStringLiterals);
249   Out.flush();
250 
251   return Result;
252 }
253 
254 bool Module::fullModuleNameIs(ArrayRef<StringRef> nameParts) const {
255   for (const Module *M = this; M; M = M->Parent) {
256     if (nameParts.empty() || M->Name != nameParts.back())
257       return false;
258     nameParts = nameParts.drop_back();
259   }
260   return nameParts.empty();
261 }
262 
263 Module::DirectoryName Module::getUmbrellaDir() const {
264   if (Header U = getUmbrellaHeader())
265     return {"", "", U.Entry->getDir()};
266 
267   return {UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory,
268           Umbrella.dyn_cast<const DirectoryEntry *>()};
269 }
270 
271 void Module::addTopHeader(const FileEntry *File) {
272   assert(File);
273   TopHeaders.insert(File);
274 }
275 
276 ArrayRef<const FileEntry *> Module::getTopHeaders(FileManager &FileMgr) {
277   if (!TopHeaderNames.empty()) {
278     for (std::vector<std::string>::iterator
279            I = TopHeaderNames.begin(), E = TopHeaderNames.end(); I != E; ++I) {
280       if (auto FE = FileMgr.getFile(*I))
281         TopHeaders.insert(*FE);
282     }
283     TopHeaderNames.clear();
284   }
285 
286   return llvm::ArrayRef(TopHeaders.begin(), TopHeaders.end());
287 }
288 
289 bool Module::directlyUses(const Module *Requested) {
290   auto *Top = getTopLevelModule();
291 
292   // A top-level module implicitly uses itself.
293   if (Requested->isSubModuleOf(Top))
294     return true;
295 
296   for (auto *Use : Top->DirectUses)
297     if (Requested->isSubModuleOf(Use))
298       return true;
299 
300   // Anyone is allowed to use our builtin stddef.h and its accompanying module.
301   if (!Requested->Parent && Requested->Name == "_Builtin_stddef_max_align_t")
302     return true;
303 
304   if (NoUndeclaredIncludes)
305     UndeclaredUses.insert(Requested);
306 
307   return false;
308 }
309 
310 void Module::addRequirement(StringRef Feature, bool RequiredState,
311                             const LangOptions &LangOpts,
312                             const TargetInfo &Target) {
313   Requirements.push_back(Requirement(std::string(Feature), RequiredState));
314 
315   // If this feature is currently available, we're done.
316   if (hasFeature(Feature, LangOpts, Target) == RequiredState)
317     return;
318 
319   markUnavailable(/*Unimportable*/true);
320 }
321 
322 void Module::markUnavailable(bool Unimportable) {
323   auto needUpdate = [Unimportable](Module *M) {
324     return M->IsAvailable || (!M->IsUnimportable && Unimportable);
325   };
326 
327   if (!needUpdate(this))
328     return;
329 
330   SmallVector<Module *, 2> Stack;
331   Stack.push_back(this);
332   while (!Stack.empty()) {
333     Module *Current = Stack.back();
334     Stack.pop_back();
335 
336     if (!needUpdate(Current))
337       continue;
338 
339     Current->IsAvailable = false;
340     Current->IsUnimportable |= Unimportable;
341     for (auto *Submodule : Current->submodules()) {
342       if (needUpdate(Submodule))
343         Stack.push_back(Submodule);
344     }
345   }
346 }
347 
348 Module *Module::findSubmodule(StringRef Name) const {
349   llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
350   if (Pos == SubModuleIndex.end())
351     return nullptr;
352 
353   return SubModules[Pos->getValue()];
354 }
355 
356 Module *Module::findOrInferSubmodule(StringRef Name) {
357   llvm::StringMap<unsigned>::const_iterator Pos = SubModuleIndex.find(Name);
358   if (Pos != SubModuleIndex.end())
359     return SubModules[Pos->getValue()];
360   if (!InferSubmodules)
361     return nullptr;
362   Module *Result = new Module(Name, SourceLocation(), this, false, InferExplicitSubmodules, 0);
363   Result->InferExplicitSubmodules = InferExplicitSubmodules;
364   Result->InferSubmodules = InferSubmodules;
365   Result->InferExportWildcard = InferExportWildcard;
366   if (Result->InferExportWildcard)
367     Result->Exports.push_back(Module::ExportDecl(nullptr, true));
368   return Result;
369 }
370 
371 void Module::getExportedModules(SmallVectorImpl<Module *> &Exported) const {
372   // All non-explicit submodules are exported.
373   for (std::vector<Module *>::const_iterator I = SubModules.begin(),
374                                              E = SubModules.end();
375        I != E; ++I) {
376     Module *Mod = *I;
377     if (!Mod->IsExplicit)
378       Exported.push_back(Mod);
379   }
380 
381   // Find re-exported modules by filtering the list of imported modules.
382   bool AnyWildcard = false;
383   bool UnrestrictedWildcard = false;
384   SmallVector<Module *, 4> WildcardRestrictions;
385   for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
386     Module *Mod = Exports[I].getPointer();
387     if (!Exports[I].getInt()) {
388       // Export a named module directly; no wildcards involved.
389       Exported.push_back(Mod);
390 
391       continue;
392     }
393 
394     // Wildcard export: export all of the imported modules that match
395     // the given pattern.
396     AnyWildcard = true;
397     if (UnrestrictedWildcard)
398       continue;
399 
400     if (Module *Restriction = Exports[I].getPointer())
401       WildcardRestrictions.push_back(Restriction);
402     else {
403       WildcardRestrictions.clear();
404       UnrestrictedWildcard = true;
405     }
406   }
407 
408   // If there were any wildcards, push any imported modules that were
409   // re-exported by the wildcard restriction.
410   if (!AnyWildcard)
411     return;
412 
413   for (unsigned I = 0, N = Imports.size(); I != N; ++I) {
414     Module *Mod = Imports[I];
415     bool Acceptable = UnrestrictedWildcard;
416     if (!Acceptable) {
417       // Check whether this module meets one of the restrictions.
418       for (unsigned R = 0, NR = WildcardRestrictions.size(); R != NR; ++R) {
419         Module *Restriction = WildcardRestrictions[R];
420         if (Mod == Restriction || Mod->isSubModuleOf(Restriction)) {
421           Acceptable = true;
422           break;
423         }
424       }
425     }
426 
427     if (!Acceptable)
428       continue;
429 
430     Exported.push_back(Mod);
431   }
432 }
433 
434 void Module::buildVisibleModulesCache() const {
435   assert(VisibleModulesCache.empty() && "cache does not need building");
436 
437   // This module is visible to itself.
438   VisibleModulesCache.insert(this);
439 
440   // Every imported module is visible.
441   SmallVector<Module *, 16> Stack(Imports.begin(), Imports.end());
442   while (!Stack.empty()) {
443     Module *CurrModule = Stack.pop_back_val();
444 
445     // Every module transitively exported by an imported module is visible.
446     if (VisibleModulesCache.insert(CurrModule).second)
447       CurrModule->getExportedModules(Stack);
448   }
449 }
450 
451 void Module::print(raw_ostream &OS, unsigned Indent, bool Dump) const {
452   OS.indent(Indent);
453   if (IsFramework)
454     OS << "framework ";
455   if (IsExplicit)
456     OS << "explicit ";
457   OS << "module ";
458   printModuleId(OS, &Name, &Name + 1);
459 
460   if (IsSystem || IsExternC) {
461     OS.indent(Indent + 2);
462     if (IsSystem)
463       OS << " [system]";
464     if (IsExternC)
465       OS << " [extern_c]";
466   }
467 
468   OS << " {\n";
469 
470   if (!Requirements.empty()) {
471     OS.indent(Indent + 2);
472     OS << "requires ";
473     for (unsigned I = 0, N = Requirements.size(); I != N; ++I) {
474       if (I)
475         OS << ", ";
476       if (!Requirements[I].second)
477         OS << "!";
478       OS << Requirements[I].first;
479     }
480     OS << "\n";
481   }
482 
483   if (Header H = getUmbrellaHeader()) {
484     OS.indent(Indent + 2);
485     OS << "umbrella header \"";
486     OS.write_escaped(H.NameAsWritten);
487     OS << "\"\n";
488   } else if (DirectoryName D = getUmbrellaDir()) {
489     OS.indent(Indent + 2);
490     OS << "umbrella \"";
491     OS.write_escaped(D.NameAsWritten);
492     OS << "\"\n";
493   }
494 
495   if (!ConfigMacros.empty() || ConfigMacrosExhaustive) {
496     OS.indent(Indent + 2);
497     OS << "config_macros ";
498     if (ConfigMacrosExhaustive)
499       OS << "[exhaustive]";
500     for (unsigned I = 0, N = ConfigMacros.size(); I != N; ++I) {
501       if (I)
502         OS << ", ";
503       OS << ConfigMacros[I];
504     }
505     OS << "\n";
506   }
507 
508   struct {
509     StringRef Prefix;
510     HeaderKind Kind;
511   } Kinds[] = {{"", HK_Normal},
512                {"textual ", HK_Textual},
513                {"private ", HK_Private},
514                {"private textual ", HK_PrivateTextual},
515                {"exclude ", HK_Excluded}};
516 
517   for (auto &K : Kinds) {
518     assert(&K == &Kinds[K.Kind] && "kinds in wrong order");
519     for (auto &H : Headers[K.Kind]) {
520       OS.indent(Indent + 2);
521       OS << K.Prefix << "header \"";
522       OS.write_escaped(H.NameAsWritten);
523       OS << "\" { size " << H.Entry->getSize()
524          << " mtime " << H.Entry->getModificationTime() << " }\n";
525     }
526   }
527   for (auto *Unresolved : {&UnresolvedHeaders, &MissingHeaders}) {
528     for (auto &U : *Unresolved) {
529       OS.indent(Indent + 2);
530       OS << Kinds[U.Kind].Prefix << "header \"";
531       OS.write_escaped(U.FileName);
532       OS << "\"";
533       if (U.Size || U.ModTime) {
534         OS << " {";
535         if (U.Size)
536           OS << " size " << *U.Size;
537         if (U.ModTime)
538           OS << " mtime " << *U.ModTime;
539         OS << " }";
540       }
541       OS << "\n";
542     }
543   }
544 
545   if (!ExportAsModule.empty()) {
546     OS.indent(Indent + 2);
547     OS << "export_as" << ExportAsModule << "\n";
548   }
549 
550   for (auto *Submodule : submodules())
551     // Print inferred subframework modules so that we don't need to re-infer
552     // them (requires expensive directory iteration + stat calls) when we build
553     // the module. Regular inferred submodules are OK, as we need to look at all
554     // those header files anyway.
555     if (!Submodule->IsInferred || Submodule->IsFramework)
556       Submodule->print(OS, Indent + 2, Dump);
557 
558   for (unsigned I = 0, N = Exports.size(); I != N; ++I) {
559     OS.indent(Indent + 2);
560     OS << "export ";
561     if (Module *Restriction = Exports[I].getPointer()) {
562       OS << Restriction->getFullModuleName(true);
563       if (Exports[I].getInt())
564         OS << ".*";
565     } else {
566       OS << "*";
567     }
568     OS << "\n";
569   }
570 
571   for (unsigned I = 0, N = UnresolvedExports.size(); I != N; ++I) {
572     OS.indent(Indent + 2);
573     OS << "export ";
574     printModuleId(OS, UnresolvedExports[I].Id);
575     if (UnresolvedExports[I].Wildcard)
576       OS << (UnresolvedExports[I].Id.empty() ? "*" : ".*");
577     OS << "\n";
578   }
579 
580   if (Dump) {
581     for (Module *M : Imports) {
582       OS.indent(Indent + 2);
583       llvm::errs() << "import " << M->getFullModuleName() << "\n";
584     }
585   }
586 
587   for (unsigned I = 0, N = DirectUses.size(); I != N; ++I) {
588     OS.indent(Indent + 2);
589     OS << "use ";
590     OS << DirectUses[I]->getFullModuleName(true);
591     OS << "\n";
592   }
593 
594   for (unsigned I = 0, N = UnresolvedDirectUses.size(); I != N; ++I) {
595     OS.indent(Indent + 2);
596     OS << "use ";
597     printModuleId(OS, UnresolvedDirectUses[I]);
598     OS << "\n";
599   }
600 
601   for (unsigned I = 0, N = LinkLibraries.size(); I != N; ++I) {
602     OS.indent(Indent + 2);
603     OS << "link ";
604     if (LinkLibraries[I].IsFramework)
605       OS << "framework ";
606     OS << "\"";
607     OS.write_escaped(LinkLibraries[I].Library);
608     OS << "\"";
609   }
610 
611   for (unsigned I = 0, N = UnresolvedConflicts.size(); I != N; ++I) {
612     OS.indent(Indent + 2);
613     OS << "conflict ";
614     printModuleId(OS, UnresolvedConflicts[I].Id);
615     OS << ", \"";
616     OS.write_escaped(UnresolvedConflicts[I].Message);
617     OS << "\"\n";
618   }
619 
620   for (unsigned I = 0, N = Conflicts.size(); I != N; ++I) {
621     OS.indent(Indent + 2);
622     OS << "conflict ";
623     OS << Conflicts[I].Other->getFullModuleName(true);
624     OS << ", \"";
625     OS.write_escaped(Conflicts[I].Message);
626     OS << "\"\n";
627   }
628 
629   if (InferSubmodules) {
630     OS.indent(Indent + 2);
631     if (InferExplicitSubmodules)
632       OS << "explicit ";
633     OS << "module * {\n";
634     if (InferExportWildcard) {
635       OS.indent(Indent + 4);
636       OS << "export *\n";
637     }
638     OS.indent(Indent + 2);
639     OS << "}\n";
640   }
641 
642   OS.indent(Indent);
643   OS << "}\n";
644 }
645 
646 LLVM_DUMP_METHOD void Module::dump() const {
647   print(llvm::errs(), 0, true);
648 }
649 
650 void VisibleModuleSet::setVisible(Module *M, SourceLocation Loc,
651                                   VisibleCallback Vis, ConflictCallback Cb) {
652   // We can't import a global module fragment so the location can be invalid.
653   assert((M->isGlobalModule() || Loc.isValid()) &&
654          "setVisible expects a valid import location");
655   if (isVisible(M))
656     return;
657 
658   ++Generation;
659 
660   struct Visiting {
661     Module *M;
662     Visiting *ExportedBy;
663   };
664 
665   std::function<void(Visiting)> VisitModule = [&](Visiting V) {
666     // Nothing to do for a module that's already visible.
667     unsigned ID = V.M->getVisibilityID();
668     if (ImportLocs.size() <= ID)
669       ImportLocs.resize(ID + 1);
670     else if (ImportLocs[ID].isValid())
671       return;
672 
673     ImportLocs[ID] = Loc;
674     Vis(V.M);
675 
676     // Make any exported modules visible.
677     SmallVector<Module *, 16> Exports;
678     V.M->getExportedModules(Exports);
679     for (Module *E : Exports) {
680       // Don't import non-importable modules.
681       if (!E->isUnimportable())
682         VisitModule({E, &V});
683     }
684 
685     for (auto &C : V.M->Conflicts) {
686       if (isVisible(C.Other)) {
687         llvm::SmallVector<Module*, 8> Path;
688         for (Visiting *I = &V; I; I = I->ExportedBy)
689           Path.push_back(I->M);
690         Cb(Path, C.Other, C.Message);
691       }
692     }
693   };
694   VisitModule({M, nullptr});
695 }
696 
697 ASTSourceDescriptor::ASTSourceDescriptor(Module &M)
698     : Signature(M.Signature), ClangModule(&M) {
699   if (M.Directory)
700     Path = M.Directory->getName();
701   if (auto File = M.getASTFile())
702     ASTFile = File->getName();
703 }
704 
705 std::string ASTSourceDescriptor::getModuleName() const {
706   if (ClangModule)
707     return ClangModule->Name;
708   else
709     return std::string(PCHModuleName);
710 }
711