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 557029ce1aSDouglas Gregor ModuleManager::AddModuleResult 56d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type, 576fb03aeaSDouglas Gregor SourceLocation ImportLoc, ModuleFile *ImportedBy, 587029ce1aSDouglas Gregor unsigned Generation, 597029ce1aSDouglas Gregor off_t ExpectedSize, time_t ExpectedModTime, 60487ea14aSBen Langmuir ASTFileSignature ExpectedSignature, 6170a1b816SBen Langmuir ASTFileSignatureReader ReadSignature, 627029ce1aSDouglas Gregor ModuleFile *&Module, 637029ce1aSDouglas Gregor std::string &ErrorStr) { 64a13603a2SCraig Topper Module = nullptr; 657029ce1aSDouglas Gregor 667029ce1aSDouglas Gregor // Look for the file entry. This only fails if the expected size or 677029ce1aSDouglas Gregor // modification time differ. 687029ce1aSDouglas Gregor const FileEntry *Entry; 6911f2a477SManman Ren if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) { 705b390756SRichard Smith // If we're not expecting to pull this file out of the module cache, it 715b390756SRichard Smith // might have a different mtime due to being moved across filesystems in 725b390756SRichard Smith // a distributed build. The size must still match, though. (As must the 735b390756SRichard Smith // contents, but we can't check that.) 745b390756SRichard Smith ExpectedModTime = 0; 755b390756SRichard Smith } 76c27d0d5eSEli Friedman if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { 77c27d0d5eSEli Friedman ErrorStr = "module file out of date"; 787029ce1aSDouglas Gregor return OutOfDate; 79c27d0d5eSEli Friedman } 807029ce1aSDouglas Gregor 81d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 82c27d0d5eSEli Friedman ErrorStr = "module file not found"; 837029ce1aSDouglas Gregor return Missing; 84d44252ecSDouglas Gregor } 85d44252ecSDouglas Gregor 86d44252ecSDouglas Gregor // Check whether we already loaded this module, before 873b99db55SRichard Smith ModuleFile *ModuleEntry = Modules[Entry]; 88d44252ecSDouglas Gregor bool NewModule = false; 89d44252ecSDouglas Gregor if (!ModuleEntry) { 90d44252ecSDouglas Gregor // Allocate a new module. 91d44252ecSDouglas Gregor NewModule = true; 923b99db55SRichard Smith ModuleEntry = new ModuleFile(Type, Generation); 933b99db55SRichard Smith ModuleEntry->Index = Chain.size(); 943b99db55SRichard Smith ModuleEntry->FileName = FileName.str(); 953b99db55SRichard Smith ModuleEntry->File = Entry; 963b99db55SRichard Smith ModuleEntry->ImportLoc = ImportLoc; 973b99db55SRichard Smith ModuleEntry->InputFilesValidationTimestamp = 0; 98d44252ecSDouglas Gregor 993b99db55SRichard Smith if (ModuleEntry->Kind == MK_ImplicitModule) { 1003b99db55SRichard Smith std::string TimestampFilename = ModuleEntry->getTimestampFilename(); 101c8130a74SBen Langmuir vfs::Status Status; 102f430da4dSDmitri Gribenko // A cached stat value would be fine as well. 103f430da4dSDmitri Gribenko if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) 1043b99db55SRichard Smith ModuleEntry->InputFilesValidationTimestamp = 105*ac71c8e2SPavel Labath llvm::sys::toTimeT(Status.getLastModificationTime()); 106f430da4dSDmitri Gribenko } 107f430da4dSDmitri Gribenko 108d44252ecSDouglas Gregor // Load the contents of the module 1095cd06f26SRafael Espindola if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) { 110d44252ecSDouglas Gregor // The buffer was already provided for us. 1113b99db55SRichard Smith ModuleEntry->Buffer = std::move(Buffer); 112d44252ecSDouglas Gregor } else { 113d44252ecSDouglas Gregor // Open the AST file. 114a885796dSBenjamin Kramer llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf( 115a885796dSBenjamin Kramer (std::error_code())); 116d44252ecSDouglas Gregor if (FileName == "-") { 117a885796dSBenjamin Kramer Buf = llvm::MemoryBuffer::getSTDIN(); 1189801b253SBen Langmuir } else { 1199801b253SBen Langmuir // Leave the FileEntry open so if it gets read again by another 1209801b253SBen Langmuir // ModuleManager it must be the same underlying file. 1219801b253SBen Langmuir // FIXME: Because FileManager::getFile() doesn't guarantee that it will 1229801b253SBen Langmuir // give us an open file, this may not be 100% reliable. 1233b99db55SRichard Smith Buf = FileMgr.getBufferForFile(ModuleEntry->File, 124a885796dSBenjamin Kramer /*IsVolatile=*/false, 125a885796dSBenjamin Kramer /*ShouldClose=*/false); 1269801b253SBen Langmuir } 127d44252ecSDouglas Gregor 128a885796dSBenjamin Kramer if (!Buf) { 129a885796dSBenjamin Kramer ErrorStr = Buf.getError().message(); 1303b99db55SRichard Smith delete ModuleEntry; 1317029ce1aSDouglas Gregor return Missing; 132d44252ecSDouglas Gregor } 133d44252ecSDouglas Gregor 1343b99db55SRichard Smith ModuleEntry->Buffer = std::move(*Buf); 135a885796dSBenjamin Kramer } 136a885796dSBenjamin Kramer 137bb165fb0SAdrian Prantl // Initialize the stream. 13877c89b69SPeter Collingbourne ModuleEntry->Data = PCHContainerRdr.ExtractPCH(*ModuleEntry->Buffer); 139ed982584SBen Langmuir } 140487ea14aSBen Langmuir 141487ea14aSBen Langmuir if (ExpectedSignature) { 1423b99db55SRichard Smith // If we've not read the control block yet, read the signature eagerly now 1433b99db55SRichard Smith // so that we can check it. 1443b99db55SRichard Smith if (!ModuleEntry->Signature) 14577c89b69SPeter Collingbourne ModuleEntry->Signature = ReadSignature(ModuleEntry->Data); 146ed982584SBen Langmuir 147ed982584SBen Langmuir if (ModuleEntry->Signature != ExpectedSignature) { 148ed982584SBen Langmuir ErrorStr = ModuleEntry->Signature ? "signature mismatch" 149487ea14aSBen Langmuir : "could not read module signature"; 150487ea14aSBen Langmuir 1513b99db55SRichard Smith if (NewModule) 152ed982584SBen Langmuir delete ModuleEntry; 153ed982584SBen Langmuir return OutOfDate; 154487ea14aSBen Langmuir } 1557029ce1aSDouglas Gregor } 156d44252ecSDouglas Gregor 157d44252ecSDouglas Gregor if (ImportedBy) { 158d44252ecSDouglas Gregor ModuleEntry->ImportedBy.insert(ImportedBy); 159d44252ecSDouglas Gregor ImportedBy->Imports.insert(ModuleEntry); 160d44252ecSDouglas Gregor } else { 1616fb03aeaSDouglas Gregor if (!ModuleEntry->DirectlyImported) 1626fb03aeaSDouglas Gregor ModuleEntry->ImportLoc = ImportLoc; 1636fb03aeaSDouglas Gregor 164d44252ecSDouglas Gregor ModuleEntry->DirectlyImported = true; 165d44252ecSDouglas Gregor } 166d44252ecSDouglas Gregor 1677029ce1aSDouglas Gregor Module = ModuleEntry; 1683b99db55SRichard Smith 1693b99db55SRichard Smith if (!NewModule) 1703b99db55SRichard Smith return AlreadyLoaded; 1713b99db55SRichard Smith 1723b99db55SRichard Smith assert(!Modules[Entry] && "module loaded twice"); 1733b99db55SRichard Smith Modules[Entry] = ModuleEntry; 1743b99db55SRichard Smith 1753b99db55SRichard Smith Chain.push_back(ModuleEntry); 1763b99db55SRichard Smith if (!ModuleEntry->isModule()) 1773b99db55SRichard Smith PCHChain.push_back(ModuleEntry); 1783b99db55SRichard Smith if (!ImportedBy) 1793b99db55SRichard Smith Roots.push_back(ModuleEntry); 1803b99db55SRichard Smith 1813b99db55SRichard Smith return NewlyLoaded; 182d44252ecSDouglas Gregor } 183d44252ecSDouglas Gregor 1849801b253SBen Langmuir void ModuleManager::removeModules( 1859801b253SBen Langmuir ModuleIterator first, ModuleIterator last, 1869801b253SBen Langmuir llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully, 1877029ce1aSDouglas Gregor ModuleMap *modMap) { 188188dbef2SDouglas Gregor if (first == last) 189188dbef2SDouglas Gregor return; 190188dbef2SDouglas Gregor 191a50dbb20SBen Langmuir // Explicitly clear VisitOrder since we might not notice it is stale. 192a50dbb20SBen Langmuir VisitOrder.clear(); 193a50dbb20SBen Langmuir 194188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 195188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); 196188dbef2SDouglas Gregor 1979eff8b14SManuel Klimek auto IsVictim = [&](ModuleFile *MF) { 1989eff8b14SManuel Klimek return victimSet.count(MF); 1999eff8b14SManuel Klimek }; 200188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 201188dbef2SDouglas Gregor for (unsigned i = 0, n = Chain.size(); i != n; ++i) { 2029eff8b14SManuel Klimek Chain[i]->ImportedBy.remove_if(IsVictim); 203188dbef2SDouglas Gregor } 2049eff8b14SManuel Klimek Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim), 2059eff8b14SManuel Klimek Roots.end()); 206188dbef2SDouglas Gregor 20716fe4d17SRichard Smith // Remove the modules from the PCH chain. 20816fe4d17SRichard Smith for (auto I = first; I != last; ++I) { 20916fe4d17SRichard Smith if (!(*I)->isModule()) { 21016fe4d17SRichard Smith PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I), 21116fe4d17SRichard Smith PCHChain.end()); 21216fe4d17SRichard Smith break; 21316fe4d17SRichard Smith } 21416fe4d17SRichard Smith } 21516fe4d17SRichard Smith 216188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 217188dbef2SDouglas Gregor for (ModuleIterator victim = first; victim != last; ++victim) { 218caea1318SBen Langmuir Modules.erase((*victim)->File); 219ca39214fSBen Langmuir 2207029ce1aSDouglas Gregor if (modMap) { 221beee15e7SBen Langmuir StringRef ModuleName = (*victim)->ModuleName; 2227029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 223a13603a2SCraig Topper mod->setASTFile(nullptr); 2247029ce1aSDouglas Gregor } 2257029ce1aSDouglas Gregor } 2264f05478cSBen Langmuir 2279801b253SBen Langmuir // Files that didn't make it through ReadASTCore successfully will be 2289801b253SBen Langmuir // rebuilt (or there was an error). Invalidate them so that we can load the 2299801b253SBen Langmuir // new files that will be renamed over the old ones. 2309801b253SBen Langmuir if (LoadedSuccessfully.count(*victim) == 0) 2314f05478cSBen Langmuir FileMgr.invalidateCache((*victim)->File); 2324f05478cSBen Langmuir 233188dbef2SDouglas Gregor delete *victim; 234188dbef2SDouglas Gregor } 235188dbef2SDouglas Gregor 236188dbef2SDouglas Gregor // Remove the modules from the chain. 237188dbef2SDouglas Gregor Chain.erase(first, last); 238188dbef2SDouglas Gregor } 239188dbef2SDouglas Gregor 2405cd06f26SRafael Espindola void 2415cd06f26SRafael Espindola ModuleManager::addInMemoryBuffer(StringRef FileName, 2425cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> Buffer) { 243d44252ecSDouglas Gregor 2445cd06f26SRafael Espindola const FileEntry *Entry = 2455cd06f26SRafael Espindola FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); 2465cd06f26SRafael Espindola InMemoryBuffers[Entry] = std::move(Buffer); 247d44252ecSDouglas Gregor } 248d44252ecSDouglas Gregor 249e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 250e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 251e97cd90aSDouglas Gregor if (FirstVisitState) { 252e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 253e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 254a13603a2SCraig Topper Result->NextState = nullptr; 255e97cd90aSDouglas Gregor return Result; 256e97cd90aSDouglas Gregor } 257e97cd90aSDouglas Gregor 258e97cd90aSDouglas Gregor // Allocate and return a new state. 259e97cd90aSDouglas Gregor return new VisitState(size()); 260e97cd90aSDouglas Gregor } 261e97cd90aSDouglas Gregor 262e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 263a13603a2SCraig Topper assert(State->NextState == nullptr && "Visited state is in list?"); 264e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 265e97cd90aSDouglas Gregor FirstVisitState = State; 266e97cd90aSDouglas Gregor } 267e97cd90aSDouglas Gregor 2687211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 2697211ac15SDouglas Gregor GlobalIndex = Index; 270603cd869SDouglas Gregor if (!GlobalIndex) { 271603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 272603cd869SDouglas Gregor return; 2737029ce1aSDouglas Gregor } 274603cd869SDouglas Gregor 275603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 276603cd869SDouglas Gregor // loaded. 277603cd869SDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 278603cd869SDouglas Gregor if (!GlobalIndex->loadedModuleFile(Chain[I])) { 279603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(Chain[I]); 280603cd869SDouglas Gregor } 281603cd869SDouglas Gregor } 282603cd869SDouglas Gregor } 283603cd869SDouglas Gregor 284603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 285603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 286603cd869SDouglas Gregor return; 287603cd869SDouglas Gregor 288603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 2897211ac15SDouglas Gregor } 2907211ac15SDouglas Gregor 291bb165fb0SAdrian Prantl ModuleManager::ModuleManager(FileManager &FileMgr, 292fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr) 293fb2398d0SAdrian Prantl : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(), 294bb165fb0SAdrian Prantl FirstVisitState(nullptr) {} 295d44252ecSDouglas Gregor 296d44252ecSDouglas Gregor ModuleManager::~ModuleManager() { 297d44252ecSDouglas Gregor for (unsigned i = 0, e = Chain.size(); i != e; ++i) 298d44252ecSDouglas Gregor delete Chain[e - i - 1]; 299e97cd90aSDouglas Gregor delete FirstVisitState; 300d44252ecSDouglas Gregor } 301d44252ecSDouglas Gregor 3029a9efbafSBenjamin Kramer void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor, 3034dd9b43cSCraig Topper llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) { 3047211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 305e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 306d44252ecSDouglas Gregor unsigned N = size(); 307e41d7feaSDouglas Gregor VisitOrder.clear(); 308e41d7feaSDouglas Gregor VisitOrder.reserve(N); 309d44252ecSDouglas Gregor 310d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 311d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 312d44252ecSDouglas Gregor // to seed the queue. 313de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 314d44252ecSDouglas Gregor Queue.reserve(N); 315bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 316a7c535b3SRichard Smith UnusedIncomingEdges.resize(size()); 317f7e3609fSDavid Majnemer for (ModuleFile *M : llvm::reverse(*this)) { 318f7e3609fSDavid Majnemer unsigned Size = M->ImportedBy.size(); 319f7e3609fSDavid Majnemer UnusedIncomingEdges[M->Index] = Size; 320a7c535b3SRichard Smith if (!Size) 321f7e3609fSDavid Majnemer Queue.push_back(M); 322d44252ecSDouglas Gregor } 323d44252ecSDouglas Gregor 324e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 325e41d7feaSDouglas Gregor // of its dependencies. 326a7c535b3SRichard Smith while (!Queue.empty()) { 327a7c535b3SRichard Smith ModuleFile *CurrentModule = Queue.pop_back_val(); 328e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 329d44252ecSDouglas Gregor 330d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 331d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 332a7c535b3SRichard Smith for (auto M = CurrentModule->Imports.rbegin(), 333a7c535b3SRichard Smith MEnd = CurrentModule->Imports.rend(); 334d44252ecSDouglas Gregor M != MEnd; ++M) { 335d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 336d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 337d44252ecSDouglas Gregor // that depends on this particular module, push it into the 338d44252ecSDouglas Gregor // queue to be visited. 339bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 340d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 341d44252ecSDouglas Gregor Queue.push_back(*M); 342d44252ecSDouglas Gregor } 343d44252ecSDouglas Gregor } 344e41d7feaSDouglas Gregor 345e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 3467211ac15SDouglas Gregor 347e97cd90aSDouglas Gregor delete FirstVisitState; 348a13603a2SCraig Topper FirstVisitState = nullptr; 349e41d7feaSDouglas Gregor } 350e41d7feaSDouglas Gregor 351e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 352e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 353e41d7feaSDouglas Gregor 3547211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 3557211ac15SDouglas Gregor // module index, mark every module file in common with the global module 3567211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 3577211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 3587211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 3597211ac15SDouglas Gregor { 3607211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 3617029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 362e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 3637211ac15SDouglas Gregor } 3647211ac15SDouglas Gregor } 3657211ac15SDouglas Gregor 366e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 367e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 368e41d7feaSDouglas Gregor // Should we skip this module file? 369e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 370e41d7feaSDouglas Gregor continue; 371e41d7feaSDouglas Gregor 372e41d7feaSDouglas Gregor // Visit the module. 373e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 374e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 3759a9efbafSBenjamin Kramer if (!Visitor(*CurrentModule)) 376e41d7feaSDouglas Gregor continue; 377e41d7feaSDouglas Gregor 378e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 379e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 380e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 381e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 382e41d7feaSDouglas Gregor do { 383e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 384e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 385e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 386e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 387e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 388e41d7feaSDouglas Gregor M != MEnd; ++M) { 389e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 390e97cd90aSDouglas Gregor State->Stack.push_back(*M); 391e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 392e41d7feaSDouglas Gregor } 393e41d7feaSDouglas Gregor } 394e41d7feaSDouglas Gregor 395e97cd90aSDouglas Gregor if (State->Stack.empty()) 396e41d7feaSDouglas Gregor break; 397e41d7feaSDouglas Gregor 398e41d7feaSDouglas Gregor // Pop the next module off the stack. 39925284cc9SRobert Wilhelm NextModule = State->Stack.pop_back_val(); 400e41d7feaSDouglas Gregor } while (true); 401e41d7feaSDouglas Gregor } 402e97cd90aSDouglas Gregor 403e97cd90aSDouglas Gregor returnVisitState(State); 404d44252ecSDouglas Gregor } 405d44252ecSDouglas Gregor 4067029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 4077029ce1aSDouglas Gregor off_t ExpectedSize, 4087029ce1aSDouglas Gregor time_t ExpectedModTime, 4097029ce1aSDouglas Gregor const FileEntry *&File) { 4103bd6d7fbSRichard Smith if (FileName == "-") { 4113bd6d7fbSRichard Smith File = nullptr; 4123bd6d7fbSRichard Smith return false; 4133bd6d7fbSRichard Smith } 4143bd6d7fbSRichard Smith 41505f82ba2SBen Langmuir // Open the file immediately to ensure there is no race between stat'ing and 41605f82ba2SBen Langmuir // opening the file. 41705f82ba2SBen Langmuir File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false); 4183bd6d7fbSRichard Smith if (!File) 4197029ce1aSDouglas Gregor return false; 4207029ce1aSDouglas Gregor 4217029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 422027731d7SBen Langmuir (ExpectedModTime && ExpectedModTime != File->getModificationTime())) 423027731d7SBen Langmuir // Do not destroy File, as it may be referenced. If we need to rebuild it, 424027731d7SBen Langmuir // it will be destroyed by removeModules. 4257029ce1aSDouglas Gregor return true; 4267029ce1aSDouglas Gregor 4277029ce1aSDouglas Gregor return false; 4287029ce1aSDouglas Gregor } 4297029ce1aSDouglas Gregor 4309d7c1a2aSDouglas Gregor #ifndef NDEBUG 4319d7c1a2aSDouglas Gregor namespace llvm { 4329d7c1a2aSDouglas Gregor template<> 4339d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 4342931d171STim Shen typedef ModuleFile *NodeRef; 435de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 4369d7c1a2aSDouglas Gregor typedef ModuleManager::ModuleConstIterator nodes_iterator; 4379d7c1a2aSDouglas Gregor 438f2187ed3STim Shen static ChildIteratorType child_begin(NodeRef Node) { 4399d7c1a2aSDouglas Gregor return Node->Imports.begin(); 4409d7c1a2aSDouglas Gregor } 4419d7c1a2aSDouglas Gregor 442f2187ed3STim Shen static ChildIteratorType child_end(NodeRef Node) { 4439d7c1a2aSDouglas Gregor return Node->Imports.end(); 4449d7c1a2aSDouglas Gregor } 4459d7c1a2aSDouglas Gregor 4469d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 4479d7c1a2aSDouglas Gregor return Manager.begin(); 4489d7c1a2aSDouglas Gregor } 4499d7c1a2aSDouglas Gregor 4509d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 4519d7c1a2aSDouglas Gregor return Manager.end(); 4529d7c1a2aSDouglas Gregor } 4539d7c1a2aSDouglas Gregor }; 4549d7c1a2aSDouglas Gregor 4559d7c1a2aSDouglas Gregor template<> 4569d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4579d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4589d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4599d7c1a2aSDouglas Gregor 4609d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4619d7c1a2aSDouglas Gregor return true; 4629d7c1a2aSDouglas Gregor } 4639d7c1a2aSDouglas Gregor 464de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 465beee15e7SBen Langmuir return M->ModuleName; 4669d7c1a2aSDouglas Gregor } 4679d7c1a2aSDouglas Gregor }; 468ab9db510SAlexander Kornienko } 4699d7c1a2aSDouglas Gregor 4709d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4719d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4729d7c1a2aSDouglas Gregor } 4739d7c1a2aSDouglas Gregor #endif 474