ModuleManager.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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, ModuleMap *modMap) {
  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 and erase them from the various structures.
  248. for (ModuleIterator victim = First; victim != Last; ++victim) {
  249. Modules.erase(victim->File);
  250. if (modMap) {
  251. StringRef ModuleName = victim->ModuleName;
  252. if (Module *mod = modMap->findModule(ModuleName)) {
  253. mod->setASTFile(None);
  254. }
  255. }
  256. }
  257. // Delete the modules.
  258. Chain.erase(Chain.begin() + (First - begin()), Chain.end());
  259. }
  260. void
  261. ModuleManager::addInMemoryBuffer(StringRef FileName,
  262. std::unique_ptr<llvm::MemoryBuffer> Buffer) {
  263. const FileEntry *Entry =
  264. FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
  265. InMemoryBuffers[Entry] = std::move(Buffer);
  266. }
  267. std::unique_ptr<ModuleManager::VisitState> ModuleManager::allocateVisitState() {
  268. // Fast path: if we have a cached state, use it.
  269. if (FirstVisitState) {
  270. auto Result = std::move(FirstVisitState);
  271. FirstVisitState = std::move(Result->NextState);
  272. return Result;
  273. }
  274. // Allocate and return a new state.
  275. return std::make_unique<VisitState>(size());
  276. }
  277. void ModuleManager::returnVisitState(std::unique_ptr<VisitState> State) {
  278. assert(State->NextState == nullptr && "Visited state is in list?");
  279. State->NextState = std::move(FirstVisitState);
  280. FirstVisitState = std::move(State);
  281. }
  282. void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
  283. GlobalIndex = Index;
  284. if (!GlobalIndex) {
  285. ModulesInCommonWithGlobalIndex.clear();
  286. return;
  287. }
  288. // Notify the global module index about all of the modules we've already
  289. // loaded.
  290. for (ModuleFile &M : *this)
  291. if (!GlobalIndex->loadedModuleFile(&M))
  292. ModulesInCommonWithGlobalIndex.push_back(&M);
  293. }
  294. void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
  295. if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
  296. return;
  297. ModulesInCommonWithGlobalIndex.push_back(MF);
  298. }
  299. ModuleManager::ModuleManager(FileManager &FileMgr,
  300. InMemoryModuleCache &ModuleCache,
  301. const PCHContainerReader &PCHContainerRdr,
  302. const HeaderSearch &HeaderSearchInfo)
  303. : FileMgr(FileMgr), ModuleCache(&ModuleCache),
  304. PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}
  305. void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
  306. llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
  307. // If the visitation order vector is the wrong size, recompute the order.
  308. if (VisitOrder.size() != Chain.size()) {
  309. unsigned N = size();
  310. VisitOrder.clear();
  311. VisitOrder.reserve(N);
  312. // Record the number of incoming edges for each module. When we
  313. // encounter a module with no incoming edges, push it into the queue
  314. // to seed the queue.
  315. SmallVector<ModuleFile *, 4> Queue;
  316. Queue.reserve(N);
  317. llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
  318. UnusedIncomingEdges.resize(size());
  319. for (ModuleFile &M : llvm::reverse(*this)) {
  320. unsigned Size = M.ImportedBy.size();
  321. UnusedIncomingEdges[M.Index] = Size;
  322. if (!Size)
  323. Queue.push_back(&M);
  324. }
  325. // Traverse the graph, making sure to visit a module before visiting any
  326. // of its dependencies.
  327. while (!Queue.empty()) {
  328. ModuleFile *CurrentModule = Queue.pop_back_val();
  329. VisitOrder.push_back(CurrentModule);
  330. // For any module that this module depends on, push it on the
  331. // stack (if it hasn't already been marked as visited).
  332. for (ModuleFile *M : llvm::reverse(CurrentModule->Imports)) {
  333. // Remove our current module as an impediment to visiting the
  334. // module we depend on. If we were the last unvisited module
  335. // that depends on this particular module, push it into the
  336. // queue to be visited.
  337. unsigned &NumUnusedEdges = UnusedIncomingEdges[M->Index];
  338. if (NumUnusedEdges && (--NumUnusedEdges == 0))
  339. Queue.push_back(M);
  340. }
  341. }
  342. assert(VisitOrder.size() == N && "Visitation order is wrong?");
  343. FirstVisitState = nullptr;
  344. }
  345. auto State = allocateVisitState();
  346. unsigned VisitNumber = State->NextVisitNumber++;
  347. // If the caller has provided us with a hit-set that came from the global
  348. // module index, mark every module file in common with the global module
  349. // index that is *not* in that set as 'visited'.
  350. if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
  351. for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
  352. {
  353. ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
  354. if (!ModuleFilesHit->count(M))
  355. State->VisitNumber[M->Index] = VisitNumber;
  356. }
  357. }
  358. for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
  359. ModuleFile *CurrentModule = VisitOrder[I];
  360. // Should we skip this module file?
  361. if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
  362. continue;
  363. // Visit the module.
  364. assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
  365. State->VisitNumber[CurrentModule->Index] = VisitNumber;
  366. if (!Visitor(*CurrentModule))
  367. continue;
  368. // The visitor has requested that cut off visitation of any
  369. // module that the current module depends on. To indicate this
  370. // behavior, we mark all of the reachable modules as having been visited.
  371. ModuleFile *NextModule = CurrentModule;
  372. do {
  373. // For any module that this module depends on, push it on the
  374. // stack (if it hasn't already been marked as visited).
  375. for (llvm::SetVector<ModuleFile *>::iterator
  376. M = NextModule->Imports.begin(),
  377. MEnd = NextModule->Imports.end();
  378. M != MEnd; ++M) {
  379. if (State->VisitNumber[(*M)->Index] != VisitNumber) {
  380. State->Stack.push_back(*M);
  381. State->VisitNumber[(*M)->Index] = VisitNumber;
  382. }
  383. }
  384. if (State->Stack.empty())
  385. break;
  386. // Pop the next module off the stack.
  387. NextModule = State->Stack.pop_back_val();
  388. } while (true);
  389. }
  390. returnVisitState(std::move(State));
  391. }
  392. bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,
  393. time_t ExpectedModTime,
  394. Optional<FileEntryRef> &File) {
  395. File = None;
  396. if (FileName == "-")
  397. return false;
  398. // Open the file immediately to ensure there is no race between stat'ing and
  399. // opening the file.
  400. Optional<FileEntryRef> FileOrErr =
  401. expectedToOptional(FileMgr.getFileRef(FileName, /*OpenFile=*/true,
  402. /*CacheFailure=*/false));
  403. if (!FileOrErr)
  404. return false;
  405. File = *FileOrErr;
  406. if ((ExpectedSize && ExpectedSize != File->getSize()) ||
  407. (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
  408. // Do not destroy File, as it may be referenced. If we need to rebuild it,
  409. // it will be destroyed by removeModules.
  410. return true;
  411. return false;
  412. }
  413. #ifndef NDEBUG
  414. namespace llvm {
  415. template<>
  416. struct GraphTraits<ModuleManager> {
  417. using NodeRef = ModuleFile *;
  418. using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator;
  419. using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>;
  420. static ChildIteratorType child_begin(NodeRef Node) {
  421. return Node->Imports.begin();
  422. }
  423. static ChildIteratorType child_end(NodeRef Node) {
  424. return Node->Imports.end();
  425. }
  426. static nodes_iterator nodes_begin(const ModuleManager &Manager) {
  427. return nodes_iterator(Manager.begin());
  428. }
  429. static nodes_iterator nodes_end(const ModuleManager &Manager) {
  430. return nodes_iterator(Manager.end());
  431. }
  432. };
  433. template<>
  434. struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
  435. explicit DOTGraphTraits(bool IsSimple = false)
  436. : DefaultDOTGraphTraits(IsSimple) {}
  437. static bool renderGraphFromBottomUp() { return true; }
  438. std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
  439. return M->ModuleName;
  440. }
  441. };
  442. } // namespace llvm
  443. void ModuleManager::viewGraph() {
  444. llvm::ViewGraph(*this, "Modules");
  445. }
  446. #endif