1 //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ModuleManager class, which manages a set of loaded
11 // modules for the ASTReader.
13 //===----------------------------------------------------------------------===//
14 #include "clang/Lex/HeaderSearch.h"
15 #include "clang/Lex/ModuleMap.h"
16 #include "clang/Serialization/GlobalModuleIndex.h"
17 #include "clang/Serialization/ModuleManager.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/Path.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include "llvm/Support/system_error.h"
24 #include "llvm/Support/GraphWriter.h"
27 using namespace clang;
28 using namespace serialization;
30 ModuleFile *ModuleManager::lookup(StringRef Name) {
31 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
32 /*cacheFailure=*/false);
39 ModuleFile *ModuleManager::lookup(const FileEntry *File) {
40 llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
42 if (Known == Modules.end())
48 llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
49 const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
50 /*cacheFailure=*/false);
51 return InMemoryBuffers[Entry];
54 ModuleManager::AddModuleResult
55 ModuleManager::addModule(StringRef FileName, ModuleKind Type,
56 SourceLocation ImportLoc, ModuleFile *ImportedBy,
58 off_t ExpectedSize, time_t ExpectedModTime,
60 std::string &ErrorStr) {
63 // Look for the file entry. This only fails if the expected size or
64 // modification time differ.
65 const FileEntry *Entry;
66 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
67 ErrorStr = "module file out of date";
71 if (!Entry && FileName != "-") {
72 ErrorStr = "module file not found";
76 // Check whether we already loaded this module, before
77 ModuleFile *&ModuleEntry = Modules[Entry];
78 bool NewModule = false;
80 // Allocate a new module.
81 ModuleFile *New = new ModuleFile(Type, Generation);
82 New->Index = Chain.size();
83 New->FileName = FileName.str();
85 New->ImportLoc = ImportLoc;
90 New->InputFilesValidationTimestamp = 0;
91 if (New->Kind == MK_Module) {
92 std::string TimestampFilename = New->getTimestampFilename();
94 // A cached stat value would be fine as well.
95 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
96 New->InputFilesValidationTimestamp =
97 Status.getLastModificationTime().toEpochTime();
100 // 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);
106 // Open the AST file.
108 if (FileName == "-") {
109 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
111 ErrorStr = ec.message();
113 New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
119 // Initialize the stream
120 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
121 (const unsigned char *)New->Buffer->getBufferEnd());
125 ModuleEntry->ImportedBy.insert(ImportedBy);
126 ImportedBy->Imports.insert(ModuleEntry);
128 if (!ModuleEntry->DirectlyImported)
129 ModuleEntry->ImportLoc = ImportLoc;
131 ModuleEntry->DirectlyImported = true;
134 Module = ModuleEntry;
135 return NewModule? NewlyLoaded : AlreadyLoaded;
138 void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last,
143 // The first file entry is about to be rebuilt (or there was an error), so
144 // there should be no references to it. Remove it from the cache to close it,
145 // as Windows doesn't seem to allow renaming over an open file.
146 FileMgr.invalidateCache((*first)->File);
148 // Collect the set of module file pointers that we'll be removing.
149 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
151 // Remove any references to the now-destroyed modules.
152 for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
153 Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
154 return victimSet.count(MF);
158 // Delete the modules and erase them from the various structures.
159 for (ModuleIterator victim = first; victim != last; ++victim) {
160 Modules.erase((*victim)->File);
163 StringRef ModuleName = (*victim)->ModuleName;
164 if (Module *mod = modMap->findModule(ModuleName)) {
165 mod->setASTFile(nullptr);
171 // Remove the modules from the chain.
172 Chain.erase(first, last);
175 void ModuleManager::addInMemoryBuffer(StringRef FileName,
176 llvm::MemoryBuffer *Buffer) {
178 const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
179 Buffer->getBufferSize(), 0);
180 InMemoryBuffers[Entry] = Buffer;
183 ModuleManager::VisitState *ModuleManager::allocateVisitState() {
184 // Fast path: if we have a cached state, use it.
185 if (FirstVisitState) {
186 VisitState *Result = FirstVisitState;
187 FirstVisitState = FirstVisitState->NextState;
188 Result->NextState = nullptr;
192 // Allocate and return a new state.
193 return new VisitState(size());
196 void ModuleManager::returnVisitState(VisitState *State) {
197 assert(State->NextState == nullptr && "Visited state is in list?");
198 State->NextState = FirstVisitState;
199 FirstVisitState = State;
202 void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
205 ModulesInCommonWithGlobalIndex.clear();
209 // Notify the global module index about all of the modules we've already
211 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
212 if (!GlobalIndex->loadedModuleFile(Chain[I])) {
213 ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
218 void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
219 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
222 ModulesInCommonWithGlobalIndex.push_back(MF);
225 ModuleManager::ModuleManager(FileManager &FileMgr)
226 : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {}
228 ModuleManager::~ModuleManager() {
229 for (unsigned i = 0, e = Chain.size(); i != e; ++i)
230 delete Chain[e - i - 1];
231 delete FirstVisitState;
235 ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
237 llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) {
238 // If the visitation order vector is the wrong size, recompute the order.
239 if (VisitOrder.size() != Chain.size()) {
242 VisitOrder.reserve(N);
244 // Record the number of incoming edges for each module. When we
245 // encounter a module with no incoming edges, push it into the queue
246 // to seed the queue.
247 SmallVector<ModuleFile *, 4> Queue;
249 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
250 UnusedIncomingEdges.reserve(size());
251 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
252 if (unsigned Size = (*M)->ImportedBy.size())
253 UnusedIncomingEdges.push_back(Size);
255 UnusedIncomingEdges.push_back(0);
260 // Traverse the graph, making sure to visit a module before visiting any
261 // of its dependencies.
262 unsigned QueueStart = 0;
263 while (QueueStart < Queue.size()) {
264 ModuleFile *CurrentModule = Queue[QueueStart++];
265 VisitOrder.push_back(CurrentModule);
267 // For any module that this module depends on, push it on the
268 // stack (if it hasn't already been marked as visited).
269 for (llvm::SetVector<ModuleFile *>::iterator
270 M = CurrentModule->Imports.begin(),
271 MEnd = CurrentModule->Imports.end();
273 // Remove our current module as an impediment to visiting the
274 // module we depend on. If we were the last unvisited module
275 // that depends on this particular module, push it into the
276 // queue to be visited.
277 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
278 if (NumUnusedEdges && (--NumUnusedEdges == 0))
283 assert(VisitOrder.size() == N && "Visitation order is wrong?");
285 delete FirstVisitState;
286 FirstVisitState = nullptr;
289 VisitState *State = allocateVisitState();
290 unsigned VisitNumber = State->NextVisitNumber++;
292 // If the caller has provided us with a hit-set that came from the global
293 // module index, mark every module file in common with the global module
294 // index that is *not* in that set as 'visited'.
295 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
296 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
298 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
299 if (!ModuleFilesHit->count(M))
300 State->VisitNumber[M->Index] = VisitNumber;
304 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
305 ModuleFile *CurrentModule = VisitOrder[I];
306 // Should we skip this module file?
307 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
311 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
312 State->VisitNumber[CurrentModule->Index] = VisitNumber;
313 if (!Visitor(*CurrentModule, UserData))
316 // The visitor has requested that cut off visitation of any
317 // module that the current module depends on. To indicate this
318 // behavior, we mark all of the reachable modules as having been visited.
319 ModuleFile *NextModule = CurrentModule;
321 // For any module that this module depends on, push it on the
322 // stack (if it hasn't already been marked as visited).
323 for (llvm::SetVector<ModuleFile *>::iterator
324 M = NextModule->Imports.begin(),
325 MEnd = NextModule->Imports.end();
327 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
328 State->Stack.push_back(*M);
329 State->VisitNumber[(*M)->Index] = VisitNumber;
333 if (State->Stack.empty())
336 // Pop the next module off the stack.
337 NextModule = State->Stack.pop_back_val();
341 returnVisitState(State);
344 /// \brief Perform a depth-first visit of the current module.
345 static bool visitDepthFirst(ModuleFile &M,
346 bool (*Visitor)(ModuleFile &M, bool Preorder,
349 SmallVectorImpl<bool> &Visited) {
350 // Preorder visitation
351 if (Visitor(M, /*Preorder=*/true, UserData))
355 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
356 IMEnd = M.Imports.end();
358 if (Visited[(*IM)->Index])
360 Visited[(*IM)->Index] = true;
362 if (visitDepthFirst(**IM, Visitor, UserData, Visited))
366 // Postorder visitation
367 return Visitor(M, /*Preorder=*/false, UserData);
370 void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
373 SmallVector<bool, 16> Visited(size(), false);
374 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
375 if (Visited[Chain[I]->Index])
377 Visited[Chain[I]->Index] = true;
379 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
384 bool ModuleManager::lookupModuleFile(StringRef FileName,
386 time_t ExpectedModTime,
387 const FileEntry *&File) {
388 // Open the file immediately to ensure there is no race between stat'ing and
390 File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
392 if (!File && FileName != "-") {
396 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
397 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
398 // Do not destroy File, as it may be referenced. If we need to rebuild it,
399 // it will be destroyed by removeModules.
408 struct GraphTraits<ModuleManager> {
409 typedef ModuleFile NodeType;
410 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
411 typedef ModuleManager::ModuleConstIterator nodes_iterator;
413 static ChildIteratorType child_begin(NodeType *Node) {
414 return Node->Imports.begin();
417 static ChildIteratorType child_end(NodeType *Node) {
418 return Node->Imports.end();
421 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
422 return Manager.begin();
425 static nodes_iterator nodes_end(const ModuleManager &Manager) {
426 return Manager.end();
431 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
432 explicit DOTGraphTraits(bool IsSimple = false)
433 : DefaultDOTGraphTraits(IsSimple) { }
435 static bool renderGraphFromBottomUp() {
439 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
440 return M->ModuleName;
445 void ModuleManager::viewGraph() {
446 llvm::ViewGraph(*this, "Modules");