Module.cpp 21 KB

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