xref: /llvm-project/clang/lib/Serialization/ModuleManager.cpp (revision caea13185e74a85509fe6674b4331d68656d9164)
1 //===--- 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 //===----------------------------------------------------------------------===//
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"
22 
23 #ifndef NDEBUG
24 #include "llvm/Support/GraphWriter.h"
25 #endif
26 
27 using namespace clang;
28 using namespace serialization;
29 
30 ModuleFile *ModuleManager::lookup(StringRef Name) {
31   const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
32                                            /*cacheFailure=*/false);
33   if (Entry)
34     return lookup(Entry);
35 
36   return 0;
37 }
38 
39 ModuleFile *ModuleManager::lookup(const FileEntry *File) {
40   llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
41     = Modules.find(File);
42   if (Known == Modules.end())
43     return 0;
44 
45   return Known->second;
46 }
47 
48 llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) {
49   const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
50                                            /*cacheFailure=*/false);
51   return InMemoryBuffers[Entry];
52 }
53 
54 ModuleManager::AddModuleResult
55 ModuleManager::addModule(StringRef FileName, ModuleKind Type,
56                          SourceLocation ImportLoc, ModuleFile *ImportedBy,
57                          unsigned Generation,
58                          off_t ExpectedSize, time_t ExpectedModTime,
59                          ModuleFile *&Module,
60                          std::string &ErrorStr) {
61   Module = 0;
62 
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";
68     return OutOfDate;
69   }
70 
71   if (!Entry && FileName != "-") {
72     ErrorStr = "module file not found";
73     return Missing;
74   }
75 
76   // Check whether we already loaded this module, before
77   ModuleFile *&ModuleEntry = Modules[Entry];
78   bool NewModule = false;
79   if (!ModuleEntry) {
80     // Allocate a new module.
81     ModuleFile *New = new ModuleFile(Type, Generation);
82     New->Index = Chain.size();
83     New->FileName = FileName.str();
84     New->File = Entry;
85     New->ImportLoc = ImportLoc;
86     Chain.push_back(New);
87     NewModule = true;
88     ModuleEntry = New;
89 
90     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 
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);
105     } else {
106       // Open the AST file.
107       llvm::error_code ec;
108       if (FileName == "-") {
109         ec = llvm::MemoryBuffer::getSTDIN(New->Buffer);
110         if (ec)
111           ErrorStr = ec.message();
112       } else
113         New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr));
114 
115       if (!New->Buffer)
116         return Missing;
117     }
118 
119     // Initialize the stream
120     New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(),
121                          (const unsigned char *)New->Buffer->getBufferEnd());
122   }
123 
124   if (ImportedBy) {
125     ModuleEntry->ImportedBy.insert(ImportedBy);
126     ImportedBy->Imports.insert(ModuleEntry);
127   } else {
128     if (!ModuleEntry->DirectlyImported)
129       ModuleEntry->ImportLoc = ImportLoc;
130 
131     ModuleEntry->DirectlyImported = true;
132   }
133 
134   Module = ModuleEntry;
135   return NewModule? NewlyLoaded : AlreadyLoaded;
136 }
137 
138 void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last,
139                                   ModuleMap *modMap) {
140   if (first == last)
141     return;
142 
143   // Collect the set of module file pointers that we'll be removing.
144   llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
145 
146   // Remove any references to the now-destroyed modules.
147   for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
148     Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) {
149       return victimSet.count(MF);
150     });
151   }
152 
153   // Delete the modules and erase them from the various structures.
154   for (ModuleIterator victim = first; victim != last; ++victim) {
155     Modules.erase((*victim)->File);
156 
157     if (modMap) {
158       StringRef ModuleName = (*victim)->ModuleName;
159       if (Module *mod = modMap->findModule(ModuleName)) {
160         mod->setASTFile(0);
161       }
162     }
163     delete *victim;
164   }
165 
166   // Remove the modules from the chain.
167   Chain.erase(first, last);
168 }
169 
170 void ModuleManager::addInMemoryBuffer(StringRef FileName,
171                                       llvm::MemoryBuffer *Buffer) {
172 
173   const FileEntry *Entry = FileMgr.getVirtualFile(FileName,
174                                                   Buffer->getBufferSize(), 0);
175   InMemoryBuffers[Entry] = Buffer;
176 }
177 
178 ModuleManager::VisitState *ModuleManager::allocateVisitState() {
179   // Fast path: if we have a cached state, use it.
180   if (FirstVisitState) {
181     VisitState *Result = FirstVisitState;
182     FirstVisitState = FirstVisitState->NextState;
183     Result->NextState = 0;
184     return Result;
185   }
186 
187   // Allocate and return a new state.
188   return new VisitState(size());
189 }
190 
191 void ModuleManager::returnVisitState(VisitState *State) {
192   assert(State->NextState == 0 && "Visited state is in list?");
193   State->NextState = FirstVisitState;
194   FirstVisitState = State;
195 }
196 
197 void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
198   GlobalIndex = Index;
199   if (!GlobalIndex) {
200     ModulesInCommonWithGlobalIndex.clear();
201     return;
202   }
203 
204   // Notify the global module index about all of the modules we've already
205   // loaded.
206   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
207     if (!GlobalIndex->loadedModuleFile(Chain[I])) {
208       ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
209     }
210   }
211 }
212 
213 void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
214   if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
215     return;
216 
217   ModulesInCommonWithGlobalIndex.push_back(MF);
218 }
219 
220 ModuleManager::ModuleManager(FileManager &FileMgr)
221   : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(0) { }
222 
223 ModuleManager::~ModuleManager() {
224   for (unsigned i = 0, e = Chain.size(); i != e; ++i)
225     delete Chain[e - i - 1];
226   delete FirstVisitState;
227 }
228 
229 void
230 ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
231                      void *UserData,
232                      llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) {
233   // If the visitation order vector is the wrong size, recompute the order.
234   if (VisitOrder.size() != Chain.size()) {
235     unsigned N = size();
236     VisitOrder.clear();
237     VisitOrder.reserve(N);
238 
239     // Record the number of incoming edges for each module. When we
240     // encounter a module with no incoming edges, push it into the queue
241     // to seed the queue.
242     SmallVector<ModuleFile *, 4> Queue;
243     Queue.reserve(N);
244     llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
245     UnusedIncomingEdges.reserve(size());
246     for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) {
247       if (unsigned Size = (*M)->ImportedBy.size())
248         UnusedIncomingEdges.push_back(Size);
249       else {
250         UnusedIncomingEdges.push_back(0);
251         Queue.push_back(*M);
252       }
253     }
254 
255     // Traverse the graph, making sure to visit a module before visiting any
256     // of its dependencies.
257     unsigned QueueStart = 0;
258     while (QueueStart < Queue.size()) {
259       ModuleFile *CurrentModule = Queue[QueueStart++];
260       VisitOrder.push_back(CurrentModule);
261 
262       // For any module that this module depends on, push it on the
263       // stack (if it hasn't already been marked as visited).
264       for (llvm::SetVector<ModuleFile *>::iterator
265              M = CurrentModule->Imports.begin(),
266              MEnd = CurrentModule->Imports.end();
267            M != MEnd; ++M) {
268         // Remove our current module as an impediment to visiting the
269         // module we depend on. If we were the last unvisited module
270         // that depends on this particular module, push it into the
271         // queue to be visited.
272         unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
273         if (NumUnusedEdges && (--NumUnusedEdges == 0))
274           Queue.push_back(*M);
275       }
276     }
277 
278     assert(VisitOrder.size() == N && "Visitation order is wrong?");
279 
280     delete FirstVisitState;
281     FirstVisitState = 0;
282   }
283 
284   VisitState *State = allocateVisitState();
285   unsigned VisitNumber = State->NextVisitNumber++;
286 
287   // If the caller has provided us with a hit-set that came from the global
288   // module index, mark every module file in common with the global module
289   // index that is *not* in that set as 'visited'.
290   if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
291     for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
292     {
293       ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
294       if (!ModuleFilesHit->count(M))
295         State->VisitNumber[M->Index] = VisitNumber;
296     }
297   }
298 
299   for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
300     ModuleFile *CurrentModule = VisitOrder[I];
301     // Should we skip this module file?
302     if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
303       continue;
304 
305     // Visit the module.
306     assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
307     State->VisitNumber[CurrentModule->Index] = VisitNumber;
308     if (!Visitor(*CurrentModule, UserData))
309       continue;
310 
311     // The visitor has requested that cut off visitation of any
312     // module that the current module depends on. To indicate this
313     // behavior, we mark all of the reachable modules as having been visited.
314     ModuleFile *NextModule = CurrentModule;
315     do {
316       // For any module that this module depends on, push it on the
317       // stack (if it hasn't already been marked as visited).
318       for (llvm::SetVector<ModuleFile *>::iterator
319              M = NextModule->Imports.begin(),
320              MEnd = NextModule->Imports.end();
321            M != MEnd; ++M) {
322         if (State->VisitNumber[(*M)->Index] != VisitNumber) {
323           State->Stack.push_back(*M);
324           State->VisitNumber[(*M)->Index] = VisitNumber;
325         }
326       }
327 
328       if (State->Stack.empty())
329         break;
330 
331       // Pop the next module off the stack.
332       NextModule = State->Stack.pop_back_val();
333     } while (true);
334   }
335 
336   returnVisitState(State);
337 }
338 
339 /// \brief Perform a depth-first visit of the current module.
340 static bool visitDepthFirst(ModuleFile &M,
341                             bool (*Visitor)(ModuleFile &M, bool Preorder,
342                                             void *UserData),
343                             void *UserData,
344                             SmallVectorImpl<bool> &Visited) {
345   // Preorder visitation
346   if (Visitor(M, /*Preorder=*/true, UserData))
347     return true;
348 
349   // Visit children
350   for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
351                                             IMEnd = M.Imports.end();
352        IM != IMEnd; ++IM) {
353     if (Visited[(*IM)->Index])
354       continue;
355     Visited[(*IM)->Index] = true;
356 
357     if (visitDepthFirst(**IM, Visitor, UserData, Visited))
358       return true;
359   }
360 
361   // Postorder visitation
362   return Visitor(M, /*Preorder=*/false, UserData);
363 }
364 
365 void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
366                                                     void *UserData),
367                                     void *UserData) {
368   SmallVector<bool, 16> Visited(size(), false);
369   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
370     if (Visited[Chain[I]->Index])
371       continue;
372     Visited[Chain[I]->Index] = true;
373 
374     if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited))
375       return;
376   }
377 }
378 
379 bool ModuleManager::lookupModuleFile(StringRef FileName,
380                                      off_t ExpectedSize,
381                                      time_t ExpectedModTime,
382                                      const FileEntry *&File) {
383   // Open the file immediately to ensure there is no race between stat'ing and
384   // opening the file.
385   File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
386 
387   if (!File && FileName != "-") {
388     return false;
389   }
390 
391   if ((ExpectedSize && ExpectedSize != File->getSize()) ||
392       (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
393     // Do not destroy File, as it may be referenced. If we need to rebuild it,
394     // it will be destroyed by removeModules.
395     return true;
396 
397   return false;
398 }
399 
400 #ifndef NDEBUG
401 namespace llvm {
402   template<>
403   struct GraphTraits<ModuleManager> {
404     typedef ModuleFile NodeType;
405     typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
406     typedef ModuleManager::ModuleConstIterator nodes_iterator;
407 
408     static ChildIteratorType child_begin(NodeType *Node) {
409       return Node->Imports.begin();
410     }
411 
412     static ChildIteratorType child_end(NodeType *Node) {
413       return Node->Imports.end();
414     }
415 
416     static nodes_iterator nodes_begin(const ModuleManager &Manager) {
417       return Manager.begin();
418     }
419 
420     static nodes_iterator nodes_end(const ModuleManager &Manager) {
421       return Manager.end();
422     }
423   };
424 
425   template<>
426   struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
427     explicit DOTGraphTraits(bool IsSimple = false)
428       : DefaultDOTGraphTraits(IsSimple) { }
429 
430     static bool renderGraphFromBottomUp() {
431       return true;
432     }
433 
434     std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
435       return M->ModuleName;
436     }
437   };
438 }
439 
440 void ModuleManager::viewGraph() {
441   llvm::ViewGraph(*this, "Modules");
442 }
443 #endif
444