blob: 8692f9e2132e6905a24aab60402e801422398eb9 [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
Douglas Gregor7029ce12013-03-19 00:28:20 +000055ModuleManager::AddModuleResult
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000056ModuleManager::addModule(StringRef FileName, ModuleKind Type,
57 SourceLocation ImportLoc, ModuleFile *ImportedBy,
Douglas Gregor7029ce12013-03-19 00:28:20 +000058 unsigned Generation,
59 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +000060 ASTFileSignature ExpectedSignature,
Ben Langmuir70a1b812015-03-24 04:43:52 +000061 ASTFileSignatureReader ReadSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +000062 ModuleFile *&Module,
63 std::string &ErrorStr) {
Craig Toppera13603a2014-05-22 05:54:18 +000064 Module = nullptr;
Douglas Gregor7029ce12013-03-19 00:28:20 +000065
66 // Look for the file entry. This only fails if the expected size or
67 // modification time differ.
68 const FileEntry *Entry;
Richard Smith5b390752014-11-21 05:37:20 +000069 if (Type == MK_ExplicitModule) {
70 // If we're not expecting to pull this file out of the module cache, it
71 // might have a different mtime due to being moved across filesystems in
72 // a distributed build. The size must still match, though. (As must the
73 // contents, but we can't check that.)
74 ExpectedModTime = 0;
75 }
Eli Friedmanc27d0d52013-09-05 23:50:58 +000076 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
77 ErrorStr = "module file out of date";
Douglas Gregor7029ce12013-03-19 00:28:20 +000078 return OutOfDate;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000079 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000080
Douglas Gregord44252e2011-08-25 20:47:51 +000081 if (!Entry && FileName != "-") {
Eli Friedmanc27d0d52013-09-05 23:50:58 +000082 ErrorStr = "module file not found";
Douglas Gregor7029ce12013-03-19 00:28:20 +000083 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +000084 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000085
86 // Check whether we already loaded this module, before
Douglas Gregorde3ef502011-11-30 23:21:26 +000087 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregord44252e2011-08-25 20:47:51 +000088 bool NewModule = false;
89 if (!ModuleEntry) {
90 // Allocate a new module.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +000091 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorbdb259d2013-01-21 20:07:12 +000092 New->Index = Chain.size();
Douglas Gregord44252e2011-08-25 20:47:51 +000093 New->FileName = FileName.str();
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +000094 New->File = Entry;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000095 New->ImportLoc = ImportLoc;
Douglas Gregord44252e2011-08-25 20:47:51 +000096 Chain.push_back(New);
Richard Smith33e0f7e2015-07-22 02:08:40 +000097 if (!New->isModule())
98 PCHChain.push_back(New);
Manuel Klimek9eff8b12015-05-20 10:29:23 +000099 if (!ImportedBy)
100 Roots.push_back(New);
Douglas Gregord44252e2011-08-25 20:47:51 +0000101 NewModule = true;
102 ModuleEntry = New;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000103
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000104 New->InputFilesValidationTimestamp = 0;
Richard Smithe842a472014-10-22 02:05:46 +0000105 if (New->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000106 std::string TimestampFilename = New->getTimestampFilename();
Ben Langmuirc8130a72014-02-20 21:59:23 +0000107 vfs::Status Status;
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000108 // A cached stat value would be fine as well.
109 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
110 New->InputFilesValidationTimestamp =
111 Status.getLastModificationTime().toEpochTime();
112 }
113
Douglas Gregord44252e2011-08-25 20:47:51 +0000114 // Load the contents of the module
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000115 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000116 // The buffer was already provided for us.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000117 New->Buffer = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000118 } else {
119 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +0000120 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
121 (std::error_code()));
Douglas Gregord44252e2011-08-25 20:47:51 +0000122 if (FileName == "-") {
Benjamin Kramera8857962014-10-26 22:44:13 +0000123 Buf = llvm::MemoryBuffer::getSTDIN();
Ben Langmuir9801b252014-06-20 00:24:56 +0000124 } else {
125 // Leave the FileEntry open so if it gets read again by another
126 // ModuleManager it must be the same underlying file.
127 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
128 // give us an open file, this may not be 100% reliable.
Benjamin Kramera8857962014-10-26 22:44:13 +0000129 Buf = FileMgr.getBufferForFile(New->File,
130 /*IsVolatile=*/false,
131 /*ShouldClose=*/false);
Ben Langmuir9801b252014-06-20 00:24:56 +0000132 }
Benjamin Kramera8857962014-10-26 22:44:13 +0000133
134 if (!Buf) {
135 ErrorStr = Buf.getError().message();
Douglas Gregor7029ce12013-03-19 00:28:20 +0000136 return Missing;
Benjamin Kramera8857962014-10-26 22:44:13 +0000137 }
138
139 New->Buffer = std::move(*Buf);
Douglas Gregord44252e2011-08-25 20:47:51 +0000140 }
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000141
142 // Initialize the stream.
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000143 PCHContainerRdr.ExtractPCH(New->Buffer->getMemBufferRef(), New->StreamFile);
Ben Langmuired982582014-11-08 00:34:30 +0000144 }
Ben Langmuir487ea142014-10-23 18:05:36 +0000145
Ben Langmuired982582014-11-08 00:34:30 +0000146 if (ExpectedSignature) {
147 if (NewModule)
148 ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile);
149 else
150 assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile));
Ben Langmuir487ea142014-10-23 18:05:36 +0000151
Ben Langmuired982582014-11-08 00:34:30 +0000152 if (ModuleEntry->Signature != ExpectedSignature) {
153 ErrorStr = ModuleEntry->Signature ? "signature mismatch"
154 : "could not read module signature";
155
156 if (NewModule) {
Ben Langmuir487ea142014-10-23 18:05:36 +0000157 // Remove the module file immediately, since removeModules might try to
158 // invalidate the file cache for Entry, and that is not safe if this
159 // module is *itself* up to date, but has an out-of-date importer.
160 Modules.erase(Entry);
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000161 assert(Chain.back() == ModuleEntry);
Ben Langmuir487ea142014-10-23 18:05:36 +0000162 Chain.pop_back();
Richard Smith33e0f7e2015-07-22 02:08:40 +0000163 if (!ModuleEntry->isModule())
164 PCHChain.pop_back();
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000165 if (Roots.back() == ModuleEntry)
166 Roots.pop_back();
167 else
168 assert(ImportedBy);
Ben Langmuired982582014-11-08 00:34:30 +0000169 delete ModuleEntry;
Ben Langmuir487ea142014-10-23 18:05:36 +0000170 }
Ben Langmuired982582014-11-08 00:34:30 +0000171 return OutOfDate;
Ben Langmuir487ea142014-10-23 18:05:36 +0000172 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000173 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000174
175 if (ImportedBy) {
176 ModuleEntry->ImportedBy.insert(ImportedBy);
177 ImportedBy->Imports.insert(ModuleEntry);
178 } else {
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000179 if (!ModuleEntry->DirectlyImported)
180 ModuleEntry->ImportLoc = ImportLoc;
181
Douglas Gregord44252e2011-08-25 20:47:51 +0000182 ModuleEntry->DirectlyImported = true;
183 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000184
185 Module = ModuleEntry;
186 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000187}
188
Ben Langmuir9801b252014-06-20 00:24:56 +0000189void ModuleManager::removeModules(
190 ModuleIterator first, ModuleIterator last,
191 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
192 ModuleMap *modMap) {
Douglas Gregor188dbef2012-11-07 17:46:15 +0000193 if (first == last)
194 return;
195
Ben Langmuira50dbb22015-10-21 23:12:45 +0000196 // Explicitly clear VisitOrder since we might not notice it is stale.
197 VisitOrder.clear();
198
Douglas Gregor188dbef2012-11-07 17:46:15 +0000199 // Collect the set of module file pointers that we'll be removing.
200 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
201
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000202 auto IsVictim = [&](ModuleFile *MF) {
203 return victimSet.count(MF);
204 };
Douglas Gregor188dbef2012-11-07 17:46:15 +0000205 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000206 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000207 Chain[i]->ImportedBy.remove_if(IsVictim);
Douglas Gregor188dbef2012-11-07 17:46:15 +0000208 }
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000209 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
210 Roots.end());
Douglas Gregor188dbef2012-11-07 17:46:15 +0000211
Richard Smith16fe4d12015-07-22 22:51:15 +0000212 // Remove the modules from the PCH chain.
213 for (auto I = first; I != last; ++I) {
214 if (!(*I)->isModule()) {
215 PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I),
216 PCHChain.end());
217 break;
218 }
219 }
220
Douglas Gregor188dbef2012-11-07 17:46:15 +0000221 // Delete the modules and erase them from the various structures.
222 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000223 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000224
Douglas Gregor7029ce12013-03-19 00:28:20 +0000225 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000226 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000227 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000228 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000229 }
230 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000231
Ben Langmuir9801b252014-06-20 00:24:56 +0000232 // Files that didn't make it through ReadASTCore successfully will be
233 // rebuilt (or there was an error). Invalidate them so that we can load the
234 // new files that will be renamed over the old ones.
235 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000236 FileMgr.invalidateCache((*victim)->File);
237
Douglas Gregor188dbef2012-11-07 17:46:15 +0000238 delete *victim;
239 }
240
241 // Remove the modules from the chain.
242 Chain.erase(first, last);
243}
244
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000245void
246ModuleManager::addInMemoryBuffer(StringRef FileName,
247 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
248
249 const FileEntry *Entry =
250 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
251 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000252}
253
Douglas Gregore97cd902013-01-28 16:46:33 +0000254ModuleManager::VisitState *ModuleManager::allocateVisitState() {
255 // Fast path: if we have a cached state, use it.
256 if (FirstVisitState) {
257 VisitState *Result = FirstVisitState;
258 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000259 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000260 return Result;
261 }
262
263 // Allocate and return a new state.
264 return new VisitState(size());
265}
266
267void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000268 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000269 State->NextState = FirstVisitState;
270 FirstVisitState = State;
271}
272
Douglas Gregor7211ac12013-01-25 23:32:03 +0000273void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
274 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000275 if (!GlobalIndex) {
276 ModulesInCommonWithGlobalIndex.clear();
277 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000278 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000279
280 // Notify the global module index about all of the modules we've already
281 // loaded.
282 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
283 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
284 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
285 }
286 }
287}
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
301ModuleManager::~ModuleManager() {
302 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
303 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000304 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000305}
306
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000307void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
308 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000309 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000310 if (VisitOrder.size() != Chain.size()) {
311 unsigned N = size();
312 VisitOrder.clear();
313 VisitOrder.reserve(N);
314
315 // Record the number of incoming edges for each module. When we
316 // encounter a module with no incoming edges, push it into the queue
317 // to seed the queue.
318 SmallVector<ModuleFile *, 4> Queue;
319 Queue.reserve(N);
320 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
Richard Smitha7c535b2015-07-22 01:28:05 +0000321 UnusedIncomingEdges.resize(size());
David Majnemerf7e36092016-06-23 00:15:04 +0000322 for (ModuleFile *M : llvm::reverse(*this)) {
323 unsigned Size = M->ImportedBy.size();
324 UnusedIncomingEdges[M->Index] = Size;
Richard Smitha7c535b2015-07-22 01:28:05 +0000325 if (!Size)
David Majnemerf7e36092016-06-23 00:15:04 +0000326 Queue.push_back(M);
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000327 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000328
329 // Traverse the graph, making sure to visit a module before visiting any
330 // of its dependencies.
Richard Smitha7c535b2015-07-22 01:28:05 +0000331 while (!Queue.empty()) {
332 ModuleFile *CurrentModule = Queue.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000333 VisitOrder.push_back(CurrentModule);
334
335 // For any module that this module depends on, push it on the
336 // stack (if it hasn't already been marked as visited).
Richard Smitha7c535b2015-07-22 01:28:05 +0000337 for (auto M = CurrentModule->Imports.rbegin(),
338 MEnd = CurrentModule->Imports.rend();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000339 M != MEnd; ++M) {
340 // Remove our current module as an impediment to visiting the
341 // module we depend on. If we were the last unvisited module
342 // that depends on this particular module, push it into the
343 // queue to be visited.
344 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
345 if (NumUnusedEdges && (--NumUnusedEdges == 0))
346 Queue.push_back(*M);
347 }
348 }
349
350 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000351
Douglas Gregore97cd902013-01-28 16:46:33 +0000352 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000353 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000354 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000355
Douglas Gregore97cd902013-01-28 16:46:33 +0000356 VisitState *State = allocateVisitState();
357 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000358
Douglas Gregor7211ac12013-01-25 23:32:03 +0000359 // If the caller has provided us with a hit-set that came from the global
360 // module index, mark every module file in common with the global module
361 // index that is *not* in that set as 'visited'.
362 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
363 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
364 {
365 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000366 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000367 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000368 }
369 }
370
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000371 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
372 ModuleFile *CurrentModule = VisitOrder[I];
373 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000374 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000375 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000376
377 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000378 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
379 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000380 if (!Visitor(*CurrentModule))
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000381 continue;
382
383 // The visitor has requested that cut off visitation of any
384 // module that the current module depends on. To indicate this
385 // behavior, we mark all of the reachable modules as having been visited.
386 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000387 do {
388 // For any module that this module depends on, push it on the
389 // stack (if it hasn't already been marked as visited).
390 for (llvm::SetVector<ModuleFile *>::iterator
391 M = NextModule->Imports.begin(),
392 MEnd = NextModule->Imports.end();
393 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000394 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
395 State->Stack.push_back(*M);
396 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000397 }
398 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000399
Douglas Gregore97cd902013-01-28 16:46:33 +0000400 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000401 break;
402
403 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000404 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000405 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000406 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000407
408 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000409}
410
Douglas Gregor7029ce12013-03-19 00:28:20 +0000411bool ModuleManager::lookupModuleFile(StringRef FileName,
412 off_t ExpectedSize,
413 time_t ExpectedModTime,
414 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000415 // Open the file immediately to ensure there is no race between stat'ing and
416 // opening the file.
417 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000418
419 if (!File && FileName != "-") {
420 return false;
421 }
422
423 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000424 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
425 // Do not destroy File, as it may be referenced. If we need to rebuild it,
426 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000427 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000428
429 return false;
430}
431
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000432#ifndef NDEBUG
433namespace llvm {
434 template<>
435 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000436 typedef ModuleFile NodeType;
437 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000438 typedef ModuleManager::ModuleConstIterator nodes_iterator;
439
440 static ChildIteratorType child_begin(NodeType *Node) {
441 return Node->Imports.begin();
442 }
443
444 static ChildIteratorType child_end(NodeType *Node) {
445 return Node->Imports.end();
446 }
447
448 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
449 return Manager.begin();
450 }
451
452 static nodes_iterator nodes_end(const ModuleManager &Manager) {
453 return Manager.end();
454 }
455 };
456
457 template<>
458 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
459 explicit DOTGraphTraits(bool IsSimple = false)
460 : DefaultDOTGraphTraits(IsSimple) { }
461
462 static bool renderGraphFromBottomUp() {
463 return true;
464 }
465
Douglas Gregorde3ef502011-11-30 23:21:26 +0000466 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000467 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000468 }
469 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000470}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000471
472void ModuleManager::viewGraph() {
473 llvm::ViewGraph(*this, "Modules");
474}
475#endif