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