blob: ac98ca0b8720306ef6a2f32415187d9c0e35b926 [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 }
138
139 // Initialize the stream
140 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
Douglas Gregor7029ce12013-03-19 00:28:20 +0000141 (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
Douglas Gregore97cd902013-01-28 16:46:33 +0000230ModuleManager::VisitState *ModuleManager::allocateVisitState() {
231 // Fast path: if we have a cached state, use it.
232 if (FirstVisitState) {
233 VisitState *Result = FirstVisitState;
234 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000235 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000236 return Result;
237 }
238
239 // Allocate and return a new state.
240 return new VisitState(size());
241}
242
243void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000244 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000245 State->NextState = FirstVisitState;
246 FirstVisitState = State;
247}
248
Douglas Gregor7211ac12013-01-25 23:32:03 +0000249void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
250 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000251 if (!GlobalIndex) {
252 ModulesInCommonWithGlobalIndex.clear();
253 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000254 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000255
256 // Notify the global module index about all of the modules we've already
257 // loaded.
258 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
259 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
260 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
261 }
262 }
263}
264
265void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
266 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
267 return;
268
269 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000270}
271
272ModuleManager::ModuleManager(FileManager &FileMgr)
Craig Toppera13603a2014-05-22 05:54:18 +0000273 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000274
275ModuleManager::~ModuleManager() {
276 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
277 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000278 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000279}
280
Douglas Gregor7211ac12013-01-25 23:32:03 +0000281void
282ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
283 void *UserData,
Craig Topper4dd9b432014-08-17 23:49:53 +0000284 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000285 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000286 if (VisitOrder.size() != Chain.size()) {
287 unsigned N = size();
288 VisitOrder.clear();
289 VisitOrder.reserve(N);
290
291 // Record the number of incoming edges for each module. When we
292 // encounter a module with no incoming edges, push it into the queue
293 // to seed the queue.
294 SmallVector<ModuleFile *, 4> Queue;
295 Queue.reserve(N);
296 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
297 UnusedIncomingEdges.reserve(size());
298 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
299 if (unsigned Size = (*M)->ImportedBy.size())
300 UnusedIncomingEdges.push_back(Size);
301 else {
302 UnusedIncomingEdges.push_back(0);
303 Queue.push_back(*M);
304 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000305 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000306
307 // Traverse the graph, making sure to visit a module before visiting any
308 // of its dependencies.
309 unsigned QueueStart = 0;
310 while (QueueStart < Queue.size()) {
311 ModuleFile *CurrentModule = Queue[QueueStart++];
312 VisitOrder.push_back(CurrentModule);
313
314 // For any module that this module depends on, push it on the
315 // stack (if it hasn't already been marked as visited).
316 for (llvm::SetVector<ModuleFile *>::iterator
317 M = CurrentModule->Imports.begin(),
318 MEnd = CurrentModule->Imports.end();
319 M != MEnd; ++M) {
320 // Remove our current module as an impediment to visiting the
321 // module we depend on. If we were the last unvisited module
322 // that depends on this particular module, push it into the
323 // queue to be visited.
324 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
325 if (NumUnusedEdges && (--NumUnusedEdges == 0))
326 Queue.push_back(*M);
327 }
328 }
329
330 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000331
Douglas Gregore97cd902013-01-28 16:46:33 +0000332 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000333 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000334 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000335
Douglas Gregore97cd902013-01-28 16:46:33 +0000336 VisitState *State = allocateVisitState();
337 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000338
Douglas Gregor7211ac12013-01-25 23:32:03 +0000339 // If the caller has provided us with a hit-set that came from the global
340 // module index, mark every module file in common with the global module
341 // index that is *not* in that set as 'visited'.
342 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
343 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
344 {
345 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000346 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000347 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000348 }
349 }
350
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000351 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
352 ModuleFile *CurrentModule = VisitOrder[I];
353 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000354 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000355 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000356
357 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000358 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
359 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000360 if (!Visitor(*CurrentModule, UserData))
361 continue;
362
363 // The visitor has requested that cut off visitation of any
364 // module that the current module depends on. To indicate this
365 // behavior, we mark all of the reachable modules as having been visited.
366 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000367 do {
368 // For any module that this module depends on, push it on the
369 // stack (if it hasn't already been marked as visited).
370 for (llvm::SetVector<ModuleFile *>::iterator
371 M = NextModule->Imports.begin(),
372 MEnd = NextModule->Imports.end();
373 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000374 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
375 State->Stack.push_back(*M);
376 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000377 }
378 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000379
Douglas Gregore97cd902013-01-28 16:46:33 +0000380 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000381 break;
382
383 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000384 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000385 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000386 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000387
388 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000389}
390
391/// \brief Perform a depth-first visit of the current module.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000392static bool visitDepthFirst(ModuleFile &M,
393 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000394 void *UserData),
395 void *UserData,
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000396 SmallVectorImpl<bool> &Visited) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000397 // Preorder visitation
398 if (Visitor(M, /*Preorder=*/true, UserData))
399 return true;
400
401 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000402 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000403 IMEnd = M.Imports.end();
Douglas Gregord44252e2011-08-25 20:47:51 +0000404 IM != IMEnd; ++IM) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000405 if (Visited[(*IM)->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000406 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000407 Visited[(*IM)->Index] = true;
408
Douglas Gregord44252e2011-08-25 20:47:51 +0000409 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
410 return true;
411 }
412
413 // Postorder visitation
414 return Visitor(M, /*Preorder=*/false, UserData);
415}
416
Douglas Gregorde3ef502011-11-30 23:21:26 +0000417void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000418 void *UserData),
419 void *UserData) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000420 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregord44252e2011-08-25 20:47:51 +0000421 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000422 if (Visited[Chain[I]->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000423 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000424 Visited[Chain[I]->Index] = true;
425
Douglas Gregord44252e2011-08-25 20:47:51 +0000426 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
427 return;
428 }
429}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000430
Douglas Gregor7029ce12013-03-19 00:28:20 +0000431bool ModuleManager::lookupModuleFile(StringRef FileName,
432 off_t ExpectedSize,
433 time_t ExpectedModTime,
434 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000435 // Open the file immediately to ensure there is no race between stat'ing and
436 // opening the file.
437 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000438
439 if (!File && FileName != "-") {
440 return false;
441 }
442
443 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000444 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
445 // Do not destroy File, as it may be referenced. If we need to rebuild it,
446 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000447 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000448
449 return false;
450}
451
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000452#ifndef NDEBUG
453namespace llvm {
454 template<>
455 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000456 typedef ModuleFile NodeType;
457 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000458 typedef ModuleManager::ModuleConstIterator nodes_iterator;
459
460 static ChildIteratorType child_begin(NodeType *Node) {
461 return Node->Imports.begin();
462 }
463
464 static ChildIteratorType child_end(NodeType *Node) {
465 return Node->Imports.end();
466 }
467
468 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
469 return Manager.begin();
470 }
471
472 static nodes_iterator nodes_end(const ModuleManager &Manager) {
473 return Manager.end();
474 }
475 };
476
477 template<>
478 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
479 explicit DOTGraphTraits(bool IsSimple = false)
480 : DefaultDOTGraphTraits(IsSimple) { }
481
482 static bool renderGraphFromBottomUp() {
483 return true;
484 }
485
Douglas Gregorde3ef502011-11-30 23:21:26 +0000486 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000487 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000488 }
489 };
490}
491
492void ModuleManager::viewGraph() {
493 llvm::ViewGraph(*this, "Modules");
494}
495#endif