clang 23.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"
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/SmallPtrSet.h"
24#include "llvm/ADT/SmallVector.h"
25#include "llvm/ADT/StringRef.h"
26#include "llvm/ADT/iterator.h"
27#include "llvm/ADT/iterator_range.h"
28#include <cstdint>
29#include <ctime>
30#include <memory>
31#include <string>
32#include <utility>
33
34namespace clang {
35
36class FileEntry;
37class FileManager;
39class HeaderSearch;
40class ModuleCache;
42
43namespace serialization {
44
45/// Manages the set of modules loaded by an AST reader.
47 /// The chain of AST files, in the order in which we started to load
48 /// them.
50
51 /// The chain of non-module PCH files. The first entry is the one named
52 /// by the user, the last one is the one that doesn't depend on anything
53 /// further.
55
56 // The roots of the dependency DAG of AST files. This is used
57 // to implement short-circuiting logic when running DFS over the dependencies.
59
60 /// All loaded modules.
61 llvm::DenseMap<ModuleFileKey, ModuleFile *> Modules;
62
63 /// FileManager that handles translating between filenames and
64 /// FileEntry *.
65 FileManager &FileMgr;
66
67 /// Cache of PCM files.
68 ModuleCache &ModCache;
69
70 /// Knows how to unwrap module containers.
71 const PCHContainerReader &PCHContainerRdr;
72
73 /// Preprocessor's HeaderSearchInfo containing the module map.
74 const HeaderSearch &HeaderSearchInfo;
75
76 /// A lookup of in-memory (virtual file) buffers
77 llvm::DenseMap<const FileEntry *, std::unique_ptr<llvm::MemoryBuffer>>
78 InMemoryBuffers;
79
80 /// The visitation order.
82
83 /// The list of module files that both we and the global module index
84 /// know about.
85 ///
86 /// Either the global index or the module manager may have modules that the
87 /// other does not know about, because the global index can be out-of-date
88 /// (in which case the module manager could have modules it does not) and
89 /// this particular translation unit might not have loaded all of the modules
90 /// known to the global index.
91 SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex;
92
93 /// The global module index, if one is attached.
94 ///
95 /// The global module index will actually be owned by the ASTReader; this is
96 /// just an non-owning pointer.
97 GlobalModuleIndex *GlobalIndex = nullptr;
98
99 /// State used by the "visit" operation to avoid malloc traffic in
100 /// calls to visit().
101 struct VisitState {
102 explicit VisitState(unsigned N) : VisitNumber(N, 0) {
103 Stack.reserve(N);
104 }
105
106 /// The stack used when marking the imports of a particular module
107 /// as not-to-be-visited.
109
110 /// The visit number of each module file, which indicates when
111 /// this module file was last visited.
112 SmallVector<unsigned, 4> VisitNumber;
113
114 /// The next visit number to use to mark visited module files.
115 unsigned NextVisitNumber = 1;
116
117 /// The next visit state.
118 std::unique_ptr<VisitState> NextState;
119 };
120
121 /// The first visit() state in the chain.
122 std::unique_ptr<VisitState> FirstVisitState;
123
124 std::unique_ptr<VisitState> allocateVisitState();
125 void returnVisitState(std::unique_ptr<VisitState> State);
126
127public:
128 using ModuleIterator = llvm::pointee_iterator<
130 using ModuleConstIterator = llvm::pointee_iterator<
132 using ModuleReverseIterator = llvm::pointee_iterator<
134 using ModuleOffset = std::pair<uint32_t, StringRef>;
135
136 ModuleManager(FileManager &FileMgr, ModuleCache &ModCache,
137 const PCHContainerReader &PCHContainerRdr,
138 const HeaderSearch &HeaderSearchInfo);
139
140 /// Forward iterator to traverse all loaded modules.
141 ModuleIterator begin() { return Chain.begin(); }
142
143 /// Forward iterator end-point to traverse all loaded modules
144 ModuleIterator end() { return Chain.end(); }
145
146 /// Const forward iterator to traverse all loaded modules.
147 ModuleConstIterator begin() const { return Chain.begin(); }
148
149 /// Const forward iterator end-point to traverse all loaded modules
150 ModuleConstIterator end() const { return Chain.end(); }
151
152 /// Reverse iterator to traverse all loaded modules.
153 ModuleReverseIterator rbegin() { return Chain.rbegin(); }
154
155 /// Reverse iterator end-point to traverse all loaded modules.
156 ModuleReverseIterator rend() { return Chain.rend(); }
157
158 /// A range covering the PCH and preamble module files loaded.
159 llvm::iterator_range<SmallVectorImpl<ModuleFile *>::const_iterator>
160 pch_modules() const {
161 return llvm::make_range(PCHChain.begin(), PCHChain.end());
162 }
163
164 /// Returns the primary module associated with the manager, that is,
165 /// the first module loaded
166 ModuleFile &getPrimaryModule() { return *Chain[0]; }
167
168 /// Returns the primary module associated with the manager, that is,
169 /// the first module loaded.
170 ModuleFile &getPrimaryModule() const { return *Chain[0]; }
171
172 /// Returns the module associated with the given index
173 ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
174
175 /// Returns the module associated with the given module name.
176 ModuleFile *lookupByModuleName(StringRef ModName) const;
177
178 /// Returns the module associated with the given module file name.
180
181 /// Returns the module associated with the given module file key.
182 ModuleFile *lookup(ModuleFileKey Key) const;
183
184 /// Returns the in-memory (virtual file) buffer with the given name
185 std::unique_ptr<llvm::MemoryBuffer> lookupBuffer(StringRef Name);
186
187 /// Number of modules loaded
188 unsigned size() const { return Chain.size(); }
189
190 /// The result of attempting to add a new module.
192 /// The module file had already been loaded.
194
195 /// The module file was just loaded in response to this call.
197
198 /// The module file is missing.
200
201 /// The module file is out-of-date.
203 };
204
206
207 /// Attempts to create a new module and add it to the list of known
208 /// modules.
209 ///
210 /// \param FileName The file name of the module to be loaded.
211 ///
212 /// \param Type The kind of module being loaded.
213 ///
214 /// \param ImportLoc The location at which the module is imported.
215 ///
216 /// \param ImportedBy The module that is importing this module, or NULL if
217 /// this module is imported directly by the user.
218 ///
219 /// \param Generation The generation in which this module was loaded.
220 ///
221 /// \param ExpectedSize The expected size of the module file, used for
222 /// validation. This will be zero if unknown.
223 ///
224 /// \param ExpectedModTime The expected modification time of the module
225 /// file, used for validation. This will be zero if unknown.
226 ///
227 /// \param ExpectedSignature The expected signature of the module file, used
228 /// for validation. This will be zero if unknown.
229 ///
230 /// \param ReadSignature Reads the signature from an AST file without actually
231 /// loading it.
232 ///
233 /// \param Module A pointer to the module file if the module was successfully
234 /// loaded.
235 ///
236 /// \param ErrorStr Will be set to a non-empty string if any errors occurred
237 /// while trying to load the module.
238 ///
239 /// \return A pointer to the module that corresponds to this file name,
240 /// and a value indicating whether the module was loaded.
242 SourceLocation ImportLoc, ModuleFile *ImportedBy,
243 unsigned Generation, off_t ExpectedSize,
244 time_t ExpectedModTime,
245 ASTFileSignature ExpectedSignature,
246 ASTFileSignatureReader ReadSignature,
247 ModuleFile *&Module, 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 /// View the graphviz representation of the module graph.
286 void viewGraph();
287
288 ModuleCache &getModuleCache() const { return ModCache; }
289};
290
291} // namespace serialization
292
293} // namespace clang
294
295#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
Deduplication key for a loaded module file in ModuleManager.
Definition Module.h:69
Identifies a module file to be loaded.
Definition Module.h:102
Describes a module or submodule.
Definition Module.h:246
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:1866
Information about a module that has been loaded by the ASTReader.
Definition ModuleFile.h:145
ModuleFile * lookup(ModuleFileKey Key) const
Returns the module associated with the given module file key.
llvm::pointee_iterator< SmallVectorImpl< std::unique_ptr< ModuleFile > >::iterator > ModuleIterator
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...
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.
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.
AddModuleResult addModule(ModuleFileName 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.
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.
ModuleFile * lookupByFileName(ModuleFileName FileName) const
Returns the module associated with the given module file name.
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.
The signature of a module, which is a hash of the AST content.
Definition Module.h:160