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 //===----------------------------------------------------------------------===// 14bb165fb0SAdrian Prantl #include "clang/Frontend/PCHContainerOperations.h" 15beee15e7SBen Langmuir #include "clang/Lex/HeaderSearch.h" 167029ce1aSDouglas Gregor #include "clang/Lex/ModuleMap.h" 177211ac15SDouglas Gregor #include "clang/Serialization/GlobalModuleIndex.h" 18af04f98cSBenjamin Kramer #include "clang/Serialization/ModuleManager.h" 19d44252ecSDouglas Gregor #include "llvm/Support/MemoryBuffer.h" 20552c169eSRafael Espindola #include "llvm/Support/Path.h" 21d44252ecSDouglas Gregor #include "llvm/Support/raw_ostream.h" 228a8e554aSRafael Espindola #include <system_error> 23d44252ecSDouglas Gregor 249d7c1a2aSDouglas Gregor #ifndef NDEBUG 259d7c1a2aSDouglas Gregor #include "llvm/Support/GraphWriter.h" 269d7c1a2aSDouglas Gregor #endif 279d7c1a2aSDouglas Gregor 28d44252ecSDouglas Gregor using namespace clang; 29d44252ecSDouglas Gregor using namespace serialization; 30d44252ecSDouglas Gregor 31de3ef502SDouglas Gregor ModuleFile *ModuleManager::lookup(StringRef Name) { 32dadd85dcSDouglas Gregor const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, 33dadd85dcSDouglas Gregor /*cacheFailure=*/false); 34bf7fc9c5SDouglas Gregor if (Entry) 35bf7fc9c5SDouglas Gregor return lookup(Entry); 36bf7fc9c5SDouglas Gregor 37a13603a2SCraig Topper return nullptr; 38bf7fc9c5SDouglas Gregor } 39bf7fc9c5SDouglas Gregor 40bf7fc9c5SDouglas Gregor ModuleFile *ModuleManager::lookup(const FileEntry *File) { 41bf7fc9c5SDouglas Gregor llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known 42bf7fc9c5SDouglas Gregor = Modules.find(File); 43bf7fc9c5SDouglas Gregor if (Known == Modules.end()) 44a13603a2SCraig Topper return nullptr; 45bf7fc9c5SDouglas Gregor 46bf7fc9c5SDouglas Gregor return Known->second; 47d44252ecSDouglas Gregor } 48d44252ecSDouglas Gregor 495cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> 505cd06f26SRafael Espindola ModuleManager::lookupBuffer(StringRef Name) { 51dadd85dcSDouglas Gregor const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, 52dadd85dcSDouglas Gregor /*cacheFailure=*/false); 535cd06f26SRafael Espindola return std::move(InMemoryBuffers[Entry]); 54d44252ecSDouglas Gregor } 55d44252ecSDouglas Gregor 567029ce1aSDouglas Gregor ModuleManager::AddModuleResult 57d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type, 586fb03aeaSDouglas Gregor SourceLocation ImportLoc, ModuleFile *ImportedBy, 597029ce1aSDouglas Gregor unsigned Generation, 607029ce1aSDouglas Gregor off_t ExpectedSize, time_t ExpectedModTime, 61487ea14aSBen Langmuir ASTFileSignature ExpectedSignature, 6270a1b816SBen Langmuir ASTFileSignatureReader ReadSignature, 637029ce1aSDouglas Gregor ModuleFile *&Module, 647029ce1aSDouglas Gregor std::string &ErrorStr) { 65a13603a2SCraig Topper Module = nullptr; 667029ce1aSDouglas Gregor 677029ce1aSDouglas Gregor // Look for the file entry. This only fails if the expected size or 687029ce1aSDouglas Gregor // modification time differ. 697029ce1aSDouglas Gregor const FileEntry *Entry; 705b390756SRichard Smith if (Type == MK_ExplicitModule) { 715b390756SRichard Smith // If we're not expecting to pull this file out of the module cache, it 725b390756SRichard Smith // might have a different mtime due to being moved across filesystems in 735b390756SRichard Smith // a distributed build. The size must still match, though. (As must the 745b390756SRichard Smith // contents, but we can't check that.) 755b390756SRichard Smith ExpectedModTime = 0; 765b390756SRichard Smith } 77c27d0d5eSEli Friedman if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { 78c27d0d5eSEli Friedman ErrorStr = "module file out of date"; 797029ce1aSDouglas Gregor return OutOfDate; 80c27d0d5eSEli Friedman } 817029ce1aSDouglas Gregor 82d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 83c27d0d5eSEli Friedman ErrorStr = "module file not found"; 847029ce1aSDouglas Gregor return Missing; 85d44252ecSDouglas Gregor } 86d44252ecSDouglas Gregor 87d44252ecSDouglas Gregor // Check whether we already loaded this module, before 88de3ef502SDouglas Gregor ModuleFile *&ModuleEntry = Modules[Entry]; 89d44252ecSDouglas Gregor bool NewModule = false; 90d44252ecSDouglas Gregor if (!ModuleEntry) { 91d44252ecSDouglas Gregor // Allocate a new module. 924fc9f3e8SDouglas Gregor ModuleFile *New = new ModuleFile(Type, Generation); 93bdb259d2SDouglas Gregor New->Index = Chain.size(); 94d44252ecSDouglas Gregor New->FileName = FileName.str(); 95aedf7144SArgyrios Kyrtzidis New->File = Entry; 966fb03aeaSDouglas Gregor New->ImportLoc = ImportLoc; 97d44252ecSDouglas Gregor Chain.push_back(New); 9833e0f7efSRichard Smith if (!New->isModule()) 9933e0f7efSRichard Smith PCHChain.push_back(New); 1009eff8b14SManuel Klimek if (!ImportedBy) 1019eff8b14SManuel Klimek Roots.push_back(New); 102d44252ecSDouglas Gregor NewModule = true; 103d44252ecSDouglas Gregor ModuleEntry = New; 104d44252ecSDouglas Gregor 105f430da4dSDmitri Gribenko New->InputFilesValidationTimestamp = 0; 106e842a474SRichard Smith if (New->Kind == MK_ImplicitModule) { 107f430da4dSDmitri Gribenko std::string TimestampFilename = New->getTimestampFilename(); 108c8130a74SBen Langmuir vfs::Status Status; 109f430da4dSDmitri Gribenko // A cached stat value would be fine as well. 110f430da4dSDmitri Gribenko if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) 111f430da4dSDmitri Gribenko New->InputFilesValidationTimestamp = 112f430da4dSDmitri Gribenko Status.getLastModificationTime().toEpochTime(); 113f430da4dSDmitri Gribenko } 114f430da4dSDmitri Gribenko 115d44252ecSDouglas Gregor // Load the contents of the module 1165cd06f26SRafael Espindola if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) { 117d44252ecSDouglas Gregor // The buffer was already provided for us. 1185cd06f26SRafael Espindola New->Buffer = std::move(Buffer); 119d44252ecSDouglas Gregor } else { 120d44252ecSDouglas Gregor // Open the AST file. 121a885796dSBenjamin Kramer llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf( 122a885796dSBenjamin Kramer (std::error_code())); 123d44252ecSDouglas Gregor if (FileName == "-") { 124a885796dSBenjamin Kramer Buf = llvm::MemoryBuffer::getSTDIN(); 1259801b253SBen Langmuir } else { 1269801b253SBen Langmuir // Leave the FileEntry open so if it gets read again by another 1279801b253SBen Langmuir // ModuleManager it must be the same underlying file. 1289801b253SBen Langmuir // FIXME: Because FileManager::getFile() doesn't guarantee that it will 1299801b253SBen Langmuir // give us an open file, this may not be 100% reliable. 130a885796dSBenjamin Kramer Buf = FileMgr.getBufferForFile(New->File, 131a885796dSBenjamin Kramer /*IsVolatile=*/false, 132a885796dSBenjamin Kramer /*ShouldClose=*/false); 1339801b253SBen Langmuir } 134d44252ecSDouglas Gregor 135a885796dSBenjamin Kramer if (!Buf) { 136a885796dSBenjamin Kramer ErrorStr = Buf.getError().message(); 1377029ce1aSDouglas Gregor return Missing; 138d44252ecSDouglas Gregor } 139d44252ecSDouglas Gregor 140a885796dSBenjamin Kramer New->Buffer = std::move(*Buf); 141a885796dSBenjamin Kramer } 142a885796dSBenjamin Kramer 143bb165fb0SAdrian Prantl // Initialize the stream. 144fb2398d0SAdrian Prantl PCHContainerRdr.ExtractPCH(New->Buffer->getMemBufferRef(), New->StreamFile); 145ed982584SBen Langmuir } 146487ea14aSBen Langmuir 147487ea14aSBen Langmuir if (ExpectedSignature) { 148ed982584SBen Langmuir if (NewModule) 149ed982584SBen Langmuir ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile); 150ed982584SBen Langmuir else 151ed982584SBen Langmuir assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile)); 152ed982584SBen Langmuir 153ed982584SBen Langmuir if (ModuleEntry->Signature != ExpectedSignature) { 154ed982584SBen Langmuir ErrorStr = ModuleEntry->Signature ? "signature mismatch" 155487ea14aSBen Langmuir : "could not read module signature"; 156487ea14aSBen Langmuir 157ed982584SBen Langmuir if (NewModule) { 158487ea14aSBen Langmuir // Remove the module file immediately, since removeModules might try to 159487ea14aSBen Langmuir // invalidate the file cache for Entry, and that is not safe if this 160487ea14aSBen Langmuir // module is *itself* up to date, but has an out-of-date importer. 161487ea14aSBen Langmuir Modules.erase(Entry); 1629eff8b14SManuel Klimek assert(Chain.back() == ModuleEntry); 163487ea14aSBen Langmuir Chain.pop_back(); 16433e0f7efSRichard Smith if (!ModuleEntry->isModule()) 16533e0f7efSRichard Smith PCHChain.pop_back(); 1669eff8b14SManuel Klimek if (Roots.back() == ModuleEntry) 1679eff8b14SManuel Klimek Roots.pop_back(); 1689eff8b14SManuel Klimek else 1699eff8b14SManuel Klimek assert(ImportedBy); 170ed982584SBen Langmuir delete ModuleEntry; 171487ea14aSBen Langmuir } 172ed982584SBen Langmuir return OutOfDate; 173487ea14aSBen Langmuir } 1747029ce1aSDouglas Gregor } 175d44252ecSDouglas Gregor 176d44252ecSDouglas Gregor if (ImportedBy) { 177d44252ecSDouglas Gregor ModuleEntry->ImportedBy.insert(ImportedBy); 178d44252ecSDouglas Gregor ImportedBy->Imports.insert(ModuleEntry); 179d44252ecSDouglas Gregor } else { 1806fb03aeaSDouglas Gregor if (!ModuleEntry->DirectlyImported) 1816fb03aeaSDouglas Gregor ModuleEntry->ImportLoc = ImportLoc; 1826fb03aeaSDouglas Gregor 183d44252ecSDouglas Gregor ModuleEntry->DirectlyImported = true; 184d44252ecSDouglas Gregor } 185d44252ecSDouglas Gregor 1867029ce1aSDouglas Gregor Module = ModuleEntry; 1877029ce1aSDouglas Gregor return NewModule? NewlyLoaded : AlreadyLoaded; 188d44252ecSDouglas Gregor } 189d44252ecSDouglas Gregor 1909801b253SBen Langmuir void ModuleManager::removeModules( 1919801b253SBen Langmuir ModuleIterator first, ModuleIterator last, 1929801b253SBen Langmuir llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully, 1937029ce1aSDouglas Gregor ModuleMap *modMap) { 194188dbef2SDouglas Gregor if (first == last) 195188dbef2SDouglas Gregor return; 196188dbef2SDouglas Gregor 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. 201188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, 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. 207188dbef2SDouglas Gregor for (unsigned i = 0, n = Chain.size(); i != n; ++i) { 2089eff8b14SManuel Klimek Chain[i]->ImportedBy.remove_if(IsVictim); 209188dbef2SDouglas Gregor } 2109eff8b14SManuel Klimek Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim), 2119eff8b14SManuel Klimek Roots.end()); 212188dbef2SDouglas Gregor 21316fe4d17SRichard Smith // Remove the modules from the PCH chain. 21416fe4d17SRichard Smith for (auto I = first; I != last; ++I) { 21516fe4d17SRichard Smith if (!(*I)->isModule()) { 21616fe4d17SRichard Smith PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I), 21716fe4d17SRichard Smith PCHChain.end()); 21816fe4d17SRichard Smith break; 21916fe4d17SRichard Smith } 22016fe4d17SRichard Smith } 22116fe4d17SRichard Smith 222188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 223188dbef2SDouglas Gregor for (ModuleIterator victim = first; victim != last; ++victim) { 224caea1318SBen Langmuir Modules.erase((*victim)->File); 225ca39214fSBen Langmuir 2267029ce1aSDouglas Gregor if (modMap) { 227beee15e7SBen Langmuir StringRef ModuleName = (*victim)->ModuleName; 2287029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 229a13603a2SCraig Topper mod->setASTFile(nullptr); 2307029ce1aSDouglas Gregor } 2317029ce1aSDouglas Gregor } 2324f05478cSBen Langmuir 2339801b253SBen Langmuir // Files that didn't make it through ReadASTCore successfully will be 2349801b253SBen Langmuir // rebuilt (or there was an error). Invalidate them so that we can load the 2359801b253SBen Langmuir // new files that will be renamed over the old ones. 2369801b253SBen Langmuir if (LoadedSuccessfully.count(*victim) == 0) 2374f05478cSBen Langmuir FileMgr.invalidateCache((*victim)->File); 2384f05478cSBen Langmuir 239188dbef2SDouglas Gregor delete *victim; 240188dbef2SDouglas Gregor } 241188dbef2SDouglas Gregor 242188dbef2SDouglas Gregor // Remove the modules from the chain. 243188dbef2SDouglas Gregor Chain.erase(first, last); 244188dbef2SDouglas Gregor } 245188dbef2SDouglas Gregor 2465cd06f26SRafael Espindola void 2475cd06f26SRafael Espindola ModuleManager::addInMemoryBuffer(StringRef FileName, 2485cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> Buffer) { 249d44252ecSDouglas Gregor 2505cd06f26SRafael Espindola const FileEntry *Entry = 2515cd06f26SRafael Espindola FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); 2525cd06f26SRafael Espindola InMemoryBuffers[Entry] = std::move(Buffer); 253d44252ecSDouglas Gregor } 254d44252ecSDouglas Gregor 255e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 256e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 257e97cd90aSDouglas Gregor if (FirstVisitState) { 258e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 259e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 260a13603a2SCraig Topper Result->NextState = nullptr; 261e97cd90aSDouglas Gregor return Result; 262e97cd90aSDouglas Gregor } 263e97cd90aSDouglas Gregor 264e97cd90aSDouglas Gregor // Allocate and return a new state. 265e97cd90aSDouglas Gregor return new VisitState(size()); 266e97cd90aSDouglas Gregor } 267e97cd90aSDouglas Gregor 268e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 269a13603a2SCraig Topper assert(State->NextState == nullptr && "Visited state is in list?"); 270e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 271e97cd90aSDouglas Gregor FirstVisitState = State; 272e97cd90aSDouglas Gregor } 273e97cd90aSDouglas Gregor 2747211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 2757211ac15SDouglas Gregor GlobalIndex = Index; 276603cd869SDouglas Gregor if (!GlobalIndex) { 277603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 278603cd869SDouglas Gregor return; 2797029ce1aSDouglas Gregor } 280603cd869SDouglas Gregor 281603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 282603cd869SDouglas Gregor // loaded. 283603cd869SDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 284603cd869SDouglas Gregor if (!GlobalIndex->loadedModuleFile(Chain[I])) { 285603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(Chain[I]); 286603cd869SDouglas Gregor } 287603cd869SDouglas Gregor } 288603cd869SDouglas Gregor } 289603cd869SDouglas Gregor 290603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 291603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 292603cd869SDouglas Gregor return; 293603cd869SDouglas Gregor 294603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 2957211ac15SDouglas Gregor } 2967211ac15SDouglas Gregor 297bb165fb0SAdrian Prantl ModuleManager::ModuleManager(FileManager &FileMgr, 298fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr) 299fb2398d0SAdrian Prantl : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(), 300bb165fb0SAdrian Prantl FirstVisitState(nullptr) {} 301d44252ecSDouglas Gregor 302d44252ecSDouglas Gregor ModuleManager::~ModuleManager() { 303d44252ecSDouglas Gregor for (unsigned i = 0, e = Chain.size(); i != e; ++i) 304d44252ecSDouglas Gregor delete Chain[e - i - 1]; 305e97cd90aSDouglas Gregor delete FirstVisitState; 306d44252ecSDouglas Gregor } 307d44252ecSDouglas Gregor 3089a9efbafSBenjamin Kramer void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor, 3094dd9b43cSCraig Topper llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) { 3107211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 311e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 312d44252ecSDouglas Gregor unsigned N = size(); 313e41d7feaSDouglas Gregor VisitOrder.clear(); 314e41d7feaSDouglas Gregor VisitOrder.reserve(N); 315d44252ecSDouglas Gregor 316d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 317d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 318d44252ecSDouglas Gregor // to seed the queue. 319de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 320d44252ecSDouglas Gregor Queue.reserve(N); 321bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 322a7c535b3SRichard Smith UnusedIncomingEdges.resize(size()); 323*f7e3609fSDavid Majnemer for (ModuleFile *M : llvm::reverse(*this)) { 324*f7e3609fSDavid Majnemer unsigned Size = M->ImportedBy.size(); 325*f7e3609fSDavid Majnemer UnusedIncomingEdges[M->Index] = Size; 326a7c535b3SRichard Smith if (!Size) 327*f7e3609fSDavid Majnemer Queue.push_back(M); 328d44252ecSDouglas Gregor } 329d44252ecSDouglas Gregor 330e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 331e41d7feaSDouglas Gregor // of its dependencies. 332a7c535b3SRichard Smith while (!Queue.empty()) { 333a7c535b3SRichard Smith ModuleFile *CurrentModule = Queue.pop_back_val(); 334e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 335d44252ecSDouglas Gregor 336d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 337d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 338a7c535b3SRichard Smith for (auto M = CurrentModule->Imports.rbegin(), 339a7c535b3SRichard Smith MEnd = CurrentModule->Imports.rend(); 340d44252ecSDouglas Gregor M != MEnd; ++M) { 341d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 342d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 343d44252ecSDouglas Gregor // that depends on this particular module, push it into the 344d44252ecSDouglas Gregor // queue to be visited. 345bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 346d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 347d44252ecSDouglas Gregor Queue.push_back(*M); 348d44252ecSDouglas Gregor } 349d44252ecSDouglas Gregor } 350e41d7feaSDouglas Gregor 351e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 3527211ac15SDouglas Gregor 353e97cd90aSDouglas Gregor delete FirstVisitState; 354a13603a2SCraig Topper FirstVisitState = nullptr; 355e41d7feaSDouglas Gregor } 356e41d7feaSDouglas Gregor 357e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 358e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 359e41d7feaSDouglas Gregor 3607211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 3617211ac15SDouglas Gregor // module index, mark every module file in common with the global module 3627211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 3637211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 3647211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 3657211ac15SDouglas Gregor { 3667211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 3677029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 368e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 3697211ac15SDouglas Gregor } 3707211ac15SDouglas Gregor } 3717211ac15SDouglas Gregor 372e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 373e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 374e41d7feaSDouglas Gregor // Should we skip this module file? 375e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 376e41d7feaSDouglas Gregor continue; 377e41d7feaSDouglas Gregor 378e41d7feaSDouglas Gregor // Visit the module. 379e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 380e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 3819a9efbafSBenjamin Kramer if (!Visitor(*CurrentModule)) 382e41d7feaSDouglas Gregor continue; 383e41d7feaSDouglas Gregor 384e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 385e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 386e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 387e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 388e41d7feaSDouglas Gregor do { 389e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 390e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 391e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 392e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 393e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 394e41d7feaSDouglas Gregor M != MEnd; ++M) { 395e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 396e97cd90aSDouglas Gregor State->Stack.push_back(*M); 397e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 398e41d7feaSDouglas Gregor } 399e41d7feaSDouglas Gregor } 400e41d7feaSDouglas Gregor 401e97cd90aSDouglas Gregor if (State->Stack.empty()) 402e41d7feaSDouglas Gregor break; 403e41d7feaSDouglas Gregor 404e41d7feaSDouglas Gregor // Pop the next module off the stack. 40525284cc9SRobert Wilhelm NextModule = State->Stack.pop_back_val(); 406e41d7feaSDouglas Gregor } while (true); 407e41d7feaSDouglas Gregor } 408e97cd90aSDouglas Gregor 409e97cd90aSDouglas Gregor returnVisitState(State); 410d44252ecSDouglas Gregor } 411d44252ecSDouglas Gregor 4127029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 4137029ce1aSDouglas Gregor off_t ExpectedSize, 4147029ce1aSDouglas Gregor time_t ExpectedModTime, 4157029ce1aSDouglas Gregor const FileEntry *&File) { 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); 4197029ce1aSDouglas Gregor 4207029ce1aSDouglas Gregor if (!File && FileName != "-") { 4217029ce1aSDouglas Gregor return false; 4227029ce1aSDouglas Gregor } 4237029ce1aSDouglas Gregor 4247029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 425027731d7SBen Langmuir (ExpectedModTime && ExpectedModTime != File->getModificationTime())) 426027731d7SBen Langmuir // Do not destroy File, as it may be referenced. If we need to rebuild it, 427027731d7SBen Langmuir // it will be destroyed by removeModules. 4287029ce1aSDouglas Gregor return true; 4297029ce1aSDouglas Gregor 4307029ce1aSDouglas Gregor return false; 4317029ce1aSDouglas Gregor } 4327029ce1aSDouglas Gregor 4339d7c1a2aSDouglas Gregor #ifndef NDEBUG 4349d7c1a2aSDouglas Gregor namespace llvm { 4359d7c1a2aSDouglas Gregor template<> 4369d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 437de3ef502SDouglas Gregor typedef ModuleFile NodeType; 438de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 4399d7c1a2aSDouglas Gregor typedef ModuleManager::ModuleConstIterator nodes_iterator; 4409d7c1a2aSDouglas Gregor 4419d7c1a2aSDouglas Gregor static ChildIteratorType child_begin(NodeType *Node) { 4429d7c1a2aSDouglas Gregor return Node->Imports.begin(); 4439d7c1a2aSDouglas Gregor } 4449d7c1a2aSDouglas Gregor 4459d7c1a2aSDouglas Gregor static ChildIteratorType child_end(NodeType *Node) { 4469d7c1a2aSDouglas Gregor return Node->Imports.end(); 4479d7c1a2aSDouglas Gregor } 4489d7c1a2aSDouglas Gregor 4499d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 4509d7c1a2aSDouglas Gregor return Manager.begin(); 4519d7c1a2aSDouglas Gregor } 4529d7c1a2aSDouglas Gregor 4539d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 4549d7c1a2aSDouglas Gregor return Manager.end(); 4559d7c1a2aSDouglas Gregor } 4569d7c1a2aSDouglas Gregor }; 4579d7c1a2aSDouglas Gregor 4589d7c1a2aSDouglas Gregor template<> 4599d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4609d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4619d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4629d7c1a2aSDouglas Gregor 4639d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4649d7c1a2aSDouglas Gregor return true; 4659d7c1a2aSDouglas Gregor } 4669d7c1a2aSDouglas Gregor 467de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 468beee15e7SBen Langmuir return M->ModuleName; 4699d7c1a2aSDouglas Gregor } 4709d7c1a2aSDouglas Gregor }; 471ab9db510SAlexander Kornienko } 4729d7c1a2aSDouglas Gregor 4739d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4749d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4759d7c1a2aSDouglas Gregor } 4769d7c1a2aSDouglas Gregor #endif 477