ModuleManager.h 12 KB

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