ModuleManager.h 11 KB

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