1 //===- ModuleManager.cpp - Module Manager ---------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the ModuleManager class, which manages a set of loaded
10 // modules for the ASTReader.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Serialization/ModuleManager.h"
15 #include "clang/Basic/FileManager.h"
16 #include "clang/Basic/LLVM.h"
17 #include "clang/Lex/HeaderSearch.h"
18 #include "clang/Lex/ModuleMap.h"
19 #include "clang/Serialization/GlobalModuleIndex.h"
20 #include "clang/Serialization/InMemoryModuleCache.h"
21 #include "clang/Serialization/ModuleFile.h"
22 #include "clang/Serialization/PCHContainerOperations.h"
23 #include "llvm/ADT/STLExtras.h"
24 #include "llvm/ADT/SetVector.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/iterator.h"
29 #include "llvm/Support/Chrono.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"
39 #include <system_error>
41 using namespace clang
;
42 using namespace serialization
;
44 ModuleFile
*ModuleManager::lookupByFileName(StringRef Name
) const {
45 auto Entry
= FileMgr
.getFile(Name
, /*OpenFile=*/false,
46 /*CacheFailure=*/false);
48 return lookup(*Entry
);
53 ModuleFile
*ModuleManager::lookupByModuleName(StringRef Name
) const {
54 if (const Module
*Mod
= HeaderSearchInfo
.getModuleMap().findModule(Name
))
55 if (OptionalFileEntryRef File
= Mod
->getASTFile())
61 ModuleFile
*ModuleManager::lookup(const FileEntry
*File
) const {
62 return Modules
.lookup(File
);
65 std::unique_ptr
<llvm::MemoryBuffer
>
66 ModuleManager::lookupBuffer(StringRef Name
) {
67 auto Entry
= FileMgr
.getFile(Name
, /*OpenFile=*/false,
68 /*CacheFailure=*/false);
71 return std::move(InMemoryBuffers
[*Entry
]);
74 static bool checkSignature(ASTFileSignature Signature
,
75 ASTFileSignature ExpectedSignature
,
76 std::string
&ErrorStr
) {
77 if (!ExpectedSignature
|| Signature
== ExpectedSignature
)
81 Signature
? "signature mismatch" : "could not read module signature";
85 static void updateModuleImports(ModuleFile
&MF
, ModuleFile
*ImportedBy
,
86 SourceLocation ImportLoc
) {
88 MF
.ImportedBy
.insert(ImportedBy
);
89 ImportedBy
->Imports
.insert(&MF
);
91 if (!MF
.DirectlyImported
)
92 MF
.ImportLoc
= ImportLoc
;
94 MF
.DirectlyImported
= true;
98 ModuleManager::AddModuleResult
99 ModuleManager::addModule(StringRef FileName
, ModuleKind Type
,
100 SourceLocation ImportLoc
, ModuleFile
*ImportedBy
,
102 off_t ExpectedSize
, time_t ExpectedModTime
,
103 ASTFileSignature ExpectedSignature
,
104 ASTFileSignatureReader ReadSignature
,
106 std::string
&ErrorStr
) {
109 // Look for the file entry. This only fails if the expected size or
110 // modification time differ.
111 OptionalFileEntryRef Entry
;
112 if (Type
== MK_ExplicitModule
|| Type
== MK_PrebuiltModule
) {
113 // If we're not expecting to pull this file out of the module cache, it
114 // might have a different mtime due to being moved across filesystems in
115 // a distributed build. The size must still match, though. (As must the
116 // contents, but we can't check that.)
119 // Note: ExpectedSize and ExpectedModTime will be 0 for MK_ImplicitModule
120 // when using an ASTFileSignature.
121 if (lookupModuleFile(FileName
, ExpectedSize
, ExpectedModTime
, Entry
)) {
122 ErrorStr
= "module file out of date";
127 ErrorStr
= "module file not found";
131 // The ModuleManager's use of FileEntry nodes as the keys for its map of
132 // loaded modules is less than ideal. Uniqueness for FileEntry nodes is
133 // maintained by FileManager, which in turn uses inode numbers on hosts
134 // that support that. When coupled with the module cache's proclivity for
135 // turning over and deleting stale PCMs, this means entries for different
136 // module files can wind up reusing the same underlying inode. When this
137 // happens, subsequent accesses to the Modules map will disagree on the
138 // ModuleFile associated with a given file. In general, it is not sufficient
139 // to resolve this conundrum with a type like FileEntryRef that stores the
140 // name of the FileEntry node on first access because of path canonicalization
141 // issues. However, the paths constructed for implicit module builds are
142 // fully under Clang's control. We *can*, therefore, rely on their structure
143 // being consistent across operating systems and across subsequent accesses
144 // to the Modules map.
145 auto implicitModuleNamesMatch
= [](ModuleKind Kind
, const ModuleFile
*MF
,
146 FileEntryRef Entry
) -> bool {
147 if (Kind
!= MK_ImplicitModule
)
149 return Entry
.getName() == MF
->FileName
;
152 // Check whether we already loaded this module, before
153 if (ModuleFile
*ModuleEntry
= Modules
.lookup(*Entry
)) {
154 if (implicitModuleNamesMatch(Type
, ModuleEntry
, *Entry
)) {
155 // Check the stored signature.
156 if (checkSignature(ModuleEntry
->Signature
, ExpectedSignature
, ErrorStr
))
159 Module
= ModuleEntry
;
160 updateModuleImports(*ModuleEntry
, ImportedBy
, ImportLoc
);
161 return AlreadyLoaded
;
165 // Allocate a new module.
166 auto NewModule
= std::make_unique
<ModuleFile
>(Type
, *Entry
, Generation
);
167 NewModule
->Index
= Chain
.size();
168 NewModule
->FileName
= FileName
.str();
169 NewModule
->ImportLoc
= ImportLoc
;
170 NewModule
->InputFilesValidationTimestamp
= 0;
172 if (NewModule
->Kind
== MK_ImplicitModule
) {
173 std::string TimestampFilename
= NewModule
->getTimestampFilename();
174 llvm::vfs::Status Status
;
175 // A cached stat value would be fine as well.
176 if (!FileMgr
.getNoncachedStatValue(TimestampFilename
, Status
))
177 NewModule
->InputFilesValidationTimestamp
=
178 llvm::sys::toTimeT(Status
.getLastModificationTime());
181 // Load the contents of the module
182 if (std::unique_ptr
<llvm::MemoryBuffer
> Buffer
= lookupBuffer(FileName
)) {
183 // The buffer was already provided for us.
184 NewModule
->Buffer
= &ModuleCache
->addBuiltPCM(FileName
, std::move(Buffer
));
185 // Since the cached buffer is reused, it is safe to close the file
186 // descriptor that was opened while stat()ing the PCM in
187 // lookupModuleFile() above, it won't be needed any longer.
189 } else if (llvm::MemoryBuffer
*Buffer
=
190 getModuleCache().lookupPCM(FileName
)) {
191 NewModule
->Buffer
= Buffer
;
192 // As above, the file descriptor is no longer needed.
194 } else if (getModuleCache().shouldBuildPCM(FileName
)) {
195 // Report that the module is out of date, since we tried (and failed) to
196 // import it earlier.
200 // Get a buffer of the file and close the file descriptor when done.
201 // The file is volatile because in a parallel build we expect multiple
202 // compiler processes to use the same module file rebuilding it if needed.
204 // RequiresNullTerminator is false because module files don't need it, and
205 // this allows the file to still be mmapped.
206 auto Buf
= FileMgr
.getBufferForFile(NewModule
->File
,
208 /*RequiresNullTerminator=*/false);
211 ErrorStr
= Buf
.getError().message();
215 NewModule
->Buffer
= &getModuleCache().addPCM(FileName
, std::move(*Buf
));
218 // Initialize the stream.
219 NewModule
->Data
= PCHContainerRdr
.ExtractPCH(*NewModule
->Buffer
);
221 // Read the signature eagerly now so that we can check it. Avoid calling
222 // ReadSignature unless there's something to check though.
223 if (ExpectedSignature
&& checkSignature(ReadSignature(NewModule
->Data
),
224 ExpectedSignature
, ErrorStr
))
227 // We're keeping this module. Store it everywhere.
228 Module
= Modules
[*Entry
] = NewModule
.get();
230 updateModuleImports(*NewModule
, ImportedBy
, ImportLoc
);
232 if (!NewModule
->isModule())
233 PCHChain
.push_back(NewModule
.get());
235 Roots
.push_back(NewModule
.get());
237 Chain
.push_back(std::move(NewModule
));
241 void ModuleManager::removeModules(ModuleIterator First
) {
246 // Explicitly clear VisitOrder since we might not notice it is stale.
249 // Collect the set of module file pointers that we'll be removing.
250 llvm::SmallPtrSet
<ModuleFile
*, 4> victimSet(
251 (llvm::pointer_iterator
<ModuleIterator
>(First
)),
252 (llvm::pointer_iterator
<ModuleIterator
>(Last
)));
254 auto IsVictim
= [&](ModuleFile
*MF
) {
255 return victimSet
.count(MF
);
257 // Remove any references to the now-destroyed modules.
258 for (auto I
= begin(); I
!= First
; ++I
) {
259 I
->Imports
.remove_if(IsVictim
);
260 I
->ImportedBy
.remove_if(IsVictim
);
262 llvm::erase_if(Roots
, IsVictim
);
264 // Remove the modules from the PCH chain.
265 for (auto I
= First
; I
!= Last
; ++I
) {
266 if (!I
->isModule()) {
267 PCHChain
.erase(llvm::find(PCHChain
, &*I
), PCHChain
.end());
272 // Delete the modules.
273 for (ModuleIterator victim
= First
; victim
!= Last
; ++victim
)
274 Modules
.erase(victim
->File
);
276 Chain
.erase(Chain
.begin() + (First
- begin()), Chain
.end());
280 ModuleManager::addInMemoryBuffer(StringRef FileName
,
281 std::unique_ptr
<llvm::MemoryBuffer
> Buffer
) {
282 const FileEntry
*Entry
=
283 FileMgr
.getVirtualFile(FileName
, Buffer
->getBufferSize(), 0);
284 InMemoryBuffers
[Entry
] = std::move(Buffer
);
287 std::unique_ptr
<ModuleManager::VisitState
> ModuleManager::allocateVisitState() {
288 // Fast path: if we have a cached state, use it.
289 if (FirstVisitState
) {
290 auto Result
= std::move(FirstVisitState
);
291 FirstVisitState
= std::move(Result
->NextState
);
295 // Allocate and return a new state.
296 return std::make_unique
<VisitState
>(size());
299 void ModuleManager::returnVisitState(std::unique_ptr
<VisitState
> State
) {
300 assert(State
->NextState
== nullptr && "Visited state is in list?");
301 State
->NextState
= std::move(FirstVisitState
);
302 FirstVisitState
= std::move(State
);
305 void ModuleManager::setGlobalIndex(GlobalModuleIndex
*Index
) {
308 ModulesInCommonWithGlobalIndex
.clear();
312 // Notify the global module index about all of the modules we've already
314 for (ModuleFile
&M
: *this)
315 if (!GlobalIndex
->loadedModuleFile(&M
))
316 ModulesInCommonWithGlobalIndex
.push_back(&M
);
319 void ModuleManager::moduleFileAccepted(ModuleFile
*MF
) {
320 if (!GlobalIndex
|| GlobalIndex
->loadedModuleFile(MF
))
323 ModulesInCommonWithGlobalIndex
.push_back(MF
);
326 ModuleManager::ModuleManager(FileManager
&FileMgr
,
327 InMemoryModuleCache
&ModuleCache
,
328 const PCHContainerReader
&PCHContainerRdr
,
329 const HeaderSearch
&HeaderSearchInfo
)
330 : FileMgr(FileMgr
), ModuleCache(&ModuleCache
),
331 PCHContainerRdr(PCHContainerRdr
), HeaderSearchInfo(HeaderSearchInfo
) {}
333 void ModuleManager::visit(llvm::function_ref
<bool(ModuleFile
&M
)> Visitor
,
334 llvm::SmallPtrSetImpl
<ModuleFile
*> *ModuleFilesHit
) {
335 // If the visitation order vector is the wrong size, recompute the order.
336 if (VisitOrder
.size() != Chain
.size()) {
339 VisitOrder
.reserve(N
);
341 // Record the number of incoming edges for each module. When we
342 // encounter a module with no incoming edges, push it into the queue
343 // to seed the queue.
344 SmallVector
<ModuleFile
*, 4> Queue
;
346 llvm::SmallVector
<unsigned, 4> UnusedIncomingEdges
;
347 UnusedIncomingEdges
.resize(size());
348 for (ModuleFile
&M
: llvm::reverse(*this)) {
349 unsigned Size
= M
.ImportedBy
.size();
350 UnusedIncomingEdges
[M
.Index
] = Size
;
355 // Traverse the graph, making sure to visit a module before visiting any
356 // of its dependencies.
357 while (!Queue
.empty()) {
358 ModuleFile
*CurrentModule
= Queue
.pop_back_val();
359 VisitOrder
.push_back(CurrentModule
);
361 // For any module that this module depends on, push it on the
362 // stack (if it hasn't already been marked as visited).
363 for (ModuleFile
*M
: llvm::reverse(CurrentModule
->Imports
)) {
364 // Remove our current module as an impediment to visiting the
365 // module we depend on. If we were the last unvisited module
366 // that depends on this particular module, push it into the
367 // queue to be visited.
368 unsigned &NumUnusedEdges
= UnusedIncomingEdges
[M
->Index
];
369 if (NumUnusedEdges
&& (--NumUnusedEdges
== 0))
374 assert(VisitOrder
.size() == N
&& "Visitation order is wrong?");
376 FirstVisitState
= nullptr;
379 auto State
= allocateVisitState();
380 unsigned VisitNumber
= State
->NextVisitNumber
++;
382 // If the caller has provided us with a hit-set that came from the global
383 // module index, mark every module file in common with the global module
384 // index that is *not* in that set as 'visited'.
385 if (ModuleFilesHit
&& !ModulesInCommonWithGlobalIndex
.empty()) {
386 for (unsigned I
= 0, N
= ModulesInCommonWithGlobalIndex
.size(); I
!= N
; ++I
)
388 ModuleFile
*M
= ModulesInCommonWithGlobalIndex
[I
];
389 if (!ModuleFilesHit
->count(M
))
390 State
->VisitNumber
[M
->Index
] = VisitNumber
;
394 for (unsigned I
= 0, N
= VisitOrder
.size(); I
!= N
; ++I
) {
395 ModuleFile
*CurrentModule
= VisitOrder
[I
];
396 // Should we skip this module file?
397 if (State
->VisitNumber
[CurrentModule
->Index
] == VisitNumber
)
401 assert(State
->VisitNumber
[CurrentModule
->Index
] == VisitNumber
- 1);
402 State
->VisitNumber
[CurrentModule
->Index
] = VisitNumber
;
403 if (!Visitor(*CurrentModule
))
406 // The visitor has requested that cut off visitation of any
407 // module that the current module depends on. To indicate this
408 // behavior, we mark all of the reachable modules as having been visited.
409 ModuleFile
*NextModule
= CurrentModule
;
411 // For any module that this module depends on, push it on the
412 // stack (if it hasn't already been marked as visited).
413 for (llvm::SetVector
<ModuleFile
*>::iterator
414 M
= NextModule
->Imports
.begin(),
415 MEnd
= NextModule
->Imports
.end();
417 if (State
->VisitNumber
[(*M
)->Index
] != VisitNumber
) {
418 State
->Stack
.push_back(*M
);
419 State
->VisitNumber
[(*M
)->Index
] = VisitNumber
;
423 if (State
->Stack
.empty())
426 // Pop the next module off the stack.
427 NextModule
= State
->Stack
.pop_back_val();
431 returnVisitState(std::move(State
));
434 bool ModuleManager::lookupModuleFile(StringRef FileName
, off_t ExpectedSize
,
435 time_t ExpectedModTime
,
436 OptionalFileEntryRef
&File
) {
437 if (FileName
== "-") {
438 File
= expectedToOptional(FileMgr
.getSTDIN());
442 // Open the file immediately to ensure there is no race between stat'ing and
444 File
= FileMgr
.getOptionalFileRef(FileName
, /*OpenFile=*/true,
445 /*CacheFailure=*/false);
448 ((ExpectedSize
&& ExpectedSize
!= File
->getSize()) ||
449 (ExpectedModTime
&& ExpectedModTime
!= File
->getModificationTime())))
450 // Do not destroy File, as it may be referenced. If we need to rebuild it,
451 // it will be destroyed by removeModules.
461 struct GraphTraits
<ModuleManager
> {
462 using NodeRef
= ModuleFile
*;
463 using ChildIteratorType
= llvm::SetVector
<ModuleFile
*>::const_iterator
;
464 using nodes_iterator
= pointer_iterator
<ModuleManager::ModuleConstIterator
>;
466 static ChildIteratorType
child_begin(NodeRef Node
) {
467 return Node
->Imports
.begin();
470 static ChildIteratorType
child_end(NodeRef Node
) {
471 return Node
->Imports
.end();
474 static nodes_iterator
nodes_begin(const ModuleManager
&Manager
) {
475 return nodes_iterator(Manager
.begin());
478 static nodes_iterator
nodes_end(const ModuleManager
&Manager
) {
479 return nodes_iterator(Manager
.end());
484 struct DOTGraphTraits
<ModuleManager
> : public DefaultDOTGraphTraits
{
485 explicit DOTGraphTraits(bool IsSimple
= false)
486 : DefaultDOTGraphTraits(IsSimple
) {}
488 static bool renderGraphFromBottomUp() { return true; }
490 std::string
getNodeLabel(ModuleFile
*M
, const ModuleManager
&) {
491 return M
->ModuleName
;
497 void ModuleManager::viewGraph() {
498 llvm::ViewGraph(*this, "Modules");