xref: /llvm-project/clang/lib/Serialization/ModuleManager.cpp (revision 16fe4d178e9d81b07e8deb59a4b4292850364898)
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/Frontend/PCHContainerOperations.h"
15 #include "clang/Lex/HeaderSearch.h"
16 #include "clang/Lex/ModuleMap.h"
17 #include "clang/Serialization/GlobalModuleIndex.h"
18 #include "clang/Serialization/ModuleManager.h"
19 #include "llvm/Support/MemoryBuffer.h"
20 #include "llvm/Support/Path.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <system_error>
23 
24 #ifndef NDEBUG
25 #include "llvm/Support/GraphWriter.h"
26 #endif
27 
28 using namespace clang;
29 using namespace serialization;
30 
31 ModuleFile *ModuleManager::lookup(StringRef Name) {
32   const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
33                                            /*cacheFailure=*/false);
34   if (Entry)
35     return lookup(Entry);
36 
37   return nullptr;
38 }
39 
40 ModuleFile *ModuleManager::lookup(const FileEntry *File) {
41   llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known
42     = Modules.find(File);
43   if (Known == Modules.end())
44     return nullptr;
45 
46   return Known->second;
47 }
48 
49 std::unique_ptr<llvm::MemoryBuffer>
50 ModuleManager::lookupBuffer(StringRef Name) {
51   const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false,
52                                            /*cacheFailure=*/false);
53   return std::move(InMemoryBuffers[Entry]);
54 }
55 
56 ModuleManager::AddModuleResult
57 ModuleManager::addModule(StringRef FileName, ModuleKind Type,
58                          SourceLocation ImportLoc, ModuleFile *ImportedBy,
59                          unsigned Generation,
60                          off_t ExpectedSize, time_t ExpectedModTime,
61                          ASTFileSignature ExpectedSignature,
62                          ASTFileSignatureReader ReadSignature,
63                          ModuleFile *&Module,
64                          std::string &ErrorStr) {
65   Module = nullptr;
66 
67   // Look for the file entry. This only fails if the expected size or
68   // modification time differ.
69   const FileEntry *Entry;
70   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   }
77   if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
78     ErrorStr = "module file out of date";
79     return OutOfDate;
80   }
81 
82   if (!Entry && FileName != "-") {
83     ErrorStr = "module file not found";
84     return Missing;
85   }
86 
87   // Check whether we already loaded this module, before
88   ModuleFile *&ModuleEntry = Modules[Entry];
89   bool NewModule = false;
90   if (!ModuleEntry) {
91     // Allocate a new module.
92     ModuleFile *New = new ModuleFile(Type, Generation);
93     New->Index = Chain.size();
94     New->FileName = FileName.str();
95     New->File = Entry;
96     New->ImportLoc = ImportLoc;
97     Chain.push_back(New);
98     if (!New->isModule())
99       PCHChain.push_back(New);
100     if (!ImportedBy)
101       Roots.push_back(New);
102     NewModule = true;
103     ModuleEntry = New;
104 
105     New->InputFilesValidationTimestamp = 0;
106     if (New->Kind == MK_ImplicitModule) {
107       std::string TimestampFilename = New->getTimestampFilename();
108       vfs::Status Status;
109       // A cached stat value would be fine as well.
110       if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
111         New->InputFilesValidationTimestamp =
112             Status.getLastModificationTime().toEpochTime();
113     }
114 
115     // Load the contents of the module
116     if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
117       // The buffer was already provided for us.
118       New->Buffer = std::move(Buffer);
119     } else {
120       // Open the AST file.
121       llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf(
122           (std::error_code()));
123       if (FileName == "-") {
124         Buf = llvm::MemoryBuffer::getSTDIN();
125       } else {
126         // Leave the FileEntry open so if it gets read again by another
127         // ModuleManager it must be the same underlying file.
128         // FIXME: Because FileManager::getFile() doesn't guarantee that it will
129         // give us an open file, this may not be 100% reliable.
130         Buf = FileMgr.getBufferForFile(New->File,
131                                        /*IsVolatile=*/false,
132                                        /*ShouldClose=*/false);
133       }
134 
135       if (!Buf) {
136         ErrorStr = Buf.getError().message();
137         return Missing;
138       }
139 
140       New->Buffer = std::move(*Buf);
141     }
142 
143     // Initialize the stream.
144     PCHContainerRdr.ExtractPCH(New->Buffer->getMemBufferRef(), New->StreamFile);
145   }
146 
147   if (ExpectedSignature) {
148     if (NewModule)
149       ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile);
150     else
151       assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile));
152 
153     if (ModuleEntry->Signature != ExpectedSignature) {
154       ErrorStr = ModuleEntry->Signature ? "signature mismatch"
155                                         : "could not read module signature";
156 
157       if (NewModule) {
158         // Remove the module file immediately, since removeModules might try to
159         // invalidate the file cache for Entry, and that is not safe if this
160         // module is *itself* up to date, but has an out-of-date importer.
161         Modules.erase(Entry);
162         assert(Chain.back() == ModuleEntry);
163         Chain.pop_back();
164         if (!ModuleEntry->isModule())
165           PCHChain.pop_back();
166         if (Roots.back() == ModuleEntry)
167           Roots.pop_back();
168         else
169           assert(ImportedBy);
170         delete ModuleEntry;
171       }
172       return OutOfDate;
173     }
174   }
175 
176   if (ImportedBy) {
177     ModuleEntry->ImportedBy.insert(ImportedBy);
178     ImportedBy->Imports.insert(ModuleEntry);
179   } else {
180     if (!ModuleEntry->DirectlyImported)
181       ModuleEntry->ImportLoc = ImportLoc;
182 
183     ModuleEntry->DirectlyImported = true;
184   }
185 
186   Module = ModuleEntry;
187   return NewModule? NewlyLoaded : AlreadyLoaded;
188 }
189 
190 void ModuleManager::removeModules(
191     ModuleIterator first, ModuleIterator last,
192     llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
193     ModuleMap *modMap) {
194   if (first == last)
195     return;
196 
197   // Collect the set of module file pointers that we'll be removing.
198   llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last);
199 
200   auto IsVictim = [&](ModuleFile *MF) {
201     return victimSet.count(MF);
202   };
203   // Remove any references to the now-destroyed modules.
204   for (unsigned i = 0, n = Chain.size(); i != n; ++i) {
205     Chain[i]->ImportedBy.remove_if(IsVictim);
206   }
207   Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
208               Roots.end());
209 
210   // Remove the modules from the PCH chain.
211   for (auto I = first; I != last; ++I) {
212     if (!(*I)->isModule()) {
213       PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I),
214                      PCHChain.end());
215       break;
216     }
217   }
218 
219   // Delete the modules and erase them from the various structures.
220   for (ModuleIterator victim = first; victim != last; ++victim) {
221     Modules.erase((*victim)->File);
222 
223     if (modMap) {
224       StringRef ModuleName = (*victim)->ModuleName;
225       if (Module *mod = modMap->findModule(ModuleName)) {
226         mod->setASTFile(nullptr);
227       }
228     }
229 
230     // Files that didn't make it through ReadASTCore successfully will be
231     // rebuilt (or there was an error). Invalidate them so that we can load the
232     // new files that will be renamed over the old ones.
233     if (LoadedSuccessfully.count(*victim) == 0)
234       FileMgr.invalidateCache((*victim)->File);
235 
236     delete *victim;
237   }
238 
239   // Remove the modules from the chain.
240   Chain.erase(first, last);
241 }
242 
243 void
244 ModuleManager::addInMemoryBuffer(StringRef FileName,
245                                  std::unique_ptr<llvm::MemoryBuffer> Buffer) {
246 
247   const FileEntry *Entry =
248       FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
249   InMemoryBuffers[Entry] = std::move(Buffer);
250 }
251 
252 bool ModuleManager::addKnownModuleFile(StringRef FileName) {
253   const FileEntry *File;
254   if (lookupModuleFile(FileName, 0, 0, File))
255     return true;
256   if (!Modules.count(File))
257     AdditionalKnownModuleFiles.insert(File);
258   return false;
259 }
260 
261 ModuleManager::VisitState *ModuleManager::allocateVisitState() {
262   // Fast path: if we have a cached state, use it.
263   if (FirstVisitState) {
264     VisitState *Result = FirstVisitState;
265     FirstVisitState = FirstVisitState->NextState;
266     Result->NextState = nullptr;
267     return Result;
268   }
269 
270   // Allocate and return a new state.
271   return new VisitState(size());
272 }
273 
274 void ModuleManager::returnVisitState(VisitState *State) {
275   assert(State->NextState == nullptr && "Visited state is in list?");
276   State->NextState = FirstVisitState;
277   FirstVisitState = State;
278 }
279 
280 void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
281   GlobalIndex = Index;
282   if (!GlobalIndex) {
283     ModulesInCommonWithGlobalIndex.clear();
284     return;
285   }
286 
287   // Notify the global module index about all of the modules we've already
288   // loaded.
289   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
290     if (!GlobalIndex->loadedModuleFile(Chain[I])) {
291       ModulesInCommonWithGlobalIndex.push_back(Chain[I]);
292     }
293   }
294 }
295 
296 void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
297   AdditionalKnownModuleFiles.remove(MF->File);
298 
299   if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
300     return;
301 
302   ModulesInCommonWithGlobalIndex.push_back(MF);
303 }
304 
305 ModuleManager::ModuleManager(FileManager &FileMgr,
306                              const PCHContainerReader &PCHContainerRdr)
307     : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(),
308       FirstVisitState(nullptr) {}
309 
310 ModuleManager::~ModuleManager() {
311   for (unsigned i = 0, e = Chain.size(); i != e; ++i)
312     delete Chain[e - i - 1];
313   delete FirstVisitState;
314 }
315 
316 void
317 ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData),
318                      void *UserData,
319                      llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
320   // If the visitation order vector is the wrong size, recompute the order.
321   if (VisitOrder.size() != Chain.size()) {
322     unsigned N = size();
323     VisitOrder.clear();
324     VisitOrder.reserve(N);
325 
326     // Record the number of incoming edges for each module. When we
327     // encounter a module with no incoming edges, push it into the queue
328     // to seed the queue.
329     SmallVector<ModuleFile *, 4> Queue;
330     Queue.reserve(N);
331     llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
332     UnusedIncomingEdges.resize(size());
333     for (auto M = rbegin(), MEnd = rend(); M != MEnd; ++M) {
334       unsigned Size = (*M)->ImportedBy.size();
335       UnusedIncomingEdges[(*M)->Index] = Size;
336       if (!Size)
337         Queue.push_back(*M);
338     }
339 
340     // Traverse the graph, making sure to visit a module before visiting any
341     // of its dependencies.
342     while (!Queue.empty()) {
343       ModuleFile *CurrentModule = Queue.pop_back_val();
344       VisitOrder.push_back(CurrentModule);
345 
346       // For any module that this module depends on, push it on the
347       // stack (if it hasn't already been marked as visited).
348       for (auto M = CurrentModule->Imports.rbegin(),
349                 MEnd = CurrentModule->Imports.rend();
350            M != MEnd; ++M) {
351         // Remove our current module as an impediment to visiting the
352         // module we depend on. If we were the last unvisited module
353         // that depends on this particular module, push it into the
354         // queue to be visited.
355         unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
356         if (NumUnusedEdges && (--NumUnusedEdges == 0))
357           Queue.push_back(*M);
358       }
359     }
360 
361     assert(VisitOrder.size() == N && "Visitation order is wrong?");
362 
363     delete FirstVisitState;
364     FirstVisitState = nullptr;
365   }
366 
367   VisitState *State = allocateVisitState();
368   unsigned VisitNumber = State->NextVisitNumber++;
369 
370   // If the caller has provided us with a hit-set that came from the global
371   // module index, mark every module file in common with the global module
372   // index that is *not* in that set as 'visited'.
373   if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
374     for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
375     {
376       ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
377       if (!ModuleFilesHit->count(M))
378         State->VisitNumber[M->Index] = VisitNumber;
379     }
380   }
381 
382   for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
383     ModuleFile *CurrentModule = VisitOrder[I];
384     // Should we skip this module file?
385     if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
386       continue;
387 
388     // Visit the module.
389     assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
390     State->VisitNumber[CurrentModule->Index] = VisitNumber;
391     if (!Visitor(*CurrentModule, UserData))
392       continue;
393 
394     // The visitor has requested that cut off visitation of any
395     // module that the current module depends on. To indicate this
396     // behavior, we mark all of the reachable modules as having been visited.
397     ModuleFile *NextModule = CurrentModule;
398     do {
399       // For any module that this module depends on, push it on the
400       // stack (if it hasn't already been marked as visited).
401       for (llvm::SetVector<ModuleFile *>::iterator
402              M = NextModule->Imports.begin(),
403              MEnd = NextModule->Imports.end();
404            M != MEnd; ++M) {
405         if (State->VisitNumber[(*M)->Index] != VisitNumber) {
406           State->Stack.push_back(*M);
407           State->VisitNumber[(*M)->Index] = VisitNumber;
408         }
409       }
410 
411       if (State->Stack.empty())
412         break;
413 
414       // Pop the next module off the stack.
415       NextModule = State->Stack.pop_back_val();
416     } while (true);
417   }
418 
419   returnVisitState(State);
420 }
421 
422 static void markVisitedDepthFirst(ModuleFile &M,
423                                   SmallVectorImpl<bool> &Visited) {
424   for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
425                                                IMEnd = M.Imports.end();
426        IM != IMEnd; ++IM) {
427     if (Visited[(*IM)->Index])
428       continue;
429     Visited[(*IM)->Index] = true;
430     if (!M.DirectlyImported)
431       markVisitedDepthFirst(**IM, Visited);
432   }
433 }
434 
435 /// \brief Perform a depth-first visit of the current module.
436 static bool visitDepthFirst(
437     ModuleFile &M,
438     ModuleManager::DFSPreorderControl (*PreorderVisitor)(ModuleFile &M,
439                                                          void *UserData),
440     bool (*PostorderVisitor)(ModuleFile &M, void *UserData), void *UserData,
441     SmallVectorImpl<bool> &Visited) {
442   if (PreorderVisitor) {
443     switch (PreorderVisitor(M, UserData)) {
444     case ModuleManager::Abort:
445       return true;
446     case ModuleManager::SkipImports:
447       markVisitedDepthFirst(M, Visited);
448       return false;
449     case ModuleManager::Continue:
450       break;
451     }
452   }
453 
454   // Visit children
455   for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(),
456                                             IMEnd = M.Imports.end();
457        IM != IMEnd; ++IM) {
458     if (Visited[(*IM)->Index])
459       continue;
460     Visited[(*IM)->Index] = true;
461 
462     if (visitDepthFirst(**IM, PreorderVisitor, PostorderVisitor, UserData, Visited))
463       return true;
464   }
465 
466   if (PostorderVisitor)
467     return PostorderVisitor(M, UserData);
468 
469   return false;
470 }
471 
472 void ModuleManager::visitDepthFirst(
473     ModuleManager::DFSPreorderControl (*PreorderVisitor)(ModuleFile &M,
474                                                          void *UserData),
475     bool (*PostorderVisitor)(ModuleFile &M, void *UserData), void *UserData) {
476   SmallVector<bool, 16> Visited(size(), false);
477   for (unsigned I = 0, N = Roots.size(); I != N; ++I) {
478     if (Visited[Roots[I]->Index])
479       continue;
480     Visited[Roots[I]->Index] = true;
481 
482     if (::visitDepthFirst(*Roots[I], PreorderVisitor, PostorderVisitor, UserData, Visited))
483       return;
484   }
485 }
486 
487 bool ModuleManager::lookupModuleFile(StringRef FileName,
488                                      off_t ExpectedSize,
489                                      time_t ExpectedModTime,
490                                      const FileEntry *&File) {
491   // Open the file immediately to ensure there is no race between stat'ing and
492   // opening the file.
493   File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false);
494 
495   if (!File && FileName != "-") {
496     return false;
497   }
498 
499   if ((ExpectedSize && ExpectedSize != File->getSize()) ||
500       (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
501     // Do not destroy File, as it may be referenced. If we need to rebuild it,
502     // it will be destroyed by removeModules.
503     return true;
504 
505   return false;
506 }
507 
508 #ifndef NDEBUG
509 namespace llvm {
510   template<>
511   struct GraphTraits<ModuleManager> {
512     typedef ModuleFile NodeType;
513     typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType;
514     typedef ModuleManager::ModuleConstIterator nodes_iterator;
515 
516     static ChildIteratorType child_begin(NodeType *Node) {
517       return Node->Imports.begin();
518     }
519 
520     static ChildIteratorType child_end(NodeType *Node) {
521       return Node->Imports.end();
522     }
523 
524     static nodes_iterator nodes_begin(const ModuleManager &Manager) {
525       return Manager.begin();
526     }
527 
528     static nodes_iterator nodes_end(const ModuleManager &Manager) {
529       return Manager.end();
530     }
531   };
532 
533   template<>
534   struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
535     explicit DOTGraphTraits(bool IsSimple = false)
536       : DefaultDOTGraphTraits(IsSimple) { }
537 
538     static bool renderGraphFromBottomUp() {
539       return true;
540     }
541 
542     std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
543       return M->ModuleName;
544     }
545   };
546 }
547 
548 void ModuleManager::viewGraph() {
549   llvm::ViewGraph(*this, "Modules");
550 }
551 #endif
552