blob: daef502cdcb5e59c9a7b5cb275e1db6aed50033f [file] [log] [blame]
Eugene Zelenkob7d89102017-11-11 00:08:50 +00001//===- ModuleManager.cpp - Module Manager ---------------------------------===//
Douglas Gregord44252e2011-08-25 20:47:51 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Douglas Gregord44252e2011-08-25 20:47:51 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the ModuleManager class, which manages a set of loaded
10// modules for the ASTReader.
11//
12//===----------------------------------------------------------------------===//
Eugene Zelenkob7d89102017-11-11 00:08:50 +000013
Mehdi Amini9670f842016-07-18 19:02:11 +000014#include "clang/Serialization/ModuleManager.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000015#include "clang/Basic/FileManager.h"
16#include "clang/Basic/LLVM.h"
Ben Langmuirbeee15e2014-04-14 18:00:01 +000017#include "clang/Lex/HeaderSearch.h"
Douglas Gregor7029ce12013-03-19 00:28:20 +000018#include "clang/Lex/ModuleMap.h"
Douglas Gregor7211ac12013-01-25 23:32:03 +000019#include "clang/Serialization/GlobalModuleIndex.h"
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +000020#include "clang/Serialization/InMemoryModuleCache.h"
Duncan P. N. Exon Smithf7170d12019-11-21 18:49:05 -080021#include "clang/Serialization/ModuleFile.h"
Richard Trieuf3b00462018-12-12 02:53:59 +000022#include "clang/Serialization/PCHContainerOperations.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000023#include "llvm/ADT/STLExtras.h"
24#include "llvm/ADT/SetVector.h"
25#include "llvm/ADT/SmallPtrSet.h"
26#include "llvm/ADT/SmallVector.h"
27#include "llvm/ADT/StringRef.h"
28#include "llvm/ADT/iterator.h"
29#include "llvm/Support/Chrono.h"
30#include "llvm/Support/DOTGraphTraits.h"
31#include "llvm/Support/ErrorOr.h"
Douglas Gregor9d7c1a22011-10-11 19:27:55 +000032#include "llvm/Support/GraphWriter.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000033#include "llvm/Support/MemoryBuffer.h"
Jonas Devliegherefc514902018-10-10 13:27:25 +000034#include "llvm/Support/VirtualFileSystem.h"
Eugene Zelenkob7d89102017-11-11 00:08:50 +000035#include <algorithm>
36#include <cassert>
37#include <memory>
38#include <string>
39#include <system_error>
Douglas Gregor9d7c1a22011-10-11 19:27:55 +000040
Douglas Gregord44252e2011-08-25 20:47:51 +000041using namespace clang;
42using namespace serialization;
43
Boris Kolpackovd30446f2017-08-31 06:26:43 +000044ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const {
Harlan Haskins8d323d12019-08-01 21:31:56 +000045 auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
46 /*CacheFailure=*/false);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000047 if (Entry)
Harlan Haskins8d323d12019-08-01 21:31:56 +000048 return lookup(*Entry);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000049
Craig Toppera13603a2014-05-22 05:54:18 +000050 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000051}
52
Boris Kolpackovd30446f2017-08-31 06:26:43 +000053ModuleFile *ModuleManager::lookupByModuleName(StringRef Name) const {
54 if (const Module *Mod = HeaderSearchInfo.getModuleMap().findModule(Name))
55 if (const FileEntry *File = Mod->getASTFile())
56 return lookup(File);
57
58 return nullptr;
59}
60
Richard Smith37a93df2017-02-18 00:32:02 +000061ModuleFile *ModuleManager::lookup(const FileEntry *File) const {
62 auto Known = Modules.find(File);
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000063 if (Known == Modules.end())
Craig Toppera13603a2014-05-22 05:54:18 +000064 return nullptr;
Douglas Gregorbf7fc9c2013-03-27 16:47:18 +000065
66 return Known->second;
Douglas Gregord44252e2011-08-25 20:47:51 +000067}
68
Rafael Espindola5cd06f22014-08-18 19:16:31 +000069std::unique_ptr<llvm::MemoryBuffer>
70ModuleManager::lookupBuffer(StringRef Name) {
Harlan Haskins8d323d12019-08-01 21:31:56 +000071 auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
72 /*CacheFailure=*/false);
73 if (!Entry)
74 return nullptr;
75 return std::move(InMemoryBuffers[*Entry]);
Douglas Gregord44252e2011-08-25 20:47:51 +000076}
77
Duncan P. N. Exon Smith14afc8e2017-01-28 21:34:28 +000078static bool checkSignature(ASTFileSignature Signature,
79 ASTFileSignature ExpectedSignature,
80 std::string &ErrorStr) {
81 if (!ExpectedSignature || Signature == ExpectedSignature)
82 return false;
83
84 ErrorStr =
85 Signature ? "signature mismatch" : "could not read module signature";
86 return true;
87}
88
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +000089static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy,
90 SourceLocation ImportLoc) {
91 if (ImportedBy) {
92 MF.ImportedBy.insert(ImportedBy);
93 ImportedBy->Imports.insert(&MF);
94 } else {
95 if (!MF.DirectlyImported)
96 MF.ImportLoc = ImportLoc;
97
98 MF.DirectlyImported = true;
99 }
100}
101
Douglas Gregor7029ce12013-03-19 00:28:20 +0000102ModuleManager::AddModuleResult
Douglas Gregor6fb03ae2012-11-30 19:28:05 +0000103ModuleManager::addModule(StringRef FileName, ModuleKind Type,
104 SourceLocation ImportLoc, ModuleFile *ImportedBy,
Douglas Gregor7029ce12013-03-19 00:28:20 +0000105 unsigned Generation,
106 off_t ExpectedSize, time_t ExpectedModTime,
Ben Langmuir487ea142014-10-23 18:05:36 +0000107 ASTFileSignature ExpectedSignature,
Ben Langmuir70a1b812015-03-24 04:43:52 +0000108 ASTFileSignatureReader ReadSignature,
Douglas Gregor7029ce12013-03-19 00:28:20 +0000109 ModuleFile *&Module,
110 std::string &ErrorStr) {
Craig Toppera13603a2014-05-22 05:54:18 +0000111 Module = nullptr;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000112
113 // Look for the file entry. This only fails if the expected size or
114 // modification time differ.
115 const FileEntry *Entry;
Manman Ren11f2a472016-08-18 17:42:15 +0000116 if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) {
Richard Smith5b390752014-11-21 05:37:20 +0000117 // If we're not expecting to pull this file out of the module cache, it
118 // might have a different mtime due to being moved across filesystems in
119 // a distributed build. The size must still match, though. (As must the
120 // contents, but we can't check that.)
121 ExpectedModTime = 0;
122 }
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +0000123 // Note: ExpectedSize and ExpectedModTime will be 0 for MK_ImplicitModule
124 // when using an ASTFileSignature.
Eli Friedmanc27d0d52013-09-05 23:50:58 +0000125 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
126 ErrorStr = "module file out of date";
Douglas Gregor7029ce12013-03-19 00:28:20 +0000127 return OutOfDate;
Eli Friedmanc27d0d52013-09-05 23:50:58 +0000128 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000129
Douglas Gregord44252e2011-08-25 20:47:51 +0000130 if (!Entry && FileName != "-") {
Eli Friedmanc27d0d52013-09-05 23:50:58 +0000131 ErrorStr = "module file not found";
Douglas Gregor7029ce12013-03-19 00:28:20 +0000132 return Missing;
Douglas Gregord44252e2011-08-25 20:47:51 +0000133 }
Douglas Gregor7029ce12013-03-19 00:28:20 +0000134
135 // Check whether we already loaded this module, before
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000136 if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) {
137 // Check the stored signature.
138 if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))
Ben Langmuired982582014-11-08 00:34:30 +0000139 return OutOfDate;
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000140
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000141 Module = ModuleEntry;
142 updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc);
Richard Smith3b99db52016-09-02 00:10:28 +0000143 return AlreadyLoaded;
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000144 }
Richard Smith3b99db52016-09-02 00:10:28 +0000145
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000146 // Allocate a new module.
Jonas Devlieghere2b3d49b2019-08-14 23:04:18 +0000147 auto NewModule = std::make_unique<ModuleFile>(Type, Generation);
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000148 NewModule->Index = Chain.size();
149 NewModule->FileName = FileName.str();
150 NewModule->File = Entry;
151 NewModule->ImportLoc = ImportLoc;
152 NewModule->InputFilesValidationTimestamp = 0;
153
154 if (NewModule->Kind == MK_ImplicitModule) {
155 std::string TimestampFilename = NewModule->getTimestampFilename();
Jonas Devliegherefc514902018-10-10 13:27:25 +0000156 llvm::vfs::Status Status;
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000157 // A cached stat value would be fine as well.
158 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
159 NewModule->InputFilesValidationTimestamp =
160 llvm::sys::toTimeT(Status.getLastModificationTime());
161 }
162
163 // Load the contents of the module
164 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
165 // The buffer was already provided for us.
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +0000166 NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer));
Adrian Prantl49092d12018-08-20 17:10:27 +0000167 // Since the cached buffer is reused, it is safe to close the file
168 // descriptor that was opened while stat()ing the PCM in
169 // lookupModuleFile() above, it won't be needed any longer.
170 Entry->closeFile();
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +0000171 } else if (llvm::MemoryBuffer *Buffer =
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +0000172 getModuleCache().lookupPCM(FileName)) {
Duncan P. N. Exon Smith030d7d62017-03-20 17:58:26 +0000173 NewModule->Buffer = Buffer;
Adrian Prantl49092d12018-08-20 17:10:27 +0000174 // As above, the file descriptor is no longer needed.
175 Entry->closeFile();
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +0000176 } else if (getModuleCache().shouldBuildPCM(FileName)) {
177 // Report that the module is out of date, since we tried (and failed) to
178 // import it earlier.
179 Entry->closeFile();
180 return OutOfDate;
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000181 } else {
182 // Open the AST file.
183 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code()));
184 if (FileName == "-") {
185 Buf = llvm::MemoryBuffer::getSTDIN();
186 } else {
Adrian Prantl49092d12018-08-20 17:10:27 +0000187 // Get a buffer of the file and close the file descriptor when done.
Duncan P. N. Exon Smith122705b2019-08-30 16:56:26 +0000188 Buf = FileMgr.getBufferForFile(NewModule->File, /*isVolatile=*/false);
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000189 }
190
191 if (!Buf) {
192 ErrorStr = Buf.getError().message();
193 return Missing;
194 }
195
Duncan P. N. Exon Smith0a2be462019-03-09 17:44:01 +0000196 NewModule->Buffer = &getModuleCache().addPCM(FileName, std::move(*Buf));
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000197 }
198
199 // Initialize the stream.
200 NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer);
201
Duncan P. N. Exon Smith688b69a2017-01-29 04:42:21 +0000202 // Read the signature eagerly now so that we can check it. Avoid calling
203 // ReadSignature unless there's something to check though.
204 if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
Volodymyr Sapsaif91b6f82019-08-28 23:31:32 +0000205 ExpectedSignature, ErrorStr))
Duncan P. N. Exon Smith26308a62017-01-28 23:22:40 +0000206 return OutOfDate;
207
208 // We're keeping this module. Store it everywhere.
209 Module = Modules[Entry] = NewModule.get();
210
211 updateModuleImports(*NewModule, ImportedBy, ImportLoc);
212
213 if (!NewModule->isModule())
214 PCHChain.push_back(NewModule.get());
215 if (!ImportedBy)
216 Roots.push_back(NewModule.get());
Richard Smith3b99db52016-09-02 00:10:28 +0000217
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000218 Chain.push_back(std::move(NewModule));
Richard Smith3b99db52016-09-02 00:10:28 +0000219 return NewlyLoaded;
Douglas Gregord44252e2011-08-25 20:47:51 +0000220}
221
Duncan P. N. Exon Smith8e9e4332019-11-10 10:31:03 -0800222void ModuleManager::removeModules(ModuleIterator First, ModuleMap *modMap) {
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000223 auto Last = end();
224 if (First == Last)
Douglas Gregor188dbef2012-11-07 17:46:15 +0000225 return;
226
Ben Langmuira50dbb22015-10-21 23:12:45 +0000227 // Explicitly clear VisitOrder since we might not notice it is stale.
228 VisitOrder.clear();
229
Douglas Gregor188dbef2012-11-07 17:46:15 +0000230 // Collect the set of module file pointers that we'll be removing.
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000231 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000232 (llvm::pointer_iterator<ModuleIterator>(First)),
233 (llvm::pointer_iterator<ModuleIterator>(Last)));
Douglas Gregor188dbef2012-11-07 17:46:15 +0000234
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000235 auto IsVictim = [&](ModuleFile *MF) {
236 return victimSet.count(MF);
237 };
Douglas Gregor188dbef2012-11-07 17:46:15 +0000238 // Remove any references to the now-destroyed modules.
Duncan P. N. Exon Smith073ec352017-01-28 23:12:13 +0000239 for (auto I = begin(); I != First; ++I) {
240 I->Imports.remove_if(IsVictim);
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000241 I->ImportedBy.remove_if(IsVictim);
Duncan P. N. Exon Smith073ec352017-01-28 23:12:13 +0000242 }
Manuel Klimek9eff8b12015-05-20 10:29:23 +0000243 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
244 Roots.end());
Douglas Gregor188dbef2012-11-07 17:46:15 +0000245
Richard Smith16fe4d12015-07-22 22:51:15 +0000246 // Remove the modules from the PCH chain.
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000247 for (auto I = First; I != Last; ++I) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000248 if (!I->isModule()) {
Fangrui Song75e74e02019-03-31 08:48:19 +0000249 PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());
Richard Smith16fe4d12015-07-22 22:51:15 +0000250 break;
251 }
252 }
253
Douglas Gregor188dbef2012-11-07 17:46:15 +0000254 // Delete the modules and erase them from the various structures.
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000255 for (ModuleIterator victim = First; victim != Last; ++victim) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000256 Modules.erase(victim->File);
Ben Langmuirca392142014-05-19 16:13:45 +0000257
Douglas Gregor7029ce12013-03-19 00:28:20 +0000258 if (modMap) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000259 StringRef ModuleName = victim->ModuleName;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000260 if (Module *mod = modMap->findModule(ModuleName)) {
Craig Toppera13603a2014-05-22 05:54:18 +0000261 mod->setASTFile(nullptr);
Douglas Gregor7029ce12013-03-19 00:28:20 +0000262 }
263 }
Douglas Gregor188dbef2012-11-07 17:46:15 +0000264 }
265
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000266 // Delete the modules.
Duncan P. N. Exon Smith8e6bc1972017-01-28 23:02:12 +0000267 Chain.erase(Chain.begin() + (First - begin()), Chain.end());
Douglas Gregor188dbef2012-11-07 17:46:15 +0000268}
269
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000270void
271ModuleManager::addInMemoryBuffer(StringRef FileName,
272 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
Rafael Espindola5cd06f22014-08-18 19:16:31 +0000273 const FileEntry *Entry =
274 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
275 InMemoryBuffers[Entry] = std::move(Buffer);
Douglas Gregord44252e2011-08-25 20:47:51 +0000276}
277
Douglas Gregore97cd902013-01-28 16:46:33 +0000278ModuleManager::VisitState *ModuleManager::allocateVisitState() {
279 // Fast path: if we have a cached state, use it.
280 if (FirstVisitState) {
281 VisitState *Result = FirstVisitState;
282 FirstVisitState = FirstVisitState->NextState;
Craig Toppera13603a2014-05-22 05:54:18 +0000283 Result->NextState = nullptr;
Douglas Gregore97cd902013-01-28 16:46:33 +0000284 return Result;
285 }
286
287 // Allocate and return a new state.
288 return new VisitState(size());
289}
290
291void ModuleManager::returnVisitState(VisitState *State) {
Craig Toppera13603a2014-05-22 05:54:18 +0000292 assert(State->NextState == nullptr && "Visited state is in list?");
Douglas Gregore97cd902013-01-28 16:46:33 +0000293 State->NextState = FirstVisitState;
294 FirstVisitState = State;
295}
296
Douglas Gregor7211ac12013-01-25 23:32:03 +0000297void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
298 GlobalIndex = Index;
Douglas Gregor603cd862013-03-22 18:50:14 +0000299 if (!GlobalIndex) {
300 ModulesInCommonWithGlobalIndex.clear();
301 return;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000302 }
Douglas Gregor603cd862013-03-22 18:50:14 +0000303
304 // Notify the global module index about all of the modules we've already
305 // loaded.
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000306 for (ModuleFile &M : *this)
307 if (!GlobalIndex->loadedModuleFile(&M))
308 ModulesInCommonWithGlobalIndex.push_back(&M);
Douglas Gregor603cd862013-03-22 18:50:14 +0000309}
310
311void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
312 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
313 return;
314
315 ModulesInCommonWithGlobalIndex.push_back(MF);
Douglas Gregor7211ac12013-01-25 23:32:03 +0000316}
317
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +0000318ModuleManager::ModuleManager(FileManager &FileMgr,
319 InMemoryModuleCache &ModuleCache,
Boris Kolpackovd30446f2017-08-31 06:26:43 +0000320 const PCHContainerReader &PCHContainerRdr,
Duncan P. N. Exon Smith8bef5cd2019-03-09 17:33:56 +0000321 const HeaderSearch &HeaderSearchInfo)
322 : FileMgr(FileMgr), ModuleCache(&ModuleCache),
323 PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}
Douglas Gregord44252e2011-08-25 20:47:51 +0000324
Duncan P. N. Exon Smitha897f7c2017-01-28 22:24:01 +0000325ModuleManager::~ModuleManager() { delete FirstVisitState; }
Douglas Gregord44252e2011-08-25 20:47:51 +0000326
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000327void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
328 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
Douglas Gregor7211ac12013-01-25 23:32:03 +0000329 // If the visitation order vector is the wrong size, recompute the order.
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000330 if (VisitOrder.size() != Chain.size()) {
331 unsigned N = size();
332 VisitOrder.clear();
333 VisitOrder.reserve(N);
Fangrui Song6907ce22018-07-30 19:24:48 +0000334
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000335 // Record the number of incoming edges for each module. When we
336 // encounter a module with no incoming edges, push it into the queue
337 // to seed the queue.
338 SmallVector<ModuleFile *, 4> Queue;
339 Queue.reserve(N);
340 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
Richard Smitha7c535b2015-07-22 01:28:05 +0000341 UnusedIncomingEdges.resize(size());
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000342 for (ModuleFile &M : llvm::reverse(*this)) {
343 unsigned Size = M.ImportedBy.size();
344 UnusedIncomingEdges[M.Index] = Size;
Richard Smitha7c535b2015-07-22 01:28:05 +0000345 if (!Size)
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000346 Queue.push_back(&M);
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000347 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000348
349 // Traverse the graph, making sure to visit a module before visiting any
350 // of its dependencies.
Richard Smitha7c535b2015-07-22 01:28:05 +0000351 while (!Queue.empty()) {
352 ModuleFile *CurrentModule = Queue.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000353 VisitOrder.push_back(CurrentModule);
354
355 // For any module that this module depends on, push it on the
356 // stack (if it hasn't already been marked as visited).
Richard Smitha7c535b2015-07-22 01:28:05 +0000357 for (auto M = CurrentModule->Imports.rbegin(),
358 MEnd = CurrentModule->Imports.rend();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000359 M != MEnd; ++M) {
360 // Remove our current module as an impediment to visiting the
361 // module we depend on. If we were the last unvisited module
362 // that depends on this particular module, push it into the
363 // queue to be visited.
364 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
365 if (NumUnusedEdges && (--NumUnusedEdges == 0))
366 Queue.push_back(*M);
367 }
368 }
369
370 assert(VisitOrder.size() == N && "Visitation order is wrong?");
Douglas Gregor7211ac12013-01-25 23:32:03 +0000371
Douglas Gregore97cd902013-01-28 16:46:33 +0000372 delete FirstVisitState;
Craig Toppera13603a2014-05-22 05:54:18 +0000373 FirstVisitState = nullptr;
Douglas Gregord44252e2011-08-25 20:47:51 +0000374 }
Douglas Gregorbdb259d2013-01-21 20:07:12 +0000375
Douglas Gregore97cd902013-01-28 16:46:33 +0000376 VisitState *State = allocateVisitState();
377 unsigned VisitNumber = State->NextVisitNumber++;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000378
Douglas Gregor7211ac12013-01-25 23:32:03 +0000379 // If the caller has provided us with a hit-set that came from the global
380 // module index, mark every module file in common with the global module
381 // index that is *not* in that set as 'visited'.
382 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
383 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
384 {
385 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
Douglas Gregor7029ce12013-03-19 00:28:20 +0000386 if (!ModuleFilesHit->count(M))
Douglas Gregore97cd902013-01-28 16:46:33 +0000387 State->VisitNumber[M->Index] = VisitNumber;
Douglas Gregor7211ac12013-01-25 23:32:03 +0000388 }
389 }
390
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000391 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
392 ModuleFile *CurrentModule = VisitOrder[I];
393 // Should we skip this module file?
Douglas Gregore97cd902013-01-28 16:46:33 +0000394 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
Douglas Gregord44252e2011-08-25 20:47:51 +0000395 continue;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000396
397 // Visit the module.
Douglas Gregore97cd902013-01-28 16:46:33 +0000398 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
399 State->VisitNumber[CurrentModule->Index] = VisitNumber;
Benjamin Kramer9a9efba2015-07-25 12:14:04 +0000400 if (!Visitor(*CurrentModule))
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000401 continue;
402
403 // The visitor has requested that cut off visitation of any
404 // module that the current module depends on. To indicate this
405 // behavior, we mark all of the reachable modules as having been visited.
406 ModuleFile *NextModule = CurrentModule;
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000407 do {
408 // For any module that this module depends on, push it on the
409 // stack (if it hasn't already been marked as visited).
410 for (llvm::SetVector<ModuleFile *>::iterator
411 M = NextModule->Imports.begin(),
412 MEnd = NextModule->Imports.end();
413 M != MEnd; ++M) {
Douglas Gregore97cd902013-01-28 16:46:33 +0000414 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
415 State->Stack.push_back(*M);
416 State->VisitNumber[(*M)->Index] = VisitNumber;
Douglas Gregord44252e2011-08-25 20:47:51 +0000417 }
418 }
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000419
Douglas Gregore97cd902013-01-28 16:46:33 +0000420 if (State->Stack.empty())
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000421 break;
422
423 // Pop the next module off the stack.
Robert Wilhelm25284cc2013-08-23 16:11:15 +0000424 NextModule = State->Stack.pop_back_val();
Douglas Gregore41d7fe2013-01-25 22:25:23 +0000425 } while (true);
Douglas Gregord44252e2011-08-25 20:47:51 +0000426 }
Douglas Gregore97cd902013-01-28 16:46:33 +0000427
428 returnVisitState(State);
Douglas Gregord44252e2011-08-25 20:47:51 +0000429}
430
Douglas Gregor7029ce12013-03-19 00:28:20 +0000431bool ModuleManager::lookupModuleFile(StringRef FileName,
432 off_t ExpectedSize,
433 time_t ExpectedModTime,
434 const FileEntry *&File) {
Richard Smith3bd6d7f2016-09-02 00:18:05 +0000435 if (FileName == "-") {
436 File = nullptr;
437 return false;
438 }
439
Ben Langmuir05f82ba2014-05-01 03:33:36 +0000440 // Open the file immediately to ensure there is no race between stat'ing and
441 // opening the file.
Harlan Haskins8d323d12019-08-01 21:31:56 +0000442 auto FileOrErr = FileMgr.getFile(FileName, /*OpenFile=*/true,
443 /*CacheFailure=*/false);
444 if (!FileOrErr) {
445 File = nullptr;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000446 return false;
Harlan Haskins8d323d12019-08-01 21:31:56 +0000447 }
448 File = *FileOrErr;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000449
450 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
Ben Langmuir027731d2014-05-04 05:20:54 +0000451 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
452 // Do not destroy File, as it may be referenced. If we need to rebuild it,
453 // it will be destroyed by removeModules.
Douglas Gregor7029ce12013-03-19 00:28:20 +0000454 return true;
Douglas Gregor7029ce12013-03-19 00:28:20 +0000455
456 return false;
457}
458
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000459#ifndef NDEBUG
460namespace llvm {
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000461
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000462 template<>
463 struct GraphTraits<ModuleManager> {
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000464 using NodeRef = ModuleFile *;
465 using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator;
466 using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>;
Tim Shenf2187ed2016-08-22 21:09:30 +0000467
468 static ChildIteratorType child_begin(NodeRef Node) {
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000469 return Node->Imports.begin();
470 }
471
Tim Shenf2187ed2016-08-22 21:09:30 +0000472 static ChildIteratorType child_end(NodeRef Node) {
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000473 return Node->Imports.end();
474 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000475
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000476 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000477 return nodes_iterator(Manager.begin());
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000478 }
Fangrui Song6907ce22018-07-30 19:24:48 +0000479
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000480 static nodes_iterator nodes_end(const ModuleManager &Manager) {
Duncan P. N. Exon Smith96a06e02017-01-28 22:15:22 +0000481 return nodes_iterator(Manager.end());
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000482 }
483 };
Fangrui Song6907ce22018-07-30 19:24:48 +0000484
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000485 template<>
486 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
487 explicit DOTGraphTraits(bool IsSimple = false)
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000488 : DefaultDOTGraphTraits(IsSimple) {}
Fangrui Song6907ce22018-07-30 19:24:48 +0000489
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000490 static bool renderGraphFromBottomUp() { return true; }
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000491
Douglas Gregorde3ef502011-11-30 23:21:26 +0000492 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
Ben Langmuirbeee15e2014-04-14 18:00:01 +0000493 return M->ModuleName;
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000494 }
495 };
Eugene Zelenkob7d89102017-11-11 00:08:50 +0000496
497} // namespace llvm
Douglas Gregor9d7c1a22011-10-11 19:27:55 +0000498
499void ModuleManager::viewGraph() {
500 llvm::ViewGraph(*this, "Modules");
501}
502#endif