ModuleManager.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  1. //===- ModuleManager.cpp - Module Manager ---------------------------------===//
  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 ModuleManager class, which manages a set of loaded
  10. // modules for the ASTReader.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Serialization/ModuleManager.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/LLVM.h"
  16. #include "clang/Lex/HeaderSearch.h"
  17. #include "clang/Lex/ModuleMap.h"
  18. #include "clang/Serialization/GlobalModuleIndex.h"
  19. #include "clang/Serialization/InMemoryModuleCache.h"
  20. #include "clang/Serialization/ModuleFile.h"
  21. #include "clang/Serialization/PCHContainerOperations.h"
  22. #include "llvm/ADT/STLExtras.h"
  23. #include "llvm/ADT/SetVector.h"
  24. #include "llvm/ADT/SmallPtrSet.h"
  25. #include "llvm/ADT/SmallVector.h"
  26. #include "llvm/ADT/StringRef.h"
  27. #include "llvm/ADT/iterator.h"
  28. #include "llvm/Support/Chrono.h"
  29. #include "llvm/Support/DOTGraphTraits.h"
  30. #include "llvm/Support/ErrorOr.h"
  31. #include "llvm/Support/GraphWriter.h"
  32. #include "llvm/Support/MemoryBuffer.h"
  33. #include "llvm/Support/VirtualFileSystem.h"
  34. #include <algorithm>
  35. #include <cassert>
  36. #include <memory>
  37. #include <string>
  38. #include <system_error>
  39. using namespace clang;
  40. using namespace serialization;
  41. ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const {
  42. auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
  43. /*CacheFailure=*/false);
  44. if (Entry)
  45. return lookup(*Entry);
  46. return nullptr;
  47. }
  48. ModuleFile *ModuleManager::lookupByModuleName(StringRef Name) const {
  49. if (const Module *Mod = HeaderSearchInfo.getModuleMap().findModule(Name))
  50. if (const FileEntry *File = Mod->getASTFile())
  51. return lookup(File);
  52. return nullptr;
  53. }
  54. ModuleFile *ModuleManager::lookup(const FileEntry *File) const {
  55. auto Known = Modules.find(File);
  56. if (Known == Modules.end())
  57. return nullptr;
  58. return Known->second;
  59. }
  60. std::unique_ptr<llvm::MemoryBuffer>
  61. ModuleManager::lookupBuffer(StringRef Name) {
  62. auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
  63. /*CacheFailure=*/false);
  64. if (!Entry)
  65. return nullptr;
  66. return std::move(InMemoryBuffers[*Entry]);
  67. }
  68. static bool checkSignature(ASTFileSignature Signature,
  69. ASTFileSignature ExpectedSignature,
  70. std::string &ErrorStr) {
  71. if (!ExpectedSignature || Signature == ExpectedSignature)
  72. return false;
  73. ErrorStr =
  74. Signature ? "signature mismatch" : "could not read module signature";
  75. return true;
  76. }
  77. static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy,
  78. SourceLocation ImportLoc) {
  79. if (ImportedBy) {
  80. MF.ImportedBy.insert(ImportedBy);
  81. ImportedBy->Imports.insert(&MF);
  82. } else {
  83. if (!MF.DirectlyImported)
  84. MF.ImportLoc = ImportLoc;
  85. MF.DirectlyImported = true;
  86. }
  87. }
  88. ModuleManager::AddModuleResult
  89. ModuleManager::addModule(StringRef FileName, ModuleKind Type,
  90. SourceLocation ImportLoc, ModuleFile *ImportedBy,
  91. unsigned Generation,
  92. off_t ExpectedSize, time_t ExpectedModTime,
  93. ASTFileSignature ExpectedSignature,
  94. ASTFileSignatureReader ReadSignature,
  95. ModuleFile *&Module,
  96. std::string &ErrorStr) {
  97. Module = nullptr;
  98. // Look for the file entry. This only fails if the expected size or
  99. // modification time differ.
  100. OptionalFileEntryRefDegradesToFileEntryPtr Entry;
  101. if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) {
  102. // If we're not expecting to pull this file out of the module cache, it
  103. // might have a different mtime due to being moved across filesystems in
  104. // a distributed build. The size must still match, though. (As must the
  105. // contents, but we can't check that.)
  106. ExpectedModTime = 0;
  107. }
  108. // Note: ExpectedSize and ExpectedModTime will be 0 for MK_ImplicitModule
  109. // when using an ASTFileSignature.
  110. if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
  111. ErrorStr = "module file out of date";
  112. return OutOfDate;
  113. }
  114. if (!Entry && FileName != "-") {
  115. ErrorStr = "module file not found";
  116. return Missing;
  117. }
  118. // The ModuleManager's use of FileEntry nodes as the keys for its map of
  119. // loaded modules is less than ideal. Uniqueness for FileEntry nodes is
  120. // maintained by FileManager, which in turn uses inode numbers on hosts
  121. // that support that. When coupled with the module cache's proclivity for
  122. // turning over and deleting stale PCMs, this means entries for different
  123. // module files can wind up reusing the same underlying inode. When this
  124. // happens, subsequent accesses to the Modules map will disagree on the
  125. // ModuleFile associated with a given file. In general, it is not sufficient
  126. // to resolve this conundrum with a type like FileEntryRef that stores the
  127. // name of the FileEntry node on first access because of path canonicalization
  128. // issues. However, the paths constructed for implicit module builds are
  129. // fully under Clang's control. We *can*, therefore, rely on their structure
  130. // being consistent across operating systems and across subsequent accesses
  131. // to the Modules map.
  132. auto implicitModuleNamesMatch = [](ModuleKind Kind, const ModuleFile *MF,
  133. const FileEntry *Entry) -> bool {
  134. if (Kind != MK_ImplicitModule)
  135. return true;
  136. return Entry->getName() == MF->FileName;
  137. };
  138. // Check whether we already loaded this module, before
  139. if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) {
  140. if (implicitModuleNamesMatch(Type, ModuleEntry, Entry)) {
  141. // Check the stored signature.
  142. if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))
  143. return OutOfDate;
  144. Module = ModuleEntry;
  145. updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc);
  146. return AlreadyLoaded;
  147. }
  148. }
  149. // Allocate a new module.
  150. auto NewModule = std::make_unique<ModuleFile>(Type, Generation);
  151. NewModule->Index = Chain.size();
  152. NewModule->FileName = FileName.str();
  153. NewModule->File = Entry;
  154. NewModule->ImportLoc = ImportLoc;
  155. NewModule->InputFilesValidationTimestamp = 0;
  156. if (NewModule->Kind == MK_ImplicitModule) {
  157. std::string TimestampFilename = NewModule->getTimestampFilename();
  158. llvm::vfs::Status Status;
  159. // A cached stat value would be fine as well.
  160. if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
  161. NewModule->InputFilesValidationTimestamp =
  162. llvm::sys::toTimeT(Status.getLastModificationTime());
  163. }
  164. // Load the contents of the module
  165. if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
  166. // The buffer was already provided for us.
  167. NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer));
  168. // Since the cached buffer is reused, it is safe to close the file
  169. // descriptor that was opened while stat()ing the PCM in
  170. // lookupModuleFile() above, it won't be needed any longer.
  171. Entry->closeFile();
  172. } else if (llvm::MemoryBuffer *Buffer =
  173. getModuleCache().lookupPCM(FileName)) {
  174. NewModule->Buffer = Buffer;
  175. // As above, the file descriptor is no longer needed.
  176. Entry->closeFile();
  177. } else if (getModuleCache().shouldBuildPCM(FileName)) {
  178. // Report that the module is out of date, since we tried (and failed) to
  179. // import it earlier.
  180. Entry->closeFile();
  181. return OutOfDate;
  182. } else {
  183. // Open the AST file.
  184. llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code()));
  185. if (FileName == "-") {
  186. Buf = llvm::MemoryBuffer::getSTDIN();
  187. } else {
  188. // Get a buffer of the file and close the file descriptor when done.
  189. // The file is volatile because in a parallel build we expect multiple
  190. // compiler processes to use the same module file rebuilding it if needed.
  191. //
  192. // RequiresNullTerminator is false because module files don't need it, and
  193. // this allows the file to still be mmapped.
  194. Buf = FileMgr.getBufferForFile(NewModule->File,
  195. /*IsVolatile=*/true,
  196. /*RequiresNullTerminator=*/false);
  197. }
  198. if (!Buf) {
  199. ErrorStr = Buf.getError().message();
  200. return Missing;
  201. }
  202. NewModule->Buffer = &getModuleCache().addPCM(FileName, std::move(*Buf));
  203. }
  204. // Initialize the stream.
  205. NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer);
  206. // Read the signature eagerly now so that we can check it. Avoid calling
  207. // ReadSignature unless there's something to check though.
  208. if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
  209. ExpectedSignature, ErrorStr))
  210. return OutOfDate;
  211. // We're keeping this module. Store it everywhere.
  212. Module = Modules[Entry] = NewModule.get();
  213. updateModuleImports(*NewModule, ImportedBy, ImportLoc);
  214. if (!NewModule->isModule())
  215. PCHChain.push_back(NewModule.get());
  216. if (!ImportedBy)
  217. Roots.push_back(NewModule.get());
  218. Chain.push_back(std::move(NewModule));
  219. return NewlyLoaded;
  220. }
  221. void ModuleManager::removeModules(ModuleIterator First) {
  222. auto Last = end();
  223. if (First == Last)
  224. return;
  225. // Explicitly clear VisitOrder since we might not notice it is stale.
  226. VisitOrder.clear();
  227. // Collect the set of module file pointers that we'll be removing.
  228. llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
  229. (llvm::pointer_iterator<ModuleIterator>(First)),
  230. (llvm::pointer_iterator<ModuleIterator>(Last)));
  231. auto IsVictim = [&](ModuleFile *MF) {
  232. return victimSet.count(MF);
  233. };
  234. // Remove any references to the now-destroyed modules.
  235. for (auto I = begin(); I != First; ++I) {
  236. I->Imports.remove_if(IsVictim);
  237. I->ImportedBy.remove_if(IsVictim);
  238. }
  239. llvm::erase_if(Roots, IsVictim);
  240. // Remove the modules from the PCH chain.
  241. for (auto I = First; I != Last; ++I) {
  242. if (!I->isModule()) {
  243. PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());
  244. break;
  245. }
  246. }
  247. // Delete the modules.
  248. for (ModuleIterator victim = First; victim != Last; ++victim)
  249. Modules.erase(victim->File);
  250. Chain.erase(Chain.begin() + (First - begin()), Chain.end());
  251. }
  252. void
  253. ModuleManager::addInMemoryBuffer(StringRef FileName,
  254. std::unique_ptr<llvm::MemoryBuffer> Buffer) {
  255. const FileEntry *Entry =
  256. FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
  257. InMemoryBuffers[Entry] = std::move(Buffer);
  258. }
  259. std::unique_ptr<ModuleManager::VisitState> ModuleManager::allocateVisitState() {
  260. // Fast path: if we have a cached state, use it.
  261. if (FirstVisitState) {
  262. auto Result = std::move(FirstVisitState);
  263. FirstVisitState = std::move(Result->NextState);
  264. return Result;
  265. }
  266. // Allocate and return a new state.
  267. return std::make_unique<VisitState>(size());
  268. }
  269. void ModuleManager::returnVisitState(std::unique_ptr<VisitState> State) {
  270. assert(State->NextState == nullptr && "Visited state is in list?");
  271. State->NextState = std::move(FirstVisitState);
  272. FirstVisitState = std::move(State);
  273. }
  274. void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
  275. GlobalIndex = Index;
  276. if (!GlobalIndex) {
  277. ModulesInCommonWithGlobalIndex.clear();
  278. return;
  279. }
  280. // Notify the global module index about all of the modules we've already
  281. // loaded.
  282. for (ModuleFile &M : *this)
  283. if (!GlobalIndex->loadedModuleFile(&M))
  284. ModulesInCommonWithGlobalIndex.push_back(&M);
  285. }
  286. void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
  287. if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
  288. return;
  289. ModulesInCommonWithGlobalIndex.push_back(MF);
  290. }
  291. ModuleManager::ModuleManager(FileManager &FileMgr,
  292. InMemoryModuleCache &ModuleCache,
  293. const PCHContainerReader &PCHContainerRdr,
  294. const HeaderSearch &HeaderSearchInfo)
  295. : FileMgr(FileMgr), ModuleCache(&ModuleCache),
  296. PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}
  297. void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
  298. llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
  299. // If the visitation order vector is the wrong size, recompute the order.
  300. if (VisitOrder.size() != Chain.size()) {
  301. unsigned N = size();
  302. VisitOrder.clear();
  303. VisitOrder.reserve(N);
  304. // Record the number of incoming edges for each module. When we
  305. // encounter a module with no incoming edges, push it into the queue
  306. // to seed the queue.
  307. SmallVector<ModuleFile *, 4> Queue;
  308. Queue.reserve(N);
  309. llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
  310. UnusedIncomingEdges.resize(size());
  311. for (ModuleFile &M : llvm::reverse(*this)) {
  312. unsigned Size = M.ImportedBy.size();
  313. UnusedIncomingEdges[M.Index] = Size;
  314. if (!Size)
  315. Queue.push_back(&M);
  316. }
  317. // Traverse the graph, making sure to visit a module before visiting any
  318. // of its dependencies.
  319. while (!Queue.empty()) {
  320. ModuleFile *CurrentModule = Queue.pop_back_val();
  321. VisitOrder.push_back(CurrentModule);
  322. // For any module that this module depends on, push it on the
  323. // stack (if it hasn't already been marked as visited).
  324. for (ModuleFile *M : llvm::reverse(CurrentModule->Imports)) {
  325. // Remove our current module as an impediment to visiting the
  326. // module we depend on. If we were the last unvisited module
  327. // that depends on this particular module, push it into the
  328. // queue to be visited.
  329. unsigned &NumUnusedEdges = UnusedIncomingEdges[M->Index];
  330. if (NumUnusedEdges && (--NumUnusedEdges == 0))
  331. Queue.push_back(M);
  332. }
  333. }
  334. assert(VisitOrder.size() == N && "Visitation order is wrong?");
  335. FirstVisitState = nullptr;
  336. }
  337. auto State = allocateVisitState();
  338. unsigned VisitNumber = State->NextVisitNumber++;
  339. // If the caller has provided us with a hit-set that came from the global
  340. // module index, mark every module file in common with the global module
  341. // index that is *not* in that set as 'visited'.
  342. if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
  343. for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
  344. {
  345. ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
  346. if (!ModuleFilesHit->count(M))
  347. State->VisitNumber[M->Index] = VisitNumber;
  348. }
  349. }
  350. for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
  351. ModuleFile *CurrentModule = VisitOrder[I];
  352. // Should we skip this module file?
  353. if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
  354. continue;
  355. // Visit the module.
  356. assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
  357. State->VisitNumber[CurrentModule->Index] = VisitNumber;
  358. if (!Visitor(*CurrentModule))
  359. continue;
  360. // The visitor has requested that cut off visitation of any
  361. // module that the current module depends on. To indicate this
  362. // behavior, we mark all of the reachable modules as having been visited.
  363. ModuleFile *NextModule = CurrentModule;
  364. do {
  365. // For any module that this module depends on, push it on the
  366. // stack (if it hasn't already been marked as visited).
  367. for (llvm::SetVector<ModuleFile *>::iterator
  368. M = NextModule->Imports.begin(),
  369. MEnd = NextModule->Imports.end();
  370. M != MEnd; ++M) {
  371. if (State->VisitNumber[(*M)->Index] != VisitNumber) {
  372. State->Stack.push_back(*M);
  373. State->VisitNumber[(*M)->Index] = VisitNumber;
  374. }
  375. }
  376. if (State->Stack.empty())
  377. break;
  378. // Pop the next module off the stack.
  379. NextModule = State->Stack.pop_back_val();
  380. } while (true);
  381. }
  382. returnVisitState(std::move(State));
  383. }
  384. bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,
  385. time_t ExpectedModTime,
  386. OptionalFileEntryRef &File) {
  387. File = std::nullopt;
  388. if (FileName == "-")
  389. return false;
  390. // Open the file immediately to ensure there is no race between stat'ing and
  391. // opening the file.
  392. OptionalFileEntryRef FileOrErr =
  393. expectedToOptional(FileMgr.getFileRef(FileName, /*OpenFile=*/true,
  394. /*CacheFailure=*/false));
  395. if (!FileOrErr)
  396. return false;
  397. File = *FileOrErr;
  398. if ((ExpectedSize && ExpectedSize != File->getSize()) ||
  399. (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
  400. // Do not destroy File, as it may be referenced. If we need to rebuild it,
  401. // it will be destroyed by removeModules.
  402. return true;
  403. return false;
  404. }
  405. #ifndef NDEBUG
  406. namespace llvm {
  407. template<>
  408. struct GraphTraits<ModuleManager> {
  409. using NodeRef = ModuleFile *;
  410. using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator;
  411. using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>;
  412. static ChildIteratorType child_begin(NodeRef Node) {
  413. return Node->Imports.begin();
  414. }
  415. static ChildIteratorType child_end(NodeRef Node) {
  416. return Node->Imports.end();
  417. }
  418. static nodes_iterator nodes_begin(const ModuleManager &Manager) {
  419. return nodes_iterator(Manager.begin());
  420. }
  421. static nodes_iterator nodes_end(const ModuleManager &Manager) {
  422. return nodes_iterator(Manager.end());
  423. }
  424. };
  425. template<>
  426. struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
  427. explicit DOTGraphTraits(bool IsSimple = false)
  428. : DefaultDOTGraphTraits(IsSimple) {}
  429. static bool renderGraphFromBottomUp() { return true; }
  430. std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
  431. return M->ModuleName;
  432. }
  433. };
  434. } // namespace llvm
  435. void ModuleManager::viewGraph() {
  436. llvm::ViewGraph(*this, "Modules");
  437. }
  438. #endif