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 //===----------------------------------------------------------------------===// 14beee15e7SBen Langmuir #include "clang/Lex/HeaderSearch.h" 157029ce1aSDouglas Gregor #include "clang/Lex/ModuleMap.h" 167211ac15SDouglas Gregor #include "clang/Serialization/GlobalModuleIndex.h" 17af04f98cSBenjamin Kramer #include "clang/Serialization/ModuleManager.h" 18d44252ecSDouglas Gregor #include "llvm/Support/MemoryBuffer.h" 19552c169eSRafael Espindola #include "llvm/Support/Path.h" 20d44252ecSDouglas Gregor #include "llvm/Support/raw_ostream.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, 61487ea14aSBen Langmuir std::function<ASTFileSignature(llvm::BitstreamReader &)> 62487ea14aSBen Langmuir 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; 70c27d0d5eSEli Friedman if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { 71c27d0d5eSEli Friedman ErrorStr = "module file out of date"; 727029ce1aSDouglas Gregor return OutOfDate; 73c27d0d5eSEli Friedman } 747029ce1aSDouglas Gregor 75d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 76c27d0d5eSEli Friedman ErrorStr = "module file not found"; 777029ce1aSDouglas Gregor return Missing; 78d44252ecSDouglas Gregor } 79d44252ecSDouglas Gregor 80d44252ecSDouglas Gregor // Check whether we already loaded this module, before 81de3ef502SDouglas Gregor ModuleFile *&ModuleEntry = Modules[Entry]; 82d44252ecSDouglas Gregor bool NewModule = false; 83d44252ecSDouglas Gregor if (!ModuleEntry) { 84d44252ecSDouglas Gregor // Allocate a new module. 854fc9f3e8SDouglas Gregor ModuleFile *New = new ModuleFile(Type, Generation); 86bdb259d2SDouglas Gregor New->Index = Chain.size(); 87d44252ecSDouglas Gregor New->FileName = FileName.str(); 88aedf7144SArgyrios Kyrtzidis New->File = Entry; 896fb03aeaSDouglas Gregor New->ImportLoc = ImportLoc; 90d44252ecSDouglas Gregor Chain.push_back(New); 91d44252ecSDouglas Gregor NewModule = true; 92d44252ecSDouglas Gregor ModuleEntry = New; 93d44252ecSDouglas Gregor 94f430da4dSDmitri Gribenko New->InputFilesValidationTimestamp = 0; 95e842a474SRichard Smith if (New->Kind == MK_ImplicitModule) { 96f430da4dSDmitri Gribenko std::string TimestampFilename = New->getTimestampFilename(); 97c8130a74SBen Langmuir vfs::Status Status; 98f430da4dSDmitri Gribenko // A cached stat value would be fine as well. 99f430da4dSDmitri Gribenko if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) 100f430da4dSDmitri Gribenko New->InputFilesValidationTimestamp = 101f430da4dSDmitri Gribenko Status.getLastModificationTime().toEpochTime(); 102f430da4dSDmitri Gribenko } 103f430da4dSDmitri Gribenko 104d44252ecSDouglas Gregor // Load the contents of the module 1055cd06f26SRafael Espindola if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) { 106d44252ecSDouglas Gregor // The buffer was already provided for us. 1075cd06f26SRafael Espindola New->Buffer = std::move(Buffer); 108d44252ecSDouglas Gregor } else { 109d44252ecSDouglas Gregor // Open the AST file. 110a885796dSBenjamin Kramer llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf( 111a885796dSBenjamin Kramer (std::error_code())); 112d44252ecSDouglas Gregor if (FileName == "-") { 113a885796dSBenjamin Kramer Buf = llvm::MemoryBuffer::getSTDIN(); 1149801b253SBen Langmuir } else { 1159801b253SBen Langmuir // Leave the FileEntry open so if it gets read again by another 1169801b253SBen Langmuir // ModuleManager it must be the same underlying file. 1179801b253SBen Langmuir // FIXME: Because FileManager::getFile() doesn't guarantee that it will 1189801b253SBen Langmuir // give us an open file, this may not be 100% reliable. 119a885796dSBenjamin Kramer Buf = FileMgr.getBufferForFile(New->File, 120a885796dSBenjamin Kramer /*IsVolatile=*/false, 121a885796dSBenjamin Kramer /*ShouldClose=*/false); 1229801b253SBen Langmuir } 123d44252ecSDouglas Gregor 124a885796dSBenjamin Kramer if (!Buf) { 125a885796dSBenjamin Kramer ErrorStr = Buf.getError().message(); 1267029ce1aSDouglas Gregor return Missing; 127d44252ecSDouglas Gregor } 128d44252ecSDouglas Gregor 129a885796dSBenjamin Kramer New->Buffer = std::move(*Buf); 130a885796dSBenjamin Kramer } 131a885796dSBenjamin Kramer 132d44252ecSDouglas Gregor // Initialize the stream 133d44252ecSDouglas Gregor New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(), 1347029ce1aSDouglas Gregor (const unsigned char *)New->Buffer->getBufferEnd()); 135*ed982584SBen Langmuir } 136487ea14aSBen Langmuir 137487ea14aSBen Langmuir if (ExpectedSignature) { 138*ed982584SBen Langmuir if (NewModule) 139*ed982584SBen Langmuir ModuleEntry->Signature = ReadSignature(ModuleEntry->StreamFile); 140*ed982584SBen Langmuir else 141*ed982584SBen Langmuir assert(ModuleEntry->Signature == ReadSignature(ModuleEntry->StreamFile)); 142*ed982584SBen Langmuir 143*ed982584SBen Langmuir if (ModuleEntry->Signature != ExpectedSignature) { 144*ed982584SBen Langmuir ErrorStr = ModuleEntry->Signature ? "signature mismatch" 145487ea14aSBen Langmuir : "could not read module signature"; 146487ea14aSBen Langmuir 147*ed982584SBen Langmuir if (NewModule) { 148487ea14aSBen Langmuir // Remove the module file immediately, since removeModules might try to 149487ea14aSBen Langmuir // invalidate the file cache for Entry, and that is not safe if this 150487ea14aSBen Langmuir // module is *itself* up to date, but has an out-of-date importer. 151487ea14aSBen Langmuir Modules.erase(Entry); 152487ea14aSBen Langmuir Chain.pop_back(); 153*ed982584SBen Langmuir delete ModuleEntry; 154487ea14aSBen Langmuir } 155*ed982584SBen Langmuir return OutOfDate; 156487ea14aSBen Langmuir } 1577029ce1aSDouglas Gregor } 158d44252ecSDouglas Gregor 159d44252ecSDouglas Gregor if (ImportedBy) { 160d44252ecSDouglas Gregor ModuleEntry->ImportedBy.insert(ImportedBy); 161d44252ecSDouglas Gregor ImportedBy->Imports.insert(ModuleEntry); 162d44252ecSDouglas Gregor } else { 1636fb03aeaSDouglas Gregor if (!ModuleEntry->DirectlyImported) 1646fb03aeaSDouglas Gregor ModuleEntry->ImportLoc = ImportLoc; 1656fb03aeaSDouglas Gregor 166d44252ecSDouglas Gregor ModuleEntry->DirectlyImported = true; 167d44252ecSDouglas Gregor } 168d44252ecSDouglas Gregor 1697029ce1aSDouglas Gregor Module = ModuleEntry; 1707029ce1aSDouglas Gregor return NewModule? NewlyLoaded : AlreadyLoaded; 171d44252ecSDouglas Gregor } 172d44252ecSDouglas Gregor 1739801b253SBen Langmuir void ModuleManager::removeModules( 1749801b253SBen Langmuir ModuleIterator first, ModuleIterator last, 1759801b253SBen Langmuir llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully, 1767029ce1aSDouglas Gregor ModuleMap *modMap) { 177188dbef2SDouglas Gregor if (first == last) 178188dbef2SDouglas Gregor return; 179188dbef2SDouglas Gregor 180188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 181188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); 182188dbef2SDouglas Gregor 183188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 184188dbef2SDouglas Gregor for (unsigned i = 0, n = Chain.size(); i != n; ++i) { 185b55d0226SChandler Carruth Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) { 186b55d0226SChandler Carruth return victimSet.count(MF); 187b55d0226SChandler Carruth }); 188188dbef2SDouglas Gregor } 189188dbef2SDouglas Gregor 190188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 191188dbef2SDouglas Gregor for (ModuleIterator victim = first; victim != last; ++victim) { 192caea1318SBen Langmuir Modules.erase((*victim)->File); 193ca39214fSBen Langmuir 1947029ce1aSDouglas Gregor if (modMap) { 195beee15e7SBen Langmuir StringRef ModuleName = (*victim)->ModuleName; 1967029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 197a13603a2SCraig Topper mod->setASTFile(nullptr); 1987029ce1aSDouglas Gregor } 1997029ce1aSDouglas Gregor } 2004f05478cSBen Langmuir 2019801b253SBen Langmuir // Files that didn't make it through ReadASTCore successfully will be 2029801b253SBen Langmuir // rebuilt (or there was an error). Invalidate them so that we can load the 2039801b253SBen Langmuir // new files that will be renamed over the old ones. 2049801b253SBen Langmuir if (LoadedSuccessfully.count(*victim) == 0) 2054f05478cSBen Langmuir FileMgr.invalidateCache((*victim)->File); 2064f05478cSBen Langmuir 207188dbef2SDouglas Gregor delete *victim; 208188dbef2SDouglas Gregor } 209188dbef2SDouglas Gregor 210188dbef2SDouglas Gregor // Remove the modules from the chain. 211188dbef2SDouglas Gregor Chain.erase(first, last); 212188dbef2SDouglas Gregor } 213188dbef2SDouglas Gregor 2145cd06f26SRafael Espindola void 2155cd06f26SRafael Espindola ModuleManager::addInMemoryBuffer(StringRef FileName, 2165cd06f26SRafael Espindola std::unique_ptr<llvm::MemoryBuffer> Buffer) { 217d44252ecSDouglas Gregor 2185cd06f26SRafael Espindola const FileEntry *Entry = 2195cd06f26SRafael Espindola FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); 2205cd06f26SRafael Espindola InMemoryBuffers[Entry] = std::move(Buffer); 221d44252ecSDouglas Gregor } 222d44252ecSDouglas Gregor 223e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 224e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 225e97cd90aSDouglas Gregor if (FirstVisitState) { 226e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 227e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 228a13603a2SCraig Topper Result->NextState = nullptr; 229e97cd90aSDouglas Gregor return Result; 230e97cd90aSDouglas Gregor } 231e97cd90aSDouglas Gregor 232e97cd90aSDouglas Gregor // Allocate and return a new state. 233e97cd90aSDouglas Gregor return new VisitState(size()); 234e97cd90aSDouglas Gregor } 235e97cd90aSDouglas Gregor 236e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 237a13603a2SCraig Topper assert(State->NextState == nullptr && "Visited state is in list?"); 238e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 239e97cd90aSDouglas Gregor FirstVisitState = State; 240e97cd90aSDouglas Gregor } 241e97cd90aSDouglas Gregor 2427211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 2437211ac15SDouglas Gregor GlobalIndex = Index; 244603cd869SDouglas Gregor if (!GlobalIndex) { 245603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 246603cd869SDouglas Gregor return; 2477029ce1aSDouglas Gregor } 248603cd869SDouglas Gregor 249603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 250603cd869SDouglas Gregor // loaded. 251603cd869SDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 252603cd869SDouglas Gregor if (!GlobalIndex->loadedModuleFile(Chain[I])) { 253603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(Chain[I]); 254603cd869SDouglas Gregor } 255603cd869SDouglas Gregor } 256603cd869SDouglas Gregor } 257603cd869SDouglas Gregor 258603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 259603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 260603cd869SDouglas Gregor return; 261603cd869SDouglas Gregor 262603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 2637211ac15SDouglas Gregor } 2647211ac15SDouglas Gregor 2657211ac15SDouglas Gregor ModuleManager::ModuleManager(FileManager &FileMgr) 266a13603a2SCraig Topper : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {} 267d44252ecSDouglas Gregor 268d44252ecSDouglas Gregor ModuleManager::~ModuleManager() { 269d44252ecSDouglas Gregor for (unsigned i = 0, e = Chain.size(); i != e; ++i) 270d44252ecSDouglas Gregor delete Chain[e - i - 1]; 271e97cd90aSDouglas Gregor delete FirstVisitState; 272d44252ecSDouglas Gregor } 273d44252ecSDouglas Gregor 2747211ac15SDouglas Gregor void 2757211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), 2767211ac15SDouglas Gregor void *UserData, 2774dd9b43cSCraig Topper llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) { 2787211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 279e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 280d44252ecSDouglas Gregor unsigned N = size(); 281e41d7feaSDouglas Gregor VisitOrder.clear(); 282e41d7feaSDouglas Gregor VisitOrder.reserve(N); 283d44252ecSDouglas Gregor 284d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 285d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 286d44252ecSDouglas Gregor // to seed the queue. 287de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 288d44252ecSDouglas Gregor Queue.reserve(N); 289bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 290bdb259d2SDouglas Gregor UnusedIncomingEdges.reserve(size()); 291d44252ecSDouglas Gregor for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) { 292d44252ecSDouglas Gregor if (unsigned Size = (*M)->ImportedBy.size()) 293bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(Size); 294bdb259d2SDouglas Gregor else { 295bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(0); 296d44252ecSDouglas Gregor Queue.push_back(*M); 297d44252ecSDouglas Gregor } 298bdb259d2SDouglas Gregor } 299d44252ecSDouglas Gregor 300e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 301e41d7feaSDouglas Gregor // of its dependencies. 302d44252ecSDouglas Gregor unsigned QueueStart = 0; 303d44252ecSDouglas Gregor while (QueueStart < Queue.size()) { 304de3ef502SDouglas Gregor ModuleFile *CurrentModule = Queue[QueueStart++]; 305e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 306d44252ecSDouglas Gregor 307d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 308d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 309bdb259d2SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 310bdb259d2SDouglas Gregor M = CurrentModule->Imports.begin(), 311d44252ecSDouglas Gregor MEnd = CurrentModule->Imports.end(); 312d44252ecSDouglas Gregor M != MEnd; ++M) { 313d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 314d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 315d44252ecSDouglas Gregor // that depends on this particular module, push it into the 316d44252ecSDouglas Gregor // queue to be visited. 317bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 318d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 319d44252ecSDouglas Gregor Queue.push_back(*M); 320d44252ecSDouglas Gregor } 321d44252ecSDouglas Gregor } 322e41d7feaSDouglas Gregor 323e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 3247211ac15SDouglas Gregor 325e97cd90aSDouglas Gregor delete FirstVisitState; 326a13603a2SCraig Topper FirstVisitState = nullptr; 327e41d7feaSDouglas Gregor } 328e41d7feaSDouglas Gregor 329e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 330e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 331e41d7feaSDouglas Gregor 3327211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 3337211ac15SDouglas Gregor // module index, mark every module file in common with the global module 3347211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 3357211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 3367211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 3377211ac15SDouglas Gregor { 3387211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 3397029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 340e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 3417211ac15SDouglas Gregor } 3427211ac15SDouglas Gregor } 3437211ac15SDouglas Gregor 344e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 345e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 346e41d7feaSDouglas Gregor // Should we skip this module file? 347e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 348e41d7feaSDouglas Gregor continue; 349e41d7feaSDouglas Gregor 350e41d7feaSDouglas Gregor // Visit the module. 351e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 352e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 353e41d7feaSDouglas Gregor if (!Visitor(*CurrentModule, UserData)) 354e41d7feaSDouglas Gregor continue; 355e41d7feaSDouglas Gregor 356e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 357e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 358e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 359e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 360e41d7feaSDouglas Gregor do { 361e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 362e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 363e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 364e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 365e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 366e41d7feaSDouglas Gregor M != MEnd; ++M) { 367e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 368e97cd90aSDouglas Gregor State->Stack.push_back(*M); 369e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 370e41d7feaSDouglas Gregor } 371e41d7feaSDouglas Gregor } 372e41d7feaSDouglas Gregor 373e97cd90aSDouglas Gregor if (State->Stack.empty()) 374e41d7feaSDouglas Gregor break; 375e41d7feaSDouglas Gregor 376e41d7feaSDouglas Gregor // Pop the next module off the stack. 37725284cc9SRobert Wilhelm NextModule = State->Stack.pop_back_val(); 378e41d7feaSDouglas Gregor } while (true); 379e41d7feaSDouglas Gregor } 380e97cd90aSDouglas Gregor 381e97cd90aSDouglas Gregor returnVisitState(State); 382d44252ecSDouglas Gregor } 383d44252ecSDouglas Gregor 384d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module. 385de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M, 386de3ef502SDouglas Gregor bool (*Visitor)(ModuleFile &M, bool Preorder, 387d44252ecSDouglas Gregor void *UserData), 388d44252ecSDouglas Gregor void *UserData, 389bdb259d2SDouglas Gregor SmallVectorImpl<bool> &Visited) { 390d44252ecSDouglas Gregor // Preorder visitation 391d44252ecSDouglas Gregor if (Visitor(M, /*Preorder=*/true, UserData)) 392d44252ecSDouglas Gregor return true; 393d44252ecSDouglas Gregor 394d44252ecSDouglas Gregor // Visit children 395de3ef502SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), 396d44252ecSDouglas Gregor IMEnd = M.Imports.end(); 397d44252ecSDouglas Gregor IM != IMEnd; ++IM) { 398bdb259d2SDouglas Gregor if (Visited[(*IM)->Index]) 399d44252ecSDouglas Gregor continue; 400bdb259d2SDouglas Gregor Visited[(*IM)->Index] = true; 401d44252ecSDouglas Gregor 402d44252ecSDouglas Gregor if (visitDepthFirst(**IM, Visitor, UserData, Visited)) 403d44252ecSDouglas Gregor return true; 404d44252ecSDouglas Gregor } 405d44252ecSDouglas Gregor 406d44252ecSDouglas Gregor // Postorder visitation 407d44252ecSDouglas Gregor return Visitor(M, /*Preorder=*/false, UserData); 408d44252ecSDouglas Gregor } 409d44252ecSDouglas Gregor 410de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 411d44252ecSDouglas Gregor void *UserData), 412d44252ecSDouglas Gregor void *UserData) { 413bdb259d2SDouglas Gregor SmallVector<bool, 16> Visited(size(), false); 414d44252ecSDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 415bdb259d2SDouglas Gregor if (Visited[Chain[I]->Index]) 416d44252ecSDouglas Gregor continue; 417bdb259d2SDouglas Gregor Visited[Chain[I]->Index] = true; 418d44252ecSDouglas Gregor 419d44252ecSDouglas Gregor if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited)) 420d44252ecSDouglas Gregor return; 421d44252ecSDouglas Gregor } 422d44252ecSDouglas Gregor } 4239d7c1a2aSDouglas Gregor 4247029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 4257029ce1aSDouglas Gregor off_t ExpectedSize, 4267029ce1aSDouglas Gregor time_t ExpectedModTime, 4277029ce1aSDouglas Gregor const FileEntry *&File) { 42805f82ba2SBen Langmuir // Open the file immediately to ensure there is no race between stat'ing and 42905f82ba2SBen Langmuir // opening the file. 43005f82ba2SBen Langmuir File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false); 4317029ce1aSDouglas Gregor 4327029ce1aSDouglas Gregor if (!File && FileName != "-") { 4337029ce1aSDouglas Gregor return false; 4347029ce1aSDouglas Gregor } 4357029ce1aSDouglas Gregor 4367029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 437027731d7SBen Langmuir (ExpectedModTime && ExpectedModTime != File->getModificationTime())) 438027731d7SBen Langmuir // Do not destroy File, as it may be referenced. If we need to rebuild it, 439027731d7SBen Langmuir // it will be destroyed by removeModules. 4407029ce1aSDouglas Gregor return true; 4417029ce1aSDouglas Gregor 4427029ce1aSDouglas Gregor return false; 4437029ce1aSDouglas Gregor } 4447029ce1aSDouglas Gregor 4459d7c1a2aSDouglas Gregor #ifndef NDEBUG 4469d7c1a2aSDouglas Gregor namespace llvm { 4479d7c1a2aSDouglas Gregor template<> 4489d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 449de3ef502SDouglas Gregor typedef ModuleFile NodeType; 450de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 4519d7c1a2aSDouglas Gregor typedef ModuleManager::ModuleConstIterator nodes_iterator; 4529d7c1a2aSDouglas Gregor 4539d7c1a2aSDouglas Gregor static ChildIteratorType child_begin(NodeType *Node) { 4549d7c1a2aSDouglas Gregor return Node->Imports.begin(); 4559d7c1a2aSDouglas Gregor } 4569d7c1a2aSDouglas Gregor 4579d7c1a2aSDouglas Gregor static ChildIteratorType child_end(NodeType *Node) { 4589d7c1a2aSDouglas Gregor return Node->Imports.end(); 4599d7c1a2aSDouglas Gregor } 4609d7c1a2aSDouglas Gregor 4619d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 4629d7c1a2aSDouglas Gregor return Manager.begin(); 4639d7c1a2aSDouglas Gregor } 4649d7c1a2aSDouglas Gregor 4659d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 4669d7c1a2aSDouglas Gregor return Manager.end(); 4679d7c1a2aSDouglas Gregor } 4689d7c1a2aSDouglas Gregor }; 4699d7c1a2aSDouglas Gregor 4709d7c1a2aSDouglas Gregor template<> 4719d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4729d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4739d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4749d7c1a2aSDouglas Gregor 4759d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4769d7c1a2aSDouglas Gregor return true; 4779d7c1a2aSDouglas Gregor } 4789d7c1a2aSDouglas Gregor 479de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 480beee15e7SBen Langmuir return M->ModuleName; 4819d7c1a2aSDouglas Gregor } 4829d7c1a2aSDouglas Gregor }; 4839d7c1a2aSDouglas Gregor } 4849d7c1a2aSDouglas Gregor 4859d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4869d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4879d7c1a2aSDouglas Gregor } 4889d7c1a2aSDouglas Gregor #endif 489