1d44252ecSDouglas Gregor //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===// 2d44252ecSDouglas Gregor // 3d44252ecSDouglas Gregor // The LLVM Compiler Infrastructure 4d44252ecSDouglas Gregor // 5d44252ecSDouglas Gregor // This file is distributed under the University of Illinois Open Source 6d44252ecSDouglas Gregor // License. See LICENSE.TXT for details. 7d44252ecSDouglas Gregor // 8d44252ecSDouglas Gregor //===----------------------------------------------------------------------===// 9d44252ecSDouglas Gregor // 10d44252ecSDouglas Gregor // This file defines the ModuleManager class, which manages a set of loaded 11d44252ecSDouglas Gregor // modules for the ASTReader. 12d44252ecSDouglas Gregor // 13d44252ecSDouglas Gregor //===----------------------------------------------------------------------===// 149670f847SMehdi Amini #include "clang/Serialization/ModuleManager.h" 15bb165fb0SAdrian Prantl #include "clang/Frontend/PCHContainerOperations.h" 16beee15e7SBen Langmuir #include "clang/Lex/HeaderSearch.h" 177029ce1aSDouglas Gregor #include "clang/Lex/ModuleMap.h" 187211ac15SDouglas Gregor #include "clang/Serialization/GlobalModuleIndex.h" 19d44252ecSDouglas Gregor #include "llvm/Support/MemoryBuffer.h" 20552c169eSRafael Espindola #include "llvm/Support/Path.h" 218a8e554aSRafael Espindola #include <system_error> 22d44252ecSDouglas Gregor 239d7c1a2aSDouglas Gregor #ifndef NDEBUG 249d7c1a2aSDouglas Gregor #include "llvm/Support/GraphWriter.h" 259d7c1a2aSDouglas Gregor #endif 269d7c1a2aSDouglas Gregor 27d44252ecSDouglas Gregor using namespace clang; 28d44252ecSDouglas Gregor using namespace serialization; 29d44252ecSDouglas Gregor 30de3ef502SDouglas Gregor ModuleFile *ModuleManager::lookup(StringRef Name) { 31dadd85dcSDouglas Gregor const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, 32dadd85dcSDouglas Gregor /*cacheFailure=*/false); 33bf7fc9c5SDouglas Gregor if (Entry) 34bf7fc9c5SDouglas Gregor return lookup(Entry); 35bf7fc9c5SDouglas Gregor 36a13603a2SCraig Topper return nullptr; 37bf7fc9c5SDouglas Gregor } 38bf7fc9c5SDouglas Gregor 39bf7fc9c5SDouglas Gregor ModuleFile *ModuleManager::lookup(const FileEntry *File) { 40bf7fc9c5SDouglas Gregor llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known 41bf7fc9c5SDouglas Gregor = Modules.find(File); 42bf7fc9c5SDouglas Gregor if (Known == Modules.end()) 43a13603a2SCraig Topper return nullptr; 44bf7fc9c5SDouglas Gregor 45bf7fc9c5SDouglas Gregor return Known->second; 46d44252ecSDouglas Gregor } 47d44252ecSDouglas Gregor 485cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> 495cd06f26SRafael Espindola ModuleManager::lookupBuffer(StringRef Name) { 50dadd85dcSDouglas Gregor const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, 51dadd85dcSDouglas Gregor /*cacheFailure=*/false); 525cd06f26SRafael Espindola return std::move(InMemoryBuffers[Entry]); 53d44252ecSDouglas Gregor } 54d44252ecSDouglas Gregor 5514afc8e7SDuncan P. N. Exon Smith static bool checkSignature(ASTFileSignature Signature, 5614afc8e7SDuncan P. N. Exon Smith ASTFileSignature ExpectedSignature, 5714afc8e7SDuncan P. N. Exon Smith std::string &ErrorStr) { 5814afc8e7SDuncan P. N. Exon Smith if (!ExpectedSignature || Signature == ExpectedSignature) 5914afc8e7SDuncan P. N. Exon Smith return false; 6014afc8e7SDuncan P. N. Exon Smith 6114afc8e7SDuncan P. N. Exon Smith ErrorStr = 6214afc8e7SDuncan P. N. Exon Smith Signature ? "signature mismatch" : "could not read module signature"; 6314afc8e7SDuncan P. N. Exon Smith return true; 6414afc8e7SDuncan P. N. Exon Smith } 6514afc8e7SDuncan P. N. Exon Smith 667029ce1aSDouglas Gregor ModuleManager::AddModuleResult 67d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type, 686fb03aeaSDouglas Gregor SourceLocation ImportLoc, ModuleFile *ImportedBy, 697029ce1aSDouglas Gregor unsigned Generation, 707029ce1aSDouglas Gregor off_t ExpectedSize, time_t ExpectedModTime, 71487ea14aSBen Langmuir ASTFileSignature ExpectedSignature, 7270a1b816SBen Langmuir ASTFileSignatureReader ReadSignature, 737029ce1aSDouglas Gregor ModuleFile *&Module, 747029ce1aSDouglas Gregor std::string &ErrorStr) { 75a13603a2SCraig Topper Module = nullptr; 767029ce1aSDouglas Gregor 777029ce1aSDouglas Gregor // Look for the file entry. This only fails if the expected size or 787029ce1aSDouglas Gregor // modification time differ. 797029ce1aSDouglas Gregor const FileEntry *Entry; 8011f2a477SManman Ren if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) { 815b390756SRichard Smith // If we're not expecting to pull this file out of the module cache, it 825b390756SRichard Smith // might have a different mtime due to being moved across filesystems in 835b390756SRichard Smith // a distributed build. The size must still match, though. (As must the 845b390756SRichard Smith // contents, but we can't check that.) 855b390756SRichard Smith ExpectedModTime = 0; 865b390756SRichard Smith } 87c27d0d5eSEli Friedman if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { 88c27d0d5eSEli Friedman ErrorStr = "module file out of date"; 897029ce1aSDouglas Gregor return OutOfDate; 90c27d0d5eSEli Friedman } 917029ce1aSDouglas Gregor 92d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 93c27d0d5eSEli Friedman ErrorStr = "module file not found"; 947029ce1aSDouglas Gregor return Missing; 95d44252ecSDouglas Gregor } 96d44252ecSDouglas Gregor 97d44252ecSDouglas Gregor // Check whether we already loaded this module, before 983b99db55SRichard Smith ModuleFile *ModuleEntry = Modules[Entry]; 99a897f7cdSDuncan P. N. Exon Smith std::unique_ptr<ModuleFile> NewModule; 100d44252ecSDouglas Gregor if (!ModuleEntry) { 101d44252ecSDouglas Gregor // Allocate a new module. 102a897f7cdSDuncan P. N. Exon Smith NewModule = llvm::make_unique<ModuleFile>(Type, Generation); 103a897f7cdSDuncan P. N. Exon Smith NewModule->Index = Chain.size(); 104a897f7cdSDuncan P. N. Exon Smith NewModule->FileName = FileName.str(); 105a897f7cdSDuncan P. N. Exon Smith NewModule->File = Entry; 106a897f7cdSDuncan P. N. Exon Smith NewModule->ImportLoc = ImportLoc; 107a897f7cdSDuncan P. N. Exon Smith NewModule->InputFilesValidationTimestamp = 0; 108d44252ecSDouglas Gregor 109a897f7cdSDuncan P. N. Exon Smith if (NewModule->Kind == MK_ImplicitModule) { 110a897f7cdSDuncan P. N. Exon Smith std::string TimestampFilename = NewModule->getTimestampFilename(); 111c8130a74SBen Langmuir vfs::Status Status; 112f430da4dSDmitri Gribenko // A cached stat value would be fine as well. 113f430da4dSDmitri Gribenko if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) 114a897f7cdSDuncan P. N. Exon Smith NewModule->InputFilesValidationTimestamp = 115ac71c8e2SPavel Labath llvm::sys::toTimeT(Status.getLastModificationTime()); 116f430da4dSDmitri Gribenko } 117f430da4dSDmitri Gribenko 118d44252ecSDouglas Gregor // Load the contents of the module 1195cd06f26SRafael Espindola if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) { 120d44252ecSDouglas Gregor // The buffer was already provided for us. 121a897f7cdSDuncan P. N. Exon Smith NewModule->Buffer = std::move(Buffer); 122d44252ecSDouglas Gregor } else { 123d44252ecSDouglas Gregor // Open the AST file. 124a885796dSBenjamin Kramer llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf( 125a885796dSBenjamin Kramer (std::error_code())); 126d44252ecSDouglas Gregor if (FileName == "-") { 127a885796dSBenjamin Kramer Buf = llvm::MemoryBuffer::getSTDIN(); 1289801b253SBen Langmuir } else { 1299801b253SBen Langmuir // Leave the FileEntry open so if it gets read again by another 1309801b253SBen Langmuir // ModuleManager it must be the same underlying file. 1319801b253SBen Langmuir // FIXME: Because FileManager::getFile() doesn't guarantee that it will 1329801b253SBen Langmuir // give us an open file, this may not be 100% reliable. 133a897f7cdSDuncan P. N. Exon Smith Buf = FileMgr.getBufferForFile(NewModule->File, 134a885796dSBenjamin Kramer /*IsVolatile=*/false, 135a885796dSBenjamin Kramer /*ShouldClose=*/false); 1369801b253SBen Langmuir } 137d44252ecSDouglas Gregor 138a885796dSBenjamin Kramer if (!Buf) { 139a885796dSBenjamin Kramer ErrorStr = Buf.getError().message(); 1407029ce1aSDouglas Gregor return Missing; 141d44252ecSDouglas Gregor } 142d44252ecSDouglas Gregor 143a897f7cdSDuncan P. N. Exon Smith NewModule->Buffer = std::move(*Buf); 144a885796dSBenjamin Kramer } 145a885796dSBenjamin Kramer 146bb165fb0SAdrian Prantl // Initialize the stream. 147a897f7cdSDuncan P. N. Exon Smith NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer); 148487ea14aSBen Langmuir 14914afc8e7SDuncan P. N. Exon Smith // Read the signature eagerly now so that we can check it. 150a897f7cdSDuncan P. N. Exon Smith if (checkSignature(ReadSignature(NewModule->Data), ExpectedSignature, ErrorStr)) 151ed982584SBen Langmuir return OutOfDate; 152a897f7cdSDuncan P. N. Exon Smith 153a897f7cdSDuncan P. N. Exon Smith // We're keeping this module. Update the map entry. 154a897f7cdSDuncan P. N. Exon Smith ModuleEntry = NewModule.get(); 15514afc8e7SDuncan P. N. Exon Smith } else if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr)) { 15614afc8e7SDuncan P. N. Exon Smith return OutOfDate; 1577029ce1aSDouglas Gregor } 158d44252ecSDouglas Gregor 159d44252ecSDouglas Gregor if (ImportedBy) { 160d44252ecSDouglas Gregor ModuleEntry->ImportedBy.insert(ImportedBy); 161d44252ecSDouglas Gregor ImportedBy->Imports.insert(ModuleEntry); 162d44252ecSDouglas Gregor } else { 1636fb03aeaSDouglas Gregor if (!ModuleEntry->DirectlyImported) 1646fb03aeaSDouglas Gregor ModuleEntry->ImportLoc = ImportLoc; 1656fb03aeaSDouglas Gregor 166d44252ecSDouglas Gregor ModuleEntry->DirectlyImported = true; 167d44252ecSDouglas Gregor } 168d44252ecSDouglas Gregor 1697029ce1aSDouglas Gregor Module = ModuleEntry; 1703b99db55SRichard Smith 1713b99db55SRichard Smith if (!NewModule) 1723b99db55SRichard Smith return AlreadyLoaded; 1733b99db55SRichard Smith 1743b99db55SRichard Smith assert(!Modules[Entry] && "module loaded twice"); 1753b99db55SRichard Smith Modules[Entry] = ModuleEntry; 1763b99db55SRichard Smith 177a897f7cdSDuncan P. N. Exon Smith Chain.push_back(std::move(NewModule)); 1783b99db55SRichard Smith if (!ModuleEntry->isModule()) 1793b99db55SRichard Smith PCHChain.push_back(ModuleEntry); 1803b99db55SRichard Smith if (!ImportedBy) 1813b99db55SRichard Smith Roots.push_back(ModuleEntry); 1823b99db55SRichard Smith 1833b99db55SRichard Smith return NewlyLoaded; 184d44252ecSDouglas Gregor } 185d44252ecSDouglas Gregor 1869801b253SBen Langmuir void ModuleManager::removeModules( 187*8e6bc197SDuncan P. N. Exon Smith ModuleIterator First, 1889801b253SBen Langmuir llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully, 1897029ce1aSDouglas Gregor ModuleMap *modMap) { 190*8e6bc197SDuncan P. N. Exon Smith auto Last = end(); 191*8e6bc197SDuncan P. N. Exon Smith if (First == Last) 192188dbef2SDouglas Gregor return; 193188dbef2SDouglas Gregor 194*8e6bc197SDuncan P. N. Exon Smith 195a50dbb20SBen Langmuir // Explicitly clear VisitOrder since we might not notice it is stale. 196a50dbb20SBen Langmuir VisitOrder.clear(); 197a50dbb20SBen Langmuir 198188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 19996a06e0eSDuncan P. N. Exon Smith llvm::SmallPtrSet<ModuleFile *, 4> victimSet( 200*8e6bc197SDuncan P. N. Exon Smith (llvm::pointer_iterator<ModuleIterator>(First)), 201*8e6bc197SDuncan P. N. Exon Smith (llvm::pointer_iterator<ModuleIterator>(Last))); 202188dbef2SDouglas Gregor 2039eff8b14SManuel Klimek auto IsVictim = [&](ModuleFile *MF) { 2049eff8b14SManuel Klimek return victimSet.count(MF); 2059eff8b14SManuel Klimek }; 206188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 207*8e6bc197SDuncan P. N. Exon Smith // 208*8e6bc197SDuncan P. N. Exon Smith // FIXME: this should probably clean up Imports as well. 209*8e6bc197SDuncan P. N. Exon Smith for (auto I = begin(); I != First; ++I) 210*8e6bc197SDuncan P. N. Exon Smith I->ImportedBy.remove_if(IsVictim); 2119eff8b14SManuel Klimek Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim), 2129eff8b14SManuel Klimek Roots.end()); 213188dbef2SDouglas Gregor 21416fe4d17SRichard Smith // Remove the modules from the PCH chain. 215*8e6bc197SDuncan P. N. Exon Smith for (auto I = First; I != Last; ++I) { 21696a06e0eSDuncan P. N. Exon Smith if (!I->isModule()) { 21796a06e0eSDuncan P. N. Exon Smith PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), &*I), 21816fe4d17SRichard Smith PCHChain.end()); 21916fe4d17SRichard Smith break; 22016fe4d17SRichard Smith } 22116fe4d17SRichard Smith } 22216fe4d17SRichard Smith 223188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 224*8e6bc197SDuncan P. N. Exon Smith for (ModuleIterator victim = First; victim != Last; ++victim) { 22596a06e0eSDuncan P. N. Exon Smith Modules.erase(victim->File); 226ca39214fSBen Langmuir 2277029ce1aSDouglas Gregor if (modMap) { 22896a06e0eSDuncan P. N. Exon Smith StringRef ModuleName = victim->ModuleName; 2297029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 230a13603a2SCraig Topper mod->setASTFile(nullptr); 2317029ce1aSDouglas Gregor } 2327029ce1aSDouglas Gregor } 2334f05478cSBen Langmuir 2349801b253SBen Langmuir // Files that didn't make it through ReadASTCore successfully will be 2359801b253SBen Langmuir // rebuilt (or there was an error). Invalidate them so that we can load the 2369801b253SBen Langmuir // new files that will be renamed over the old ones. 23796a06e0eSDuncan P. N. Exon Smith if (LoadedSuccessfully.count(&*victim) == 0) 23896a06e0eSDuncan P. N. Exon Smith FileMgr.invalidateCache(victim->File); 239188dbef2SDouglas Gregor } 240188dbef2SDouglas Gregor 241a897f7cdSDuncan P. N. Exon Smith // Delete the modules. 242*8e6bc197SDuncan P. N. Exon Smith Chain.erase(Chain.begin() + (First - begin()), Chain.end()); 243188dbef2SDouglas Gregor } 244188dbef2SDouglas Gregor 2455cd06f26SRafael Espindola void 2465cd06f26SRafael Espindola ModuleManager::addInMemoryBuffer(StringRef FileName, 2475cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> Buffer) { 248d44252ecSDouglas Gregor 2495cd06f26SRafael Espindola const FileEntry *Entry = 2505cd06f26SRafael Espindola FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); 2515cd06f26SRafael Espindola InMemoryBuffers[Entry] = std::move(Buffer); 252d44252ecSDouglas Gregor } 253d44252ecSDouglas Gregor 254e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 255e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 256e97cd90aSDouglas Gregor if (FirstVisitState) { 257e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 258e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 259a13603a2SCraig Topper Result->NextState = nullptr; 260e97cd90aSDouglas Gregor return Result; 261e97cd90aSDouglas Gregor } 262e97cd90aSDouglas Gregor 263e97cd90aSDouglas Gregor // Allocate and return a new state. 264e97cd90aSDouglas Gregor return new VisitState(size()); 265e97cd90aSDouglas Gregor } 266e97cd90aSDouglas Gregor 267e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 268a13603a2SCraig Topper assert(State->NextState == nullptr && "Visited state is in list?"); 269e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 270e97cd90aSDouglas Gregor FirstVisitState = State; 271e97cd90aSDouglas Gregor } 272e97cd90aSDouglas Gregor 2737211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 2747211ac15SDouglas Gregor GlobalIndex = Index; 275603cd869SDouglas Gregor if (!GlobalIndex) { 276603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 277603cd869SDouglas Gregor return; 2787029ce1aSDouglas Gregor } 279603cd869SDouglas Gregor 280603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 281603cd869SDouglas Gregor // loaded. 282a897f7cdSDuncan P. N. Exon Smith for (ModuleFile &M : *this) 283a897f7cdSDuncan P. N. Exon Smith if (!GlobalIndex->loadedModuleFile(&M)) 284a897f7cdSDuncan P. N. Exon Smith ModulesInCommonWithGlobalIndex.push_back(&M); 285603cd869SDouglas Gregor } 286603cd869SDouglas Gregor 287603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 288603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 289603cd869SDouglas Gregor return; 290603cd869SDouglas Gregor 291603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 2927211ac15SDouglas Gregor } 2937211ac15SDouglas Gregor 294bb165fb0SAdrian Prantl ModuleManager::ModuleManager(FileManager &FileMgr, 295fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr) 296fb2398d0SAdrian Prantl : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(), 297bb165fb0SAdrian Prantl FirstVisitState(nullptr) {} 298d44252ecSDouglas Gregor 299a897f7cdSDuncan P. N. Exon Smith ModuleManager::~ModuleManager() { delete FirstVisitState; } 300d44252ecSDouglas Gregor 3019a9efbafSBenjamin Kramer void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor, 3024dd9b43cSCraig Topper llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) { 3037211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 304e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 305d44252ecSDouglas Gregor unsigned N = size(); 306e41d7feaSDouglas Gregor VisitOrder.clear(); 307e41d7feaSDouglas Gregor VisitOrder.reserve(N); 308d44252ecSDouglas Gregor 309d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 310d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 311d44252ecSDouglas Gregor // to seed the queue. 312de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 313d44252ecSDouglas Gregor Queue.reserve(N); 314bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 315a7c535b3SRichard Smith UnusedIncomingEdges.resize(size()); 31696a06e0eSDuncan P. N. Exon Smith for (ModuleFile &M : llvm::reverse(*this)) { 31796a06e0eSDuncan P. N. Exon Smith unsigned Size = M.ImportedBy.size(); 31896a06e0eSDuncan P. N. Exon Smith UnusedIncomingEdges[M.Index] = Size; 319a7c535b3SRichard Smith if (!Size) 32096a06e0eSDuncan P. N. Exon Smith Queue.push_back(&M); 321d44252ecSDouglas Gregor } 322d44252ecSDouglas Gregor 323e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 324e41d7feaSDouglas Gregor // of its dependencies. 325a7c535b3SRichard Smith while (!Queue.empty()) { 326a7c535b3SRichard Smith ModuleFile *CurrentModule = Queue.pop_back_val(); 327e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 328d44252ecSDouglas Gregor 329d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 330d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 331a7c535b3SRichard Smith for (auto M = CurrentModule->Imports.rbegin(), 332a7c535b3SRichard Smith MEnd = CurrentModule->Imports.rend(); 333d44252ecSDouglas Gregor M != MEnd; ++M) { 334d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 335d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 336d44252ecSDouglas Gregor // that depends on this particular module, push it into the 337d44252ecSDouglas Gregor // queue to be visited. 338bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 339d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 340d44252ecSDouglas Gregor Queue.push_back(*M); 341d44252ecSDouglas Gregor } 342d44252ecSDouglas Gregor } 343e41d7feaSDouglas Gregor 344e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 3457211ac15SDouglas Gregor 346e97cd90aSDouglas Gregor delete FirstVisitState; 347a13603a2SCraig Topper FirstVisitState = nullptr; 348e41d7feaSDouglas Gregor } 349e41d7feaSDouglas Gregor 350e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 351e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 352e41d7feaSDouglas Gregor 3537211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 3547211ac15SDouglas Gregor // module index, mark every module file in common with the global module 3557211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 3567211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 3577211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 3587211ac15SDouglas Gregor { 3597211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 3607029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 361e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 3627211ac15SDouglas Gregor } 3637211ac15SDouglas Gregor } 3647211ac15SDouglas Gregor 365e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 366e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 367e41d7feaSDouglas Gregor // Should we skip this module file? 368e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 369e41d7feaSDouglas Gregor continue; 370e41d7feaSDouglas Gregor 371e41d7feaSDouglas Gregor // Visit the module. 372e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 373e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 3749a9efbafSBenjamin Kramer if (!Visitor(*CurrentModule)) 375e41d7feaSDouglas Gregor continue; 376e41d7feaSDouglas Gregor 377e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 378e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 379e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 380e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 381e41d7feaSDouglas Gregor do { 382e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 383e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 384e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 385e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 386e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 387e41d7feaSDouglas Gregor M != MEnd; ++M) { 388e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 389e97cd90aSDouglas Gregor State->Stack.push_back(*M); 390e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 391e41d7feaSDouglas Gregor } 392e41d7feaSDouglas Gregor } 393e41d7feaSDouglas Gregor 394e97cd90aSDouglas Gregor if (State->Stack.empty()) 395e41d7feaSDouglas Gregor break; 396e41d7feaSDouglas Gregor 397e41d7feaSDouglas Gregor // Pop the next module off the stack. 39825284cc9SRobert Wilhelm NextModule = State->Stack.pop_back_val(); 399e41d7feaSDouglas Gregor } while (true); 400e41d7feaSDouglas Gregor } 401e97cd90aSDouglas Gregor 402e97cd90aSDouglas Gregor returnVisitState(State); 403d44252ecSDouglas Gregor } 404d44252ecSDouglas Gregor 4057029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 4067029ce1aSDouglas Gregor off_t ExpectedSize, 4077029ce1aSDouglas Gregor time_t ExpectedModTime, 4087029ce1aSDouglas Gregor const FileEntry *&File) { 4093bd6d7fbSRichard Smith if (FileName == "-") { 4103bd6d7fbSRichard Smith File = nullptr; 4113bd6d7fbSRichard Smith return false; 4123bd6d7fbSRichard Smith } 4133bd6d7fbSRichard Smith 41405f82ba2SBen Langmuir // Open the file immediately to ensure there is no race between stat'ing and 41505f82ba2SBen Langmuir // opening the file. 41605f82ba2SBen Langmuir File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false); 4173bd6d7fbSRichard Smith if (!File) 4187029ce1aSDouglas Gregor return false; 4197029ce1aSDouglas Gregor 4207029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 421027731d7SBen Langmuir (ExpectedModTime && ExpectedModTime != File->getModificationTime())) 422027731d7SBen Langmuir // Do not destroy File, as it may be referenced. If we need to rebuild it, 423027731d7SBen Langmuir // it will be destroyed by removeModules. 4247029ce1aSDouglas Gregor return true; 4257029ce1aSDouglas Gregor 4267029ce1aSDouglas Gregor return false; 4277029ce1aSDouglas Gregor } 4287029ce1aSDouglas Gregor 4299d7c1a2aSDouglas Gregor #ifndef NDEBUG 4309d7c1a2aSDouglas Gregor namespace llvm { 4319d7c1a2aSDouglas Gregor template<> 4329d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 4332931d171STim Shen typedef ModuleFile *NodeRef; 434de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 43596a06e0eSDuncan P. N. Exon Smith typedef pointer_iterator<ModuleManager::ModuleConstIterator> nodes_iterator; 4369d7c1a2aSDouglas Gregor 437f2187ed3STim Shen static ChildIteratorType child_begin(NodeRef Node) { 4389d7c1a2aSDouglas Gregor return Node->Imports.begin(); 4399d7c1a2aSDouglas Gregor } 4409d7c1a2aSDouglas Gregor 441f2187ed3STim Shen static ChildIteratorType child_end(NodeRef Node) { 4429d7c1a2aSDouglas Gregor return Node->Imports.end(); 4439d7c1a2aSDouglas Gregor } 4449d7c1a2aSDouglas Gregor 4459d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 44696a06e0eSDuncan P. N. Exon Smith return nodes_iterator(Manager.begin()); 4479d7c1a2aSDouglas Gregor } 4489d7c1a2aSDouglas Gregor 4499d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 45096a06e0eSDuncan P. N. Exon Smith return nodes_iterator(Manager.end()); 4519d7c1a2aSDouglas Gregor } 4529d7c1a2aSDouglas Gregor }; 4539d7c1a2aSDouglas Gregor 4549d7c1a2aSDouglas Gregor template<> 4559d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4569d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4579d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4589d7c1a2aSDouglas Gregor 4599d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4609d7c1a2aSDouglas Gregor return true; 4619d7c1a2aSDouglas Gregor } 4629d7c1a2aSDouglas Gregor 463de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 464beee15e7SBen Langmuir return M->ModuleName; 4659d7c1a2aSDouglas Gregor } 4669d7c1a2aSDouglas Gregor }; 467ab9db510SAlexander Kornienko } 4689d7c1a2aSDouglas Gregor 4699d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4709d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4719d7c1a2aSDouglas Gregor } 4729d7c1a2aSDouglas Gregor #endif 473