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