ModuleManager.h 12 KB

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