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