clang  15.0.0git
ModuleManager.h
Go to the documentation of this file.
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"
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.
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).
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.
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.
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.
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.
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.
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<
132  using ModuleConstIterator = llvm::pointee_iterator<
134  using ModuleReverseIterator = llvm::pointee_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.
194  /// The module file had already been loaded.
196 
197  /// The module file was just loaded in response to this call.
199 
200  /// The module file is missing.
202 
203  /// The module file is out-of-date.
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
clang::GlobalModuleIndex
A global index for a set of module files, providing information about the identifiers within those mo...
Definition: GlobalModuleIndex.h:54
clang::serialization::ModuleManager::setGlobalIndex
void setGlobalIndex(GlobalModuleIndex *Index)
Set the global module index.
Definition: ModuleManager.cpp:325
clang::serialization::ModuleManager
Manages the set of modules loaded by an AST reader.
Definition: ModuleManager.h:48
clang::serialization::ModuleManager::OutOfDate
@ OutOfDate
The module file is out-of-date.
Definition: ModuleManager.h:204
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::serialization::ModuleManager::addInMemoryBuffer
void addInMemoryBuffer(StringRef FileName, std::unique_ptr< llvm::MemoryBuffer > Buffer)
Add an in-memory buffer the list of known buffers.
Definition: ModuleManager.cpp:300
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::serialization::ModuleManager::removeModules
void removeModules(ModuleIterator First, ModuleMap *modMap)
Remove the modules starting from First (to the end).
Definition: ModuleManager.cpp:252
Module.h
llvm::Optional< FileEntryRef >
clang::serialization::ModuleManager::size
unsigned size() const
Number of modules loaded.
Definition: ModuleManager.h:190
clang::serialization::ModuleManager::AlreadyLoaded
@ AlreadyLoaded
The module file had already been loaded.
Definition: ModuleManager.h:195
clang::serialization::ModuleManager::ModuleReverseIterator
llvm::pointee_iterator< SmallVectorImpl< std::unique_ptr< ModuleFile > >::reverse_iterator > ModuleReverseIterator
Definition: ModuleManager.h:135
clang::serialization::ModuleManager::ModuleManager
ModuleManager(FileManager &FileMgr, InMemoryModuleCache &ModuleCache, const PCHContainerReader &PCHContainerRdr, const HeaderSearch &HeaderSearchInfo)
Definition: ModuleManager.cpp:346
clang::PCHContainerReader
This abstract interface provides operations for unwrapping containers for serialized ASTs (precompile...
Definition: PCHContainerOperations.h:55
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1556
clang::HeaderSearch
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
Definition: HeaderSearch.h:223
clang::Module
Describes a module or submodule.
Definition: Module.h:96
clang::serialization::ModuleManager::getPrimaryModule
ModuleFile & getPrimaryModule() const
Returns the primary module associated with the manager, that is, the first module loaded.
Definition: ModuleManager.h:172
clang::serialization::ModuleManager::ModuleIterator
llvm::pointee_iterator< SmallVectorImpl< std::unique_ptr< ModuleFile > >::iterator > ModuleIterator
Definition: ModuleManager.h:131
clang::serialization::ModuleManager::lookupBuffer
std::unique_ptr< llvm::MemoryBuffer > lookupBuffer(StringRef Name)
Returns the in-memory (virtual file) buffer with the given name.
Definition: ModuleManager.cpp:70
clang::serialization::ModuleManager::visit
void visit(llvm::function_ref< bool(ModuleFile &M)> Visitor, llvm::SmallPtrSetImpl< ModuleFile * > *ModuleFilesHit=nullptr)
Visit each of the modules.
Definition: ModuleManager.cpp:353
clang::serialization::ModuleManager::addModule
AddModuleResult addModule(StringRef FileName, ModuleKind Type, SourceLocation ImportLoc, ModuleFile *ImportedBy, unsigned Generation, off_t ExpectedSize, time_t ExpectedModTime, ASTFileSignature ExpectedSignature, ASTFileSignatureReader ReadSignature, ModuleFile *&Module, std::string &ErrorStr)
Attempts to create a new module and add it to the list of known modules.
Definition: ModuleManager.cpp:103
clang::serialization::ModuleManager::viewGraph
void viewGraph()
View the graphviz representation of the module graph.
Definition: ModuleManager.cpp:520
clang::ASTFileSignature
The signature of a module, which is a hash of the AST content.
Definition: Module.h:56
clang::FileEntry
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:330
clang::ModuleMap
Definition: ModuleMap.h:75
clang::serialization::ModuleManager::pch_modules
llvm::iterator_range< SmallVectorImpl< ModuleFile * >::const_iterator > pch_modules() const
A range covering the PCH and preamble module files loaded.
Definition: ModuleManager.h:162
clang::serialization::ModuleManager::Missing
@ Missing
The module file is missing.
Definition: ModuleManager.h:201
clang::serialization::ModuleManager::operator[]
ModuleFile & operator[](unsigned Index) const
Returns the module associated with the given index.
Definition: ModuleManager.h:175
clang::serialization::ModuleManager::getModuleCache
InMemoryModuleCache & getModuleCache() const
Definition: ModuleManager.h:311
clang::serialization::ModuleManager::lookupByModuleName
ModuleFile * lookupByModuleName(StringRef ModName) const
Returns the module associated with the given module name.
Definition: ModuleManager.cpp:53
clang::serialization::ModuleManager::lookupModuleFile
bool lookupModuleFile(StringRef FileName, off_t ExpectedSize, time_t ExpectedModTime, Optional< FileEntryRef > &File)
Attempt to resolve the given module file name to a file entry.
Definition: ModuleManager.cpp:454
SourceLocation.h
clang::serialization::ModuleManager::lookupByFileName
ModuleFile * lookupByFileName(StringRef FileName) const
Returns the module associated with the given file name.
Definition: ModuleManager.cpp:44
clang::serialization::ModuleManager::ASTFileSignatureReader
ASTFileSignature(*)(StringRef) ASTFileSignatureReader
Definition: ModuleManager.h:207
clang::serialization::ModuleManager::moduleFileAccepted
void moduleFileAccepted(ModuleFile *MF)
Notification from the AST reader that the given module file has been "accepted", and will not (can no...
Definition: ModuleManager.cpp:339
clang::serialization::ModuleManager::NewlyLoaded
@ NewlyLoaded
The module file was just loaded in response to this call.
Definition: ModuleManager.h:198
LLVM.h
State
LineState State
Definition: UnwrappedLineFormatter.cpp:1126
clang::serialization::ModuleManager::getPrimaryModule
ModuleFile & getPrimaryModule()
Returns the primary module associated with the manager, that is, the first module loaded.
Definition: ModuleManager.h:168
clang::serialization::ModuleManager::ModuleConstIterator
llvm::pointee_iterator< SmallVectorImpl< std::unique_ptr< ModuleFile > >::const_iterator > ModuleConstIterator
Definition: ModuleManager.h:133
clang::serialization::ModuleManager::begin
ModuleIterator begin()
Forward iterator to traverse all loaded modules.
Definition: ModuleManager.h:143
clang::serialization::ModuleManager::rbegin
ModuleReverseIterator rbegin()
Reverse iterator to traverse all loaded modules.
Definition: ModuleManager.h:155
ModuleFile.h
clang::serialization::ModuleManager::rend
ModuleReverseIterator rend()
Reverse iterator end-point to traverse all loaded modules.
Definition: ModuleManager.h:158
clang::serialization::ModuleManager::ModuleOffset
std::pair< uint32_t, StringRef > ModuleOffset
Definition: ModuleManager.h:136
clang
Definition: CalledOnceCheck.h:17
clang::FileManager
Implements support for file system lookup, file system caching, and directory search management.
Definition: FileManager.h:53
clang::serialization::ModuleManager::AddModuleResult
AddModuleResult
The result of attempting to add a new module.
Definition: ModuleManager.h:193
clang::InMemoryModuleCache
In-memory cache for modules.
Definition: InMemoryModuleCache.h:30
clang::serialization::ModuleManager::lookup
ModuleFile * lookup(const FileEntry *File) const
Returns the module associated with the given module file.
Definition: ModuleManager.cpp:61
clang::serialization::ModuleManager::begin
ModuleConstIterator begin() const
Const forward iterator to traverse all loaded modules.
Definition: ModuleManager.h:149
clang::serialization::ModuleManager::end
ModuleIterator end()
Forward iterator end-point to traverse all loaded modules.
Definition: ModuleManager.h:146
clang::serialization::ModuleKind
ModuleKind
Specifies the kind of module that has been loaded.
Definition: ModuleFile.h:42
llvm::SmallVectorImpl
Definition: Randstruct.h:18
clang::serialization::ModuleManager::end
ModuleConstIterator end() const
Const forward iterator end-point to traverse all loaded modules.
Definition: ModuleManager.h:152
clang::serialization::ModuleFile
Information about a module that has been loaded by the ASTReader.
Definition: ModuleFile.h:111
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:47