blob: b073984119f856250770335498afb890e564e3ac [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,
61 std::function<ASTFileSignature(llvm::BitstreamReader &)>
62 ReadSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +000063 ModuleFile *&Module,
64 std::string &ErrorStr) {
Craig Toppera13603a2014-05-22 05:54:18 +000065 Module = nullptr;
Douglas Gregor7029ce12013-03-19 00:28:20 +000066
67 // Look for the file entry. This only fails if the expected size or
68 // modification time differ.
69 const FileEntry *Entry;
Richard Smith5b390752014-11-21 05:37:20 +000070 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.)
75 ExpectedModTime = 0;
76 }
Eli Friedmanc27d0d52013-09-05 23:50:58 +000077 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
78 ErrorStr = "module file out of date";
Douglas Gregor7029ce12013-03-19 00:28:20 +000079 return OutOfDate;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000080 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000081
Douglas Gregord44252e2011-08-25 20:47:51 +000082 if (!Entry && FileName != "-") {
Eli Friedmanc27d0d52013-09-05 23:50:58 +000083 ErrorStr = "module file not found";
Douglas Gregor7029ce12013-03-19 00:28:20 +000084 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +000085 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000086
87 // Check whether we already loaded this module, before
Douglas Gregorde3ef502011-11-30 23:21:26 +000088 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregord44252e2011-08-25 20:47:51 +000089 bool NewModule = false;
90 if (!ModuleEntry) {
91 // Allocate a new module.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +000092 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorbdb259d2013-01-21 20:07:12 +000093 New->Index = Chain.size();
Douglas Gregord44252e2011-08-25 20:47:51 +000094 New->FileName = FileName.str();
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +000095 New->File = Entry;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000096 New->ImportLoc = ImportLoc;
Douglas Gregord44252e2011-08-25 20:47:51 +000097 Chain.push_back(New);
98 NewModule = true;
99 ModuleEntry = New;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000100
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000101 New->InputFilesValidationTimestamp = 0;
Richard Smithe842a472014-10-22 02:05:46 +0000102 if (New->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000103 std::string TimestampFilename = New->getTimestampFilename();
Ben Langmuirc8130a72014-02-20 21:59:23 +0000104 vfs::Status Status;
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000105 // A cached stat value would be fine as well.
106 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
107 New->InputFilesValidationTimestamp =
108 Status.getLastModificationTime().toEpochTime();
109 }
110
Douglas Gregord44252e2011-08-25 20:47:51 +0000111 // Load the contents of the module
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000112 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000113 // The buffer was already provided for us.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000114 New->Buffer = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000115 } else {
116 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +0000117 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
118 (std::error_code()));
Douglas Gregord44252e2011-08-25 20:47:51 +0000119 if (FileName == "-") {
Benjamin Kramera8857962014-10-26 22:44:13 +0000120 Buf = llvm::MemoryBuffer::getSTDIN();
Ben Langmuir9801b252014-06-20 00:24:56 +0000121 } else {
122 // Leave the FileEntry open so if it gets read again by another
123 // ModuleManager it must be the same underlying file.
124 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
125 // give us an open file, this may not be 100% reliable.
Benjamin Kramera8857962014-10-26 22:44:13 +0000126 Buf = FileMgr.getBufferForFile(New->File,
127 /*IsVolatile=*/false,
128 /*ShouldClose=*/false);
Ben Langmuir9801b252014-06-20 00:24:56 +0000129 }
Benjamin Kramera8857962014-10-26 22:44:13 +0000130
131 if (!Buf) {
132 ErrorStr = Buf.getError().message();
Douglas Gregor7029ce12013-03-19 00:28:20 +0000133 return Missing;
Benjamin Kramera8857962014-10-26 22:44:13 +0000134 }
135
136 New->Buffer = std::move(*Buf);
Douglas Gregord44252e2011-08-25 20:47:51 +0000137 }
Adrian Prantlcbc368c2015-02-25 02:44:04 +0000138
139 // Initialize the stream
140 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
141 (const unsigned char *)New->Buffer->getBufferEnd());
Ben Langmuired982582014-11-08 00:34:30 +0000142 }
Ben Langmuir487ea142014-10-23 18:05:36 +0000143
Ben Langmuired982582014-11-08 00:34:30 +0000144 if (ExpectedSignature) {
145 if (NewModule)
146 ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile);
147 else
148 assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile));
Ben Langmuir487ea142014-10-23 18:05:36 +0000149
Ben Langmuired982582014-11-08 00:34:30 +0000150 if (ModuleEntry->Signature != ExpectedSignature) {
151 ErrorStr = ModuleEntry->Signature ? "signature mismatch"
152 : "could not read module signature";
153
154 if (NewModule) {
Ben Langmuir487ea142014-10-23 18:05:36 +0000155 // Remove the module file immediately, since removeModules might try to
156 // invalidate the file cache for Entry, and that is not safe if this
157 // module is *itself* up to date, but has an out-of-date importer.
158 Modules.erase(Entry);
159 Chain.pop_back();
Ben Langmuired982582014-11-08 00:34:30 +0000160 delete ModuleEntry;
Ben Langmuir487ea142014-10-23 18:05:36 +0000161 }
Ben Langmuired982582014-11-08 00:34:30 +0000162 return OutOfDate;
Ben Langmuir487ea142014-10-23 18:05:36 +0000163 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000164 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000165
166 if (ImportedBy) {
167 ModuleEntry->ImportedBy.insert(ImportedBy);
168 ImportedBy->Imports.insert(ModuleEntry);
169 } else {
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000170 if (!ModuleEntry->DirectlyImported)
171 ModuleEntry->ImportLoc = ImportLoc;
172
Douglas Gregord44252e2011-08-25 20:47:51 +0000173 ModuleEntry->DirectlyImported = true;
174 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000175
176 Module = ModuleEntry;
177 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000178}
179
Ben Langmuir9801b252014-06-20 00:24:56 +0000180void ModuleManager::removeModules(
181 ModuleIterator first, ModuleIterator last,
182 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
183 ModuleMap *modMap) {
Douglas Gregor188dbef2012-11-07 17:46:15 +0000184 if (first == last)
185 return;
186
187 // Collect the set of module file pointers that we'll be removing.
188 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
189
190 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000191 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Chandler Carruthb55d0222014-03-03 19:36:27 +0000192 Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
193 return victimSet.count(MF);
194 });
Douglas Gregor188dbef2012-11-07 17:46:15 +0000195 }
196
197 // Delete the modules and erase them from the various structures.
198 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000199 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000200
Douglas Gregor7029ce12013-03-19 00:28:20 +0000201 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000202 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000203 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000204 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000205 }
206 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000207
Ben Langmuir9801b252014-06-20 00:24:56 +0000208 // Files that didn't make it through ReadASTCore successfully will be
209 // rebuilt (or there was an error). Invalidate them so that we can load the
210 // new files that will be renamed over the old ones.
211 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000212 FileMgr.invalidateCache((*victim)->File);
213
Douglas Gregor188dbef2012-11-07 17:46:15 +0000214 delete *victim;
215 }
216
217 // Remove the modules from the chain.
218 Chain.erase(first, last);
219}
220
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000221void
222ModuleManager::addInMemoryBuffer(StringRef FileName,
223 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
224
225 const FileEntry *Entry =
226 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
227 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000228}
229
Richard Smith7f330cd2015-03-18 01:42:29 +0000230bool ModuleManager::addKnownModuleFile(StringRef FileName) {
231 const FileEntry *File;
232 if (lookupModuleFile(FileName, 0, 0, File))
233 return true;
234 if (!Modules.count(File))
235 AdditionalKnownModuleFiles.insert(File);
236 return false;
237}
238
Douglas Gregore97cd902013-01-28 16:46:33 +0000239ModuleManager::VisitState *ModuleManager::allocateVisitState() {
240 // Fast path: if we have a cached state, use it.
241 if (FirstVisitState) {
242 VisitState *Result = FirstVisitState;
243 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000244 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000245 return Result;
246 }
247
248 // Allocate and return a new state.
249 return new VisitState(size());
250}
251
252void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000253 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000254 State->NextState = FirstVisitState;
255 FirstVisitState = State;
256}
257
Douglas Gregor7211ac12013-01-25 23:32:03 +0000258void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
259 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000260 if (!GlobalIndex) {
261 ModulesInCommonWithGlobalIndex.clear();
262 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000263 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000264
265 // Notify the global module index about all of the modules we've already
266 // loaded.
267 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
268 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
269 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
270 }
271 }
272}
273
274void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
Richard Smith7f330cd2015-03-18 01:42:29 +0000275 AdditionalKnownModuleFiles.remove(MF->File);
276
Douglas Gregor603cd862013-03-22 18:50:14 +0000277 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
278 return;
279
280 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000281}
282
283ModuleManager::ModuleManager(FileManager &FileMgr)
Craig Toppera13603a2014-05-22 05:54:18 +0000284 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000285
286ModuleManager::~ModuleManager() {
287 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
288 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000289 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000290}
291
Douglas Gregor7211ac12013-01-25 23:32:03 +0000292void
293ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
294 void *UserData,
Craig Topper4dd9b432014-08-17 23:49:53 +0000295 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000296 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000297 if (VisitOrder.size() != Chain.size()) {
298 unsigned N = size();
299 VisitOrder.clear();
300 VisitOrder.reserve(N);
301
302 // Record the number of incoming edges for each module. When we
303 // encounter a module with no incoming edges, push it into the queue
304 // to seed the queue.
305 SmallVector<ModuleFile *, 4> Queue;
306 Queue.reserve(N);
307 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
308 UnusedIncomingEdges.reserve(size());
309 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
310 if (unsigned Size = (*M)->ImportedBy.size())
311 UnusedIncomingEdges.push_back(Size);
312 else {
313 UnusedIncomingEdges.push_back(0);
314 Queue.push_back(*M);
315 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000316 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000317
318 // Traverse the graph, making sure to visit a module before visiting any
319 // of its dependencies.
320 unsigned QueueStart = 0;
321 while (QueueStart < Queue.size()) {
322 ModuleFile *CurrentModule = Queue[QueueStart++];
323 VisitOrder.push_back(CurrentModule);
324
325 // For any module that this module depends on, push it on the
326 // stack (if it hasn't already been marked as visited).
327 for (llvm::SetVector<ModuleFile *>::iterator
328 M = CurrentModule->Imports.begin(),
329 MEnd = CurrentModule->Imports.end();
330 M != MEnd; ++M) {
331 // Remove our current module as an impediment to visiting the
332 // module we depend on. If we were the last unvisited module
333 // that depends on this particular module, push it into the
334 // queue to be visited.
335 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
336 if (NumUnusedEdges && (--NumUnusedEdges == 0))
337 Queue.push_back(*M);
338 }
339 }
340
341 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000342
Douglas Gregore97cd902013-01-28 16:46:33 +0000343 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000344 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000345 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000346
Douglas Gregore97cd902013-01-28 16:46:33 +0000347 VisitState *State = allocateVisitState();
348 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000349
Douglas Gregor7211ac12013-01-25 23:32:03 +0000350 // If the caller has provided us with a hit-set that came from the global
351 // module index, mark every module file in common with the global module
352 // index that is *not* in that set as 'visited'.
353 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
354 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
355 {
356 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000357 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000358 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000359 }
360 }
361
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000362 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
363 ModuleFile *CurrentModule = VisitOrder[I];
364 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000365 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000366 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000367
368 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000369 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
370 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000371 if (!Visitor(*CurrentModule, UserData))
372 continue;
373
374 // The visitor has requested that cut off visitation of any
375 // module that the current module depends on. To indicate this
376 // behavior, we mark all of the reachable modules as having been visited.
377 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000378 do {
379 // For any module that this module depends on, push it on the
380 // stack (if it hasn't already been marked as visited).
381 for (llvm::SetVector<ModuleFile *>::iterator
382 M = NextModule->Imports.begin(),
383 MEnd = NextModule->Imports.end();
384 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000385 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
386 State->Stack.push_back(*M);
387 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000388 }
389 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000390
Douglas Gregore97cd902013-01-28 16:46:33 +0000391 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000392 break;
393
394 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000395 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000396 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000397 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000398
399 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000400}
401
402/// \brief Perform a depth-first visit of the current module.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000403static bool visitDepthFirst(ModuleFile &M,
404 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000405 void *UserData),
406 void *UserData,
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000407 SmallVectorImpl<bool> &Visited) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000408 // Preorder visitation
409 if (Visitor(M, /*Preorder=*/true, UserData))
410 return true;
411
412 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000413 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000414 IMEnd = M.Imports.end();
Douglas Gregord44252e2011-08-25 20:47:51 +0000415 IM != IMEnd; ++IM) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000416 if (Visited[(*IM)->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000417 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000418 Visited[(*IM)->Index] = true;
419
Douglas Gregord44252e2011-08-25 20:47:51 +0000420 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
421 return true;
422 }
423
424 // Postorder visitation
425 return Visitor(M, /*Preorder=*/false, UserData);
426}
427
Douglas Gregorde3ef502011-11-30 23:21:26 +0000428void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000429 void *UserData),
430 void *UserData) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000431 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregord44252e2011-08-25 20:47:51 +0000432 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000433 if (Visited[Chain[I]->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000434 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000435 Visited[Chain[I]->Index] = true;
436
Douglas Gregord44252e2011-08-25 20:47:51 +0000437 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
438 return;
439 }
440}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000441
Douglas Gregor7029ce12013-03-19 00:28:20 +0000442bool ModuleManager::lookupModuleFile(StringRef FileName,
443 off_t ExpectedSize,
444 time_t ExpectedModTime,
445 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000446 // Open the file immediately to ensure there is no race between stat'ing and
447 // opening the file.
448 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000449
450 if (!File && FileName != "-") {
451 return false;
452 }
453
454 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000455 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
456 // Do not destroy File, as it may be referenced. If we need to rebuild it,
457 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000458 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000459
460 return false;
461}
462
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000463#ifndef NDEBUG
464namespace llvm {
465 template<>
466 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000467 typedef ModuleFile NodeType;
468 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000469 typedef ModuleManager::ModuleConstIterator nodes_iterator;
470
471 static ChildIteratorType child_begin(NodeType *Node) {
472 return Node->Imports.begin();
473 }
474
475 static ChildIteratorType child_end(NodeType *Node) {
476 return Node->Imports.end();
477 }
478
479 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
480 return Manager.begin();
481 }
482
483 static nodes_iterator nodes_end(const ModuleManager &Manager) {
484 return Manager.end();
485 }
486 };
487
488 template<>
489 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
490 explicit DOTGraphTraits(bool IsSimple = false)
491 : DefaultDOTGraphTraits(IsSimple) { }
492
493 static bool renderGraphFromBottomUp() {
494 return true;
495 }
496
Douglas Gregorde3ef502011-11-30 23:21:26 +0000497 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000498 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000499 }
500 };
501}
502
503void ModuleManager::viewGraph() {
504 llvm::ViewGraph(*this, "Modules");
505}
506#endif