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); 98*33e0f7efSRichard Smith if (!New->isModule()) 99*33e0f7efSRichard 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(); 164*33e0f7efSRichard Smith if (!ModuleEntry->isModule()) 165*33e0f7efSRichard 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 197188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 198188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); 199188dbef2SDouglas Gregor 2009eff8b14SManuel Klimek auto IsVictim = [&](ModuleFile *MF) { 2019eff8b14SManuel Klimek return victimSet.count(MF); 2029eff8b14SManuel Klimek }; 203188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 204188dbef2SDouglas Gregor for (unsigned i = 0, n = Chain.size(); i != n; ++i) { 2059eff8b14SManuel Klimek Chain[i]->ImportedBy.remove_if(IsVictim); 206188dbef2SDouglas Gregor } 2079eff8b14SManuel Klimek Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim), 2089eff8b14SManuel Klimek Roots.end()); 209188dbef2SDouglas Gregor 210188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 211188dbef2SDouglas Gregor for (ModuleIterator victim = first; victim != last; ++victim) { 212caea1318SBen Langmuir Modules.erase((*victim)->File); 213ca39214fSBen Langmuir 2147029ce1aSDouglas Gregor if (modMap) { 215beee15e7SBen Langmuir StringRef ModuleName = (*victim)->ModuleName; 2167029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 217a13603a2SCraig Topper mod->setASTFile(nullptr); 2187029ce1aSDouglas Gregor } 2197029ce1aSDouglas Gregor } 2204f05478cSBen Langmuir 2219801b253SBen Langmuir // Files that didn't make it through ReadASTCore successfully will be 2229801b253SBen Langmuir // rebuilt (or there was an error). Invalidate them so that we can load the 2239801b253SBen Langmuir // new files that will be renamed over the old ones. 2249801b253SBen Langmuir if (LoadedSuccessfully.count(*victim) == 0) 2254f05478cSBen Langmuir FileMgr.invalidateCache((*victim)->File); 2264f05478cSBen Langmuir 227188dbef2SDouglas Gregor delete *victim; 228188dbef2SDouglas Gregor } 229188dbef2SDouglas Gregor 230188dbef2SDouglas Gregor // Remove the modules from the chain. 231188dbef2SDouglas Gregor Chain.erase(first, last); 232*33e0f7efSRichard Smith 233*33e0f7efSRichard Smith // Also remove them from the PCH chain. 234*33e0f7efSRichard Smith for (auto I = first; I != last; ++I) { 235*33e0f7efSRichard Smith if (!(*I)->isModule()) { 236*33e0f7efSRichard Smith PCHChain.erase(std::find(PCHChain.begin(), PCHChain.end(), *I), 237*33e0f7efSRichard Smith PCHChain.end()); 238*33e0f7efSRichard Smith break; 239*33e0f7efSRichard Smith } 240*33e0f7efSRichard Smith } 241188dbef2SDouglas Gregor } 242188dbef2SDouglas Gregor 2435cd06f26SRafael Espindola void 2445cd06f26SRafael Espindola ModuleManager::addInMemoryBuffer(StringRef FileName, 2455cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> Buffer) { 246d44252ecSDouglas Gregor 2475cd06f26SRafael Espindola const FileEntry *Entry = 2485cd06f26SRafael Espindola FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); 2495cd06f26SRafael Espindola InMemoryBuffers[Entry] = std::move(Buffer); 250d44252ecSDouglas Gregor } 251d44252ecSDouglas Gregor 2527f330cdbSRichard Smith bool ModuleManager::addKnownModuleFile(StringRef FileName) { 2537f330cdbSRichard Smith const FileEntry *File; 2547f330cdbSRichard Smith if (lookupModuleFile(FileName, 0, 0, File)) 2557f330cdbSRichard Smith return true; 2567f330cdbSRichard Smith if (!Modules.count(File)) 2577f330cdbSRichard Smith AdditionalKnownModuleFiles.insert(File); 2587f330cdbSRichard Smith return false; 2597f330cdbSRichard Smith } 2607f330cdbSRichard Smith 261e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 262e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 263e97cd90aSDouglas Gregor if (FirstVisitState) { 264e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 265e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 266a13603a2SCraig Topper Result->NextState = nullptr; 267e97cd90aSDouglas Gregor return Result; 268e97cd90aSDouglas Gregor } 269e97cd90aSDouglas Gregor 270e97cd90aSDouglas Gregor // Allocate and return a new state. 271e97cd90aSDouglas Gregor return new VisitState(size()); 272e97cd90aSDouglas Gregor } 273e97cd90aSDouglas Gregor 274e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 275a13603a2SCraig Topper assert(State->NextState == nullptr && "Visited state is in list?"); 276e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 277e97cd90aSDouglas Gregor FirstVisitState = State; 278e97cd90aSDouglas Gregor } 279e97cd90aSDouglas Gregor 2807211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 2817211ac15SDouglas Gregor GlobalIndex = Index; 282603cd869SDouglas Gregor if (!GlobalIndex) { 283603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 284603cd869SDouglas Gregor return; 2857029ce1aSDouglas Gregor } 286603cd869SDouglas Gregor 287603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 288603cd869SDouglas Gregor // loaded. 289603cd869SDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 290603cd869SDouglas Gregor if (!GlobalIndex->loadedModuleFile(Chain[I])) { 291603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(Chain[I]); 292603cd869SDouglas Gregor } 293603cd869SDouglas Gregor } 294603cd869SDouglas Gregor } 295603cd869SDouglas Gregor 296603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 2977f330cdbSRichard Smith AdditionalKnownModuleFiles.remove(MF->File); 2987f330cdbSRichard Smith 299603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 300603cd869SDouglas Gregor return; 301603cd869SDouglas Gregor 302603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 3037211ac15SDouglas Gregor } 3047211ac15SDouglas Gregor 305bb165fb0SAdrian Prantl ModuleManager::ModuleManager(FileManager &FileMgr, 306fb2398d0SAdrian Prantl const PCHContainerReader &PCHContainerRdr) 307fb2398d0SAdrian Prantl : FileMgr(FileMgr), PCHContainerRdr(PCHContainerRdr), GlobalIndex(), 308bb165fb0SAdrian Prantl FirstVisitState(nullptr) {} 309d44252ecSDouglas Gregor 310d44252ecSDouglas Gregor ModuleManager::~ModuleManager() { 311d44252ecSDouglas Gregor for (unsigned i = 0, e = Chain.size(); i != e; ++i) 312d44252ecSDouglas Gregor delete Chain[e - i - 1]; 313e97cd90aSDouglas Gregor delete FirstVisitState; 314d44252ecSDouglas Gregor } 315d44252ecSDouglas Gregor 3167211ac15SDouglas Gregor void 3177211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), 3187211ac15SDouglas Gregor void *UserData, 3194dd9b43cSCraig Topper llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) { 3207211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 321e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 322d44252ecSDouglas Gregor unsigned N = size(); 323e41d7feaSDouglas Gregor VisitOrder.clear(); 324e41d7feaSDouglas Gregor VisitOrder.reserve(N); 325d44252ecSDouglas Gregor 326d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 327d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 328d44252ecSDouglas Gregor // to seed the queue. 329de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 330d44252ecSDouglas Gregor Queue.reserve(N); 331bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 332a7c535b3SRichard Smith UnusedIncomingEdges.resize(size()); 333a7c535b3SRichard Smith for (auto M = rbegin(), MEnd = rend(); M != MEnd; ++M) { 334a7c535b3SRichard Smith unsigned Size = (*M)->ImportedBy.size(); 335a7c535b3SRichard Smith UnusedIncomingEdges[(*M)->Index] = Size; 336a7c535b3SRichard Smith if (!Size) 337d44252ecSDouglas Gregor Queue.push_back(*M); 338d44252ecSDouglas Gregor } 339d44252ecSDouglas Gregor 340e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 341e41d7feaSDouglas Gregor // of its dependencies. 342a7c535b3SRichard Smith while (!Queue.empty()) { 343a7c535b3SRichard Smith ModuleFile *CurrentModule = Queue.pop_back_val(); 344e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 345d44252ecSDouglas Gregor 346d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 347d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 348a7c535b3SRichard Smith for (auto M = CurrentModule->Imports.rbegin(), 349a7c535b3SRichard Smith MEnd = CurrentModule->Imports.rend(); 350d44252ecSDouglas Gregor M != MEnd; ++M) { 351d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 352d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 353d44252ecSDouglas Gregor // that depends on this particular module, push it into the 354d44252ecSDouglas Gregor // queue to be visited. 355bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 356d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 357d44252ecSDouglas Gregor Queue.push_back(*M); 358d44252ecSDouglas Gregor } 359d44252ecSDouglas Gregor } 360e41d7feaSDouglas Gregor 361e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 3627211ac15SDouglas Gregor 363e97cd90aSDouglas Gregor delete FirstVisitState; 364a13603a2SCraig Topper FirstVisitState = nullptr; 365e41d7feaSDouglas Gregor } 366e41d7feaSDouglas Gregor 367e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 368e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 369e41d7feaSDouglas Gregor 3707211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 3717211ac15SDouglas Gregor // module index, mark every module file in common with the global module 3727211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 3737211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 3747211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 3757211ac15SDouglas Gregor { 3767211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 3777029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 378e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 3797211ac15SDouglas Gregor } 3807211ac15SDouglas Gregor } 3817211ac15SDouglas Gregor 382e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 383e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 384e41d7feaSDouglas Gregor // Should we skip this module file? 385e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 386e41d7feaSDouglas Gregor continue; 387e41d7feaSDouglas Gregor 388e41d7feaSDouglas Gregor // Visit the module. 389e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 390e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 391e41d7feaSDouglas Gregor if (!Visitor(*CurrentModule, UserData)) 392e41d7feaSDouglas Gregor continue; 393e41d7feaSDouglas Gregor 394e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 395e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 396e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 397e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 398e41d7feaSDouglas Gregor do { 399e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 400e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 401e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 402e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 403e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 404e41d7feaSDouglas Gregor M != MEnd; ++M) { 405e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 406e97cd90aSDouglas Gregor State->Stack.push_back(*M); 407e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 408e41d7feaSDouglas Gregor } 409e41d7feaSDouglas Gregor } 410e41d7feaSDouglas Gregor 411e97cd90aSDouglas Gregor if (State->Stack.empty()) 412e41d7feaSDouglas Gregor break; 413e41d7feaSDouglas Gregor 414e41d7feaSDouglas Gregor // Pop the next module off the stack. 41525284cc9SRobert Wilhelm NextModule = State->Stack.pop_back_val(); 416e41d7feaSDouglas Gregor } while (true); 417e41d7feaSDouglas Gregor } 418e97cd90aSDouglas Gregor 419e97cd90aSDouglas Gregor returnVisitState(State); 420d44252ecSDouglas Gregor } 421d44252ecSDouglas Gregor 4229eff8b14SManuel Klimek static void markVisitedDepthFirst(ModuleFile &M, 423bdb259d2SDouglas Gregor SmallVectorImpl<bool> &Visited) { 4249eff8b14SManuel Klimek for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), 4259eff8b14SManuel Klimek IMEnd = M.Imports.end(); 4269eff8b14SManuel Klimek IM != IMEnd; ++IM) { 4279eff8b14SManuel Klimek if (Visited[(*IM)->Index]) 4289eff8b14SManuel Klimek continue; 4299eff8b14SManuel Klimek Visited[(*IM)->Index] = true; 4309eff8b14SManuel Klimek if (!M.DirectlyImported) 4319eff8b14SManuel Klimek markVisitedDepthFirst(**IM, Visited); 4329eff8b14SManuel Klimek } 4339eff8b14SManuel Klimek } 4349eff8b14SManuel Klimek 4359eff8b14SManuel Klimek /// \brief Perform a depth-first visit of the current module. 4369eff8b14SManuel Klimek static bool visitDepthFirst( 4379eff8b14SManuel Klimek ModuleFile &M, 4389eff8b14SManuel Klimek ModuleManager::DFSPreorderControl (*PreorderVisitor)(ModuleFile &M, 4399eff8b14SManuel Klimek void *UserData), 4409eff8b14SManuel Klimek bool (*PostorderVisitor)(ModuleFile &M, void *UserData), void *UserData, 4419eff8b14SManuel Klimek SmallVectorImpl<bool> &Visited) { 4429eff8b14SManuel Klimek if (PreorderVisitor) { 4439eff8b14SManuel Klimek switch (PreorderVisitor(M, UserData)) { 4449eff8b14SManuel Klimek case ModuleManager::Abort: 445d44252ecSDouglas Gregor return true; 4469eff8b14SManuel Klimek case ModuleManager::SkipImports: 4479eff8b14SManuel Klimek markVisitedDepthFirst(M, Visited); 4489eff8b14SManuel Klimek return false; 4499eff8b14SManuel Klimek case ModuleManager::Continue: 4509eff8b14SManuel Klimek break; 4519eff8b14SManuel Klimek } 4529eff8b14SManuel Klimek } 453d44252ecSDouglas Gregor 454d44252ecSDouglas Gregor // Visit children 455de3ef502SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), 456d44252ecSDouglas Gregor IMEnd = M.Imports.end(); 457d44252ecSDouglas Gregor IM != IMEnd; ++IM) { 458bdb259d2SDouglas Gregor if (Visited[(*IM)->Index]) 459d44252ecSDouglas Gregor continue; 460bdb259d2SDouglas Gregor Visited[(*IM)->Index] = true; 461d44252ecSDouglas Gregor 4629eff8b14SManuel Klimek if (visitDepthFirst(**IM, PreorderVisitor, PostorderVisitor, UserData, Visited)) 463d44252ecSDouglas Gregor return true; 464d44252ecSDouglas Gregor } 465d44252ecSDouglas Gregor 4669eff8b14SManuel Klimek if (PostorderVisitor) 4679eff8b14SManuel Klimek return PostorderVisitor(M, UserData); 4689eff8b14SManuel Klimek 4699eff8b14SManuel Klimek return false; 470d44252ecSDouglas Gregor } 471d44252ecSDouglas Gregor 4729eff8b14SManuel Klimek void ModuleManager::visitDepthFirst( 4739eff8b14SManuel Klimek ModuleManager::DFSPreorderControl (*PreorderVisitor)(ModuleFile &M, 474d44252ecSDouglas Gregor void *UserData), 4759eff8b14SManuel Klimek bool (*PostorderVisitor)(ModuleFile &M, void *UserData), void *UserData) { 476bdb259d2SDouglas Gregor SmallVector<bool, 16> Visited(size(), false); 4779eff8b14SManuel Klimek for (unsigned I = 0, N = Roots.size(); I != N; ++I) { 4789eff8b14SManuel Klimek if (Visited[Roots[I]->Index]) 479d44252ecSDouglas Gregor continue; 4809eff8b14SManuel Klimek Visited[Roots[I]->Index] = true; 481d44252ecSDouglas Gregor 4829eff8b14SManuel Klimek if (::visitDepthFirst(*Roots[I], PreorderVisitor, PostorderVisitor, UserData, Visited)) 483d44252ecSDouglas Gregor return; 484d44252ecSDouglas Gregor } 485d44252ecSDouglas Gregor } 4869d7c1a2aSDouglas Gregor 4877029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 4887029ce1aSDouglas Gregor off_t ExpectedSize, 4897029ce1aSDouglas Gregor time_t ExpectedModTime, 4907029ce1aSDouglas Gregor const FileEntry *&File) { 49105f82ba2SBen Langmuir // Open the file immediately to ensure there is no race between stat'ing and 49205f82ba2SBen Langmuir // opening the file. 49305f82ba2SBen Langmuir File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false); 4947029ce1aSDouglas Gregor 4957029ce1aSDouglas Gregor if (!File && FileName != "-") { 4967029ce1aSDouglas Gregor return false; 4977029ce1aSDouglas Gregor } 4987029ce1aSDouglas Gregor 4997029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 500027731d7SBen Langmuir (ExpectedModTime && ExpectedModTime != File->getModificationTime())) 501027731d7SBen Langmuir // Do not destroy File, as it may be referenced. If we need to rebuild it, 502027731d7SBen Langmuir // it will be destroyed by removeModules. 5037029ce1aSDouglas Gregor return true; 5047029ce1aSDouglas Gregor 5057029ce1aSDouglas Gregor return false; 5067029ce1aSDouglas Gregor } 5077029ce1aSDouglas Gregor 5089d7c1a2aSDouglas Gregor #ifndef NDEBUG 5099d7c1a2aSDouglas Gregor namespace llvm { 5109d7c1a2aSDouglas Gregor template<> 5119d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 512de3ef502SDouglas Gregor typedef ModuleFile NodeType; 513de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 5149d7c1a2aSDouglas Gregor typedef ModuleManager::ModuleConstIterator nodes_iterator; 5159d7c1a2aSDouglas Gregor 5169d7c1a2aSDouglas Gregor static ChildIteratorType child_begin(NodeType *Node) { 5179d7c1a2aSDouglas Gregor return Node->Imports.begin(); 5189d7c1a2aSDouglas Gregor } 5199d7c1a2aSDouglas Gregor 5209d7c1a2aSDouglas Gregor static ChildIteratorType child_end(NodeType *Node) { 5219d7c1a2aSDouglas Gregor return Node->Imports.end(); 5229d7c1a2aSDouglas Gregor } 5239d7c1a2aSDouglas Gregor 5249d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 5259d7c1a2aSDouglas Gregor return Manager.begin(); 5269d7c1a2aSDouglas Gregor } 5279d7c1a2aSDouglas Gregor 5289d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 5299d7c1a2aSDouglas Gregor return Manager.end(); 5309d7c1a2aSDouglas Gregor } 5319d7c1a2aSDouglas Gregor }; 5329d7c1a2aSDouglas Gregor 5339d7c1a2aSDouglas Gregor template<> 5349d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 5359d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 5369d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 5379d7c1a2aSDouglas Gregor 5389d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 5399d7c1a2aSDouglas Gregor return true; 5409d7c1a2aSDouglas Gregor } 5419d7c1a2aSDouglas Gregor 542de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 543beee15e7SBen Langmuir return M->ModuleName; 5449d7c1a2aSDouglas Gregor } 5459d7c1a2aSDouglas Gregor }; 546ab9db510SAlexander Kornienko } 5479d7c1a2aSDouglas Gregor 5489d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 5499d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 5509d7c1a2aSDouglas Gregor } 5519d7c1a2aSDouglas Gregor #endif 552