blob: 8f1473f3a3a03dbbf0616e4d73cc8a3dcde711af [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"
Adrian Prantlc4091aa2015-02-20 19:44:52 +000016#include "clang/Serialization/ASTReader.h"
Douglas Gregor7211ac12013-01-25 23:32:03 +000017#include "clang/Serialization/GlobalModuleIndex.h"
Benjamin Krameraf04f982013-08-24 13:16:22 +000018#include "clang/Serialization/ModuleManager.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"
Douglas Gregord44252e2011-08-25 20:47:51 +000021#include "llvm/Support/raw_ostream.h"
Rafael Espindola8a8e5542014-06-12 17:19:42 +000022#include <system_error>
Douglas Gregord44252e2011-08-25 20:47:51 +000023
Douglas Gregor9d7c1a22011-10-11 19:27:55 +000024#ifndef NDEBUG
25#include "llvm/Support/GraphWriter.h"
26#endif
27
Douglas Gregord44252e2011-08-25 20:47:51 +000028using namespace clang;
29using namespace serialization;
30
Douglas Gregorde3ef502011-11-30 23:21:26 +000031ModuleFile *ModuleManager::lookup(StringRef Name) {
Douglas Gregordadd85d2013-02-08 21:27:45 +000032 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
33 /*cacheFailure=*/false);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000034 if (Entry)
35 return lookup(Entry);
36
Craig Toppera13603a2014-05-22 05:54:18 +000037 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000038}
39
40ModuleFile *ModuleManager::lookup(const FileEntry *File) {
41 llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
42 = Modules.find(File);
43 if (Known == Modules.end())
Craig Toppera13603a2014-05-22 05:54:18 +000044 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000045
46 return Known->second;
Douglas Gregord44252e2011-08-25 20:47:51 +000047}
48
Rafael Espindola5cd06f22014-08-18 19:16:31 +000049std::unique_ptr<llvm::MemoryBuffer>
50ModuleManager::lookupBuffer(StringRef Name) {
Douglas Gregordadd85d2013-02-08 21:27:45 +000051 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
52 /*cacheFailure=*/false);
Rafael Espindola5cd06f22014-08-18 19:16:31 +000053 return std::move(InMemoryBuffers[Entry]);
Douglas Gregord44252e2011-08-25 20:47:51 +000054}
55
Douglas Gregor7029ce12013-03-19 00:28:20 +000056ModuleManager::AddModuleResult
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000057ModuleManager::addModule(StringRef FileName, ModuleKind Type,
58 SourceLocation ImportLoc, ModuleFile *ImportedBy,
Douglas Gregor7029ce12013-03-19 00:28:20 +000059 unsigned Generation,
60 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +000061 ASTFileSignature ExpectedSignature,
62 std::function<ASTFileSignature(llvm::BitstreamReader &)>
63 ReadSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +000064 ModuleFile *&Module,
65 std::string &ErrorStr) {
Craig Toppera13603a2014-05-22 05:54:18 +000066 Module = nullptr;
Douglas Gregor7029ce12013-03-19 00:28:20 +000067
68 // Look for the file entry. This only fails if the expected size or
69 // modification time differ.
70 const FileEntry *Entry;
Richard Smith5b390752014-11-21 05:37:20 +000071 if (Type == MK_ExplicitModule) {
72 // If we're not expecting to pull this file out of the module cache, it
73 // might have a different mtime due to being moved across filesystems in
74 // a distributed build. The size must still match, though. (As must the
75 // contents, but we can't check that.)
76 ExpectedModTime = 0;
77 }
Eli Friedmanc27d0d52013-09-05 23:50:58 +000078 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
79 ErrorStr = "module file out of date";
Douglas Gregor7029ce12013-03-19 00:28:20 +000080 return OutOfDate;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000081 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000082
Douglas Gregord44252e2011-08-25 20:47:51 +000083 if (!Entry && FileName != "-") {
Eli Friedmanc27d0d52013-09-05 23:50:58 +000084 ErrorStr = "module file not found";
Douglas Gregor7029ce12013-03-19 00:28:20 +000085 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +000086 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000087
88 // Check whether we already loaded this module, before
Douglas Gregorde3ef502011-11-30 23:21:26 +000089 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregord44252e2011-08-25 20:47:51 +000090 bool NewModule = false;
91 if (!ModuleEntry) {
92 // Allocate a new module.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +000093 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorbdb259d2013-01-21 20:07:12 +000094 New->Index = Chain.size();
Douglas Gregord44252e2011-08-25 20:47:51 +000095 New->FileName = FileName.str();
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +000096 New->File = Entry;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000097 New->ImportLoc = ImportLoc;
Douglas Gregord44252e2011-08-25 20:47:51 +000098 Chain.push_back(New);
99 NewModule = true;
100 ModuleEntry = New;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000101
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000102 New->InputFilesValidationTimestamp = 0;
Richard Smithe842a472014-10-22 02:05:46 +0000103 if (New->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000104 std::string TimestampFilename = New->getTimestampFilename();
Ben Langmuirc8130a72014-02-20 21:59:23 +0000105 vfs::Status Status;
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000106 // A cached stat value would be fine as well.
107 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
108 New->InputFilesValidationTimestamp =
109 Status.getLastModificationTime().toEpochTime();
110 }
111
Douglas Gregord44252e2011-08-25 20:47:51 +0000112 // Load the contents of the module
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000113 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000114 // The buffer was already provided for us.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000115 New->Buffer = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000116 } else {
117 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +0000118 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
119 (std::error_code()));
Douglas Gregord44252e2011-08-25 20:47:51 +0000120 if (FileName == "-") {
Benjamin Kramera8857962014-10-26 22:44:13 +0000121 Buf = llvm::MemoryBuffer::getSTDIN();
Ben Langmuir9801b252014-06-20 00:24:56 +0000122 } else {
123 // Leave the FileEntry open so if it gets read again by another
124 // ModuleManager it must be the same underlying file.
125 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
126 // give us an open file, this may not be 100% reliable.
Benjamin Kramera8857962014-10-26 22:44:13 +0000127 Buf = FileMgr.getBufferForFile(New->File,
128 /*IsVolatile=*/false,
129 /*ShouldClose=*/false);
Ben Langmuir9801b252014-06-20 00:24:56 +0000130 }
Benjamin Kramera8857962014-10-26 22:44:13 +0000131
132 if (!Buf) {
133 ErrorStr = Buf.getError().message();
Douglas Gregor7029ce12013-03-19 00:28:20 +0000134 return Missing;
Benjamin Kramera8857962014-10-26 22:44:13 +0000135 }
136
137 New->Buffer = std::move(*Buf);
Douglas Gregord44252e2011-08-25 20:47:51 +0000138 }
Adrian Prantlc4091aa2015-02-20 19:44:52 +0000139
140 // Initialize the stream.
141 ASTReader::InitStreamFileWithModule(New->Buffer->getMemBufferRef(),
142 New->StreamFile);
Ben Langmuired982582014-11-08 00:34:30 +0000143 }
Ben Langmuir487ea142014-10-23 18:05:36 +0000144
Ben Langmuired982582014-11-08 00:34:30 +0000145 if (ExpectedSignature) {
146 if (NewModule)
147 ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile);
148 else
149 assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile));
Ben Langmuir487ea142014-10-23 18:05:36 +0000150
Ben Langmuired982582014-11-08 00:34:30 +0000151 if (ModuleEntry->Signature != ExpectedSignature) {
152 ErrorStr = ModuleEntry->Signature ? "signature mismatch"
153 : "could not read module signature";
154
155 if (NewModule) {
Ben Langmuir487ea142014-10-23 18:05:36 +0000156 // Remove the module file immediately, since removeModules might try to
157 // invalidate the file cache for Entry, and that is not safe if this
158 // module is *itself* up to date, but has an out-of-date importer.
159 Modules.erase(Entry);
160 Chain.pop_back();
Ben Langmuired982582014-11-08 00:34:30 +0000161 delete ModuleEntry;
Ben Langmuir487ea142014-10-23 18:05:36 +0000162 }
Ben Langmuired982582014-11-08 00:34:30 +0000163 return OutOfDate;
Ben Langmuir487ea142014-10-23 18:05:36 +0000164 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000165 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000166
167 if (ImportedBy) {
168 ModuleEntry->ImportedBy.insert(ImportedBy);
169 ImportedBy->Imports.insert(ModuleEntry);
170 } else {
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000171 if (!ModuleEntry->DirectlyImported)
172 ModuleEntry->ImportLoc = ImportLoc;
173
Douglas Gregord44252e2011-08-25 20:47:51 +0000174 ModuleEntry->DirectlyImported = true;
175 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000176
177 Module = ModuleEntry;
178 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000179}
180
Ben Langmuir9801b252014-06-20 00:24:56 +0000181void ModuleManager::removeModules(
182 ModuleIterator first, ModuleIterator last,
183 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
184 ModuleMap *modMap) {
Douglas Gregor188dbef2012-11-07 17:46:15 +0000185 if (first == last)
186 return;
187
188 // Collect the set of module file pointers that we'll be removing.
189 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
190
191 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000192 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Chandler Carruthb55d0222014-03-03 19:36:27 +0000193 Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
194 return victimSet.count(MF);
195 });
Douglas Gregor188dbef2012-11-07 17:46:15 +0000196 }
197
198 // Delete the modules and erase them from the various structures.
199 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000200 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000201
Douglas Gregor7029ce12013-03-19 00:28:20 +0000202 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000203 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000204 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000205 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000206 }
207 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000208
Ben Langmuir9801b252014-06-20 00:24:56 +0000209 // Files that didn't make it through ReadASTCore successfully will be
210 // rebuilt (or there was an error). Invalidate them so that we can load the
211 // new files that will be renamed over the old ones.
212 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000213 FileMgr.invalidateCache((*victim)->File);
214
Douglas Gregor188dbef2012-11-07 17:46:15 +0000215 delete *victim;
216 }
217
218 // Remove the modules from the chain.
219 Chain.erase(first, last);
220}
221
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000222void
223ModuleManager::addInMemoryBuffer(StringRef FileName,
224 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
225
226 const FileEntry *Entry =
227 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
228 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000229}
230
Douglas Gregore97cd902013-01-28 16:46:33 +0000231ModuleManager::VisitState *ModuleManager::allocateVisitState() {
232 // Fast path: if we have a cached state, use it.
233 if (FirstVisitState) {
234 VisitState *Result = FirstVisitState;
235 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000236 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000237 return Result;
238 }
239
240 // Allocate and return a new state.
241 return new VisitState(size());
242}
243
244void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000245 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000246 State->NextState = FirstVisitState;
247 FirstVisitState = State;
248}
249
Douglas Gregor7211ac12013-01-25 23:32:03 +0000250void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
251 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000252 if (!GlobalIndex) {
253 ModulesInCommonWithGlobalIndex.clear();
254 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000255 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000256
257 // Notify the global module index about all of the modules we've already
258 // loaded.
259 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
260 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
261 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
262 }
263 }
264}
265
266void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
267 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
268 return;
269
270 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000271}
272
273ModuleManager::ModuleManager(FileManager &FileMgr)
Craig Toppera13603a2014-05-22 05:54:18 +0000274 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000275
276ModuleManager::~ModuleManager() {
277 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
278 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000279 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000280}
281
Douglas Gregor7211ac12013-01-25 23:32:03 +0000282void
283ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
284 void *UserData,
Craig Topper4dd9b432014-08-17 23:49:53 +0000285 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000286 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000287 if (VisitOrder.size() != Chain.size()) {
288 unsigned N = size();
289 VisitOrder.clear();
290 VisitOrder.reserve(N);
291
292 // Record the number of incoming edges for each module. When we
293 // encounter a module with no incoming edges, push it into the queue
294 // to seed the queue.
295 SmallVector<ModuleFile *, 4> Queue;
296 Queue.reserve(N);
297 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
298 UnusedIncomingEdges.reserve(size());
299 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
300 if (unsigned Size = (*M)->ImportedBy.size())
301 UnusedIncomingEdges.push_back(Size);
302 else {
303 UnusedIncomingEdges.push_back(0);
304 Queue.push_back(*M);
305 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000306 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000307
308 // Traverse the graph, making sure to visit a module before visiting any
309 // of its dependencies.
310 unsigned QueueStart = 0;
311 while (QueueStart < Queue.size()) {
312 ModuleFile *CurrentModule = Queue[QueueStart++];
313 VisitOrder.push_back(CurrentModule);
314
315 // For any module that this module depends on, push it on the
316 // stack (if it hasn't already been marked as visited).
317 for (llvm::SetVector<ModuleFile *>::iterator
318 M = CurrentModule->Imports.begin(),
319 MEnd = CurrentModule->Imports.end();
320 M != MEnd; ++M) {
321 // Remove our current module as an impediment to visiting the
322 // module we depend on. If we were the last unvisited module
323 // that depends on this particular module, push it into the
324 // queue to be visited.
325 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
326 if (NumUnusedEdges && (--NumUnusedEdges == 0))
327 Queue.push_back(*M);
328 }
329 }
330
331 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000332
Douglas Gregore97cd902013-01-28 16:46:33 +0000333 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000334 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000335 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000336
Douglas Gregore97cd902013-01-28 16:46:33 +0000337 VisitState *State = allocateVisitState();
338 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000339
Douglas Gregor7211ac12013-01-25 23:32:03 +0000340 // If the caller has provided us with a hit-set that came from the global
341 // module index, mark every module file in common with the global module
342 // index that is *not* in that set as 'visited'.
343 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
344 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
345 {
346 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000347 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000348 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000349 }
350 }
351
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000352 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
353 ModuleFile *CurrentModule = VisitOrder[I];
354 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000355 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000356 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000357
358 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000359 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
360 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000361 if (!Visitor(*CurrentModule, UserData))
362 continue;
363
364 // The visitor has requested that cut off visitation of any
365 // module that the current module depends on. To indicate this
366 // behavior, we mark all of the reachable modules as having been visited.
367 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000368 do {
369 // For any module that this module depends on, push it on the
370 // stack (if it hasn't already been marked as visited).
371 for (llvm::SetVector<ModuleFile *>::iterator
372 M = NextModule->Imports.begin(),
373 MEnd = NextModule->Imports.end();
374 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000375 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
376 State->Stack.push_back(*M);
377 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000378 }
379 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000380
Douglas Gregore97cd902013-01-28 16:46:33 +0000381 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000382 break;
383
384 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000385 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000386 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000387 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000388
389 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000390}
391
392/// \brief Perform a depth-first visit of the current module.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000393static bool visitDepthFirst(ModuleFile &M,
394 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000395 void *UserData),
396 void *UserData,
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000397 SmallVectorImpl<bool> &Visited) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000398 // Preorder visitation
399 if (Visitor(M, /*Preorder=*/true, UserData))
400 return true;
401
402 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000403 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000404 IMEnd = M.Imports.end();
Douglas Gregord44252e2011-08-25 20:47:51 +0000405 IM != IMEnd; ++IM) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000406 if (Visited[(*IM)->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000407 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000408 Visited[(*IM)->Index] = true;
409
Douglas Gregord44252e2011-08-25 20:47:51 +0000410 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
411 return true;
412 }
413
414 // Postorder visitation
415 return Visitor(M, /*Preorder=*/false, UserData);
416}
417
Douglas Gregorde3ef502011-11-30 23:21:26 +0000418void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000419 void *UserData),
420 void *UserData) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000421 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregord44252e2011-08-25 20:47:51 +0000422 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000423 if (Visited[Chain[I]->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000424 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000425 Visited[Chain[I]->Index] = true;
426
Douglas Gregord44252e2011-08-25 20:47:51 +0000427 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
428 return;
429 }
430}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000431
Douglas Gregor7029ce12013-03-19 00:28:20 +0000432bool ModuleManager::lookupModuleFile(StringRef FileName,
433 off_t ExpectedSize,
434 time_t ExpectedModTime,
435 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000436 // Open the file immediately to ensure there is no race between stat'ing and
437 // opening the file.
438 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000439
440 if (!File && FileName != "-") {
441 return false;
442 }
443
444 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000445 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
446 // Do not destroy File, as it may be referenced. If we need to rebuild it,
447 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000448 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000449
450 return false;
451}
452
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000453#ifndef NDEBUG
454namespace llvm {
455 template<>
456 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000457 typedef ModuleFile NodeType;
458 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000459 typedef ModuleManager::ModuleConstIterator nodes_iterator;
460
461 static ChildIteratorType child_begin(NodeType *Node) {
462 return Node->Imports.begin();
463 }
464
465 static ChildIteratorType child_end(NodeType *Node) {
466 return Node->Imports.end();
467 }
468
469 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
470 return Manager.begin();
471 }
472
473 static nodes_iterator nodes_end(const ModuleManager &Manager) {
474 return Manager.end();
475 }
476 };
477
478 template<>
479 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
480 explicit DOTGraphTraits(bool IsSimple = false)
481 : DefaultDOTGraphTraits(IsSimple) { }
482
483 static bool renderGraphFromBottomUp() {
484 return true;
485 }
486
Douglas Gregorde3ef502011-11-30 23:21:26 +0000487 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000488 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000489 }
490 };
491}
492
493void ModuleManager::viewGraph() {
494 llvm::ViewGraph(*this, "Modules");
495}
496#endif