clang 23.0.0git
ModuleManager.cpp
Go to the documentation of this file.
1//===- ModuleManager.cpp - Module Manager ---------------------------------===//
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
16#include "clang/Basic/LLVM.h"
18#include "clang/Lex/ModuleMap.h"
24#include "llvm/ADT/STLExtras.h"
25#include "llvm/ADT/SetVector.h"
26#include "llvm/ADT/SmallPtrSet.h"
27#include "llvm/ADT/SmallVector.h"
28#include "llvm/ADT/StringRef.h"
29#include "llvm/ADT/iterator.h"
30#include "llvm/Support/DOTGraphTraits.h"
31#include "llvm/Support/ErrorOr.h"
32#include "llvm/Support/GraphWriter.h"
33#include "llvm/Support/MemoryBuffer.h"
34#include "llvm/Support/VirtualFileSystem.h"
35#include <cassert>
36#include <memory>
37#include <string>
38#include <system_error>
39
40using namespace clang;
41using namespace serialization;
42
44 auto Entry = FileMgr.getOptionalFileRef(Name, /*OpenFile=*/false,
45 /*CacheFailure=*/false);
46 if (Entry)
47 return lookup(*Entry);
48
49 return nullptr;
50}
51
53 if (const Module *Mod = HeaderSearchInfo.getModuleMap().findModule(Name))
54 if (OptionalFileEntryRef File = Mod->getASTFile())
55 return lookup(*File);
56
57 return nullptr;
58}
59
61 return Modules.lookup(File);
62}
63
64std::unique_ptr<llvm::MemoryBuffer>
66 auto Entry = FileMgr.getOptionalFileRef(Name, /*OpenFile=*/false,
67 /*CacheFailure=*/false);
68 if (!Entry)
69 return nullptr;
70 return std::move(InMemoryBuffers[*Entry]);
71}
72
73static bool checkSignature(ASTFileSignature Signature,
74 ASTFileSignature ExpectedSignature,
75 std::string &ErrorStr) {
76 if (!ExpectedSignature || Signature == ExpectedSignature)
77 return false;
78
79 ErrorStr =
80 Signature ? "signature mismatch" : "could not read module signature";
81 return true;
82}
83
84static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy,
85 SourceLocation ImportLoc) {
86 if (ImportedBy) {
87 MF.ImportedBy.insert(ImportedBy);
88 ImportedBy->Imports.insert(&MF);
89 } else {
90 if (!MF.DirectlyImported)
91 MF.ImportLoc = ImportLoc;
92
93 MF.DirectlyImported = true;
94 }
95}
96
99 SourceLocation ImportLoc, ModuleFile *ImportedBy,
100 unsigned Generation,
101 off_t ExpectedSize, time_t ExpectedModTime,
102 ASTFileSignature ExpectedSignature,
103 ASTFileSignatureReader ReadSignature,
105 std::string &ErrorStr) {
106 Module = nullptr;
107
108 uint64_t InputFilesValidationTimestamp = 0;
109 if (Type == MK_ImplicitModule)
110 InputFilesValidationTimestamp = ModCache.getModuleTimestamp(FileName);
111
112 // Look for the file entry. This only fails if the expected size or
113 // modification time differ.
115 bool IgnoreModTime = Type == MK_ExplicitModule || Type == MK_PrebuiltModule;
116 if (ImportedBy)
117 IgnoreModTime &= ImportedBy->Kind == MK_ExplicitModule ||
118 ImportedBy->Kind == MK_PrebuiltModule;
119 if (IgnoreModTime) {
120 // If neither this file nor the importer are in the module cache, this file
121 // might have a different mtime due to being moved across filesystems in
122 // a distributed build. The size must still match, though. (As must the
123 // contents, but we can't check that.)
124 ExpectedModTime = 0;
125 }
126 // Note: ExpectedSize and ExpectedModTime will be 0 for MK_ImplicitModule
127 // when using an ASTFileSignature.
128 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
129 ErrorStr = IgnoreModTime ? "module file has a different size than expected"
130 : "module file has a different size or "
131 "modification time than expected";
132 return OutOfDate;
133 }
134
135 if (!Entry) {
136 ErrorStr = "module file not found";
137 return Missing;
138 }
139
140 // The ModuleManager's use of FileEntry nodes as the keys for its map of
141 // loaded modules is less than ideal. Uniqueness for FileEntry nodes is
142 // maintained by FileManager, which in turn uses inode numbers on hosts
143 // that support that. When coupled with the module cache's proclivity for
144 // turning over and deleting stale PCMs, this means entries for different
145 // module files can wind up reusing the same underlying inode. When this
146 // happens, subsequent accesses to the Modules map will disagree on the
147 // ModuleFile associated with a given file. In general, it is not sufficient
148 // to resolve this conundrum with a type like FileEntryRef that stores the
149 // name of the FileEntry node on first access because of path canonicalization
150 // issues. However, the paths constructed for implicit module builds are
151 // fully under Clang's control. We *can*, therefore, rely on their structure
152 // being consistent across operating systems and across subsequent accesses
153 // to the Modules map.
154 auto implicitModuleNamesMatch = [](ModuleKind Kind, const ModuleFile *MF,
155 FileEntryRef Entry) -> bool {
156 if (Kind != MK_ImplicitModule)
157 return true;
158 return Entry.getName() == MF->FileName;
159 };
160
161 // Check whether we already loaded this module, before
162 if (ModuleFile *ModuleEntry = Modules.lookup(*Entry)) {
163 if (implicitModuleNamesMatch(Type, ModuleEntry, *Entry)) {
164 // Check the stored signature.
165 if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))
166 return OutOfDate;
167
168 Module = ModuleEntry;
169 updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc);
170 return AlreadyLoaded;
171 }
172 }
173
174 // Allocate a new module.
175 auto NewModule = std::make_unique<ModuleFile>(Type, *Entry, Generation);
176 NewModule->Index = Chain.size();
177 NewModule->FileName = FileName.str();
178 NewModule->ImportLoc = ImportLoc;
179 NewModule->InputFilesValidationTimestamp = InputFilesValidationTimestamp;
180
181 // Load the contents of the module
182 std::unique_ptr<llvm::MemoryBuffer> NewFileBuffer = nullptr;
183 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
184 // The buffer was already provided for us.
185 NewModule->Buffer = &getModuleCache().getInMemoryModuleCache().addBuiltPCM(
186 FileName, std::move(Buffer));
187 // Since the cached buffer is reused, it is safe to close the file
188 // descriptor that was opened while stat()ing the PCM in
189 // lookupModuleFile() above, it won't be needed any longer.
190 Entry->closeFile();
191 } else if (llvm::MemoryBuffer *Buffer =
192 getModuleCache().getInMemoryModuleCache().lookupPCM(
193 FileName)) {
194 NewModule->Buffer = Buffer;
195 // As above, the file descriptor is no longer needed.
196 Entry->closeFile();
197 } else if (getModuleCache().getInMemoryModuleCache().shouldBuildPCM(
198 FileName)) {
199 // Report that the module is out of date, since we tried (and failed) to
200 // import it earlier.
201 Entry->closeFile();
202 return OutOfDate;
203 } else {
204 // Get a buffer of the file and close the file descriptor when done.
205 // The file is volatile because in a parallel build we expect multiple
206 // compiler processes to use the same module file rebuilding it if needed.
207 //
208 // RequiresNullTerminator is false because module files don't need it, and
209 // this allows the file to still be mmapped.
210 auto Buf = FileMgr.getBufferForFile(NewModule->File,
211 /*IsVolatile=*/true,
212 /*RequiresNullTerminator=*/false);
213
214 if (!Buf) {
215 ErrorStr = Buf.getError().message();
216 return Missing;
217 }
218
219 NewFileBuffer = std::move(*Buf);
220 NewModule->Buffer = NewFileBuffer.get();
221 }
222
223 // Initialize the stream.
224 NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer);
225
226 // Read the signature eagerly now so that we can check it. Avoid calling
227 // ReadSignature unless there's something to check though.
228 if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
229 ExpectedSignature, ErrorStr))
230 return OutOfDate;
231
232 if (NewFileBuffer)
234 std::move(NewFileBuffer));
235
236 // We're keeping this module. Store it everywhere.
237 Module = Modules[*Entry] = NewModule.get();
238
239 updateModuleImports(*NewModule, ImportedBy, ImportLoc);
240
241 if (!NewModule->isModule())
242 PCHChain.push_back(NewModule.get());
243 if (!ImportedBy)
244 Roots.push_back(NewModule.get());
245
246 Chain.push_back(std::move(NewModule));
247 return NewlyLoaded;
248}
249
251 auto Last = end();
252 if (First == Last)
253 return;
254
255 // Explicitly clear VisitOrder since we might not notice it is stale.
256 VisitOrder.clear();
257
258 // Collect the set of module file pointers that we'll be removing.
260 (llvm::pointer_iterator<ModuleIterator>(First)),
261 (llvm::pointer_iterator<ModuleIterator>(Last)));
262
263 auto IsVictim = [&](ModuleFile *MF) {
264 return victimSet.count(MF);
265 };
266 // Remove any references to the now-destroyed modules.
267 for (auto I = begin(); I != First; ++I) {
268 I->Imports.remove_if(IsVictim);
269 I->ImportedBy.remove_if(IsVictim);
270 }
271 llvm::erase_if(Roots, IsVictim);
272
273 // Remove the modules from the PCH chain.
274 for (auto I = First; I != Last; ++I) {
275 if (!I->isModule()) {
276 PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());
277 break;
278 }
279 }
280
281 // Delete the modules.
282 for (ModuleIterator victim = First; victim != Last; ++victim)
283 Modules.erase(victim->File);
284
285 Chain.erase(Chain.begin() + (First - begin()), Chain.end());
286}
287
288void
290 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
291 FileEntryRef Entry =
292 FileMgr.getVirtualFileRef(FileName, Buffer->getBufferSize(), 0);
293 InMemoryBuffers[Entry] = std::move(Buffer);
294}
295
296std::unique_ptr<ModuleManager::VisitState> ModuleManager::allocateVisitState() {
297 // Fast path: if we have a cached state, use it.
298 if (FirstVisitState) {
299 auto Result = std::move(FirstVisitState);
300 FirstVisitState = std::move(Result->NextState);
301 return Result;
302 }
303
304 // Allocate and return a new state.
305 return std::make_unique<VisitState>(size());
306}
307
308void ModuleManager::returnVisitState(std::unique_ptr<VisitState> State) {
309 assert(State->NextState == nullptr && "Visited state is in list?");
310 State->NextState = std::move(FirstVisitState);
311 FirstVisitState = std::move(State);
312}
313
315 GlobalIndex = Index;
316 if (!GlobalIndex) {
317 ModulesInCommonWithGlobalIndex.clear();
318 return;
319 }
320
321 // Notify the global module index about all of the modules we've already
322 // loaded.
323 for (ModuleFile &M : *this)
324 if (!GlobalIndex->loadedModuleFile(&M))
325 ModulesInCommonWithGlobalIndex.push_back(&M);
326}
327
329 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
330 return;
331
332 ModulesInCommonWithGlobalIndex.push_back(MF);
333}
334
336 const PCHContainerReader &PCHContainerRdr,
337 const HeaderSearch &HeaderSearchInfo)
338 : FileMgr(FileMgr), ModCache(ModCache), PCHContainerRdr(PCHContainerRdr),
339 HeaderSearchInfo(HeaderSearchInfo) {}
340
341void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
342 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
343 // If the visitation order vector is the wrong size, recompute the order.
344 if (VisitOrder.size() != Chain.size()) {
345 unsigned N = size();
346 VisitOrder.clear();
347 VisitOrder.reserve(N);
348
349 // Record the number of incoming edges for each module. When we
350 // encounter a module with no incoming edges, push it into the queue
351 // to seed the queue.
353 Queue.reserve(N);
354 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
355 UnusedIncomingEdges.resize(size());
356 for (ModuleFile &M : llvm::reverse(*this)) {
357 unsigned Size = M.ImportedBy.size();
358 UnusedIncomingEdges[M.Index] = Size;
359 if (!Size)
360 Queue.push_back(&M);
361 }
362
363 // Traverse the graph, making sure to visit a module before visiting any
364 // of its dependencies.
365 while (!Queue.empty()) {
366 ModuleFile *CurrentModule = Queue.pop_back_val();
367 VisitOrder.push_back(CurrentModule);
368
369 // For any module that this module depends on, push it on the
370 // stack (if it hasn't already been marked as visited).
371 for (ModuleFile *M : llvm::reverse(CurrentModule->Imports)) {
372 // Remove our current module as an impediment to visiting the
373 // module we depend on. If we were the last unvisited module
374 // that depends on this particular module, push it into the
375 // queue to be visited.
376 unsigned &NumUnusedEdges = UnusedIncomingEdges[M->Index];
377 if (NumUnusedEdges && (--NumUnusedEdges == 0))
378 Queue.push_back(M);
379 }
380 }
381
382 assert(VisitOrder.size() == N && "Visitation order is wrong?");
383
384 FirstVisitState = nullptr;
385 }
386
387 auto State = allocateVisitState();
388 unsigned VisitNumber = State->NextVisitNumber++;
389
390 // If the caller has provided us with a hit-set that came from the global
391 // module index, mark every module file in common with the global module
392 // index that is *not* in that set as 'visited'.
393 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
394 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
395 {
396 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
397 if (!ModuleFilesHit->count(M))
398 State->VisitNumber[M->Index] = VisitNumber;
399 }
400 }
401
402 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
403 ModuleFile *CurrentModule = VisitOrder[I];
404 // Should we skip this module file?
405 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
406 continue;
407
408 // Visit the module.
409 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
410 State->VisitNumber[CurrentModule->Index] = VisitNumber;
411 if (!Visitor(*CurrentModule))
412 continue;
413
414 // The visitor has requested that cut off visitation of any
415 // module that the current module depends on. To indicate this
416 // behavior, we mark all of the reachable modules as having been visited.
417 ModuleFile *NextModule = CurrentModule;
418 do {
419 // For any module that this module depends on, push it on the
420 // stack (if it hasn't already been marked as visited).
421 for (llvm::SetVector<ModuleFile *>::iterator
422 M = NextModule->Imports.begin(),
423 MEnd = NextModule->Imports.end();
424 M != MEnd; ++M) {
425 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
426 State->Stack.push_back(*M);
427 State->VisitNumber[(*M)->Index] = VisitNumber;
428 }
429 }
430
431 if (State->Stack.empty())
432 break;
433
434 // Pop the next module off the stack.
435 NextModule = State->Stack.pop_back_val();
436 } while (true);
437 }
438
439 returnVisitState(std::move(State));
440}
441
442bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,
443 time_t ExpectedModTime,
445 if (FileName == "-") {
446 File = expectedToOptional(FileMgr.getSTDIN());
447 return false;
448 }
449
450 // Open the file immediately to ensure there is no race between stat'ing and
451 // opening the file.
452 File = FileMgr.getOptionalFileRef(FileName, /*OpenFile=*/true,
453 /*CacheFailure=*/false);
454
455 if (File &&
456 ((ExpectedSize && ExpectedSize != File->getSize()) ||
457 (ExpectedModTime && ExpectedModTime != File->getModificationTime())))
458 // Do not destroy File, as it may be referenced. If we need to rebuild it,
459 // it will be destroyed by removeModules.
460 return true;
461
462 return false;
463}
464
465#ifndef NDEBUG
466namespace llvm {
467
468 template<>
469 struct GraphTraits<ModuleManager> {
471 using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator;
472 using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>;
473
475 return Node->Imports.begin();
476 }
477
479 return Node->Imports.end();
480 }
481
482 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
483 return nodes_iterator(Manager.begin());
484 }
485
486 static nodes_iterator nodes_end(const ModuleManager &Manager) {
487 return nodes_iterator(Manager.end());
488 }
489 };
490
491 template<>
493 explicit DOTGraphTraits(bool IsSimple = false)
494 : DefaultDOTGraphTraits(IsSimple) {}
495
496 static bool renderGraphFromBottomUp() { return true; }
497
498 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
499 return M->ModuleName;
500 }
501 };
502
503} // namespace llvm
504
506 llvm::ViewGraph(*this, "Modules");
507}
508#endif
Defines the clang::FileManager interface and associated types.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
static bool checkSignature(ASTFileSignature Signature, ASTFileSignature ExpectedSignature, std::string &ErrorStr)
static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy, SourceLocation ImportLoc)
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition FileEntry.h:57
void closeFile() const
Definition FileEntry.h:363
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,...
llvm::MemoryBuffer & addBuiltPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer)
Store a just-built PCM under the Filename.
llvm::MemoryBuffer & addPCM(llvm::StringRef Filename, std::unique_ptr< llvm::MemoryBuffer > Buffer)
Store the PCM under the Filename.
The module cache used for compiling modules implicitly.
Definition ModuleCache.h:25
virtual InMemoryModuleCache & getInMemoryModuleCache()=0
Returns this process's view of the module cache.
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
bool DirectlyImported
Whether this module has been directly imported by the user.
Definition ModuleFile.h:203
llvm::SetVector< ModuleFile * > ImportedBy
List of modules which depend on this module.
Definition ModuleFile.h:491
SourceLocation ImportLoc
The source location where this module was first imported.
Definition ModuleFile.h:239
unsigned Index
The index of this module in the list of modules.
Definition ModuleFile.h:139
llvm::SetVector< ModuleFile * > Imports
List of modules which this module directly imported.
Definition ModuleFile.h:494
ModuleKind Kind
The type of this module.
Definition ModuleFile.h:142
std::string ModuleName
The name of the module.
Definition ModuleFile.h:148
Manages the set of modules loaded by an AST reader.
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.
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.
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.
ModuleManager(FileManager &FileMgr, ModuleCache &ModCache, const PCHContainerReader &PCHContainerRdr, const HeaderSearch &HeaderSearchInfo)
void viewGraph()
View the graphviz representation of the module graph.
ModuleCache & getModuleCache() const
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).
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.
void visit(llvm::function_ref< bool(ModuleFile &M)> Visitor, llvm::SmallPtrSetImpl< ModuleFile * > *ModuleFilesHit=nullptr)
Visit each of the modules.
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
@ MK_ExplicitModule
File is an explicitly-loaded module.
Definition ModuleFile.h:48
@ MK_ImplicitModule
File is an implicitly-loaded module.
Definition ModuleFile.h:45
@ MK_PrebuiltModule
File is from a prebuilt module path.
Definition ModuleFile.h:60
The JSON file list parser is used to communicate input to InstallAPI.
CustomizableOptional< FileEntryRef > OptionalFileEntryRef
Definition FileEntry.h:208
@ Result
The result type of a method or function.
Definition TypeBase.h:905
Diagnostic wrappers for TextAPI types for error reporting.
Definition Dominators.h:30
The signature of a module, which is a hash of the AST content.
Definition Module.h:58
std::string getNodeLabel(ModuleFile *M, const ModuleManager &)
static ChildIteratorType child_end(NodeRef Node)
static nodes_iterator nodes_begin(const ModuleManager &Manager)
static ChildIteratorType child_begin(NodeRef Node)
pointer_iterator< ModuleManager::ModuleConstIterator > nodes_iterator
llvm::SetVector< ModuleFile * >::const_iterator ChildIteratorType
static nodes_iterator nodes_end(const ModuleManager &Manager)