blob: b7ee30254ff1eea7d9d5b91c2f71a609893da304 [file] [log] [blame]
Douglas Gregord44252e2011-08-25 20:47:51 +00001//===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the ModuleManager class, which manages a set of loaded
11// modules for the ASTReader.
12//
13//===----------------------------------------------------------------------===//
Mehdi Amini9670f842016-07-18 19:02:11 +000014#include "clang/Serialization/ModuleManager.h"
Adrian Prantlbb165fb2015-06-20 18:53:08 +000015#include "clang/Frontend/PCHContainerOperations.h"
Ben Langmuirbeee15e2014-04-14 18:00:01 +000016#include "clang/Lex/HeaderSearch.h"
Douglas Gregor7029ce12013-03-19 00:28:20 +000017#include "clang/Lex/ModuleMap.h"
Douglas Gregor7211ac12013-01-25 23:32:03 +000018#include "clang/Serialization/GlobalModuleIndex.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000019#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola552c1692013-06-11 22:15:02 +000020#include "llvm/Support/Path.h"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000021#include <system_error>
Douglas Gregord44252e2011-08-25 20:47:51 +000022
Douglas Gregor9d7c1a22011-10-11 19:27:55 +000023#ifndef NDEBUG
24#include "llvm/Support/GraphWriter.h"
25#endif
26
Douglas Gregord44252e2011-08-25 20:47:51 +000027using namespace clang;
28using namespace serialization;
29
Douglas Gregorde3ef502011-11-30 23:21:26 +000030ModuleFile *ModuleManager::lookup(StringRef Name) {
Douglas Gregordadd85d2013-02-08 21:27:45 +000031 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
32 /*cacheFailure=*/false);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000033 if (Entry)
34 return lookup(Entry);
35
Craig Toppera13603a2014-05-22 05:54:18 +000036 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000037}
38
39ModuleFile *ModuleManager::lookup(const FileEntry *File) {
40 llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
41 = Modules.find(File);
42 if (Known == Modules.end())
Craig Toppera13603a2014-05-22 05:54:18 +000043 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000044
45 return Known->second;
Douglas Gregord44252e2011-08-25 20:47:51 +000046}
47
Rafael Espindola5cd06f22014-08-18 19:16:31 +000048std::unique_ptr<llvm::MemoryBuffer>
49ModuleManager::lookupBuffer(StringRef Name) {
Douglas Gregordadd85d2013-02-08 21:27:45 +000050 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
51 /*cacheFailure=*/false);
Rafael Espindola5cd06f22014-08-18 19:16:31 +000052 return std::move(InMemoryBuffers[Entry]);
Douglas Gregord44252e2011-08-25 20:47:51 +000053}
54
Duncan P. N. Exon Smith14afc8e2017-01-28 21:34:28 +000055static bool checkSignature(ASTFileSignature Signature,
56 ASTFileSignature ExpectedSignature,
57 std::string &ErrorStr) {
58 if (!ExpectedSignature || Signature == ExpectedSignature)
59 return false;
60
61 ErrorStr =
62 Signature ? "signature mismatch" : "could not read module signature";
63 return true;
64}
65
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +000066static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy,
67 SourceLocation ImportLoc) {
68 if (ImportedBy) {
69 MF.ImportedBy.insert(ImportedBy);
70 ImportedBy->Imports.insert(&MF);
71 } else {
72 if (!MF.DirectlyImported)
73 MF.ImportLoc = ImportLoc;
74
75 MF.DirectlyImported = true;
76 }
77}
78
Douglas Gregor7029ce12013-03-19 00:28:20 +000079ModuleManager::AddModuleResult
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000080ModuleManager::addModule(StringRef FileName, ModuleKind Type,
81 SourceLocation ImportLoc, ModuleFile *ImportedBy,
Douglas Gregor7029ce12013-03-19 00:28:20 +000082 unsigned Generation,
83 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +000084 ASTFileSignature ExpectedSignature,
Ben Langmuir70a1b812015-03-24 04:43:52 +000085 ASTFileSignatureReader ReadSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +000086 ModuleFile *&Module,
87 std::string &ErrorStr) {
Craig Toppera13603a2014-05-22 05:54:18 +000088 Module = nullptr;
Douglas Gregor7029ce12013-03-19 00:28:20 +000089
90 // Look for the file entry. This only fails if the expected size or
91 // modification time differ.
92 const FileEntry *Entry;
Manman Ren11f2a472016-08-18 17:42:15 +000093 if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) {
Richard Smith5b390752014-11-21 05:37:20 +000094 // If we're not expecting to pull this file out of the module cache, it
95 // might have a different mtime due to being moved across filesystems in
96 // a distributed build. The size must still match, though. (As must the
97 // contents, but we can't check that.)
98 ExpectedModTime = 0;
99 }
Eli Friedmanc27d0d52013-09-05 23:50:58 +0000100 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
101 ErrorStr = "module file out of date";
Douglas Gregor7029ce12013-03-19 00:28:20 +0000102 return OutOfDate;
Eli Friedmanc27d0d52013-09-05 23:50:58 +0000103 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000104
Douglas Gregord44252e2011-08-25 20:47:51 +0000105 if (!Entry && FileName != "-") {
Eli Friedmanc27d0d52013-09-05 23:50:58 +0000106 ErrorStr = "module file not found";
Douglas Gregor7029ce12013-03-19 00:28:20 +0000107 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +0000108 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000109
110 // Check whether we already loaded this module, before
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000111 if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) {
112 // Check the stored signature.
113 if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))
Ben Langmuired982582014-11-08 00:34:30 +0000114 return OutOfDate;
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000115
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000116 Module = ModuleEntry;
117 updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc);
Richard Smith3b99db52016-09-02 00:10:28 +0000118 return AlreadyLoaded;
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000119 }
Richard Smith3b99db52016-09-02 00:10:28 +0000120
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000121 // Allocate a new module.
122 auto NewModule = llvm::make_unique<ModuleFile>(Type, Generation);
123 NewModule->Index = Chain.size();
124 NewModule->FileName = FileName.str();
125 NewModule->File = Entry;
126 NewModule->ImportLoc = ImportLoc;
127 NewModule->InputFilesValidationTimestamp = 0;
128
129 if (NewModule->Kind == MK_ImplicitModule) {
130 std::string TimestampFilename = NewModule->getTimestampFilename();
131 vfs::Status Status;
132 // A cached stat value would be fine as well.
133 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
134 NewModule->InputFilesValidationTimestamp =
135 llvm::sys::toTimeT(Status.getLastModificationTime());
136 }
137
138 // Load the contents of the module
139 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
140 // The buffer was already provided for us.
141 NewModule->Buffer = std::move(Buffer);
142 } else {
143 // Open the AST file.
144 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code()));
145 if (FileName == "-") {
146 Buf = llvm::MemoryBuffer::getSTDIN();
147 } else {
148 // Leave the FileEntry open so if it gets read again by another
149 // ModuleManager it must be the same underlying file.
150 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
151 // give us an open file, this may not be 100% reliable.
152 Buf = FileMgr.getBufferForFile(NewModule->File,
153 /*IsVolatile=*/false,
154 /*ShouldClose=*/false);
155 }
156
157 if (!Buf) {
158 ErrorStr = Buf.getError().message();
159 return Missing;
160 }
161
162 NewModule->Buffer = std::move(*Buf);
163 }
164
165 // Initialize the stream.
166 NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer);
167
Duncan P. N. Exon Smith688b69a2017-01-29 04:42:21 +0000168 // Read the signature eagerly now so that we can check it. Avoid calling
169 // ReadSignature unless there's something to check though.
170 if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
171 ExpectedSignature, ErrorStr))
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000172 return OutOfDate;
173
174 // We're keeping this module. Store it everywhere.
175 Module = Modules[Entry] = NewModule.get();
176
177 updateModuleImports(*NewModule, ImportedBy, ImportLoc);
178
179 if (!NewModule->isModule())
180 PCHChain.push_back(NewModule.get());
181 if (!ImportedBy)
182 Roots.push_back(NewModule.get());
Richard Smith3b99db52016-09-02 00:10:28 +0000183
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000184 Chain.push_back(std::move(NewModule));
Richard Smith3b99db52016-09-02 00:10:28 +0000185 return NewlyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000186}
187
Ben Langmuir9801b252014-06-20 00:24:56 +0000188void ModuleManager::removeModules(
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000189 ModuleIterator First,
Ben Langmuir9801b252014-06-20 00:24:56 +0000190 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
191 ModuleMap *modMap) {
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000192 auto Last = end();
193 if (First == Last)
Douglas Gregor188dbef2012-11-07 17:46:15 +0000194 return;
195
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000196
Ben Langmuira50dbb22015-10-21 23:12:45 +0000197 // Explicitly clear VisitOrder since we might not notice it is stale.
198 VisitOrder.clear();
199
Douglas Gregor188dbef2012-11-07 17:46:15 +0000200 // Collect the set of module file pointers that we'll be removing.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000201 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000202 (llvm::pointer_iterator<ModuleIterator>(First)),
203 (llvm::pointer_iterator<ModuleIterator>(Last)));
Douglas Gregor188dbef2012-11-07 17:46:15 +0000204
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000205 auto IsVictim = [&](ModuleFile *MF) {
206 return victimSet.count(MF);
207 };
Douglas Gregor188dbef2012-11-07 17:46:15 +0000208 // Remove any references to the now-destroyed modules.
Duncan P. N. Exon Smith073ec352017-01-28 23:12:13 +0000209 for (auto I = begin(); I != First; ++I) {
210 I->Imports.remove_if(IsVictim);
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000211 I->ImportedBy.remove_if(IsVictim);
Duncan P. N. Exon Smith073ec352017-01-28 23:12:13 +0000212 }
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000213 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
214 Roots.end());
Douglas Gregor188dbef2012-11-07 17:46:15 +0000215
Richard Smith16fe4d12015-07-22 22:51:15 +0000216 // Remove the modules from the PCH chain.
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000217 for (auto I = First; I != Last; ++I) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000218 if (!I->isModule()) {
219 PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), &*I),
Richard Smith16fe4d12015-07-22 22:51:15 +0000220 PCHChain.end());
221 break;
222 }
223 }
224
Douglas Gregor188dbef2012-11-07 17:46:15 +0000225 // Delete the modules and erase them from the various structures.
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000226 for (ModuleIterator victim = First; victim != Last; ++victim) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000227 Modules.erase(victim->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000228
Douglas Gregor7029ce12013-03-19 00:28:20 +0000229 if (modMap) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000230 StringRef ModuleName = victim->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000231 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000232 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000233 }
234 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000235
Ben Langmuir9801b252014-06-20 00:24:56 +0000236 // Files that didn't make it through ReadASTCore successfully will be
237 // rebuilt (or there was an error). Invalidate them so that we can load the
238 // new files that will be renamed over the old ones.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000239 if (LoadedSuccessfully.count(&*victim) == 0)
240 FileMgr.invalidateCache(victim->File);
Douglas Gregor188dbef2012-11-07 17:46:15 +0000241 }
242
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000243 // Delete the modules.
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000244 Chain.erase(Chain.begin() + (First - begin()), Chain.end());
Douglas Gregor188dbef2012-11-07 17:46:15 +0000245}
246
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000247void
248ModuleManager::addInMemoryBuffer(StringRef FileName,
249 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
250
251 const FileEntry *Entry =
252 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
253 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000254}
255
Douglas Gregore97cd902013-01-28 16:46:33 +0000256ModuleManager::VisitState *ModuleManager::allocateVisitState() {
257 // Fast path: if we have a cached state, use it.
258 if (FirstVisitState) {
259 VisitState *Result = FirstVisitState;
260 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000261 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000262 return Result;
263 }
264
265 // Allocate and return a new state.
266 return new VisitState(size());
267}
268
269void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000270 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000271 State->NextState = FirstVisitState;
272 FirstVisitState = State;
273}
274
Douglas Gregor7211ac12013-01-25 23:32:03 +0000275void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
276 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000277 if (!GlobalIndex) {
278 ModulesInCommonWithGlobalIndex.clear();
279 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000280 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000281
282 // Notify the global module index about all of the modules we've already
283 // loaded.
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000284 for (ModuleFile &M : *this)
285 if (!GlobalIndex->loadedModuleFile(&M))
286 ModulesInCommonWithGlobalIndex.push_back(&M);
Douglas Gregor603cd862013-03-22 18:50:14 +0000287}
288
289void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
290 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
291 return;
292
293 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000294}
295
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000296ModuleManager::ModuleManager(FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000297 const PCHContainerReader &PCHContainerRdr)
298 : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(),
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000299 FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000300
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000301ModuleManager::~ModuleManager() { delete FirstVisitState; }
Douglas Gregord44252e2011-08-25 20:47:51 +0000302
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000303void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
304 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000305 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000306 if (VisitOrder.size() != Chain.size()) {
307 unsigned N = size();
308 VisitOrder.clear();
309 VisitOrder.reserve(N);
310
311 // Record the number of incoming edges for each module. When we
312 // encounter a module with no incoming edges, push it into the queue
313 // to seed the queue.
314 SmallVector<ModuleFile *, 4> Queue;
315 Queue.reserve(N);
316 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
Richard Smitha7c535b2015-07-22 01:28:05 +0000317 UnusedIncomingEdges.resize(size());
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000318 for (ModuleFile &M : llvm::reverse(*this)) {
319 unsigned Size = M.ImportedBy.size();
320 UnusedIncomingEdges[M.Index] = Size;
Richard Smitha7c535b2015-07-22 01:28:05 +0000321 if (!Size)
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000322 Queue.push_back(&M);
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000323 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000324
325 // Traverse the graph, making sure to visit a module before visiting any
326 // of its dependencies.
Richard Smitha7c535b2015-07-22 01:28:05 +0000327 while (!Queue.empty()) {
328 ModuleFile *CurrentModule = Queue.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000329 VisitOrder.push_back(CurrentModule);
330
331 // For any module that this module depends on, push it on the
332 // stack (if it hasn't already been marked as visited).
Richard Smitha7c535b2015-07-22 01:28:05 +0000333 for (auto M = CurrentModule->Imports.rbegin(),
334 MEnd = CurrentModule->Imports.rend();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000335 M != MEnd; ++M) {
336 // Remove our current module as an impediment to visiting the
337 // module we depend on. If we were the last unvisited module
338 // that depends on this particular module, push it into the
339 // queue to be visited.
340 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
341 if (NumUnusedEdges && (--NumUnusedEdges == 0))
342 Queue.push_back(*M);
343 }
344 }
345
346 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000347
Douglas Gregore97cd902013-01-28 16:46:33 +0000348 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000349 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000350 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000351
Douglas Gregore97cd902013-01-28 16:46:33 +0000352 VisitState *State = allocateVisitState();
353 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000354
Douglas Gregor7211ac12013-01-25 23:32:03 +0000355 // If the caller has provided us with a hit-set that came from the global
356 // module index, mark every module file in common with the global module
357 // index that is *not* in that set as 'visited'.
358 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
359 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
360 {
361 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000362 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000363 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000364 }
365 }
366
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000367 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
368 ModuleFile *CurrentModule = VisitOrder[I];
369 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000370 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000371 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000372
373 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000374 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
375 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000376 if (!Visitor(*CurrentModule))
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000377 continue;
378
379 // The visitor has requested that cut off visitation of any
380 // module that the current module depends on. To indicate this
381 // behavior, we mark all of the reachable modules as having been visited.
382 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000383 do {
384 // For any module that this module depends on, push it on the
385 // stack (if it hasn't already been marked as visited).
386 for (llvm::SetVector<ModuleFile *>::iterator
387 M = NextModule->Imports.begin(),
388 MEnd = NextModule->Imports.end();
389 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000390 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
391 State->Stack.push_back(*M);
392 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000393 }
394 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000395
Douglas Gregore97cd902013-01-28 16:46:33 +0000396 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000397 break;
398
399 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000400 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000401 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000402 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000403
404 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000405}
406
Douglas Gregor7029ce12013-03-19 00:28:20 +0000407bool ModuleManager::lookupModuleFile(StringRef FileName,
408 off_t ExpectedSize,
409 time_t ExpectedModTime,
410 const FileEntry *&File) {
Richard Smith3bd6d7f2016-09-02 00:18:05 +0000411 if (FileName == "-") {
412 File = nullptr;
413 return false;
414 }
415
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000416 // Open the file immediately to ensure there is no race between stat'ing and
417 // opening the file.
418 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Richard Smith3bd6d7f2016-09-02 00:18:05 +0000419 if (!File)
Douglas Gregor7029ce12013-03-19 00:28:20 +0000420 return false;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000421
422 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000423 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
424 // Do not destroy File, as it may be referenced. If we need to rebuild it,
425 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000426 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000427
428 return false;
429}
430
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000431#ifndef NDEBUG
432namespace llvm {
433 template<>
434 struct GraphTraits<ModuleManager> {
Tim Shen2931d172016-08-17 20:02:38 +0000435 typedef ModuleFile *NodeRef;
Douglas Gregorde3ef502011-11-30 23:21:26 +0000436 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000437 typedef pointer_iterator<ModuleManager::ModuleConstIterator> nodes_iterator;
Tim Shenf2187ed2016-08-22 21:09:30 +0000438
439 static ChildIteratorType child_begin(NodeRef Node) {
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000440 return Node->Imports.begin();
441 }
442
Tim Shenf2187ed2016-08-22 21:09:30 +0000443 static ChildIteratorType child_end(NodeRef Node) {
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000444 return Node->Imports.end();
445 }
446
447 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000448 return nodes_iterator(Manager.begin());
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000449 }
450
451 static nodes_iterator nodes_end(const ModuleManager &Manager) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000452 return nodes_iterator(Manager.end());
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000453 }
454 };
455
456 template<>
457 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
458 explicit DOTGraphTraits(bool IsSimple = false)
459 : DefaultDOTGraphTraits(IsSimple) { }
460
461 static bool renderGraphFromBottomUp() {
462 return true;
463 }
464
Douglas Gregorde3ef502011-11-30 23:21:26 +0000465 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000466 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000467 }
468 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000469}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000470
471void ModuleManager::viewGraph() {
472 llvm::ViewGraph(*this, "Modules");
473}
474#endif