blob: 300ef31b59004f0bbe0db620dd167a57be75346e [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;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000070 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
71 ErrorStr = "module file out of date";
Douglas Gregor7029ce12013-03-19 00:28:20 +000072 return OutOfDate;
Eli Friedmanc27d0d52013-09-05 23:50:58 +000073 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000074
Douglas Gregord44252e2011-08-25 20:47:51 +000075 if (!Entry && FileName != "-") {
Eli Friedmanc27d0d52013-09-05 23:50:58 +000076 ErrorStr = "module file not found";
Douglas Gregor7029ce12013-03-19 00:28:20 +000077 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +000078 }
Douglas Gregor7029ce12013-03-19 00:28:20 +000079
80 // Check whether we already loaded this module, before
Douglas Gregorde3ef502011-11-30 23:21:26 +000081 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregord44252e2011-08-25 20:47:51 +000082 bool NewModule = false;
83 if (!ModuleEntry) {
84 // Allocate a new module.
Douglas Gregor4fc9f3e2012-01-18 20:56:22 +000085 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorbdb259d2013-01-21 20:07:12 +000086 New->Index = Chain.size();
Douglas Gregord44252e2011-08-25 20:47:51 +000087 New->FileName = FileName.str();
Argyrios Kyrtzidisaedf7142012-10-03 01:58:42 +000088 New->File = Entry;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000089 New->ImportLoc = ImportLoc;
Douglas Gregord44252e2011-08-25 20:47:51 +000090 Chain.push_back(New);
91 NewModule = true;
92 ModuleEntry = New;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +000093
Dmitri Gribenkof430da42014-02-12 10:33:14 +000094 New->InputFilesValidationTimestamp = 0;
Richard Smithe842a472014-10-22 02:05:46 +000095 if (New->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +000096 std::string TimestampFilename = New->getTimestampFilename();
Ben Langmuirc8130a72014-02-20 21:59:23 +000097 vfs::Status Status;
Dmitri Gribenkof430da42014-02-12 10:33:14 +000098 // A cached stat value would be fine as well.
99 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
100 New->InputFilesValidationTimestamp =
101 Status.getLastModificationTime().toEpochTime();
102 }
103
Douglas Gregord44252e2011-08-25 20:47:51 +0000104 // Load the contents of the module
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000105 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000106 // The buffer was already provided for us.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000107 New->Buffer = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000108 } else {
109 // Open the AST file.
Rafael Espindolac0809172014-06-12 14:02:15 +0000110 std::error_code ec;
Douglas Gregord44252e2011-08-25 20:47:51 +0000111 if (FileName == "-") {
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000112 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf =
113 llvm::MemoryBuffer::getSTDIN();
114 ec = Buf.getError();
Douglas Gregord44252e2011-08-25 20:47:51 +0000115 if (ec)
116 ErrorStr = ec.message();
Rafael Espindola2d2b4202014-07-06 17:43:24 +0000117 else
118 New->Buffer = std::move(Buf.get());
Ben Langmuir9801b252014-06-20 00:24:56 +0000119 } else {
120 // Leave the FileEntry open so if it gets read again by another
121 // ModuleManager it must be the same underlying file.
122 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
123 // give us an open file, this may not be 100% reliable.
Rafael Espindola6406f7b2014-08-26 19:54:40 +0000124 New->Buffer = FileMgr.getBufferForFile(New->File, &ErrorStr,
125 /*IsVolatile*/ false,
126 /*ShouldClose*/ false);
Ben Langmuir9801b252014-06-20 00:24:56 +0000127 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000128
129 if (!New->Buffer)
Douglas Gregor7029ce12013-03-19 00:28:20 +0000130 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +0000131 }
132
133 // Initialize the stream
134 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
Douglas Gregor7029ce12013-03-19 00:28:20 +0000135 (const unsigned char *)New->Buffer->getBufferEnd());
Ben Langmuir487ea142014-10-23 18:05:36 +0000136
137 if (ExpectedSignature) {
138 New->Signature = ReadSignature(New->StreamFile);
139 if (New->Signature != ExpectedSignature) {
140 ErrorStr = New->Signature ? "signature mismatch"
141 : "could not read module signature";
142
143 // Remove the module file immediately, since removeModules might try to
144 // invalidate the file cache for Entry, and that is not safe if this
145 // module is *itself* up to date, but has an out-of-date importer.
146 Modules.erase(Entry);
147 Chain.pop_back();
148 return OutOfDate;
149 }
150 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000151 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000152
153 if (ImportedBy) {
154 ModuleEntry->ImportedBy.insert(ImportedBy);
155 ImportedBy->Imports.insert(ModuleEntry);
156 } else {
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000157 if (!ModuleEntry->DirectlyImported)
158 ModuleEntry->ImportLoc = ImportLoc;
159
Douglas Gregord44252e2011-08-25 20:47:51 +0000160 ModuleEntry->DirectlyImported = true;
161 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000162
163 Module = ModuleEntry;
164 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000165}
166
Ben Langmuir9801b252014-06-20 00:24:56 +0000167void ModuleManager::removeModules(
168 ModuleIterator first, ModuleIterator last,
169 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
170 ModuleMap *modMap) {
Douglas Gregor188dbef2012-11-07 17:46:15 +0000171 if (first == last)
172 return;
173
174 // Collect the set of module file pointers that we'll be removing.
175 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
176
177 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000178 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Chandler Carruthb55d0222014-03-03 19:36:27 +0000179 Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
180 return victimSet.count(MF);
181 });
Douglas Gregor188dbef2012-11-07 17:46:15 +0000182 }
183
184 // Delete the modules and erase them from the various structures.
185 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000186 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000187
Douglas Gregor7029ce12013-03-19 00:28:20 +0000188 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000189 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000190 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000191 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000192 }
193 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000194
Ben Langmuir9801b252014-06-20 00:24:56 +0000195 // Files that didn't make it through ReadASTCore successfully will be
196 // rebuilt (or there was an error). Invalidate them so that we can load the
197 // new files that will be renamed over the old ones.
198 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000199 FileMgr.invalidateCache((*victim)->File);
200
Douglas Gregor188dbef2012-11-07 17:46:15 +0000201 delete *victim;
202 }
203
204 // Remove the modules from the chain.
205 Chain.erase(first, last);
206}
207
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000208void
209ModuleManager::addInMemoryBuffer(StringRef FileName,
210 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
211
212 const FileEntry *Entry =
213 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
214 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000215}
216
Douglas Gregore97cd902013-01-28 16:46:33 +0000217ModuleManager::VisitState *ModuleManager::allocateVisitState() {
218 // Fast path: if we have a cached state, use it.
219 if (FirstVisitState) {
220 VisitState *Result = FirstVisitState;
221 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000222 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000223 return Result;
224 }
225
226 // Allocate and return a new state.
227 return new VisitState(size());
228}
229
230void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000231 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000232 State->NextState = FirstVisitState;
233 FirstVisitState = State;
234}
235
Douglas Gregor7211ac12013-01-25 23:32:03 +0000236void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
237 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000238 if (!GlobalIndex) {
239 ModulesInCommonWithGlobalIndex.clear();
240 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000241 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000242
243 // Notify the global module index about all of the modules we've already
244 // loaded.
245 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
246 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
247 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
248 }
249 }
250}
251
252void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
253 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
254 return;
255
256 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000257}
258
259ModuleManager::ModuleManager(FileManager &FileMgr)
Craig Toppera13603a2014-05-22 05:54:18 +0000260 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000261
262ModuleManager::~ModuleManager() {
263 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
264 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000265 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000266}
267
Douglas Gregor7211ac12013-01-25 23:32:03 +0000268void
269ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
270 void *UserData,
Craig Topper4dd9b432014-08-17 23:49:53 +0000271 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000272 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000273 if (VisitOrder.size() != Chain.size()) {
274 unsigned N = size();
275 VisitOrder.clear();
276 VisitOrder.reserve(N);
277
278 // Record the number of incoming edges for each module. When we
279 // encounter a module with no incoming edges, push it into the queue
280 // to seed the queue.
281 SmallVector<ModuleFile *, 4> Queue;
282 Queue.reserve(N);
283 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
284 UnusedIncomingEdges.reserve(size());
285 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
286 if (unsigned Size = (*M)->ImportedBy.size())
287 UnusedIncomingEdges.push_back(Size);
288 else {
289 UnusedIncomingEdges.push_back(0);
290 Queue.push_back(*M);
291 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000292 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000293
294 // Traverse the graph, making sure to visit a module before visiting any
295 // of its dependencies.
296 unsigned QueueStart = 0;
297 while (QueueStart < Queue.size()) {
298 ModuleFile *CurrentModule = Queue[QueueStart++];
299 VisitOrder.push_back(CurrentModule);
300
301 // For any module that this module depends on, push it on the
302 // stack (if it hasn't already been marked as visited).
303 for (llvm::SetVector<ModuleFile *>::iterator
304 M = CurrentModule->Imports.begin(),
305 MEnd = CurrentModule->Imports.end();
306 M != MEnd; ++M) {
307 // Remove our current module as an impediment to visiting the
308 // module we depend on. If we were the last unvisited module
309 // that depends on this particular module, push it into the
310 // queue to be visited.
311 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
312 if (NumUnusedEdges && (--NumUnusedEdges == 0))
313 Queue.push_back(*M);
314 }
315 }
316
317 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000318
Douglas Gregore97cd902013-01-28 16:46:33 +0000319 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000320 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000321 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000322
Douglas Gregore97cd902013-01-28 16:46:33 +0000323 VisitState *State = allocateVisitState();
324 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000325
Douglas Gregor7211ac12013-01-25 23:32:03 +0000326 // If the caller has provided us with a hit-set that came from the global
327 // module index, mark every module file in common with the global module
328 // index that is *not* in that set as 'visited'.
329 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
330 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
331 {
332 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000333 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000334 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000335 }
336 }
337
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000338 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
339 ModuleFile *CurrentModule = VisitOrder[I];
340 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000341 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000342 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000343
344 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000345 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
346 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000347 if (!Visitor(*CurrentModule, UserData))
348 continue;
349
350 // The visitor has requested that cut off visitation of any
351 // module that the current module depends on. To indicate this
352 // behavior, we mark all of the reachable modules as having been visited.
353 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000354 do {
355 // For any module that this module depends on, push it on the
356 // stack (if it hasn't already been marked as visited).
357 for (llvm::SetVector<ModuleFile *>::iterator
358 M = NextModule->Imports.begin(),
359 MEnd = NextModule->Imports.end();
360 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000361 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
362 State->Stack.push_back(*M);
363 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000364 }
365 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000366
Douglas Gregore97cd902013-01-28 16:46:33 +0000367 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000368 break;
369
370 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000371 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000372 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000373 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000374
375 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000376}
377
378/// \brief Perform a depth-first visit of the current module.
Douglas Gregorde3ef502011-11-30 23:21:26 +0000379static bool visitDepthFirst(ModuleFile &M,
380 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregord44252e2011-08-25 20:47:51 +0000381 void *UserData),
382 void *UserData,
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000383 SmallVectorImpl<bool> &Visited) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000384 // Preorder visitation
385 if (Visitor(M, /*Preorder=*/true, UserData))
386 return true;
387
388 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000389 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000390 IMEnd = M.Imports.end();
Douglas Gregord44252e2011-08-25 20:47:51 +0000391 IM != IMEnd; ++IM) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000392 if (Visited[(*IM)->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000393 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000394 Visited[(*IM)->Index] = true;
395
Douglas Gregord44252e2011-08-25 20:47:51 +0000396 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
397 return true;
398 }
399
400 // Postorder visitation
401 return Visitor(M, /*Preorder=*/false, UserData);
402}
403
Douglas Gregorde3ef502011-11-30 23:21:26 +0000404void ModuleManager::visitDepthFirst(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 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregord44252e2011-08-25 20:47:51 +0000408 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000409 if (Visited[Chain[I]->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000410 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000411 Visited[Chain[I]->Index] = true;
412
Douglas Gregord44252e2011-08-25 20:47:51 +0000413 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
414 return;
415 }
416}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000417
Douglas Gregor7029ce12013-03-19 00:28:20 +0000418bool ModuleManager::lookupModuleFile(StringRef FileName,
419 off_t ExpectedSize,
420 time_t ExpectedModTime,
421 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000422 // Open the file immediately to ensure there is no race between stat'ing and
423 // opening the file.
424 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000425
426 if (!File && FileName != "-") {
427 return false;
428 }
429
430 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000431 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
432 // Do not destroy File, as it may be referenced. If we need to rebuild it,
433 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000434 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000435
436 return false;
437}
438
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000439#ifndef NDEBUG
440namespace llvm {
441 template<>
442 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000443 typedef ModuleFile NodeType;
444 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000445 typedef ModuleManager::ModuleConstIterator nodes_iterator;
446
447 static ChildIteratorType child_begin(NodeType *Node) {
448 return Node->Imports.begin();
449 }
450
451 static ChildIteratorType child_end(NodeType *Node) {
452 return Node->Imports.end();
453 }
454
455 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
456 return Manager.begin();
457 }
458
459 static nodes_iterator nodes_end(const ModuleManager &Manager) {
460 return Manager.end();
461 }
462 };
463
464 template<>
465 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
466 explicit DOTGraphTraits(bool IsSimple = false)
467 : DefaultDOTGraphTraits(IsSimple) { }
468
469 static bool renderGraphFromBottomUp() {
470 return true;
471 }
472
Douglas Gregorde3ef502011-11-30 23:21:26 +0000473 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000474 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000475 }
476 };
477}
478
479void ModuleManager::viewGraph() {
480 llvm::ViewGraph(*this, "Modules");
481}
482#endif