1 //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ModuleManager class, which manages a set of loaded
11 // modules for the ASTReader.
13 //===----------------------------------------------------------------------===//
14 #include "clang/Frontend/PCHContainerOperations.h"
15 #include "clang/Lex/HeaderSearch.h"
16 #include "clang/Lex/ModuleMap.h"
17 #include "clang/Serialization/GlobalModuleIndex.h"
18 #include "clang/Serialization/ModuleManager.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <system_error>
25 #include "llvm/Support/GraphWriter.h"
28 using namespace clang;
29 using namespace serialization;
31 ModuleFile *ModuleManager::lookup(StringRef Name) {
32 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
33 /*cacheFailure=*/false);
40 ModuleFile *ModuleManager::lookup(const FileEntry *File) {
41 llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
43 if (Known == Modules.end())
49 std::unique_ptr<llvm::MemoryBuffer>
50 ModuleManager::lookupBuffer(StringRef Name) {
51 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
52 /*cacheFailure=*/false);
53 return std::move(InMemoryBuffers[Entry]);
56 ModuleManager::AddModuleResult
57 ModuleManager::addModule(StringRef FileName, ModuleKind Type,
58 SourceLocation ImportLoc, ModuleFile *ImportedBy,
60 off_t ExpectedSize, time_t ExpectedModTime,
61 ASTFileSignature ExpectedSignature,
62 ASTFileSignatureReader ReadSignature,
64 std::string &ErrorStr) {
67 // Look for the file entry. This only fails if the expected size or
68 // modification time differ.
69 const FileEntry *Entry;
70 if (Type == MK_ExplicitModule) {
71 // If we're not expecting to pull this file out of the module cache, it
72 // might have a different mtime due to being moved across filesystems in
73 // a distributed build. The size must still match, though. (As must the
74 // contents, but we can't check that.)
77 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
78 ErrorStr = "module file out of date";
82 if (!Entry && FileName != "-") {
83 ErrorStr = "module file not found";
87 // Check whether we already loaded this module, before
88 ModuleFile *&ModuleEntry = Modules[Entry];
89 bool NewModule = false;
91 // Allocate a new module.
92 ModuleFile *New = new ModuleFile(Type, Generation);
93 New->Index = Chain.size();
94 New->FileName = FileName.str();
96 New->ImportLoc = ImportLoc;
99 PCHChain.push_back(New);
101 Roots.push_back(New);
105 New->InputFilesValidationTimestamp = 0;
106 if (New->Kind == MK_ImplicitModule) {
107 std::string TimestampFilename = New->getTimestampFilename();
109 // A cached stat value would be fine as well.
110 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
111 New->InputFilesValidationTimestamp =
112 Status.getLastModificationTime().toEpochTime();
115 // Load the contents of the module
116 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
117 // The buffer was already provided for us.
118 New->Buffer = std::move(Buffer);
120 // Open the AST file.
121 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
122 (std::error_code()));
123 if (FileName == "-") {
124 Buf = llvm::MemoryBuffer::getSTDIN();
126 // Leave the FileEntry open so if it gets read again by another
127 // ModuleManager it must be the same underlying file.
128 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
129 // give us an open file, this may not be 100% reliable.
130 Buf = FileMgr.getBufferForFile(New->File,
131 /*IsVolatile=*/false,
132 /*ShouldClose=*/false);
136 ErrorStr = Buf.getError().message();
140 New->Buffer = std::move(*Buf);
143 // Initialize the stream.
144 PCHContainerRdr.ExtractPCH(New->Buffer->getMemBufferRef(), New->StreamFile);
147 if (ExpectedSignature) {
149 ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile);
151 assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile));
153 if (ModuleEntry->Signature != ExpectedSignature) {
154 ErrorStr = ModuleEntry->Signature ? "signature mismatch"
155 : "could not read module signature";
158 // Remove the module file immediately, since removeModules might try to
159 // invalidate the file cache for Entry, and that is not safe if this
160 // module is *itself* up to date, but has an out-of-date importer.
161 Modules.erase(Entry);
162 assert(Chain.back() == ModuleEntry);
164 if (!ModuleEntry->isModule())
166 if (Roots.back() == ModuleEntry)
177 ModuleEntry->ImportedBy.insert(ImportedBy);
178 ImportedBy->Imports.insert(ModuleEntry);
180 if (!ModuleEntry->DirectlyImported)
181 ModuleEntry->ImportLoc = ImportLoc;
183 ModuleEntry->DirectlyImported = true;
186 Module = ModuleEntry;
187 return NewModule? NewlyLoaded : AlreadyLoaded;
190 void ModuleManager::removeModules(
191 ModuleIterator first, ModuleIterator last,
192 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
197 // Explicitly clear VisitOrder since we might not notice it is stale.
200 // Collect the set of module file pointers that we'll be removing.
201 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
203 auto IsVictim = [&](ModuleFile *MF) {
204 return victimSet.count(MF);
206 // Remove any references to the now-destroyed modules.
207 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
208 Chain[i]->ImportedBy.remove_if(IsVictim);
210 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
213 // Remove the modules from the PCH chain.
214 for (auto I = first; I != last; ++I) {
215 if (!(*I)->isModule()) {
216 PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I),
222 // Delete the modules and erase them from the various structures.
223 for (ModuleIterator victim = first; victim != last; ++victim) {
224 Modules.erase((*victim)->File);
227 StringRef ModuleName = (*victim)->ModuleName;
228 if (Module *mod = modMap->findModule(ModuleName)) {
229 mod->setASTFile(nullptr);
233 // Files that didn't make it through ReadASTCore successfully will be
234 // rebuilt (or there was an error). Invalidate them so that we can load the
235 // new files that will be renamed over the old ones.
236 if (LoadedSuccessfully.count(*victim) == 0)
237 FileMgr.invalidateCache((*victim)->File);
242 // Remove the modules from the chain.
243 Chain.erase(first, last);
247 ModuleManager::addInMemoryBuffer(StringRef FileName,
248 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
250 const FileEntry *Entry =
251 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
252 InMemoryBuffers[Entry] = std::move(Buffer);
255 ModuleManager::VisitState *ModuleManager::allocateVisitState() {
256 // Fast path: if we have a cached state, use it.
257 if (FirstVisitState) {
258 VisitState *Result = FirstVisitState;
259 FirstVisitState = FirstVisitState->NextState;
260 Result->NextState = nullptr;
264 // Allocate and return a new state.
265 return new VisitState(size());
268 void ModuleManager::returnVisitState(VisitState *State) {
269 assert(State->NextState == nullptr && "Visited state is in list?");
270 State->NextState = FirstVisitState;
271 FirstVisitState = State;
274 void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
277 ModulesInCommonWithGlobalIndex.clear();
281 // Notify the global module index about all of the modules we've already
283 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
284 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
285 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
290 void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
291 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
294 ModulesInCommonWithGlobalIndex.push_back(MF);
297 ModuleManager::ModuleManager(FileManager &FileMgr,
298 const PCHContainerReader &PCHContainerRdr)
299 : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(),
300 FirstVisitState(nullptr) {}
302 ModuleManager::~ModuleManager() {
303 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
304 delete Chain[e - i - 1];
305 delete FirstVisitState;
308 void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
309 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
310 // If the visitation order vector is the wrong size, recompute the order.
311 if (VisitOrder.size() != Chain.size()) {
314 VisitOrder.reserve(N);
316 // Record the number of incoming edges for each module. When we
317 // encounter a module with no incoming edges, push it into the queue
318 // to seed the queue.
319 SmallVector<ModuleFile *, 4> Queue;
321 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
322 UnusedIncomingEdges.resize(size());
323 for (auto M = rbegin(), MEnd = rend(); M != MEnd; ++M) {
324 unsigned Size = (*M)->ImportedBy.size();
325 UnusedIncomingEdges[(*M)->Index] = Size;
330 // Traverse the graph, making sure to visit a module before visiting any
331 // of its dependencies.
332 while (!Queue.empty()) {
333 ModuleFile *CurrentModule = Queue.pop_back_val();
334 VisitOrder.push_back(CurrentModule);
336 // For any module that this module depends on, push it on the
337 // stack (if it hasn't already been marked as visited).
338 for (auto M = CurrentModule->Imports.rbegin(),
339 MEnd = CurrentModule->Imports.rend();
341 // Remove our current module as an impediment to visiting the
342 // module we depend on. If we were the last unvisited module
343 // that depends on this particular module, push it into the
344 // queue to be visited.
345 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
346 if (NumUnusedEdges && (--NumUnusedEdges == 0))
351 assert(VisitOrder.size() == N && "Visitation order is wrong?");
353 delete FirstVisitState;
354 FirstVisitState = nullptr;
357 VisitState *State = allocateVisitState();
358 unsigned VisitNumber = State->NextVisitNumber++;
360 // If the caller has provided us with a hit-set that came from the global
361 // module index, mark every module file in common with the global module
362 // index that is *not* in that set as 'visited'.
363 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
364 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
366 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
367 if (!ModuleFilesHit->count(M))
368 State->VisitNumber[M->Index] = VisitNumber;
372 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
373 ModuleFile *CurrentModule = VisitOrder[I];
374 // Should we skip this module file?
375 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
379 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
380 State->VisitNumber[CurrentModule->Index] = VisitNumber;
381 if (!Visitor(*CurrentModule))
384 // The visitor has requested that cut off visitation of any
385 // module that the current module depends on. To indicate this
386 // behavior, we mark all of the reachable modules as having been visited.
387 ModuleFile *NextModule = CurrentModule;
389 // For any module that this module depends on, push it on the
390 // stack (if it hasn't already been marked as visited).
391 for (llvm::SetVector<ModuleFile *>::iterator
392 M = NextModule->Imports.begin(),
393 MEnd = NextModule->Imports.end();
395 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
396 State->Stack.push_back(*M);
397 State->VisitNumber[(*M)->Index] = VisitNumber;
401 if (State->Stack.empty())
404 // Pop the next module off the stack.
405 NextModule = State->Stack.pop_back_val();
409 returnVisitState(State);
412 bool ModuleManager::lookupModuleFile(StringRef FileName,
414 time_t ExpectedModTime,
415 const FileEntry *&File) {
416 // Open the file immediately to ensure there is no race between stat'ing and
418 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
420 if (!File && FileName != "-") {
424 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
425 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
426 // Do not destroy File, as it may be referenced. If we need to rebuild it,
427 // it will be destroyed by removeModules.
436 struct GraphTraits<ModuleManager> {
437 typedef ModuleFile NodeType;
438 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
439 typedef ModuleManager::ModuleConstIterator nodes_iterator;
441 static ChildIteratorType child_begin(NodeType *Node) {
442 return Node->Imports.begin();
445 static ChildIteratorType child_end(NodeType *Node) {
446 return Node->Imports.end();
449 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
450 return Manager.begin();
453 static nodes_iterator nodes_end(const ModuleManager &Manager) {
454 return Manager.end();
459 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
460 explicit DOTGraphTraits(bool IsSimple = false)
461 : DefaultDOTGraphTraits(IsSimple) { }
463 static bool renderGraphFromBottomUp() {
467 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
468 return M->ModuleName;
473 void ModuleManager::viewGraph() {
474 llvm::ViewGraph(*this, "Modules");