ModuleManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ModuleManager.cpp - Module Manager -----------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines the ModuleManager class, which manages a set of loaded
  15. // modules for the ASTReader.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
  19. #define LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
  20. #include "clang/Basic/LLVM.h"
  21. #include "clang/Basic/Module.h"
  22. #include "clang/Basic/SourceLocation.h"
  23. #include "clang/Serialization/ModuleFile.h"
  24. #include "llvm/ADT/DenseMap.h"
  25. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  26. #include "llvm/ADT/STLExtras.h"
  27. #include "llvm/ADT/SmallPtrSet.h"
  28. #include "llvm/ADT/SmallVector.h"
  29. #include "llvm/ADT/StringRef.h"
  30. #include "llvm/ADT/iterator.h"
  31. #include "llvm/ADT/iterator_range.h"
  32. #include <cstdint>
  33. #include <ctime>
  34. #include <memory>
  35. #include <string>
  36. #include <utility>
  37. namespace clang {
  38. class FileEntry;
  39. class FileManager;
  40. class GlobalModuleIndex;
  41. class HeaderSearch;
  42. class InMemoryModuleCache;
  43. class ModuleMap;
  44. class PCHContainerReader;
  45. namespace serialization {
  46. /// Manages the set of modules loaded by an AST reader.
  47. class ModuleManager {
  48. /// The chain of AST files, in the order in which we started to load
  49. /// them (this order isn't really useful for anything).
  50. SmallVector<std::unique_ptr<ModuleFile>, 2> Chain;
  51. /// The chain of non-module PCH files. The first entry is the one named
  52. /// by the user, the last one is the one that doesn't depend on anything
  53. /// further.
  54. SmallVector<ModuleFile *, 2> PCHChain;
  55. // The roots of the dependency DAG of AST files. This is used
  56. // to implement short-circuiting logic when running DFS over the dependencies.
  57. SmallVector<ModuleFile *, 2> Roots;
  58. /// All loaded modules, indexed by name.
  59. llvm::DenseMap<const FileEntry *, ModuleFile *> Modules;
  60. /// FileManager that handles translating between filenames and
  61. /// FileEntry *.
  62. FileManager &FileMgr;
  63. /// Cache of PCM files.
  64. IntrusiveRefCntPtr<InMemoryModuleCache> ModuleCache;
  65. /// Knows how to unwrap module containers.
  66. const PCHContainerReader &PCHContainerRdr;
  67. /// Preprocessor's HeaderSearchInfo containing the module map.
  68. const HeaderSearch &HeaderSearchInfo;
  69. /// A lookup of in-memory (virtual file) buffers
  70. llvm::DenseMap<const FileEntry *, std::unique_ptr<llvm::MemoryBuffer>>
  71. InMemoryBuffers;
  72. /// The visitation order.
  73. SmallVector<ModuleFile *, 4> VisitOrder;
  74. /// The list of module files that both we and the global module index
  75. /// know about.
  76. ///
  77. /// Either the global index or the module manager may have modules that the
  78. /// other does not know about, because the global index can be out-of-date
  79. /// (in which case the module manager could have modules it does not) and
  80. /// this particular translation unit might not have loaded all of the modules
  81. /// known to the global index.
  82. SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex;
  83. /// The global module index, if one is attached.
  84. ///
  85. /// The global module index will actually be owned by the ASTReader; this is
  86. /// just an non-owning pointer.
  87. GlobalModuleIndex *GlobalIndex = nullptr;
  88. /// State used by the "visit" operation to avoid malloc traffic in
  89. /// calls to visit().
  90. struct VisitState {
  91. explicit VisitState(unsigned N) : VisitNumber(N, 0) {
  92. Stack.reserve(N);
  93. }
  94. /// The stack used when marking the imports of a particular module
  95. /// as not-to-be-visited.
  96. SmallVector<ModuleFile *, 4> Stack;
  97. /// The visit number of each module file, which indicates when
  98. /// this module file was last visited.
  99. SmallVector<unsigned, 4> VisitNumber;
  100. /// The next visit number to use to mark visited module files.
  101. unsigned NextVisitNumber = 1;
  102. /// The next visit state.
  103. std::unique_ptr<VisitState> NextState;
  104. };
  105. /// The first visit() state in the chain.
  106. std::unique_ptr<VisitState> FirstVisitState;
  107. std::unique_ptr<VisitState> allocateVisitState();
  108. void returnVisitState(std::unique_ptr<VisitState> State);
  109. public:
  110. using ModuleIterator = llvm::pointee_iterator<
  111. SmallVectorImpl<std::unique_ptr<ModuleFile>>::iterator>;
  112. using ModuleConstIterator = llvm::pointee_iterator<
  113. SmallVectorImpl<std::unique_ptr<ModuleFile>>::const_iterator>;
  114. using ModuleReverseIterator = llvm::pointee_iterator<
  115. SmallVectorImpl<std::unique_ptr<ModuleFile>>::reverse_iterator>;
  116. using ModuleOffset = std::pair<uint32_t, StringRef>;
  117. explicit ModuleManager(FileManager &FileMgr, InMemoryModuleCache &ModuleCache,
  118. const PCHContainerReader &PCHContainerRdr,
  119. const HeaderSearch &HeaderSearchInfo);
  120. /// Forward iterator to traverse all loaded modules.
  121. ModuleIterator begin() { return Chain.begin(); }
  122. /// Forward iterator end-point to traverse all loaded modules
  123. ModuleIterator end() { return Chain.end(); }
  124. /// Const forward iterator to traverse all loaded modules.
  125. ModuleConstIterator begin() const { return Chain.begin(); }
  126. /// Const forward iterator end-point to traverse all loaded modules
  127. ModuleConstIterator end() const { return Chain.end(); }
  128. /// Reverse iterator to traverse all loaded modules.
  129. ModuleReverseIterator rbegin() { return Chain.rbegin(); }
  130. /// Reverse iterator end-point to traverse all loaded modules.
  131. ModuleReverseIterator rend() { return Chain.rend(); }
  132. /// A range covering the PCH and preamble module files loaded.
  133. llvm::iterator_range<SmallVectorImpl<ModuleFile *>::const_iterator>
  134. pch_modules() const {
  135. return llvm::make_range(PCHChain.begin(), PCHChain.end());
  136. }
  137. /// Returns the primary module associated with the manager, that is,
  138. /// the first module loaded
  139. ModuleFile &getPrimaryModule() { return *Chain[0]; }
  140. /// Returns the primary module associated with the manager, that is,
  141. /// the first module loaded.
  142. ModuleFile &getPrimaryModule() const { return *Chain[0]; }
  143. /// Returns the module associated with the given index
  144. ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
  145. /// Returns the module associated with the given file name.
  146. ModuleFile *lookupByFileName(StringRef FileName) const;
  147. /// Returns the module associated with the given module name.
  148. ModuleFile *lookupByModuleName(StringRef ModName) const;
  149. /// Returns the module associated with the given module file.
  150. ModuleFile *lookup(const FileEntry *File) const;
  151. /// Returns the in-memory (virtual file) buffer with the given name
  152. std::unique_ptr<llvm::MemoryBuffer> lookupBuffer(StringRef Name);
  153. /// Number of modules loaded
  154. unsigned size() const { return Chain.size(); }
  155. /// The result of attempting to add a new module.
  156. enum AddModuleResult {
  157. /// The module file had already been loaded.
  158. AlreadyLoaded,
  159. /// The module file was just loaded in response to this call.
  160. NewlyLoaded,
  161. /// The module file is missing.
  162. Missing,
  163. /// The module file is out-of-date.
  164. OutOfDate
  165. };
  166. using ASTFileSignatureReader = ASTFileSignature (*)(StringRef);
  167. /// Attempts to create a new module and add it to the list of known
  168. /// modules.
  169. ///
  170. /// \param FileName The file name of the module to be loaded.
  171. ///
  172. /// \param Type The kind of module being loaded.
  173. ///
  174. /// \param ImportLoc The location at which the module is imported.
  175. ///
  176. /// \param ImportedBy The module that is importing this module, or NULL if
  177. /// this module is imported directly by the user.
  178. ///
  179. /// \param Generation The generation in which this module was loaded.
  180. ///
  181. /// \param ExpectedSize The expected size of the module file, used for
  182. /// validation. This will be zero if unknown.
  183. ///
  184. /// \param ExpectedModTime The expected modification time of the module
  185. /// file, used for validation. This will be zero if unknown.
  186. ///
  187. /// \param ExpectedSignature The expected signature of the module file, used
  188. /// for validation. This will be zero if unknown.
  189. ///
  190. /// \param ReadSignature Reads the signature from an AST file without actually
  191. /// loading it.
  192. ///
  193. /// \param Module A pointer to the module file if the module was successfully
  194. /// loaded.
  195. ///
  196. /// \param ErrorStr Will be set to a non-empty string if any errors occurred
  197. /// while trying to load the module.
  198. ///
  199. /// \return A pointer to the module that corresponds to this file name,
  200. /// and a value indicating whether the module was loaded.
  201. AddModuleResult addModule(StringRef FileName, ModuleKind Type,
  202. SourceLocation ImportLoc,
  203. ModuleFile *ImportedBy, unsigned Generation,
  204. off_t ExpectedSize, time_t ExpectedModTime,
  205. ASTFileSignature ExpectedSignature,
  206. ASTFileSignatureReader ReadSignature,
  207. ModuleFile *&Module,
  208. std::string &ErrorStr);
  209. /// Remove the modules starting from First (to the end).
  210. void removeModules(ModuleIterator First, ModuleMap *modMap);
  211. /// Add an in-memory buffer the list of known buffers
  212. void addInMemoryBuffer(StringRef FileName,
  213. std::unique_ptr<llvm::MemoryBuffer> Buffer);
  214. /// Set the global module index.
  215. void setGlobalIndex(GlobalModuleIndex *Index);
  216. /// Notification from the AST reader that the given module file
  217. /// has been "accepted", and will not (can not) be unloaded.
  218. void moduleFileAccepted(ModuleFile *MF);
  219. /// Visit each of the modules.
  220. ///
  221. /// This routine visits each of the modules, starting with the
  222. /// "root" modules that no other loaded modules depend on, and
  223. /// proceeding to the leaf modules, visiting each module only once
  224. /// during the traversal.
  225. ///
  226. /// This traversal is intended to support various "lookup"
  227. /// operations that can find data in any of the loaded modules.
  228. ///
  229. /// \param Visitor A visitor function that will be invoked with each
  230. /// module. The return value must be convertible to bool; when false, the
  231. /// visitation continues to modules that the current module depends on. When
  232. /// true, the visitation skips any modules that the current module depends on.
  233. ///
  234. /// \param ModuleFilesHit If non-NULL, contains the set of module files
  235. /// that we know we need to visit because the global module index told us to.
  236. /// Any module that is known to both the global module index and the module
  237. /// manager that is *not* in this set can be skipped.
  238. void visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
  239. llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit = nullptr);
  240. /// Attempt to resolve the given module file name to a file entry.
  241. ///
  242. /// \param FileName The name of the module file.
  243. ///
  244. /// \param ExpectedSize The size that the module file is expected to have.
  245. /// If the actual size differs, the resolver should return \c true.
  246. ///
  247. /// \param ExpectedModTime The modification time that the module file is
  248. /// expected to have. If the actual modification time differs, the resolver
  249. /// should return \c true.
  250. ///
  251. /// \param File Will be set to the file if there is one, or null
  252. /// otherwise.
  253. ///
  254. /// \returns True if a file exists but does not meet the size/
  255. /// modification time criteria, false if the file is either available and
  256. /// suitable, or is missing.
  257. bool lookupModuleFile(StringRef FileName, off_t ExpectedSize,
  258. time_t ExpectedModTime, Optional<FileEntryRef> &File);
  259. /// View the graphviz representation of the module graph.
  260. void viewGraph();
  261. InMemoryModuleCache &getModuleCache() const { return *ModuleCache; }
  262. };
  263. } // namespace serialization
  264. } // namespace clang
  265. #endif // LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
  266. #ifdef __GNUC__
  267. #pragma GCC diagnostic pop
  268. #endif