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 6626308a68SDuncan P. N. Exon Smith static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy, 6726308a68SDuncan P. N. Exon Smith SourceLocation ImportLoc) { 6826308a68SDuncan P. N. Exon Smith if (ImportedBy) { 6926308a68SDuncan P. N. Exon Smith MF.ImportedBy.insert(ImportedBy); 7026308a68SDuncan P. N. Exon Smith ImportedBy->Imports.insert(&MF); 7126308a68SDuncan P. N. Exon Smith } else { 7226308a68SDuncan P. N. Exon Smith if (!MF.DirectlyImported) 7326308a68SDuncan P. N. Exon Smith MF.ImportLoc = ImportLoc; 7426308a68SDuncan P. N. Exon Smith 7526308a68SDuncan P. N. Exon Smith MF.DirectlyImported = true; 7626308a68SDuncan P. N. Exon Smith } 7726308a68SDuncan P. N. Exon Smith } 7826308a68SDuncan P. N. Exon Smith 797029ce1aSDouglas Gregor ModuleManager::AddModuleResult 80d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type, 816fb03aeaSDouglas Gregor SourceLocation ImportLoc, ModuleFile *ImportedBy, 827029ce1aSDouglas Gregor unsigned Generation, 837029ce1aSDouglas Gregor off_t ExpectedSize, time_t ExpectedModTime, 84487ea14aSBen Langmuir ASTFileSignature ExpectedSignature, 8570a1b816SBen Langmuir ASTFileSignatureReader ReadSignature, 867029ce1aSDouglas Gregor ModuleFile *&Module, 877029ce1aSDouglas Gregor std::string &ErrorStr) { 88a13603a2SCraig Topper Module = nullptr; 897029ce1aSDouglas Gregor 907029ce1aSDouglas Gregor // Look for the file entry. This only fails if the expected size or 917029ce1aSDouglas Gregor // modification time differ. 927029ce1aSDouglas Gregor const FileEntry *Entry; 9311f2a477SManman Ren if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) { 945b390756SRichard Smith // If we're not expecting to pull this file out of the module cache, it 955b390756SRichard Smith // might have a different mtime due to being moved across filesystems in 965b390756SRichard Smith // a distributed build. The size must still match, though. (As must the 975b390756SRichard Smith // contents, but we can't check that.) 985b390756SRichard Smith ExpectedModTime = 0; 995b390756SRichard Smith } 100c27d0d5eSEli Friedman if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { 101c27d0d5eSEli Friedman ErrorStr = "module file out of date"; 1027029ce1aSDouglas Gregor return OutOfDate; 103c27d0d5eSEli Friedman } 1047029ce1aSDouglas Gregor 105d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 106c27d0d5eSEli Friedman ErrorStr = "module file not found"; 1077029ce1aSDouglas Gregor return Missing; 108d44252ecSDouglas Gregor } 109d44252ecSDouglas Gregor 110d44252ecSDouglas Gregor // Check whether we already loaded this module, before 11126308a68SDuncan P. N. Exon Smith if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) { 11226308a68SDuncan P. N. Exon Smith // Check the stored signature. 11326308a68SDuncan P. N. Exon Smith if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr)) 11426308a68SDuncan P. N. Exon Smith return OutOfDate; 11526308a68SDuncan P. N. Exon Smith 11626308a68SDuncan P. N. Exon Smith Module = ModuleEntry; 11726308a68SDuncan P. N. Exon Smith updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc); 11826308a68SDuncan P. N. Exon Smith return AlreadyLoaded; 11926308a68SDuncan P. N. Exon Smith } 12026308a68SDuncan P. N. Exon Smith 121d44252ecSDouglas Gregor // Allocate a new module. 12226308a68SDuncan P. N. Exon Smith auto NewModule = llvm::make_unique<ModuleFile>(Type, Generation); 123a897f7cdSDuncan P. N. Exon Smith NewModule->Index = Chain.size(); 124a897f7cdSDuncan P. N. Exon Smith NewModule->FileName = FileName.str(); 125a897f7cdSDuncan P. N. Exon Smith NewModule->File = Entry; 126a897f7cdSDuncan P. N. Exon Smith NewModule->ImportLoc = ImportLoc; 127a897f7cdSDuncan P. N. Exon Smith NewModule->InputFilesValidationTimestamp = 0; 128d44252ecSDouglas Gregor 129a897f7cdSDuncan P. N. Exon Smith if (NewModule->Kind == MK_ImplicitModule) { 130a897f7cdSDuncan P. N. Exon Smith std::string TimestampFilename = NewModule->getTimestampFilename(); 131c8130a74SBen Langmuir vfs::Status Status; 132f430da4dSDmitri Gribenko // A cached stat value would be fine as well. 133f430da4dSDmitri Gribenko if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) 134a897f7cdSDuncan P. N. Exon Smith NewModule->InputFilesValidationTimestamp = 135ac71c8e2SPavel Labath llvm::sys::toTimeT(Status.getLastModificationTime()); 136f430da4dSDmitri Gribenko } 137f430da4dSDmitri Gribenko 138d44252ecSDouglas Gregor // Load the contents of the module 1395cd06f26SRafael Espindola if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) { 140d44252ecSDouglas Gregor // The buffer was already provided for us. 141a897f7cdSDuncan P. N. Exon Smith NewModule->Buffer = std::move(Buffer); 142d44252ecSDouglas Gregor } else { 143d44252ecSDouglas Gregor // Open the AST file. 14426308a68SDuncan P. N. Exon Smith llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code())); 145d44252ecSDouglas Gregor if (FileName == "-") { 146a885796dSBenjamin Kramer Buf = llvm::MemoryBuffer::getSTDIN(); 1479801b253SBen Langmuir } else { 1489801b253SBen Langmuir // Leave the FileEntry open so if it gets read again by another 1499801b253SBen Langmuir // ModuleManager it must be the same underlying file. 1509801b253SBen Langmuir // FIXME: Because FileManager::getFile() doesn't guarantee that it will 1519801b253SBen Langmuir // give us an open file, this may not be 100% reliable. 152a897f7cdSDuncan P. N. Exon Smith Buf = FileMgr.getBufferForFile(NewModule->File, 153a885796dSBenjamin Kramer /*IsVolatile=*/false, 154a885796dSBenjamin Kramer /*ShouldClose=*/false); 1559801b253SBen Langmuir } 156d44252ecSDouglas Gregor 157a885796dSBenjamin Kramer if (!Buf) { 158a885796dSBenjamin Kramer ErrorStr = Buf.getError().message(); 1597029ce1aSDouglas Gregor return Missing; 160d44252ecSDouglas Gregor } 161d44252ecSDouglas Gregor 162a897f7cdSDuncan P. N. Exon Smith NewModule->Buffer = std::move(*Buf); 163a885796dSBenjamin Kramer } 164a885796dSBenjamin Kramer 165bb165fb0SAdrian Prantl // Initialize the stream. 166a897f7cdSDuncan P. N. Exon Smith NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer); 167487ea14aSBen Langmuir 168*688b69adSDuncan P. N. Exon Smith // Read the signature eagerly now so that we can check it. Avoid calling 169*688b69adSDuncan P. N. Exon Smith // ReadSignature unless there's something to check though. 170*688b69adSDuncan P. N. Exon Smith if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data), 171*688b69adSDuncan P. N. Exon Smith ExpectedSignature, ErrorStr)) 172ed982584SBen Langmuir return OutOfDate; 173a897f7cdSDuncan P. N. Exon Smith 17426308a68SDuncan P. N. Exon Smith // We're keeping this module. Store it everywhere. 17526308a68SDuncan P. N. Exon Smith Module = Modules[Entry] = NewModule.get(); 176d44252ecSDouglas Gregor 17726308a68SDuncan P. N. Exon Smith updateModuleImports(*NewModule, ImportedBy, ImportLoc); 1786fb03aeaSDouglas Gregor 17926308a68SDuncan P. N. Exon Smith if (!NewModule->isModule()) 18026308a68SDuncan P. N. Exon Smith PCHChain.push_back(NewModule.get()); 18126308a68SDuncan P. N. Exon Smith if (!ImportedBy) 18226308a68SDuncan P. N. Exon Smith Roots.push_back(NewModule.get()); 1833b99db55SRichard Smith 184a897f7cdSDuncan P. N. Exon Smith Chain.push_back(std::move(NewModule)); 1853b99db55SRichard Smith return NewlyLoaded; 186d44252ecSDouglas Gregor } 187d44252ecSDouglas Gregor 1889801b253SBen Langmuir void ModuleManager::removeModules( 1898e6bc197SDuncan P. N. Exon Smith ModuleIterator First, 1909801b253SBen Langmuir llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully, 1917029ce1aSDouglas Gregor ModuleMap *modMap) { 1928e6bc197SDuncan P. N. Exon Smith auto Last = end(); 1938e6bc197SDuncan P. N. Exon Smith if (First == Last) 194188dbef2SDouglas Gregor return; 195188dbef2SDouglas Gregor 1968e6bc197SDuncan P. N. Exon Smith 197a50dbb20SBen Langmuir // Explicitly clear VisitOrder since we might not notice it is stale. 198a50dbb20SBen Langmuir VisitOrder.clear(); 199a50dbb20SBen Langmuir 200188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 20196a06e0eSDuncan P. N. Exon Smith llvm::SmallPtrSet<ModuleFile *, 4> victimSet( 2028e6bc197SDuncan P. N. Exon Smith (llvm::pointer_iterator<ModuleIterator>(First)), 2038e6bc197SDuncan P. N. Exon Smith (llvm::pointer_iterator<ModuleIterator>(Last))); 204188dbef2SDouglas Gregor 2059eff8b14SManuel Klimek auto IsVictim = [&](ModuleFile *MF) { 2069eff8b14SManuel Klimek return victimSet.count(MF); 2079eff8b14SManuel Klimek }; 208188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 209073ec350SDuncan P. N. Exon Smith for (auto I = begin(); I != First; ++I) { 210073ec350SDuncan P. N. Exon Smith I->Imports.remove_if(IsVictim); 2118e6bc197SDuncan P. N. Exon Smith I->ImportedBy.remove_if(IsVictim); 212073ec350SDuncan P. N. Exon Smith } 2139eff8b14SManuel Klimek Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim), 2149eff8b14SManuel Klimek Roots.end()); 215188dbef2SDouglas Gregor 21616fe4d17SRichard Smith // Remove the modules from the PCH chain. 2178e6bc197SDuncan P. N. Exon Smith for (auto I = First; I != Last; ++I) { 21896a06e0eSDuncan P. N. Exon Smith if (!I->isModule()) { 21996a06e0eSDuncan P. N. Exon Smith PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), &*I), 22016fe4d17SRichard Smith PCHChain.end()); 22116fe4d17SRichard Smith break; 22216fe4d17SRichard Smith } 22316fe4d17SRichard Smith } 22416fe4d17SRichard Smith 225188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 2268e6bc197SDuncan P. N. Exon Smith for (ModuleIterator victim = First; victim != Last; ++victim) { 22796a06e0eSDuncan P. N. Exon Smith Modules.erase(victim->File); 228ca39214fSBen Langmuir 2297029ce1aSDouglas Gregor if (modMap) { 23096a06e0eSDuncan P. N. Exon Smith StringRef ModuleName = victim->ModuleName; 2317029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 232a13603a2SCraig Topper mod->setASTFile(nullptr); 2337029ce1aSDouglas Gregor } 2347029ce1aSDouglas Gregor } 2354f05478cSBen Langmuir 2369801b253SBen Langmuir // Files that didn't make it through ReadASTCore successfully will be 2379801b253SBen Langmuir // rebuilt (or there was an error). Invalidate them so that we can load the 2389801b253SBen Langmuir // new files that will be renamed over the old ones. 23996a06e0eSDuncan P. N. Exon Smith if (LoadedSuccessfully.count(&*victim) == 0) 24096a06e0eSDuncan P. N. Exon Smith FileMgr.invalidateCache(victim->File); 241188dbef2SDouglas Gregor } 242188dbef2SDouglas Gregor 243a897f7cdSDuncan P. N. Exon Smith // Delete the modules. 2448e6bc197SDuncan P. N. Exon Smith Chain.erase(Chain.begin() + (First - begin()), Chain.end()); 245188dbef2SDouglas Gregor } 246188dbef2SDouglas Gregor 2475cd06f26SRafael Espindola void 2485cd06f26SRafael Espindola ModuleManager::addInMemoryBuffer(StringRef FileName, 2495cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> Buffer) { 250d44252ecSDouglas Gregor 2515cd06f26SRafael Espindola const FileEntry *Entry = 2525cd06f26SRafael Espindola FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); 2535cd06f26SRafael Espindola InMemoryBuffers[Entry] = std::move(Buffer); 254d44252ecSDouglas Gregor } 255d44252ecSDouglas Gregor 256e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 257e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 258e97cd90aSDouglas Gregor if (FirstVisitState) { 259e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 260e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 261a13603a2SCraig Topper Result->NextState = nullptr; 262e97cd90aSDouglas Gregor return Result; 263e97cd90aSDouglas Gregor } 264e97cd90aSDouglas Gregor 265e97cd90aSDouglas Gregor // Allocate and return a new state. 266e97cd90aSDouglas Gregor return new VisitState(size()); 267e97cd90aSDouglas Gregor } 268e97cd90aSDouglas Gregor 269e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 270a13603a2SCraig Topper assert(State->NextState == nullptr && "Visited state is in list?"); 271e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 272e97cd90aSDouglas Gregor FirstVisitState = State; 273e97cd90aSDouglas Gregor } 274e97cd90aSDouglas Gregor 2757211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 2767211ac15SDouglas Gregor GlobalIndex = Index; 277603cd869SDouglas Gregor if (!GlobalIndex) { 278603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 279603cd869SDouglas Gregor return; 2807029ce1aSDouglas Gregor } 281603cd869SDouglas Gregor 282603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 283603cd869SDouglas Gregor // loaded. 284a897f7cdSDuncan P. N. Exon Smith for (ModuleFile &M : *this) 285a897f7cdSDuncan P. N. Exon Smith if (!GlobalIndex->loadedModuleFile(&M)) 286a897f7cdSDuncan P. N. Exon Smith ModulesInCommonWithGlobalIndex.push_back(&M); 287603cd869SDouglas Gregor } 288603cd869SDouglas Gregor 289603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 290603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 291603cd869SDouglas Gregor return; 292603cd869SDouglas Gregor 293603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 2947211ac15SDouglas Gregor } 2957211ac15SDouglas Gregor 296bb165fb0SAdrian Prantl ModuleManager::ModuleManager(FileManager &FileMgr, 297fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr) 298fb2398d0SAdrian Prantl : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(), 299bb165fb0SAdrian Prantl FirstVisitState(nullptr) {} 300d44252ecSDouglas Gregor 301a897f7cdSDuncan P. N. Exon Smith ModuleManager::~ModuleManager() { delete FirstVisitState; } 302d44252ecSDouglas Gregor 3039a9efbafSBenjamin Kramer void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor, 3044dd9b43cSCraig Topper llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) { 3057211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 306e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 307d44252ecSDouglas Gregor unsigned N = size(); 308e41d7feaSDouglas Gregor VisitOrder.clear(); 309e41d7feaSDouglas Gregor VisitOrder.reserve(N); 310d44252ecSDouglas Gregor 311d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 312d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 313d44252ecSDouglas Gregor // to seed the queue. 314de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 315d44252ecSDouglas Gregor Queue.reserve(N); 316bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 317a7c535b3SRichard Smith UnusedIncomingEdges.resize(size()); 31896a06e0eSDuncan P. N. Exon Smith for (ModuleFile &M : llvm::reverse(*this)) { 31996a06e0eSDuncan P. N. Exon Smith unsigned Size = M.ImportedBy.size(); 32096a06e0eSDuncan P. N. Exon Smith UnusedIncomingEdges[M.Index] = Size; 321a7c535b3SRichard Smith if (!Size) 32296a06e0eSDuncan P. N. Exon Smith Queue.push_back(&M); 323d44252ecSDouglas Gregor } 324d44252ecSDouglas Gregor 325e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 326e41d7feaSDouglas Gregor // of its dependencies. 327a7c535b3SRichard Smith while (!Queue.empty()) { 328a7c535b3SRichard Smith ModuleFile *CurrentModule = Queue.pop_back_val(); 329e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 330d44252ecSDouglas Gregor 331d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 332d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 333a7c535b3SRichard Smith for (auto M = CurrentModule->Imports.rbegin(), 334a7c535b3SRichard Smith MEnd = CurrentModule->Imports.rend(); 335d44252ecSDouglas Gregor M != MEnd; ++M) { 336d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 337d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 338d44252ecSDouglas Gregor // that depends on this particular module, push it into the 339d44252ecSDouglas Gregor // queue to be visited. 340bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 341d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 342d44252ecSDouglas Gregor Queue.push_back(*M); 343d44252ecSDouglas Gregor } 344d44252ecSDouglas Gregor } 345e41d7feaSDouglas Gregor 346e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 3477211ac15SDouglas Gregor 348e97cd90aSDouglas Gregor delete FirstVisitState; 349a13603a2SCraig Topper FirstVisitState = nullptr; 350e41d7feaSDouglas Gregor } 351e41d7feaSDouglas Gregor 352e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 353e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 354e41d7feaSDouglas Gregor 3557211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 3567211ac15SDouglas Gregor // module index, mark every module file in common with the global module 3577211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 3587211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 3597211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 3607211ac15SDouglas Gregor { 3617211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 3627029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 363e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 3647211ac15SDouglas Gregor } 3657211ac15SDouglas Gregor } 3667211ac15SDouglas Gregor 367e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 368e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 369e41d7feaSDouglas Gregor // Should we skip this module file? 370e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 371e41d7feaSDouglas Gregor continue; 372e41d7feaSDouglas Gregor 373e41d7feaSDouglas Gregor // Visit the module. 374e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 375e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 3769a9efbafSBenjamin Kramer if (!Visitor(*CurrentModule)) 377e41d7feaSDouglas Gregor continue; 378e41d7feaSDouglas Gregor 379e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 380e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 381e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 382e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 383e41d7feaSDouglas Gregor do { 384e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 385e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 386e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 387e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 388e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 389e41d7feaSDouglas Gregor M != MEnd; ++M) { 390e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 391e97cd90aSDouglas Gregor State->Stack.push_back(*M); 392e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 393e41d7feaSDouglas Gregor } 394e41d7feaSDouglas Gregor } 395e41d7feaSDouglas Gregor 396e97cd90aSDouglas Gregor if (State->Stack.empty()) 397e41d7feaSDouglas Gregor break; 398e41d7feaSDouglas Gregor 399e41d7feaSDouglas Gregor // Pop the next module off the stack. 40025284cc9SRobert Wilhelm NextModule = State->Stack.pop_back_val(); 401e41d7feaSDouglas Gregor } while (true); 402e41d7feaSDouglas Gregor } 403e97cd90aSDouglas Gregor 404e97cd90aSDouglas Gregor returnVisitState(State); 405d44252ecSDouglas Gregor } 406d44252ecSDouglas Gregor 4077029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 4087029ce1aSDouglas Gregor off_t ExpectedSize, 4097029ce1aSDouglas Gregor time_t ExpectedModTime, 4107029ce1aSDouglas Gregor const FileEntry *&File) { 4113bd6d7fbSRichard Smith if (FileName == "-") { 4123bd6d7fbSRichard Smith File = nullptr; 4133bd6d7fbSRichard Smith return false; 4143bd6d7fbSRichard Smith } 4153bd6d7fbSRichard Smith 41605f82ba2SBen Langmuir // Open the file immediately to ensure there is no race between stat'ing and 41705f82ba2SBen Langmuir // opening the file. 41805f82ba2SBen Langmuir File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false); 4193bd6d7fbSRichard Smith if (!File) 4207029ce1aSDouglas Gregor return false; 4217029ce1aSDouglas Gregor 4227029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 423027731d7SBen Langmuir (ExpectedModTime && ExpectedModTime != File->getModificationTime())) 424027731d7SBen Langmuir // Do not destroy File, as it may be referenced. If we need to rebuild it, 425027731d7SBen Langmuir // it will be destroyed by removeModules. 4267029ce1aSDouglas Gregor return true; 4277029ce1aSDouglas Gregor 4287029ce1aSDouglas Gregor return false; 4297029ce1aSDouglas Gregor } 4307029ce1aSDouglas Gregor 4319d7c1a2aSDouglas Gregor #ifndef NDEBUG 4329d7c1a2aSDouglas Gregor namespace llvm { 4339d7c1a2aSDouglas Gregor template<> 4349d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 4352931d171STim Shen typedef ModuleFile *NodeRef; 436de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 43796a06e0eSDuncan P. N. Exon Smith typedef pointer_iterator<ModuleManager::ModuleConstIterator> nodes_iterator; 4389d7c1a2aSDouglas Gregor 439f2187ed3STim Shen static ChildIteratorType child_begin(NodeRef Node) { 4409d7c1a2aSDouglas Gregor return Node->Imports.begin(); 4419d7c1a2aSDouglas Gregor } 4429d7c1a2aSDouglas Gregor 443f2187ed3STim Shen static ChildIteratorType child_end(NodeRef Node) { 4449d7c1a2aSDouglas Gregor return Node->Imports.end(); 4459d7c1a2aSDouglas Gregor } 4469d7c1a2aSDouglas Gregor 4479d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 44896a06e0eSDuncan P. N. Exon Smith return nodes_iterator(Manager.begin()); 4499d7c1a2aSDouglas Gregor } 4509d7c1a2aSDouglas Gregor 4519d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 45296a06e0eSDuncan P. N. Exon Smith return nodes_iterator(Manager.end()); 4539d7c1a2aSDouglas Gregor } 4549d7c1a2aSDouglas Gregor }; 4559d7c1a2aSDouglas Gregor 4569d7c1a2aSDouglas Gregor template<> 4579d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4589d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4599d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4609d7c1a2aSDouglas Gregor 4619d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4629d7c1a2aSDouglas Gregor return true; 4639d7c1a2aSDouglas Gregor } 4649d7c1a2aSDouglas Gregor 465de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 466beee15e7SBen Langmuir return M->ModuleName; 4679d7c1a2aSDouglas Gregor } 4689d7c1a2aSDouglas Gregor }; 469ab9db510SAlexander Kornienko } 4709d7c1a2aSDouglas Gregor 4719d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4729d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4739d7c1a2aSDouglas Gregor } 4749d7c1a2aSDouglas Gregor #endif 475