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 //===----------------------------------------------------------------------===// 147029ce1aSDouglas Gregor #include "clang/Lex/ModuleMap.h" 15d44252ecSDouglas Gregor #include "clang/Serialization/ModuleManager.h" 167211ac15SDouglas Gregor #include "clang/Serialization/GlobalModuleIndex.h" 17d44252ecSDouglas Gregor #include "llvm/Support/MemoryBuffer.h" 187029ce1aSDouglas Gregor #include "llvm/Support/PathV2.h" 19d44252ecSDouglas Gregor #include "llvm/Support/raw_ostream.h" 20d44252ecSDouglas Gregor #include "llvm/Support/system_error.h" 21d44252ecSDouglas Gregor 229d7c1a2aSDouglas Gregor #ifndef NDEBUG 239d7c1a2aSDouglas Gregor #include "llvm/Support/GraphWriter.h" 249d7c1a2aSDouglas Gregor #endif 259d7c1a2aSDouglas Gregor 26d44252ecSDouglas Gregor using namespace clang; 27d44252ecSDouglas Gregor using namespace serialization; 28d44252ecSDouglas Gregor 29de3ef502SDouglas Gregor ModuleFile *ModuleManager::lookup(StringRef Name) { 30dadd85dcSDouglas Gregor const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, 31dadd85dcSDouglas Gregor /*cacheFailure=*/false); 32d44252ecSDouglas Gregor return Modules[Entry]; 33d44252ecSDouglas Gregor } 34d44252ecSDouglas Gregor 35d44252ecSDouglas Gregor llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) { 36dadd85dcSDouglas Gregor const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, 37dadd85dcSDouglas Gregor /*cacheFailure=*/false); 38d44252ecSDouglas Gregor return InMemoryBuffers[Entry]; 39d44252ecSDouglas Gregor } 40d44252ecSDouglas Gregor 417029ce1aSDouglas Gregor ModuleManager::AddModuleResult 42d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type, 436fb03aeaSDouglas Gregor SourceLocation ImportLoc, ModuleFile *ImportedBy, 447029ce1aSDouglas Gregor unsigned Generation, 457029ce1aSDouglas Gregor off_t ExpectedSize, time_t ExpectedModTime, 467029ce1aSDouglas Gregor ModuleFile *&Module, 477029ce1aSDouglas Gregor std::string &ErrorStr) { 487029ce1aSDouglas Gregor Module = 0; 497029ce1aSDouglas Gregor 507029ce1aSDouglas Gregor // Look for the file entry. This only fails if the expected size or 517029ce1aSDouglas Gregor // modification time differ. 527029ce1aSDouglas Gregor const FileEntry *Entry; 537029ce1aSDouglas Gregor if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) 547029ce1aSDouglas Gregor return OutOfDate; 557029ce1aSDouglas Gregor 56d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 57d44252ecSDouglas Gregor ErrorStr = "file not found"; 587029ce1aSDouglas Gregor return Missing; 59d44252ecSDouglas Gregor } 60d44252ecSDouglas Gregor 61d44252ecSDouglas Gregor // Check whether we already loaded this module, before 62de3ef502SDouglas Gregor ModuleFile *&ModuleEntry = Modules[Entry]; 63d44252ecSDouglas Gregor bool NewModule = false; 64d44252ecSDouglas Gregor if (!ModuleEntry) { 65d44252ecSDouglas Gregor // Allocate a new module. 664fc9f3e8SDouglas Gregor ModuleFile *New = new ModuleFile(Type, Generation); 67bdb259d2SDouglas Gregor New->Index = Chain.size(); 68d44252ecSDouglas Gregor New->FileName = FileName.str(); 69aedf7144SArgyrios Kyrtzidis New->File = Entry; 706fb03aeaSDouglas Gregor New->ImportLoc = ImportLoc; 71d44252ecSDouglas Gregor Chain.push_back(New); 72d44252ecSDouglas Gregor NewModule = true; 73d44252ecSDouglas Gregor ModuleEntry = New; 74d44252ecSDouglas Gregor 75d44252ecSDouglas Gregor // Load the contents of the module 76d44252ecSDouglas Gregor if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) { 77d44252ecSDouglas Gregor // The buffer was already provided for us. 78d44252ecSDouglas Gregor assert(Buffer && "Passed null buffer"); 79d44252ecSDouglas Gregor New->Buffer.reset(Buffer); 80d44252ecSDouglas Gregor } else { 81d44252ecSDouglas Gregor // Open the AST file. 82d44252ecSDouglas Gregor llvm::error_code ec; 83d44252ecSDouglas Gregor if (FileName == "-") { 84d44252ecSDouglas Gregor ec = llvm::MemoryBuffer::getSTDIN(New->Buffer); 85d44252ecSDouglas Gregor if (ec) 86d44252ecSDouglas Gregor ErrorStr = ec.message(); 87d44252ecSDouglas Gregor } else 88d44252ecSDouglas Gregor New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr)); 89d44252ecSDouglas Gregor 90d44252ecSDouglas Gregor if (!New->Buffer) 917029ce1aSDouglas Gregor return Missing; 92d44252ecSDouglas Gregor } 93d44252ecSDouglas Gregor 94d44252ecSDouglas Gregor // Initialize the stream 95d44252ecSDouglas Gregor New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(), 967029ce1aSDouglas Gregor (const unsigned char *)New->Buffer->getBufferEnd()); 977029ce1aSDouglas Gregor } 98d44252ecSDouglas Gregor 99d44252ecSDouglas Gregor if (ImportedBy) { 100d44252ecSDouglas Gregor ModuleEntry->ImportedBy.insert(ImportedBy); 101d44252ecSDouglas Gregor ImportedBy->Imports.insert(ModuleEntry); 102d44252ecSDouglas Gregor } else { 1036fb03aeaSDouglas Gregor if (!ModuleEntry->DirectlyImported) 1046fb03aeaSDouglas Gregor ModuleEntry->ImportLoc = ImportLoc; 1056fb03aeaSDouglas Gregor 106d44252ecSDouglas Gregor ModuleEntry->DirectlyImported = true; 107d44252ecSDouglas Gregor } 108d44252ecSDouglas Gregor 1097029ce1aSDouglas Gregor Module = ModuleEntry; 1107029ce1aSDouglas Gregor return NewModule? NewlyLoaded : AlreadyLoaded; 111d44252ecSDouglas Gregor } 112d44252ecSDouglas Gregor 113188dbef2SDouglas Gregor namespace { 114188dbef2SDouglas Gregor /// \brief Predicate that checks whether a module file occurs within 115188dbef2SDouglas Gregor /// the given set. 116188dbef2SDouglas Gregor class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> { 117188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> &Removed; 118188dbef2SDouglas Gregor 119188dbef2SDouglas Gregor public: 120188dbef2SDouglas Gregor IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed) 121188dbef2SDouglas Gregor : Removed(Removed) { } 122188dbef2SDouglas Gregor 123188dbef2SDouglas Gregor bool operator()(ModuleFile *MF) const { 124188dbef2SDouglas Gregor return Removed.count(MF); 125188dbef2SDouglas Gregor } 126188dbef2SDouglas Gregor }; 127188dbef2SDouglas Gregor } 128188dbef2SDouglas Gregor 1297029ce1aSDouglas Gregor void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last, 1307029ce1aSDouglas Gregor ModuleMap *modMap) { 131188dbef2SDouglas Gregor if (first == last) 132188dbef2SDouglas Gregor return; 133188dbef2SDouglas Gregor 134188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 135188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); 136188dbef2SDouglas Gregor 137188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 138188dbef2SDouglas Gregor IsInModuleFileSet checkInSet(victimSet); 139188dbef2SDouglas Gregor for (unsigned i = 0, n = Chain.size(); i != n; ++i) { 140188dbef2SDouglas Gregor Chain[i]->ImportedBy.remove_if(checkInSet); 141188dbef2SDouglas Gregor } 142188dbef2SDouglas Gregor 143188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 144188dbef2SDouglas Gregor for (ModuleIterator victim = first; victim != last; ++victim) { 145188dbef2SDouglas Gregor Modules.erase((*victim)->File); 1467029ce1aSDouglas Gregor 1477029ce1aSDouglas Gregor FileMgr.invalidateCache((*victim)->File); 1487029ce1aSDouglas Gregor if (modMap) { 1497029ce1aSDouglas Gregor StringRef ModuleName = llvm::sys::path::stem((*victim)->FileName); 1507029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 1517029ce1aSDouglas Gregor mod->setASTFile(0); 1527029ce1aSDouglas Gregor } 1537029ce1aSDouglas Gregor } 154188dbef2SDouglas Gregor delete *victim; 155188dbef2SDouglas Gregor } 156188dbef2SDouglas Gregor 157188dbef2SDouglas Gregor // Remove the modules from the chain. 158188dbef2SDouglas Gregor Chain.erase(first, last); 159188dbef2SDouglas Gregor } 160188dbef2SDouglas Gregor 161d44252ecSDouglas Gregor void ModuleManager::addInMemoryBuffer(StringRef FileName, 162d44252ecSDouglas Gregor llvm::MemoryBuffer *Buffer) { 163d44252ecSDouglas Gregor 164d44252ecSDouglas Gregor const FileEntry *Entry = FileMgr.getVirtualFile(FileName, 165d44252ecSDouglas Gregor Buffer->getBufferSize(), 0); 166d44252ecSDouglas Gregor InMemoryBuffers[Entry] = Buffer; 167d44252ecSDouglas Gregor } 168d44252ecSDouglas Gregor 169e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 170e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 171e97cd90aSDouglas Gregor if (FirstVisitState) { 172e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 173e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 174e97cd90aSDouglas Gregor Result->NextState = 0; 175e97cd90aSDouglas Gregor return Result; 176e97cd90aSDouglas Gregor } 177e97cd90aSDouglas Gregor 178e97cd90aSDouglas Gregor // Allocate and return a new state. 179e97cd90aSDouglas Gregor return new VisitState(size()); 180e97cd90aSDouglas Gregor } 181e97cd90aSDouglas Gregor 182e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 183e97cd90aSDouglas Gregor assert(State->NextState == 0 && "Visited state is in list?"); 184e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 185e97cd90aSDouglas Gregor FirstVisitState = State; 186e97cd90aSDouglas Gregor } 187e97cd90aSDouglas Gregor 1887211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 1897211ac15SDouglas Gregor GlobalIndex = Index; 190*603cd869SDouglas Gregor if (!GlobalIndex) { 191*603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 192*603cd869SDouglas Gregor return; 1937029ce1aSDouglas Gregor } 194*603cd869SDouglas Gregor 195*603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 196*603cd869SDouglas Gregor // loaded. 197*603cd869SDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 198*603cd869SDouglas Gregor if (!GlobalIndex->loadedModuleFile(Chain[I])) { 199*603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(Chain[I]); 200*603cd869SDouglas Gregor } 201*603cd869SDouglas Gregor } 202*603cd869SDouglas Gregor } 203*603cd869SDouglas Gregor 204*603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 205*603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 206*603cd869SDouglas Gregor return; 207*603cd869SDouglas Gregor 208*603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 2097211ac15SDouglas Gregor } 2107211ac15SDouglas Gregor 2117211ac15SDouglas Gregor ModuleManager::ModuleManager(FileManager &FileMgr) 212e97cd90aSDouglas Gregor : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(0) { } 213d44252ecSDouglas Gregor 214d44252ecSDouglas Gregor ModuleManager::~ModuleManager() { 215d44252ecSDouglas Gregor for (unsigned i = 0, e = Chain.size(); i != e; ++i) 216d44252ecSDouglas Gregor delete Chain[e - i - 1]; 217e97cd90aSDouglas Gregor delete FirstVisitState; 218d44252ecSDouglas Gregor } 219d44252ecSDouglas Gregor 2207211ac15SDouglas Gregor void 2217211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), 2227211ac15SDouglas Gregor void *UserData, 2237029ce1aSDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) { 2247211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 225e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 226d44252ecSDouglas Gregor unsigned N = size(); 227e41d7feaSDouglas Gregor VisitOrder.clear(); 228e41d7feaSDouglas Gregor VisitOrder.reserve(N); 229d44252ecSDouglas Gregor 230d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 231d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 232d44252ecSDouglas Gregor // to seed the queue. 233de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 234d44252ecSDouglas Gregor Queue.reserve(N); 235bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 236bdb259d2SDouglas Gregor UnusedIncomingEdges.reserve(size()); 237d44252ecSDouglas Gregor for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) { 238d44252ecSDouglas Gregor if (unsigned Size = (*M)->ImportedBy.size()) 239bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(Size); 240bdb259d2SDouglas Gregor else { 241bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(0); 242d44252ecSDouglas Gregor Queue.push_back(*M); 243d44252ecSDouglas Gregor } 244bdb259d2SDouglas Gregor } 245d44252ecSDouglas Gregor 246e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 247e41d7feaSDouglas Gregor // of its dependencies. 248d44252ecSDouglas Gregor unsigned QueueStart = 0; 249d44252ecSDouglas Gregor while (QueueStart < Queue.size()) { 250de3ef502SDouglas Gregor ModuleFile *CurrentModule = Queue[QueueStart++]; 251e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 252d44252ecSDouglas Gregor 253d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 254d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 255bdb259d2SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 256bdb259d2SDouglas Gregor M = CurrentModule->Imports.begin(), 257d44252ecSDouglas Gregor MEnd = CurrentModule->Imports.end(); 258d44252ecSDouglas Gregor M != MEnd; ++M) { 259d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 260d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 261d44252ecSDouglas Gregor // that depends on this particular module, push it into the 262d44252ecSDouglas Gregor // queue to be visited. 263bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 264d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 265d44252ecSDouglas Gregor Queue.push_back(*M); 266d44252ecSDouglas Gregor } 267d44252ecSDouglas Gregor } 268e41d7feaSDouglas Gregor 269e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 2707211ac15SDouglas Gregor 271e97cd90aSDouglas Gregor delete FirstVisitState; 272e97cd90aSDouglas Gregor FirstVisitState = 0; 273e41d7feaSDouglas Gregor } 274e41d7feaSDouglas Gregor 275e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 276e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 277e41d7feaSDouglas Gregor 2787211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 2797211ac15SDouglas Gregor // module index, mark every module file in common with the global module 2807211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 2817211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 2827211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 2837211ac15SDouglas Gregor { 2847211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 2857029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 286e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 2877211ac15SDouglas Gregor } 2887211ac15SDouglas Gregor } 2897211ac15SDouglas Gregor 290e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 291e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 292e41d7feaSDouglas Gregor // Should we skip this module file? 293e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 294e41d7feaSDouglas Gregor continue; 295e41d7feaSDouglas Gregor 296e41d7feaSDouglas Gregor // Visit the module. 297e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 298e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 299e41d7feaSDouglas Gregor if (!Visitor(*CurrentModule, UserData)) 300e41d7feaSDouglas Gregor continue; 301e41d7feaSDouglas Gregor 302e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 303e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 304e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 305e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 306e41d7feaSDouglas Gregor do { 307e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 308e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 309e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 310e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 311e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 312e41d7feaSDouglas Gregor M != MEnd; ++M) { 313e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 314e97cd90aSDouglas Gregor State->Stack.push_back(*M); 315e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 316e41d7feaSDouglas Gregor } 317e41d7feaSDouglas Gregor } 318e41d7feaSDouglas Gregor 319e97cd90aSDouglas Gregor if (State->Stack.empty()) 320e41d7feaSDouglas Gregor break; 321e41d7feaSDouglas Gregor 322e41d7feaSDouglas Gregor // Pop the next module off the stack. 323e97cd90aSDouglas Gregor NextModule = State->Stack.back(); 324e97cd90aSDouglas Gregor State->Stack.pop_back(); 325e41d7feaSDouglas Gregor } while (true); 326e41d7feaSDouglas Gregor } 327e97cd90aSDouglas Gregor 328e97cd90aSDouglas Gregor returnVisitState(State); 329d44252ecSDouglas Gregor } 330d44252ecSDouglas Gregor 331d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module. 332de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M, 333de3ef502SDouglas Gregor bool (*Visitor)(ModuleFile &M, bool Preorder, 334d44252ecSDouglas Gregor void *UserData), 335d44252ecSDouglas Gregor void *UserData, 336bdb259d2SDouglas Gregor SmallVectorImpl<bool> &Visited) { 337d44252ecSDouglas Gregor // Preorder visitation 338d44252ecSDouglas Gregor if (Visitor(M, /*Preorder=*/true, UserData)) 339d44252ecSDouglas Gregor return true; 340d44252ecSDouglas Gregor 341d44252ecSDouglas Gregor // Visit children 342de3ef502SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), 343d44252ecSDouglas Gregor IMEnd = M.Imports.end(); 344d44252ecSDouglas Gregor IM != IMEnd; ++IM) { 345bdb259d2SDouglas Gregor if (Visited[(*IM)->Index]) 346d44252ecSDouglas Gregor continue; 347bdb259d2SDouglas Gregor Visited[(*IM)->Index] = true; 348d44252ecSDouglas Gregor 349d44252ecSDouglas Gregor if (visitDepthFirst(**IM, Visitor, UserData, Visited)) 350d44252ecSDouglas Gregor return true; 351d44252ecSDouglas Gregor } 352d44252ecSDouglas Gregor 353d44252ecSDouglas Gregor // Postorder visitation 354d44252ecSDouglas Gregor return Visitor(M, /*Preorder=*/false, UserData); 355d44252ecSDouglas Gregor } 356d44252ecSDouglas Gregor 357de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 358d44252ecSDouglas Gregor void *UserData), 359d44252ecSDouglas Gregor void *UserData) { 360bdb259d2SDouglas Gregor SmallVector<bool, 16> Visited(size(), false); 361d44252ecSDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 362bdb259d2SDouglas Gregor if (Visited[Chain[I]->Index]) 363d44252ecSDouglas Gregor continue; 364bdb259d2SDouglas Gregor Visited[Chain[I]->Index] = true; 365d44252ecSDouglas Gregor 366d44252ecSDouglas Gregor if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited)) 367d44252ecSDouglas Gregor return; 368d44252ecSDouglas Gregor } 369d44252ecSDouglas Gregor } 3709d7c1a2aSDouglas Gregor 3717029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 3727029ce1aSDouglas Gregor off_t ExpectedSize, 3737029ce1aSDouglas Gregor time_t ExpectedModTime, 3747029ce1aSDouglas Gregor const FileEntry *&File) { 3757029ce1aSDouglas Gregor File = FileMgr.getFile(FileName, /*openFile=*/false, /*cacheFailure=*/false); 3767029ce1aSDouglas Gregor 3777029ce1aSDouglas Gregor if (!File && FileName != "-") { 3787029ce1aSDouglas Gregor return false; 3797029ce1aSDouglas Gregor } 3807029ce1aSDouglas Gregor 3817029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 3827029ce1aSDouglas Gregor (ExpectedModTime && ExpectedModTime != File->getModificationTime())) { 3837029ce1aSDouglas Gregor return true; 3847029ce1aSDouglas Gregor } 3857029ce1aSDouglas Gregor 3867029ce1aSDouglas Gregor return false; 3877029ce1aSDouglas Gregor } 3887029ce1aSDouglas Gregor 3899d7c1a2aSDouglas Gregor #ifndef NDEBUG 3909d7c1a2aSDouglas Gregor namespace llvm { 3919d7c1a2aSDouglas Gregor template<> 3929d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 393de3ef502SDouglas Gregor typedef ModuleFile NodeType; 394de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 3959d7c1a2aSDouglas Gregor typedef ModuleManager::ModuleConstIterator nodes_iterator; 3969d7c1a2aSDouglas Gregor 3979d7c1a2aSDouglas Gregor static ChildIteratorType child_begin(NodeType *Node) { 3989d7c1a2aSDouglas Gregor return Node->Imports.begin(); 3999d7c1a2aSDouglas Gregor } 4009d7c1a2aSDouglas Gregor 4019d7c1a2aSDouglas Gregor static ChildIteratorType child_end(NodeType *Node) { 4029d7c1a2aSDouglas Gregor return Node->Imports.end(); 4039d7c1a2aSDouglas Gregor } 4049d7c1a2aSDouglas Gregor 4059d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 4069d7c1a2aSDouglas Gregor return Manager.begin(); 4079d7c1a2aSDouglas Gregor } 4089d7c1a2aSDouglas Gregor 4099d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 4109d7c1a2aSDouglas Gregor return Manager.end(); 4119d7c1a2aSDouglas Gregor } 4129d7c1a2aSDouglas Gregor }; 4139d7c1a2aSDouglas Gregor 4149d7c1a2aSDouglas Gregor template<> 4159d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4169d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4179d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4189d7c1a2aSDouglas Gregor 4199d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4209d7c1a2aSDouglas Gregor return true; 4219d7c1a2aSDouglas Gregor } 4229d7c1a2aSDouglas Gregor 423de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 4249d7c1a2aSDouglas Gregor return llvm::sys::path::stem(M->FileName); 4259d7c1a2aSDouglas Gregor } 4269d7c1a2aSDouglas Gregor }; 4279d7c1a2aSDouglas Gregor } 4289d7c1a2aSDouglas Gregor 4299d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4309d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4319d7c1a2aSDouglas Gregor } 4329d7c1a2aSDouglas Gregor #endif 433