blob: a50c2b158cbed613fe6683655a4afbe0db05852c [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//===----------------------------------------------------------------------===//
Ben Langmuirbeee15e2014-04-14 18:00:01 +000014#include "clang/Lex/HeaderSearch.h"
Douglas Gregor7029ce12013-03-19 00:28:20 +000015#include "clang/Lex/ModuleMap.h"
Douglas Gregor7211ac12013-01-25 23:32:03 +000016#include "clang/Serialization/GlobalModuleIndex.h"
Benjamin Krameraf04f982013-08-24 13:16:22 +000017#include "clang/Serialization/ModuleManager.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000018#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola552c1692013-06-11 22:15:02 +000019#include "llvm/Support/Path.h"
Douglas Gregord44252e2011-08-25 20:47:51 +000020#include "llvm/Support/raw_ostream.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);
97 NewModule = true;
98 ModuleEntry = New;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000099
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000100 New->InputFilesValidationTimestamp = 0;
Richard Smithe842a472014-10-22 02:05:46 +0000101 if (New->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000102 std::string TimestampFilename = New->getTimestampFilename();
Ben Langmuirc8130a72014-02-20 21:59:23 +0000103 vfs::Status Status;
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000104 // A cached stat value would be fine as well.
105 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
106 New->InputFilesValidationTimestamp =
107 Status.getLastModificationTime().toEpochTime();
108 }
109
Douglas Gregord44252e2011-08-25 20:47:51 +0000110 // Load the contents of the module
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000111 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000112 // The buffer was already provided for us.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000113 New->Buffer = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000114 } else {
115 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +0000116 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
117 (std::error_code()));
Douglas Gregord44252e2011-08-25 20:47:51 +0000118 if (FileName == "-") {
Benjamin Kramera8857962014-10-26 22:44:13 +0000119 Buf = llvm::MemoryBuffer::getSTDIN();
Ben Langmuir9801b252014-06-20 00:24:56 +0000120 } else {
121 // Leave the FileEntry open so if it gets read again by another
122 // ModuleManager it must be the same underlying file.
123 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
124 // give us an open file, this may not be 100% reliable.
Benjamin Kramera8857962014-10-26 22:44:13 +0000125 Buf = FileMgr.getBufferForFile(New->File,
126 /*IsVolatile=*/false,
127 /*ShouldClose=*/false);
Ben Langmuir9801b252014-06-20 00:24:56 +0000128 }
Benjamin Kramera8857962014-10-26 22:44:13 +0000129
130 if (!Buf) {
131 ErrorStr = Buf.getError().message();
Douglas Gregor7029ce12013-03-19 00:28:20 +0000132 return Missing;
Benjamin Kramera8857962014-10-26 22:44:13 +0000133 }
134
135 New->Buffer = std::move(*Buf);
Douglas Gregord44252e2011-08-25 20:47:51 +0000136 }
Adrian Prantlcbc368c2015-02-25 02:44:04 +0000137
138 // Initialize the stream
139 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
140 (const unsigned char *)New->Buffer->getBufferEnd());
Ben Langmuired982582014-11-08 00:34:30 +0000141 }
Ben Langmuir487ea142014-10-23 18:05:36 +0000142
Ben Langmuired982582014-11-08 00:34:30 +0000143 if (ExpectedSignature) {
144 if (NewModule)
145 ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile);
146 else
147 assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile));
Ben Langmuir487ea142014-10-23 18:05:36 +0000148
Ben Langmuired982582014-11-08 00:34:30 +0000149 if (ModuleEntry->Signature != ExpectedSignature) {
150 ErrorStr = ModuleEntry->Signature ? "signature mismatch"
151 : "could not read module signature";
152
153 if (NewModule) {
Ben Langmuir487ea142014-10-23 18:05:36 +0000154 // Remove the module file immediately, since removeModules might try to
155 // invalidate the file cache for Entry, and that is not safe if this
156 // module is *itself* up to date, but has an out-of-date importer.
157 Modules.erase(Entry);
158 Chain.pop_back();
Ben Langmuired982582014-11-08 00:34:30 +0000159 delete ModuleEntry;
Ben Langmuir487ea142014-10-23 18:05:36 +0000160 }
Ben Langmuired982582014-11-08 00:34:30 +0000161 return OutOfDate;
Ben Langmuir487ea142014-10-23 18:05:36 +0000162 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000163 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000164
165 if (ImportedBy) {
166 ModuleEntry->ImportedBy.insert(ImportedBy);
167 ImportedBy->Imports.insert(ModuleEntry);
168 } else {
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000169 if (!ModuleEntry->DirectlyImported)
170 ModuleEntry->ImportLoc = ImportLoc;
171
Douglas Gregord44252e2011-08-25 20:47:51 +0000172 ModuleEntry->DirectlyImported = true;
173 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000174
175 Module = ModuleEntry;
176 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000177}
178
Ben Langmuir9801b252014-06-20 00:24:56 +0000179void ModuleManager::removeModules(
180 ModuleIterator first, ModuleIterator last,
181 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
182 ModuleMap *modMap) {
Douglas Gregor188dbef2012-11-07 17:46:15 +0000183 if (first == last)
184 return;
185
186 // Collect the set of module file pointers that we'll be removing.
187 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
188
189 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000190 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Chandler Carruthb55d0222014-03-03 19:36:27 +0000191 Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
192 return victimSet.count(MF);
193 });
Douglas Gregor188dbef2012-11-07 17:46:15 +0000194 }
195
196 // Delete the modules and erase them from the various structures.
197 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000198 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000199
Douglas Gregor7029ce12013-03-19 00:28:20 +0000200 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000201 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000202 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000203 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000204 }
205 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000206
Ben Langmuir9801b252014-06-20 00:24:56 +0000207 // Files that didn't make it through ReadASTCore successfully will be
208 // rebuilt (or there was an error). Invalidate them so that we can load the
209 // new files that will be renamed over the old ones.
210 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000211 FileMgr.invalidateCache((*victim)->File);
212
Douglas Gregor188dbef2012-11-07 17:46:15 +0000213 delete *victim;
214 }
215
216 // Remove the modules from the chain.
217 Chain.erase(first, last);
218}
219
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000220void
221ModuleManager::addInMemoryBuffer(StringRef FileName,
222 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
223
224 const FileEntry *Entry =
225 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
226 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000227}
228
Richard Smith7f330cd2015-03-18 01:42:29 +0000229bool ModuleManager::addKnownModuleFile(StringRef FileName) {
230 const FileEntry *File;
231 if (lookupModuleFile(FileName, 0, 0, File))
232 return true;
233 if (!Modules.count(File))
234 AdditionalKnownModuleFiles.insert(File);
235 return false;
236}
237
Douglas Gregore97cd902013-01-28 16:46:33 +0000238ModuleManager::VisitState *ModuleManager::allocateVisitState() {
239 // Fast path: if we have a cached state, use it.
240 if (FirstVisitState) {
241 VisitState *Result = FirstVisitState;
242 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000243 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000244 return Result;
245 }
246
247 // Allocate and return a new state.
248 return new VisitState(size());
249}
250
251void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000252 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000253 State->NextState = FirstVisitState;
254 FirstVisitState = State;
255}
256
Douglas Gregor7211ac12013-01-25 23:32:03 +0000257void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
258 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000259 if (!GlobalIndex) {
260 ModulesInCommonWithGlobalIndex.clear();
261 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000262 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000263
264 // Notify the global module index about all of the modules we've already
265 // loaded.
266 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
267 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
268 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
269 }
270 }
271}
272
273void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
Richard Smith7f330cd2015-03-18 01:42:29 +0000274 AdditionalKnownModuleFiles.remove(MF->File);
275
Douglas Gregor603cd862013-03-22 18:50:14 +0000276 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
277 return;
278
279 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000280}
281
282ModuleManager::ModuleManager(FileManager &FileMgr)
Craig Toppera13603a2014-05-22 05:54:18 +0000283 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000284
285ModuleManager::~ModuleManager() {
286 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
287 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000288 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000289}
290
Douglas Gregor7211ac12013-01-25 23:32:03 +0000291void
292ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
293 void *UserData,
Craig Topper4dd9b432014-08-17 23:49:53 +0000294 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000295 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000296 if (VisitOrder.size() != Chain.size()) {
297 unsigned N = size();
298 VisitOrder.clear();
299 VisitOrder.reserve(N);
300
301 // Record the number of incoming edges for each module. When we
302 // encounter a module with no incoming edges, push it into the queue
303 // to seed the queue.
304 SmallVector<ModuleFile *, 4> Queue;
305 Queue.reserve(N);
306 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
307 UnusedIncomingEdges.reserve(size());
308 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
309 if (unsigned Size = (*M)->ImportedBy.size())
310 UnusedIncomingEdges.push_back(Size);
311 else {
312 UnusedIncomingEdges.push_back(0);
313 Queue.push_back(*M);
314 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000315 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000316
317 // Traverse the graph, making sure to visit a module before visiting any
318 // of its dependencies.
319 unsigned QueueStart = 0;
320 while (QueueStart < Queue.size()) {
321 ModuleFile *CurrentModule = Queue[QueueStart++];
322 VisitOrder.push_back(CurrentModule);
323
324 // For any module that this module depends on, push it on the
325 // stack (if it hasn't already been marked as visited).
326 for (llvm::SetVector<ModuleFile *>::iterator
327 M = CurrentModule->Imports.begin(),
328 MEnd = CurrentModule->Imports.end();
329 M != MEnd; ++M) {
330 // Remove our current module as an impediment to visiting the
331 // module we depend on. If we were the last unvisited module
332 // that depends on this particular module, push it into the
333 // queue to be visited.
334 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
335 if (NumUnusedEdges && (--NumUnusedEdges == 0))
336 Queue.push_back(*M);
337 }
338 }
339
340 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000341
Douglas Gregore97cd902013-01-28 16:46:33 +0000342 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000343 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000344 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000345
Douglas Gregore97cd902013-01-28 16:46:33 +0000346 VisitState *State = allocateVisitState();
347 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000348
Douglas Gregor7211ac12013-01-25 23:32:03 +0000349 // If the caller has provided us with a hit-set that came from the global
350 // module index, mark every module file in common with the global module
351 // index that is *not* in that set as 'visited'.
352 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
353 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
354 {
355 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000356 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000357 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000358 }
359 }
360
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000361 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
362 ModuleFile *CurrentModule = VisitOrder[I];
363 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000364 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000365 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000366
367 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000368 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
369 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000370 if (!Visitor(*CurrentModule, UserData))
371 continue;
372
373 // The visitor has requested that cut off visitation of any
374 // module that the current module depends on. To indicate this
375 // behavior, we mark all of the reachable modules as having been visited.
376 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000377 do {
378 // For any module that this module depends on, push it on the
379 // stack (if it hasn't already been marked as visited).
380 for (llvm::SetVector<ModuleFile *>::iterator
381 M = NextModule->Imports.begin(),
382 MEnd = NextModule->Imports.end();
383 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000384 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
385 State->Stack.push_back(*M);
386 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000387 }
388 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000389
Douglas Gregore97cd902013-01-28 16:46:33 +0000390 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000391 break;
392
393 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000394 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000395 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000396 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000397
398 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000399}
400
401/// \brief Perform a depth-first visit of the current module.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000402static bool visitDepthFirst(ModuleFile &M,
403 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000404 void *UserData),
405 void *UserData,
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000406 SmallVectorImpl<bool> &Visited) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000407 // Preorder visitation
408 if (Visitor(M, /*Preorder=*/true, UserData))
409 return true;
410
411 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000412 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000413 IMEnd = M.Imports.end();
Douglas Gregord44252e2011-08-25 20:47:51 +0000414 IM != IMEnd; ++IM) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000415 if (Visited[(*IM)->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000416 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000417 Visited[(*IM)->Index] = true;
418
Douglas Gregord44252e2011-08-25 20:47:51 +0000419 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
420 return true;
421 }
422
423 // Postorder visitation
424 return Visitor(M, /*Preorder=*/false, UserData);
425}
426
Douglas Gregorde3ef502011-11-30 23:21:26 +0000427void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000428 void *UserData),
429 void *UserData) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000430 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregord44252e2011-08-25 20:47:51 +0000431 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000432 if (Visited[Chain[I]->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000433 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000434 Visited[Chain[I]->Index] = true;
435
Douglas Gregord44252e2011-08-25 20:47:51 +0000436 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
437 return;
438 }
439}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000440
Douglas Gregor7029ce12013-03-19 00:28:20 +0000441bool ModuleManager::lookupModuleFile(StringRef FileName,
442 off_t ExpectedSize,
443 time_t ExpectedModTime,
444 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000445 // Open the file immediately to ensure there is no race between stat'ing and
446 // opening the file.
447 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000448
449 if (!File && FileName != "-") {
450 return false;
451 }
452
453 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000454 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
455 // Do not destroy File, as it may be referenced. If we need to rebuild it,
456 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000457 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000458
459 return false;
460}
461
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000462#ifndef NDEBUG
463namespace llvm {
464 template<>
465 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000466 typedef ModuleFile NodeType;
467 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000468 typedef ModuleManager::ModuleConstIterator nodes_iterator;
469
470 static ChildIteratorType child_begin(NodeType *Node) {
471 return Node->Imports.begin();
472 }
473
474 static ChildIteratorType child_end(NodeType *Node) {
475 return Node->Imports.end();
476 }
477
478 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
479 return Manager.begin();
480 }
481
482 static nodes_iterator nodes_end(const ModuleManager &Manager) {
483 return Manager.end();
484 }
485 };
486
487 template<>
488 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
489 explicit DOTGraphTraits(bool IsSimple = false)
490 : DefaultDOTGraphTraits(IsSimple) { }
491
492 static bool renderGraphFromBottomUp() {
493 return true;
494 }
495
Douglas Gregorde3ef502011-11-30 23:21:26 +0000496 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000497 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000498 }
499 };
500}
501
502void ModuleManager::viewGraph() {
503 llvm::ViewGraph(*this, "Modules");
504}
505#endif