blob: 271619404d2fbbde951976723c1383cff00190f8 [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//===----------------------------------------------------------------------===//
Adrian Prantlbb165fb2015-06-20 18:53:08 +000014#include "clang/Frontend/PCHContainerOperations.h"
Ben Langmuirbeee15e2014-04-14 18:00:01 +000015#include "clang/Lex/HeaderSearch.h"
Douglas Gregor7029ce12013-03-19 00:28:20 +000016#include "clang/Lex/ModuleMap.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,
Ben Langmuir70a1b812015-03-24 04:43:52 +000062 ASTFileSignatureReader 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);
Manuel Klimek9eff8b12015-05-20 10:29:23 +000098 if (!ImportedBy)
99 Roots.push_back(New);
Douglas Gregord44252e2011-08-25 20:47:51 +0000100 NewModule = true;
101 ModuleEntry = New;
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000102
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000103 New->InputFilesValidationTimestamp = 0;
Richard Smithe842a472014-10-22 02:05:46 +0000104 if (New->Kind == MK_ImplicitModule) {
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000105 std::string TimestampFilename = New->getTimestampFilename();
Ben Langmuirc8130a72014-02-20 21:59:23 +0000106 vfs::Status Status;
Dmitri Gribenkof430da42014-02-12 10:33:14 +0000107 // A cached stat value would be fine as well.
108 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
109 New->InputFilesValidationTimestamp =
110 Status.getLastModificationTime().toEpochTime();
111 }
112
Douglas Gregord44252e2011-08-25 20:47:51 +0000113 // Load the contents of the module
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000114 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
Douglas Gregord44252e2011-08-25 20:47:51 +0000115 // The buffer was already provided for us.
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000116 New->Buffer = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000117 } else {
118 // Open the AST file.
Benjamin Kramera8857962014-10-26 22:44:13 +0000119 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
120 (std::error_code()));
Douglas Gregord44252e2011-08-25 20:47:51 +0000121 if (FileName == "-") {
Benjamin Kramera8857962014-10-26 22:44:13 +0000122 Buf = llvm::MemoryBuffer::getSTDIN();
Ben Langmuir9801b252014-06-20 00:24:56 +0000123 } else {
124 // Leave the FileEntry open so if it gets read again by another
125 // ModuleManager it must be the same underlying file.
126 // FIXME: Because FileManager::getFile() doesn't guarantee that it will
127 // give us an open file, this may not be 100% reliable.
Benjamin Kramera8857962014-10-26 22:44:13 +0000128 Buf = FileMgr.getBufferForFile(New->File,
129 /*IsVolatile=*/false,
130 /*ShouldClose=*/false);
Ben Langmuir9801b252014-06-20 00:24:56 +0000131 }
Benjamin Kramera8857962014-10-26 22:44:13 +0000132
133 if (!Buf) {
134 ErrorStr = Buf.getError().message();
Douglas Gregor7029ce12013-03-19 00:28:20 +0000135 return Missing;
Benjamin Kramera8857962014-10-26 22:44:13 +0000136 }
137
138 New->Buffer = std::move(*Buf);
Douglas Gregord44252e2011-08-25 20:47:51 +0000139 }
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000140
141 // Initialize the stream.
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000142 PCHContainerRdr.ExtractPCH(New->Buffer->getMemBufferRef(), 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);
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000160 assert(Chain.back() == ModuleEntry);
Ben Langmuir487ea142014-10-23 18:05:36 +0000161 Chain.pop_back();
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000162 if (Roots.back() == ModuleEntry)
163 Roots.pop_back();
164 else
165 assert(ImportedBy);
Ben Langmuired982582014-11-08 00:34:30 +0000166 delete ModuleEntry;
Ben Langmuir487ea142014-10-23 18:05:36 +0000167 }
Ben Langmuired982582014-11-08 00:34:30 +0000168 return OutOfDate;
Ben Langmuir487ea142014-10-23 18:05:36 +0000169 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000170 }
Douglas Gregord44252e2011-08-25 20:47:51 +0000171
172 if (ImportedBy) {
173 ModuleEntry->ImportedBy.insert(ImportedBy);
174 ImportedBy->Imports.insert(ModuleEntry);
175 } else {
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000176 if (!ModuleEntry->DirectlyImported)
177 ModuleEntry->ImportLoc = ImportLoc;
178
Douglas Gregord44252e2011-08-25 20:47:51 +0000179 ModuleEntry->DirectlyImported = true;
180 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000181
182 Module = ModuleEntry;
183 return NewModule? NewlyLoaded : AlreadyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000184}
185
Ben Langmuir9801b252014-06-20 00:24:56 +0000186void ModuleManager::removeModules(
187 ModuleIterator first, ModuleIterator last,
188 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
189 ModuleMap *modMap) {
Douglas Gregor188dbef2012-11-07 17:46:15 +0000190 if (first == last)
191 return;
192
193 // Collect the set of module file pointers that we'll be removing.
194 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
195
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000196 auto IsVictim = [&](ModuleFile *MF) {
197 return victimSet.count(MF);
198 };
Douglas Gregor188dbef2012-11-07 17:46:15 +0000199 // Remove any references to the now-destroyed modules.
Douglas Gregor188dbef2012-11-07 17:46:15 +0000200 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000201 Chain[i]->ImportedBy.remove_if(IsVictim);
Douglas Gregor188dbef2012-11-07 17:46:15 +0000202 }
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000203 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
204 Roots.end());
Douglas Gregor188dbef2012-11-07 17:46:15 +0000205
206 // Delete the modules and erase them from the various structures.
207 for (ModuleIterator victim = first; victim != last; ++victim) {
Ben Langmuircaea1312014-05-19 17:04:28 +0000208 Modules.erase((*victim)->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000209
Douglas Gregor7029ce12013-03-19 00:28:20 +0000210 if (modMap) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000211 StringRef ModuleName = (*victim)->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000212 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000213 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000214 }
215 }
Ben Langmuir4f054782014-05-30 21:20:54 +0000216
Ben Langmuir9801b252014-06-20 00:24:56 +0000217 // Files that didn't make it through ReadASTCore successfully will be
218 // rebuilt (or there was an error). Invalidate them so that we can load the
219 // new files that will be renamed over the old ones.
220 if (LoadedSuccessfully.count(*victim) == 0)
Ben Langmuir4f054782014-05-30 21:20:54 +0000221 FileMgr.invalidateCache((*victim)->File);
222
Douglas Gregor188dbef2012-11-07 17:46:15 +0000223 delete *victim;
224 }
225
226 // Remove the modules from the chain.
227 Chain.erase(first, last);
228}
229
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000230void
231ModuleManager::addInMemoryBuffer(StringRef FileName,
232 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
233
234 const FileEntry *Entry =
235 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
236 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000237}
238
Richard Smith7f330cd2015-03-18 01:42:29 +0000239bool ModuleManager::addKnownModuleFile(StringRef FileName) {
240 const FileEntry *File;
241 if (lookupModuleFile(FileName, 0, 0, File))
242 return true;
243 if (!Modules.count(File))
244 AdditionalKnownModuleFiles.insert(File);
245 return false;
246}
247
Douglas Gregore97cd902013-01-28 16:46:33 +0000248ModuleManager::VisitState *ModuleManager::allocateVisitState() {
249 // Fast path: if we have a cached state, use it.
250 if (FirstVisitState) {
251 VisitState *Result = FirstVisitState;
252 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000253 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000254 return Result;
255 }
256
257 // Allocate and return a new state.
258 return new VisitState(size());
259}
260
261void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000262 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000263 State->NextState = FirstVisitState;
264 FirstVisitState = State;
265}
266
Douglas Gregor7211ac12013-01-25 23:32:03 +0000267void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
268 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000269 if (!GlobalIndex) {
270 ModulesInCommonWithGlobalIndex.clear();
271 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000272 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000273
274 // Notify the global module index about all of the modules we've already
275 // loaded.
276 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
277 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
278 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
279 }
280 }
281}
282
283void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
Richard Smith7f330cd2015-03-18 01:42:29 +0000284 AdditionalKnownModuleFiles.remove(MF->File);
285
Douglas Gregor603cd862013-03-22 18:50:14 +0000286 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
287 return;
288
289 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000290}
291
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000292ModuleManager::ModuleManager(FileManager &FileMgr,
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000293 const PCHContainerReader &PCHContainerRdr)
294 : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(),
Adrian Prantlbb165fb2015-06-20 18:53:08 +0000295 FirstVisitState(nullptr) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000296
297ModuleManager::~ModuleManager() {
298 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
299 delete Chain[e - i - 1];
Douglas Gregore97cd902013-01-28 16:46:33 +0000300 delete FirstVisitState;
Douglas Gregord44252e2011-08-25 20:47:51 +0000301}
302
Douglas Gregor7211ac12013-01-25 23:32:03 +0000303void
304ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
305 void *UserData,
Craig Topper4dd9b432014-08-17 23:49:53 +0000306 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000307 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000308 if (VisitOrder.size() != Chain.size()) {
309 unsigned N = size();
310 VisitOrder.clear();
311 VisitOrder.reserve(N);
312
313 // Record the number of incoming edges for each module. When we
314 // encounter a module with no incoming edges, push it into the queue
315 // to seed the queue.
316 SmallVector<ModuleFile *, 4> Queue;
317 Queue.reserve(N);
318 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
319 UnusedIncomingEdges.reserve(size());
320 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
321 if (unsigned Size = (*M)->ImportedBy.size())
322 UnusedIncomingEdges.push_back(Size);
323 else {
324 UnusedIncomingEdges.push_back(0);
325 Queue.push_back(*M);
326 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000327 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000328
329 // Traverse the graph, making sure to visit a module before visiting any
330 // of its dependencies.
331 unsigned QueueStart = 0;
332 while (QueueStart < Queue.size()) {
333 ModuleFile *CurrentModule = Queue[QueueStart++];
334 VisitOrder.push_back(CurrentModule);
335
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 = CurrentModule->Imports.begin(),
340 MEnd = CurrentModule->Imports.end();
341 M != MEnd; ++M) {
342 // Remove our current module as an impediment to visiting the
343 // module we depend on. If we were the last unvisited module
344 // that depends on this particular module, push it into the
345 // queue to be visited.
346 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
347 if (NumUnusedEdges && (--NumUnusedEdges == 0))
348 Queue.push_back(*M);
349 }
350 }
351
352 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000353
Douglas Gregore97cd902013-01-28 16:46:33 +0000354 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000355 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000356 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000357
Douglas Gregore97cd902013-01-28 16:46:33 +0000358 VisitState *State = allocateVisitState();
359 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000360
Douglas Gregor7211ac12013-01-25 23:32:03 +0000361 // If the caller has provided us with a hit-set that came from the global
362 // module index, mark every module file in common with the global module
363 // index that is *not* in that set as 'visited'.
364 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
365 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
366 {
367 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000368 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000369 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000370 }
371 }
372
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000373 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
374 ModuleFile *CurrentModule = VisitOrder[I];
375 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000376 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000377 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000378
379 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000380 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
381 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000382 if (!Visitor(*CurrentModule, UserData))
383 continue;
384
385 // The visitor has requested that cut off visitation of any
386 // module that the current module depends on. To indicate this
387 // behavior, we mark all of the reachable modules as having been visited.
388 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000389 do {
390 // For any module that this module depends on, push it on the
391 // stack (if it hasn't already been marked as visited).
392 for (llvm::SetVector<ModuleFile *>::iterator
393 M = NextModule->Imports.begin(),
394 MEnd = NextModule->Imports.end();
395 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000396 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
397 State->Stack.push_back(*M);
398 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000399 }
400 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000401
Douglas Gregore97cd902013-01-28 16:46:33 +0000402 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000403 break;
404
405 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000406 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000407 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000408 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000409
410 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000411}
412
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000413static void markVisitedDepthFirst(ModuleFile &M,
414 SmallVectorImpl<bool> &Visited) {
415 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
416 IMEnd = M.Imports.end();
417 IM != IMEnd; ++IM) {
418 if (Visited[(*IM)->Index])
419 continue;
420 Visited[(*IM)->Index] = true;
421 if (!M.DirectlyImported)
422 markVisitedDepthFirst(**IM, Visited);
423 }
424}
425
Douglas Gregord44252e2011-08-25 20:47:51 +0000426/// \brief Perform a depth-first visit of the current module.
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000427static bool visitDepthFirst(
428 ModuleFile &M,
429 ModuleManager::DFSPreorderControl (*PreorderVisitor)(ModuleFile &M,
430 void *UserData),
431 bool (*PostorderVisitor)(ModuleFile &M, void *UserData), void *UserData,
432 SmallVectorImpl<bool> &Visited) {
433 if (PreorderVisitor) {
434 switch (PreorderVisitor(M, UserData)) {
435 case ModuleManager::Abort:
436 return true;
437 case ModuleManager::SkipImports:
438 markVisitedDepthFirst(M, Visited);
439 return false;
440 case ModuleManager::Continue:
441 break;
442 }
443 }
444
Douglas Gregord44252e2011-08-25 20:47:51 +0000445 // Visit children
Douglas Gregorde3ef502011-11-30 23:21:26 +0000446 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000447 IMEnd = M.Imports.end();
Douglas Gregord44252e2011-08-25 20:47:51 +0000448 IM != IMEnd; ++IM) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000449 if (Visited[(*IM)->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000450 continue;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000451 Visited[(*IM)->Index] = true;
452
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000453 if (visitDepthFirst(**IM, PreorderVisitor, PostorderVisitor, UserData, Visited))
Douglas Gregord44252e2011-08-25 20:47:51 +0000454 return true;
455 }
456
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000457 if (PostorderVisitor)
458 return PostorderVisitor(M, UserData);
459
460 return false;
Douglas Gregord44252e2011-08-25 20:47:51 +0000461}
462
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000463void ModuleManager::visitDepthFirst(
464 ModuleManager::DFSPreorderControl (*PreorderVisitor)(ModuleFile &M,
465 void *UserData),
466 bool (*PostorderVisitor)(ModuleFile &M, void *UserData), void *UserData) {
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000467 SmallVector<bool, 16> Visited(size(), false);
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000468 for (unsigned I = 0, N = Roots.size(); I != N; ++I) {
469 if (Visited[Roots[I]->Index])
Douglas Gregord44252e2011-08-25 20:47:51 +0000470 continue;
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000471 Visited[Roots[I]->Index] = true;
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000472
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000473 if (::visitDepthFirst(*Roots[I], PreorderVisitor, PostorderVisitor, UserData, Visited))
Douglas Gregord44252e2011-08-25 20:47:51 +0000474 return;
475 }
476}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000477
Douglas Gregor7029ce12013-03-19 00:28:20 +0000478bool ModuleManager::lookupModuleFile(StringRef FileName,
479 off_t ExpectedSize,
480 time_t ExpectedModTime,
481 const FileEntry *&File) {
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000482 // Open the file immediately to ensure there is no race between stat'ing and
483 // opening the file.
484 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000485
486 if (!File && FileName != "-") {
487 return false;
488 }
489
490 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000491 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
492 // Do not destroy File, as it may be referenced. If we need to rebuild it,
493 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000494 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000495
496 return false;
497}
498
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000499#ifndef NDEBUG
500namespace llvm {
501 template<>
502 struct GraphTraits<ModuleManager> {
Douglas Gregorde3ef502011-11-30 23:21:26 +0000503 typedef ModuleFile NodeType;
504 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000505 typedef ModuleManager::ModuleConstIterator nodes_iterator;
506
507 static ChildIteratorType child_begin(NodeType *Node) {
508 return Node->Imports.begin();
509 }
510
511 static ChildIteratorType child_end(NodeType *Node) {
512 return Node->Imports.end();
513 }
514
515 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
516 return Manager.begin();
517 }
518
519 static nodes_iterator nodes_end(const ModuleManager &Manager) {
520 return Manager.end();
521 }
522 };
523
524 template<>
525 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
526 explicit DOTGraphTraits(bool IsSimple = false)
527 : DefaultDOTGraphTraits(IsSimple) { }
528
529 static bool renderGraphFromBottomUp() {
530 return true;
531 }
532
Douglas Gregorde3ef502011-11-30 23:21:26 +0000533 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000534 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000535 }
536 };
Alexander Kornienkoab9db512015-06-22 23:07:51 +0000537}
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000538
539void ModuleManager::viewGraph() {
540 llvm::ViewGraph(*this, "Modules");
541}
542#endif