blob: 2c10c119a07bd268ebacf6159bef5bd77a1361e0 [file] [log] [blame]
Douglas Gregor98339b92011-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//===----------------------------------------------------------------------===//
Stephen Hines6bcf27b2014-05-29 04:14:42 -070014#include "clang/Lex/HeaderSearch.h"
Douglas Gregor677e15f2013-03-19 00:28:20 +000015#include "clang/Lex/ModuleMap.h"
Douglas Gregor188bdcd2013-01-25 23:32:03 +000016#include "clang/Serialization/GlobalModuleIndex.h"
Benjamin Kramerae0cdff2013-08-24 13:16:22 +000017#include "clang/Serialization/ModuleManager.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000018#include "llvm/Support/MemoryBuffer.h"
Rafael Espindola8229d222013-06-11 22:15:02 +000019#include "llvm/Support/Path.h"
Douglas Gregor98339b92011-08-25 20:47:51 +000020#include "llvm/Support/raw_ostream.h"
Stephen Hinesc568f1e2014-07-21 00:47:37 -070021#include <system_error>
Douglas Gregor98339b92011-08-25 20:47:51 +000022
Douglas Gregor2492c892011-10-11 19:27:55 +000023#ifndef NDEBUG
24#include "llvm/Support/GraphWriter.h"
25#endif
26
Douglas Gregor98339b92011-08-25 20:47:51 +000027using namespace clang;
28using namespace serialization;
29
Douglas Gregor1a4761e2011-11-30 23:21:26 +000030ModuleFile *ModuleManager::lookup(StringRef Name) {
Douglas Gregorea14a872013-02-08 21:27:45 +000031 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
32 /*cacheFailure=*/false);
Douglas Gregorc544ba02013-03-27 16:47:18 +000033 if (Entry)
34 return lookup(Entry);
35
Stephen Hines6bcf27b2014-05-29 04:14:42 -070036 return nullptr;
Douglas Gregorc544ba02013-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())
Stephen Hines6bcf27b2014-05-29 04:14:42 -070043 return nullptr;
Douglas Gregorc544ba02013-03-27 16:47:18 +000044
45 return Known->second;
Douglas Gregor98339b92011-08-25 20:47:51 +000046}
47
48llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
Douglas Gregorea14a872013-02-08 21:27:45 +000049 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
50 /*cacheFailure=*/false);
Douglas Gregor98339b92011-08-25 20:47:51 +000051 return InMemoryBuffers[Entry];
52}
53
Douglas Gregor677e15f2013-03-19 00:28:20 +000054ModuleManager::AddModuleResult
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000055ModuleManager::addModule(StringRef FileName, ModuleKind Type,
56 SourceLocation ImportLoc, ModuleFile *ImportedBy,
Douglas Gregor677e15f2013-03-19 00:28:20 +000057 unsigned Generation,
58 off_t ExpectedSize, time_t ExpectedModTime,
59 ModuleFile *&Module,
60 std::string &ErrorStr) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -070061 Module = nullptr;
Douglas Gregor677e15f2013-03-19 00:28:20 +000062
63 // Look for the file entry. This only fails if the expected size or
64 // modification time differ.
65 const FileEntry *Entry;
Eli Friedmanedadb9a2013-09-05 23:50:58 +000066 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
67 ErrorStr = "module file out of date";
Douglas Gregor677e15f2013-03-19 00:28:20 +000068 return OutOfDate;
Eli Friedmanedadb9a2013-09-05 23:50:58 +000069 }
Douglas Gregor677e15f2013-03-19 00:28:20 +000070
Douglas Gregor98339b92011-08-25 20:47:51 +000071 if (!Entry && FileName != "-") {
Eli Friedmanedadb9a2013-09-05 23:50:58 +000072 ErrorStr = "module file not found";
Douglas Gregor677e15f2013-03-19 00:28:20 +000073 return Missing;
Douglas Gregor98339b92011-08-25 20:47:51 +000074 }
Douglas Gregor677e15f2013-03-19 00:28:20 +000075
76 // Check whether we already loaded this module, before
Douglas Gregor1a4761e2011-11-30 23:21:26 +000077 ModuleFile *&ModuleEntry = Modules[Entry];
Douglas Gregor98339b92011-08-25 20:47:51 +000078 bool NewModule = false;
79 if (!ModuleEntry) {
80 // Allocate a new module.
Douglas Gregor057df202012-01-18 20:56:22 +000081 ModuleFile *New = new ModuleFile(Type, Generation);
Douglas Gregorcc71dbe2013-01-21 20:07:12 +000082 New->Index = Chain.size();
Douglas Gregor98339b92011-08-25 20:47:51 +000083 New->FileName = FileName.str();
Argyrios Kyrtzidisd64c26f2012-10-03 01:58:42 +000084 New->File = Entry;
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000085 New->ImportLoc = ImportLoc;
Douglas Gregor98339b92011-08-25 20:47:51 +000086 Chain.push_back(New);
87 NewModule = true;
88 ModuleEntry = New;
Douglas Gregor87e2cfc2012-11-30 19:28:05 +000089
Stephen Hines651f13c2014-04-23 16:59:28 -070090 New->InputFilesValidationTimestamp = 0;
91 if (New->Kind == MK_Module) {
92 std::string TimestampFilename = New->getTimestampFilename();
93 vfs::Status Status;
94 // A cached stat value would be fine as well.
95 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
96 New->InputFilesValidationTimestamp =
97 Status.getLastModificationTime().toEpochTime();
98 }
99
Douglas Gregor98339b92011-08-25 20:47:51 +0000100 // Load the contents of the module
101 if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) {
102 // The buffer was already provided for us.
103 assert(Buffer && "Passed null buffer");
104 New->Buffer.reset(Buffer);
105 } else {
106 // Open the AST file.
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700107 std::error_code ec;
Douglas Gregor98339b92011-08-25 20:47:51 +0000108 if (FileName == "-") {
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700109 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf =
110 llvm::MemoryBuffer::getSTDIN();
111 ec = Buf.getError();
Douglas Gregor98339b92011-08-25 20:47:51 +0000112 if (ec)
113 ErrorStr = ec.message();
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700114 else
115 New->Buffer = std::move(Buf.get());
116 } else {
117 // Leave the FileEntry open so if it gets read again by another
118 // ModuleManager it must be the same underlying file.
119 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
120 // give us an open file, this may not be 100% reliable.
121 New->Buffer.reset(FileMgr.getBufferForFile(New->File, &ErrorStr,
122 /*IsVolatile*/false,
123 /*ShouldClose*/false));
124 }
Douglas Gregor98339b92011-08-25 20:47:51 +0000125
126 if (!New->Buffer)
Douglas Gregor677e15f2013-03-19 00:28:20 +0000127 return Missing;
Douglas Gregor98339b92011-08-25 20:47:51 +0000128 }
129
130 // Initialize the stream
131 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
Douglas Gregor677e15f2013-03-19 00:28:20 +0000132 (const unsigned char *)New->Buffer->getBufferEnd());
Douglas Gregor677e15f2013-03-19 00:28:20 +0000133 }
Douglas Gregor98339b92011-08-25 20:47:51 +0000134
135 if (ImportedBy) {
136 ModuleEntry->ImportedBy.insert(ImportedBy);
137 ImportedBy->Imports.insert(ModuleEntry);
138 } else {
Douglas Gregor87e2cfc2012-11-30 19:28:05 +0000139 if (!ModuleEntry->DirectlyImported)
140 ModuleEntry->ImportLoc = ImportLoc;
141
Douglas Gregor98339b92011-08-25 20:47:51 +0000142 ModuleEntry->DirectlyImported = true;
143 }
Douglas Gregor677e15f2013-03-19 00:28:20 +0000144
145 Module = ModuleEntry;
146 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregor98339b92011-08-25 20:47:51 +0000147}
148
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700149void ModuleManager::removeModules(
150 ModuleIterator first, ModuleIterator last,
151 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
152 ModuleMap *modMap) {
Douglas Gregor7cdd2812012-11-07 17:46:15 +0000153 if (first == last)
154 return;
155
156 // Collect the set of module file pointers that we'll be removing.
157 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
158
159 // Remove any references to the now-destroyed modules.
Douglas Gregor7cdd2812012-11-07 17:46:15 +0000160 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Stephen Hines651f13c2014-04-23 16:59:28 -0700161 Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
162 return victimSet.count(MF);
163 });
Douglas Gregor7cdd2812012-11-07 17:46:15 +0000164 }
165
166 // Delete the modules and erase them from the various structures.
167 for (ModuleIterator victim = first; victim != last; ++victim) {
168 Modules.erase((*victim)->File);
Douglas Gregor677e15f2013-03-19 00:28:20 +0000169
Douglas Gregor677e15f2013-03-19 00:28:20 +0000170 if (modMap) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700171 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor677e15f2013-03-19 00:28:20 +0000172 if (Module *mod = modMap->findModule(ModuleName)) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700173 mod->setASTFile(nullptr);
Douglas Gregor677e15f2013-03-19 00:28:20 +0000174 }
175 }
Stephen Hinesc568f1e2014-07-21 00:47:37 -0700176
177 // Files that didn't make it through ReadASTCore successfully will be
178 // rebuilt (or there was an error). Invalidate them so that we can load the
179 // new files that will be renamed over the old ones.
180 if (LoadedSuccessfully.count(*victim) == 0)
181 FileMgr.invalidateCache((*victim)->File);
182
Douglas Gregor7cdd2812012-11-07 17:46:15 +0000183 delete *victim;
184 }
185
186 // Remove the modules from the chain.
187 Chain.erase(first, last);
188}
189
Douglas Gregor98339b92011-08-25 20:47:51 +0000190void ModuleManager::addInMemoryBuffer(StringRef FileName,
191 llvm::MemoryBuffer *Buffer) {
192
193 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
194 Buffer->getBufferSize(), 0);
195 InMemoryBuffers[Entry] = Buffer;
196}
197
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000198ModuleManager::VisitState *ModuleManager::allocateVisitState() {
199 // Fast path: if we have a cached state, use it.
200 if (FirstVisitState) {
201 VisitState *Result = FirstVisitState;
202 FirstVisitState = FirstVisitState->NextState;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700203 Result->NextState = nullptr;
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000204 return Result;
205 }
206
207 // Allocate and return a new state.
208 return new VisitState(size());
209}
210
211void ModuleManager::returnVisitState(VisitState *State) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700212 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000213 State->NextState = FirstVisitState;
214 FirstVisitState = State;
215}
216
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000217void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
218 GlobalIndex = Index;
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000219 if (!GlobalIndex) {
220 ModulesInCommonWithGlobalIndex.clear();
221 return;
Douglas Gregor677e15f2013-03-19 00:28:20 +0000222 }
Douglas Gregorfa69fc12013-03-22 18:50:14 +0000223
224 // Notify the global module index about all of the modules we've already
225 // loaded.
226 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
227 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
228 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
229 }
230 }
231}
232
233void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
234 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
235 return;
236
237 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000238}
239
240ModuleManager::ModuleManager(FileManager &FileMgr)
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700241 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
Douglas Gregor98339b92011-08-25 20:47:51 +0000242
243ModuleManager::~ModuleManager() {
244 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
245 delete Chain[e - i - 1];
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000246 delete FirstVisitState;
Douglas Gregor98339b92011-08-25 20:47:51 +0000247}
248
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000249void
250ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
251 void *UserData,
Douglas Gregor677e15f2013-03-19 00:28:20 +0000252 llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) {
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000253 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregord07865b2013-01-25 22:25:23 +0000254 if (VisitOrder.size() != Chain.size()) {
255 unsigned N = size();
256 VisitOrder.clear();
257 VisitOrder.reserve(N);
258
259 // Record the number of incoming edges for each module. When we
260 // encounter a module with no incoming edges, push it into the queue
261 // to seed the queue.
262 SmallVector<ModuleFile *, 4> Queue;
263 Queue.reserve(N);
264 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
265 UnusedIncomingEdges.reserve(size());
266 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
267 if (unsigned Size = (*M)->ImportedBy.size())
268 UnusedIncomingEdges.push_back(Size);
269 else {
270 UnusedIncomingEdges.push_back(0);
271 Queue.push_back(*M);
272 }
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000273 }
Douglas Gregord07865b2013-01-25 22:25:23 +0000274
275 // Traverse the graph, making sure to visit a module before visiting any
276 // of its dependencies.
277 unsigned QueueStart = 0;
278 while (QueueStart < Queue.size()) {
279 ModuleFile *CurrentModule = Queue[QueueStart++];
280 VisitOrder.push_back(CurrentModule);
281
282 // For any module that this module depends on, push it on the
283 // stack (if it hasn't already been marked as visited).
284 for (llvm::SetVector<ModuleFile *>::iterator
285 M = CurrentModule->Imports.begin(),
286 MEnd = CurrentModule->Imports.end();
287 M != MEnd; ++M) {
288 // Remove our current module as an impediment to visiting the
289 // module we depend on. If we were the last unvisited module
290 // that depends on this particular module, push it into the
291 // queue to be visited.
292 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
293 if (NumUnusedEdges && (--NumUnusedEdges == 0))
294 Queue.push_back(*M);
295 }
296 }
297
298 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000299
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000300 delete FirstVisitState;
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700301 FirstVisitState = nullptr;
Douglas Gregor98339b92011-08-25 20:47:51 +0000302 }
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000303
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000304 VisitState *State = allocateVisitState();
305 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregord07865b2013-01-25 22:25:23 +0000306
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000307 // If the caller has provided us with a hit-set that came from the global
308 // module index, mark every module file in common with the global module
309 // index that is *not* in that set as 'visited'.
310 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
311 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
312 {
313 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor677e15f2013-03-19 00:28:20 +0000314 if (!ModuleFilesHit->count(M))
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000315 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor188bdcd2013-01-25 23:32:03 +0000316 }
317 }
318
Douglas Gregord07865b2013-01-25 22:25:23 +0000319 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
320 ModuleFile *CurrentModule = VisitOrder[I];
321 // Should we skip this module file?
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000322 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregor98339b92011-08-25 20:47:51 +0000323 continue;
Douglas Gregord07865b2013-01-25 22:25:23 +0000324
325 // Visit the module.
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000326 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
327 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregord07865b2013-01-25 22:25:23 +0000328 if (!Visitor(*CurrentModule, UserData))
329 continue;
330
331 // The visitor has requested that cut off visitation of any
332 // module that the current module depends on. To indicate this
333 // behavior, we mark all of the reachable modules as having been visited.
334 ModuleFile *NextModule = CurrentModule;
Douglas Gregord07865b2013-01-25 22:25:23 +0000335 do {
336 // For any module that this module depends on, push it on the
337 // stack (if it hasn't already been marked as visited).
338 for (llvm::SetVector<ModuleFile *>::iterator
339 M = NextModule->Imports.begin(),
340 MEnd = NextModule->Imports.end();
341 M != MEnd; ++M) {
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000342 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
343 State->Stack.push_back(*M);
344 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregor98339b92011-08-25 20:47:51 +0000345 }
346 }
Douglas Gregord07865b2013-01-25 22:25:23 +0000347
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000348 if (State->Stack.empty())
Douglas Gregord07865b2013-01-25 22:25:23 +0000349 break;
350
351 // Pop the next module off the stack.
Robert Wilhelm344472e2013-08-23 16:11:15 +0000352 NextModule = State->Stack.pop_back_val();
Douglas Gregord07865b2013-01-25 22:25:23 +0000353 } while (true);
Douglas Gregor98339b92011-08-25 20:47:51 +0000354 }
Douglas Gregord3cf5fb2013-01-28 16:46:33 +0000355
356 returnVisitState(State);
Douglas Gregor98339b92011-08-25 20:47:51 +0000357}
358
359/// \brief Perform a depth-first visit of the current module.
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000360static bool visitDepthFirst(ModuleFile &M,
361 bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregor98339b92011-08-25 20:47:51 +0000362 void *UserData),
363 void *UserData,
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000364 SmallVectorImpl<bool> &Visited) {
Douglas Gregor98339b92011-08-25 20:47:51 +0000365 // Preorder visitation
366 if (Visitor(M, /*Preorder=*/true, UserData))
367 return true;
368
369 // Visit children
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000370 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000371 IMEnd = M.Imports.end();
Douglas Gregor98339b92011-08-25 20:47:51 +0000372 IM != IMEnd; ++IM) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000373 if (Visited[(*IM)->Index])
Douglas Gregor98339b92011-08-25 20:47:51 +0000374 continue;
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000375 Visited[(*IM)->Index] = true;
376
Douglas Gregor98339b92011-08-25 20:47:51 +0000377 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
378 return true;
379 }
380
381 // Postorder visitation
382 return Visitor(M, /*Preorder=*/false, UserData);
383}
384
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000385void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
Douglas Gregor98339b92011-08-25 20:47:51 +0000386 void *UserData),
387 void *UserData) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000388 SmallVector<bool, 16> Visited(size(), false);
Douglas Gregor98339b92011-08-25 20:47:51 +0000389 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000390 if (Visited[Chain[I]->Index])
Douglas Gregor98339b92011-08-25 20:47:51 +0000391 continue;
Douglas Gregorcc71dbe2013-01-21 20:07:12 +0000392 Visited[Chain[I]->Index] = true;
393
Douglas Gregor98339b92011-08-25 20:47:51 +0000394 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
395 return;
396 }
397}
Douglas Gregor2492c892011-10-11 19:27:55 +0000398
Douglas Gregor677e15f2013-03-19 00:28:20 +0000399bool ModuleManager::lookupModuleFile(StringRef FileName,
400 off_t ExpectedSize,
401 time_t ExpectedModTime,
402 const FileEntry *&File) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700403 // Open the file immediately to ensure there is no race between stat'ing and
404 // opening the file.
405 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor677e15f2013-03-19 00:28:20 +0000406
407 if (!File && FileName != "-") {
408 return false;
409 }
410
411 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700412 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
413 // Do not destroy File, as it may be referenced. If we need to rebuild it,
414 // it will be destroyed by removeModules.
Douglas Gregor677e15f2013-03-19 00:28:20 +0000415 return true;
Douglas Gregor677e15f2013-03-19 00:28:20 +0000416
417 return false;
418}
419
Douglas Gregor2492c892011-10-11 19:27:55 +0000420#ifndef NDEBUG
421namespace llvm {
422 template<>
423 struct GraphTraits<ModuleManager> {
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000424 typedef ModuleFile NodeType;
425 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor2492c892011-10-11 19:27:55 +0000426 typedef ModuleManager::ModuleConstIterator nodes_iterator;
427
428 static ChildIteratorType child_begin(NodeType *Node) {
429 return Node->Imports.begin();
430 }
431
432 static ChildIteratorType child_end(NodeType *Node) {
433 return Node->Imports.end();
434 }
435
436 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
437 return Manager.begin();
438 }
439
440 static nodes_iterator nodes_end(const ModuleManager &Manager) {
441 return Manager.end();
442 }
443 };
444
445 template<>
446 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
447 explicit DOTGraphTraits(bool IsSimple = false)
448 : DefaultDOTGraphTraits(IsSimple) { }
449
450 static bool renderGraphFromBottomUp() {
451 return true;
452 }
453
Douglas Gregor1a4761e2011-11-30 23:21:26 +0000454 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Stephen Hines6bcf27b2014-05-29 04:14:42 -0700455 return M->ModuleName;
Douglas Gregor2492c892011-10-11 19:27:55 +0000456 }
457 };
458}
459
460void ModuleManager::viewGraph() {
461 llvm::ViewGraph(*this, "Modules");
462}
463#endif