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 //===----------------------------------------------------------------------===// 14*7029ce1aSDouglas 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" 18*7029ce1aSDouglas 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 41*7029ce1aSDouglas Gregor ModuleManager::AddModuleResult 42d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type, 436fb03aeaSDouglas Gregor SourceLocation ImportLoc, ModuleFile *ImportedBy, 44*7029ce1aSDouglas Gregor unsigned Generation, 45*7029ce1aSDouglas Gregor off_t ExpectedSize, time_t ExpectedModTime, 46*7029ce1aSDouglas Gregor ModuleFile *&Module, 47*7029ce1aSDouglas Gregor std::string &ErrorStr) { 48*7029ce1aSDouglas Gregor Module = 0; 49*7029ce1aSDouglas Gregor 50*7029ce1aSDouglas Gregor // Look for the file entry. This only fails if the expected size or 51*7029ce1aSDouglas Gregor // modification time differ. 52*7029ce1aSDouglas Gregor const FileEntry *Entry; 53*7029ce1aSDouglas Gregor if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) 54*7029ce1aSDouglas Gregor return OutOfDate; 55*7029ce1aSDouglas Gregor 56d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 57d44252ecSDouglas Gregor ErrorStr = "file not found"; 58*7029ce1aSDouglas Gregor return Missing; 59d44252ecSDouglas Gregor } 60d44252ecSDouglas Gregor 61d44252ecSDouglas Gregor // Check whether we already loaded this module, before 62*7029ce1aSDouglas Gregor AddModuleResult Result = AlreadyLoaded; 63de3ef502SDouglas Gregor ModuleFile *&ModuleEntry = Modules[Entry]; 64d44252ecSDouglas Gregor bool NewModule = false; 65d44252ecSDouglas Gregor if (!ModuleEntry) { 66d44252ecSDouglas Gregor // Allocate a new module. 674fc9f3e8SDouglas Gregor ModuleFile *New = new ModuleFile(Type, Generation); 68bdb259d2SDouglas Gregor New->Index = Chain.size(); 69d44252ecSDouglas Gregor New->FileName = FileName.str(); 70aedf7144SArgyrios Kyrtzidis New->File = Entry; 716fb03aeaSDouglas Gregor New->ImportLoc = ImportLoc; 72d44252ecSDouglas Gregor Chain.push_back(New); 73d44252ecSDouglas Gregor NewModule = true; 74d44252ecSDouglas Gregor ModuleEntry = New; 75d44252ecSDouglas Gregor 76d44252ecSDouglas Gregor // Load the contents of the module 77d44252ecSDouglas Gregor if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) { 78d44252ecSDouglas Gregor // The buffer was already provided for us. 79d44252ecSDouglas Gregor assert(Buffer && "Passed null buffer"); 80d44252ecSDouglas Gregor New->Buffer.reset(Buffer); 81d44252ecSDouglas Gregor } else { 82d44252ecSDouglas Gregor // Open the AST file. 83d44252ecSDouglas Gregor llvm::error_code ec; 84d44252ecSDouglas Gregor if (FileName == "-") { 85d44252ecSDouglas Gregor ec = llvm::MemoryBuffer::getSTDIN(New->Buffer); 86d44252ecSDouglas Gregor if (ec) 87d44252ecSDouglas Gregor ErrorStr = ec.message(); 88d44252ecSDouglas Gregor } else 89d44252ecSDouglas Gregor New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr)); 90d44252ecSDouglas Gregor 91d44252ecSDouglas Gregor if (!New->Buffer) 92*7029ce1aSDouglas Gregor return Missing; 93d44252ecSDouglas Gregor } 94d44252ecSDouglas Gregor 95d44252ecSDouglas Gregor // Initialize the stream 96d44252ecSDouglas Gregor New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(), 97*7029ce1aSDouglas Gregor (const unsigned char *)New->Buffer->getBufferEnd()); 98*7029ce1aSDouglas Gregor 99*7029ce1aSDouglas Gregor Result = NewlyLoaded; 100*7029ce1aSDouglas Gregor } 101d44252ecSDouglas Gregor 102d44252ecSDouglas Gregor if (ImportedBy) { 103d44252ecSDouglas Gregor ModuleEntry->ImportedBy.insert(ImportedBy); 104d44252ecSDouglas Gregor ImportedBy->Imports.insert(ModuleEntry); 105d44252ecSDouglas Gregor } else { 1066fb03aeaSDouglas Gregor if (!ModuleEntry->DirectlyImported) 1076fb03aeaSDouglas Gregor ModuleEntry->ImportLoc = ImportLoc; 1086fb03aeaSDouglas Gregor 109d44252ecSDouglas Gregor ModuleEntry->DirectlyImported = true; 110d44252ecSDouglas Gregor } 111d44252ecSDouglas Gregor 112*7029ce1aSDouglas Gregor Module = ModuleEntry; 113*7029ce1aSDouglas Gregor return NewModule? NewlyLoaded : AlreadyLoaded; 114d44252ecSDouglas Gregor } 115d44252ecSDouglas Gregor 116188dbef2SDouglas Gregor namespace { 117188dbef2SDouglas Gregor /// \brief Predicate that checks whether a module file occurs within 118188dbef2SDouglas Gregor /// the given set. 119188dbef2SDouglas Gregor class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> { 120188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> &Removed; 121188dbef2SDouglas Gregor 122188dbef2SDouglas Gregor public: 123188dbef2SDouglas Gregor IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed) 124188dbef2SDouglas Gregor : Removed(Removed) { } 125188dbef2SDouglas Gregor 126188dbef2SDouglas Gregor bool operator()(ModuleFile *MF) const { 127188dbef2SDouglas Gregor return Removed.count(MF); 128188dbef2SDouglas Gregor } 129188dbef2SDouglas Gregor }; 130188dbef2SDouglas Gregor } 131188dbef2SDouglas Gregor 132*7029ce1aSDouglas Gregor void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last, 133*7029ce1aSDouglas Gregor ModuleMap *modMap) { 134188dbef2SDouglas Gregor if (first == last) 135188dbef2SDouglas Gregor return; 136188dbef2SDouglas Gregor 137188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 138188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); 139188dbef2SDouglas Gregor 140188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 141188dbef2SDouglas Gregor IsInModuleFileSet checkInSet(victimSet); 142188dbef2SDouglas Gregor for (unsigned i = 0, n = Chain.size(); i != n; ++i) { 143188dbef2SDouglas Gregor Chain[i]->ImportedBy.remove_if(checkInSet); 144188dbef2SDouglas Gregor } 145188dbef2SDouglas Gregor 146188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 147188dbef2SDouglas Gregor for (ModuleIterator victim = first; victim != last; ++victim) { 148188dbef2SDouglas Gregor Modules.erase((*victim)->File); 149*7029ce1aSDouglas Gregor 150*7029ce1aSDouglas Gregor FileMgr.invalidateCache((*victim)->File); 151*7029ce1aSDouglas Gregor if (modMap) { 152*7029ce1aSDouglas Gregor StringRef ModuleName = llvm::sys::path::stem((*victim)->FileName); 153*7029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 154*7029ce1aSDouglas Gregor mod->setASTFile(0); 155*7029ce1aSDouglas Gregor } 156*7029ce1aSDouglas Gregor } 157188dbef2SDouglas Gregor delete *victim; 158188dbef2SDouglas Gregor } 159188dbef2SDouglas Gregor 160188dbef2SDouglas Gregor // Remove the modules from the chain. 161188dbef2SDouglas Gregor Chain.erase(first, last); 162188dbef2SDouglas Gregor } 163188dbef2SDouglas Gregor 164d44252ecSDouglas Gregor void ModuleManager::addInMemoryBuffer(StringRef FileName, 165d44252ecSDouglas Gregor llvm::MemoryBuffer *Buffer) { 166d44252ecSDouglas Gregor 167d44252ecSDouglas Gregor const FileEntry *Entry = FileMgr.getVirtualFile(FileName, 168d44252ecSDouglas Gregor Buffer->getBufferSize(), 0); 169d44252ecSDouglas Gregor InMemoryBuffers[Entry] = Buffer; 170d44252ecSDouglas Gregor } 171d44252ecSDouglas Gregor 1727211ac15SDouglas Gregor void ModuleManager::updateModulesInCommonWithGlobalIndex() { 1737211ac15SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 1747211ac15SDouglas Gregor 1757211ac15SDouglas Gregor if (!GlobalIndex) 1767211ac15SDouglas Gregor return; 1777211ac15SDouglas Gregor 1787211ac15SDouglas Gregor // Collect the set of modules known to the global index. 179*7029ce1aSDouglas Gregor GlobalIndex->noteAdditionalModulesLoaded(); 180*7029ce1aSDouglas Gregor GlobalIndex->getKnownModules(ModulesInCommonWithGlobalIndex); 1817211ac15SDouglas Gregor } 1827211ac15SDouglas Gregor 183e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 184e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 185e97cd90aSDouglas Gregor if (FirstVisitState) { 186e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 187e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 188e97cd90aSDouglas Gregor Result->NextState = 0; 189e97cd90aSDouglas Gregor return Result; 190e97cd90aSDouglas Gregor } 191e97cd90aSDouglas Gregor 192e97cd90aSDouglas Gregor // Allocate and return a new state. 193e97cd90aSDouglas Gregor return new VisitState(size()); 194e97cd90aSDouglas Gregor } 195e97cd90aSDouglas Gregor 196e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 197e97cd90aSDouglas Gregor assert(State->NextState == 0 && "Visited state is in list?"); 198e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 199e97cd90aSDouglas Gregor FirstVisitState = State; 200e97cd90aSDouglas Gregor } 201e97cd90aSDouglas Gregor 2027211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 2037211ac15SDouglas Gregor GlobalIndex = Index; 204*7029ce1aSDouglas Gregor if (GlobalIndex) { 205*7029ce1aSDouglas Gregor GlobalIndex->setResolver(this); 206*7029ce1aSDouglas Gregor } 2077211ac15SDouglas Gregor updateModulesInCommonWithGlobalIndex(); 2087211ac15SDouglas Gregor } 2097211ac15SDouglas Gregor 2107211ac15SDouglas Gregor ModuleManager::ModuleManager(FileManager &FileMgr) 211e97cd90aSDouglas Gregor : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(0) { } 212d44252ecSDouglas Gregor 213d44252ecSDouglas Gregor ModuleManager::~ModuleManager() { 214d44252ecSDouglas Gregor for (unsigned i = 0, e = Chain.size(); i != e; ++i) 215d44252ecSDouglas Gregor delete Chain[e - i - 1]; 216e97cd90aSDouglas Gregor delete FirstVisitState; 217d44252ecSDouglas Gregor } 218d44252ecSDouglas Gregor 2197211ac15SDouglas Gregor void 2207211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), 2217211ac15SDouglas Gregor void *UserData, 222*7029ce1aSDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) { 2237211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 224e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 225d44252ecSDouglas Gregor unsigned N = size(); 226e41d7feaSDouglas Gregor VisitOrder.clear(); 227e41d7feaSDouglas Gregor VisitOrder.reserve(N); 228d44252ecSDouglas Gregor 229d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 230d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 231d44252ecSDouglas Gregor // to seed the queue. 232de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 233d44252ecSDouglas Gregor Queue.reserve(N); 234bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 235bdb259d2SDouglas Gregor UnusedIncomingEdges.reserve(size()); 236d44252ecSDouglas Gregor for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) { 237d44252ecSDouglas Gregor if (unsigned Size = (*M)->ImportedBy.size()) 238bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(Size); 239bdb259d2SDouglas Gregor else { 240bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(0); 241d44252ecSDouglas Gregor Queue.push_back(*M); 242d44252ecSDouglas Gregor } 243bdb259d2SDouglas Gregor } 244d44252ecSDouglas Gregor 245e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 246e41d7feaSDouglas Gregor // of its dependencies. 247d44252ecSDouglas Gregor unsigned QueueStart = 0; 248d44252ecSDouglas Gregor while (QueueStart < Queue.size()) { 249de3ef502SDouglas Gregor ModuleFile *CurrentModule = Queue[QueueStart++]; 250e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 251d44252ecSDouglas Gregor 252d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 253d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 254bdb259d2SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 255bdb259d2SDouglas Gregor M = CurrentModule->Imports.begin(), 256d44252ecSDouglas Gregor MEnd = CurrentModule->Imports.end(); 257d44252ecSDouglas Gregor M != MEnd; ++M) { 258d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 259d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 260d44252ecSDouglas Gregor // that depends on this particular module, push it into the 261d44252ecSDouglas Gregor // queue to be visited. 262bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 263d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 264d44252ecSDouglas Gregor Queue.push_back(*M); 265d44252ecSDouglas Gregor } 266d44252ecSDouglas Gregor } 267e41d7feaSDouglas Gregor 268e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 2697211ac15SDouglas Gregor 2707211ac15SDouglas Gregor // We may need to update the set of modules we have in common with the 2717211ac15SDouglas Gregor // global module index, since modules could have been added to the module 2727211ac15SDouglas Gregor // manager since we loaded the global module index. 2737211ac15SDouglas Gregor updateModulesInCommonWithGlobalIndex(); 274e97cd90aSDouglas Gregor 275e97cd90aSDouglas Gregor delete FirstVisitState; 276e97cd90aSDouglas Gregor FirstVisitState = 0; 277e41d7feaSDouglas Gregor } 278e41d7feaSDouglas Gregor 279e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 280e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 281e41d7feaSDouglas Gregor 2827211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 2837211ac15SDouglas Gregor // module index, mark every module file in common with the global module 2847211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 2857211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 2867211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 2877211ac15SDouglas Gregor { 2887211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 289*7029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 290e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 2917211ac15SDouglas Gregor } 2927211ac15SDouglas Gregor } 2937211ac15SDouglas Gregor 294e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 295e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 296e41d7feaSDouglas Gregor // Should we skip this module file? 297e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 298e41d7feaSDouglas Gregor continue; 299e41d7feaSDouglas Gregor 300e41d7feaSDouglas Gregor // Visit the module. 301e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 302e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 303e41d7feaSDouglas Gregor if (!Visitor(*CurrentModule, UserData)) 304e41d7feaSDouglas Gregor continue; 305e41d7feaSDouglas Gregor 306e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 307e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 308e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 309e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 310e41d7feaSDouglas Gregor do { 311e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 312e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 313e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 314e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 315e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 316e41d7feaSDouglas Gregor M != MEnd; ++M) { 317e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 318e97cd90aSDouglas Gregor State->Stack.push_back(*M); 319e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 320e41d7feaSDouglas Gregor } 321e41d7feaSDouglas Gregor } 322e41d7feaSDouglas Gregor 323e97cd90aSDouglas Gregor if (State->Stack.empty()) 324e41d7feaSDouglas Gregor break; 325e41d7feaSDouglas Gregor 326e41d7feaSDouglas Gregor // Pop the next module off the stack. 327e97cd90aSDouglas Gregor NextModule = State->Stack.back(); 328e97cd90aSDouglas Gregor State->Stack.pop_back(); 329e41d7feaSDouglas Gregor } while (true); 330e41d7feaSDouglas Gregor } 331e97cd90aSDouglas Gregor 332e97cd90aSDouglas Gregor returnVisitState(State); 333d44252ecSDouglas Gregor } 334d44252ecSDouglas Gregor 335d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module. 336de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M, 337de3ef502SDouglas Gregor bool (*Visitor)(ModuleFile &M, bool Preorder, 338d44252ecSDouglas Gregor void *UserData), 339d44252ecSDouglas Gregor void *UserData, 340bdb259d2SDouglas Gregor SmallVectorImpl<bool> &Visited) { 341d44252ecSDouglas Gregor // Preorder visitation 342d44252ecSDouglas Gregor if (Visitor(M, /*Preorder=*/true, UserData)) 343d44252ecSDouglas Gregor return true; 344d44252ecSDouglas Gregor 345d44252ecSDouglas Gregor // Visit children 346de3ef502SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), 347d44252ecSDouglas Gregor IMEnd = M.Imports.end(); 348d44252ecSDouglas Gregor IM != IMEnd; ++IM) { 349bdb259d2SDouglas Gregor if (Visited[(*IM)->Index]) 350d44252ecSDouglas Gregor continue; 351bdb259d2SDouglas Gregor Visited[(*IM)->Index] = true; 352d44252ecSDouglas Gregor 353d44252ecSDouglas Gregor if (visitDepthFirst(**IM, Visitor, UserData, Visited)) 354d44252ecSDouglas Gregor return true; 355d44252ecSDouglas Gregor } 356d44252ecSDouglas Gregor 357d44252ecSDouglas Gregor // Postorder visitation 358d44252ecSDouglas Gregor return Visitor(M, /*Preorder=*/false, UserData); 359d44252ecSDouglas Gregor } 360d44252ecSDouglas Gregor 361de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 362d44252ecSDouglas Gregor void *UserData), 363d44252ecSDouglas Gregor void *UserData) { 364bdb259d2SDouglas Gregor SmallVector<bool, 16> Visited(size(), false); 365d44252ecSDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 366bdb259d2SDouglas Gregor if (Visited[Chain[I]->Index]) 367d44252ecSDouglas Gregor continue; 368bdb259d2SDouglas Gregor Visited[Chain[I]->Index] = true; 369d44252ecSDouglas Gregor 370d44252ecSDouglas Gregor if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited)) 371d44252ecSDouglas Gregor return; 372d44252ecSDouglas Gregor } 373d44252ecSDouglas Gregor } 3749d7c1a2aSDouglas Gregor 375*7029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 376*7029ce1aSDouglas Gregor off_t ExpectedSize, 377*7029ce1aSDouglas Gregor time_t ExpectedModTime, 378*7029ce1aSDouglas Gregor const FileEntry *&File) { 379*7029ce1aSDouglas Gregor File = FileMgr.getFile(FileName, /*openFile=*/false, /*cacheFailure=*/false); 380*7029ce1aSDouglas Gregor 381*7029ce1aSDouglas Gregor if (!File && FileName != "-") { 382*7029ce1aSDouglas Gregor return false; 383*7029ce1aSDouglas Gregor } 384*7029ce1aSDouglas Gregor 385*7029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 386*7029ce1aSDouglas Gregor (ExpectedModTime && ExpectedModTime != File->getModificationTime())) { 387*7029ce1aSDouglas Gregor return true; 388*7029ce1aSDouglas Gregor } 389*7029ce1aSDouglas Gregor 390*7029ce1aSDouglas Gregor return false; 391*7029ce1aSDouglas Gregor } 392*7029ce1aSDouglas Gregor 393*7029ce1aSDouglas Gregor bool ModuleManager::resolveModuleFileName(StringRef FileName, 394*7029ce1aSDouglas Gregor off_t ExpectedSize, 395*7029ce1aSDouglas Gregor time_t ExpectedModTime, 396*7029ce1aSDouglas Gregor ModuleFile *&File) { 397*7029ce1aSDouglas Gregor File = 0; 398*7029ce1aSDouglas Gregor 399*7029ce1aSDouglas Gregor // Look for the file entry corresponding to this name. 400*7029ce1aSDouglas Gregor const FileEntry *F; 401*7029ce1aSDouglas Gregor if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, F)) 402*7029ce1aSDouglas Gregor return true; 403*7029ce1aSDouglas Gregor 404*7029ce1aSDouglas Gregor // If there is no file, we've succeeded (trivially). 405*7029ce1aSDouglas Gregor if (!F) 406*7029ce1aSDouglas Gregor return false; 407*7029ce1aSDouglas Gregor 408*7029ce1aSDouglas Gregor // Determine whether we have a module file associated with this file entry. 409*7029ce1aSDouglas Gregor llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known 410*7029ce1aSDouglas Gregor = Modules.find(F); 411*7029ce1aSDouglas Gregor if (Known == Modules.end()) { 412*7029ce1aSDouglas Gregor // We don't know about this module file; invalidate the cache. 413*7029ce1aSDouglas Gregor FileMgr.invalidateCache(F); 414*7029ce1aSDouglas Gregor return false; 415*7029ce1aSDouglas Gregor } 416*7029ce1aSDouglas Gregor 417*7029ce1aSDouglas Gregor File = Known->second; 418*7029ce1aSDouglas Gregor return false; 419*7029ce1aSDouglas Gregor } 420*7029ce1aSDouglas Gregor 4219d7c1a2aSDouglas Gregor #ifndef NDEBUG 4229d7c1a2aSDouglas Gregor namespace llvm { 4239d7c1a2aSDouglas Gregor template<> 4249d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 425de3ef502SDouglas Gregor typedef ModuleFile NodeType; 426de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 4279d7c1a2aSDouglas Gregor typedef ModuleManager::ModuleConstIterator nodes_iterator; 4289d7c1a2aSDouglas Gregor 4299d7c1a2aSDouglas Gregor static ChildIteratorType child_begin(NodeType *Node) { 4309d7c1a2aSDouglas Gregor return Node->Imports.begin(); 4319d7c1a2aSDouglas Gregor } 4329d7c1a2aSDouglas Gregor 4339d7c1a2aSDouglas Gregor static ChildIteratorType child_end(NodeType *Node) { 4349d7c1a2aSDouglas Gregor return Node->Imports.end(); 4359d7c1a2aSDouglas Gregor } 4369d7c1a2aSDouglas Gregor 4379d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 4389d7c1a2aSDouglas Gregor return Manager.begin(); 4399d7c1a2aSDouglas Gregor } 4409d7c1a2aSDouglas Gregor 4419d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 4429d7c1a2aSDouglas Gregor return Manager.end(); 4439d7c1a2aSDouglas Gregor } 4449d7c1a2aSDouglas Gregor }; 4459d7c1a2aSDouglas Gregor 4469d7c1a2aSDouglas Gregor template<> 4479d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4489d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4499d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4509d7c1a2aSDouglas Gregor 4519d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4529d7c1a2aSDouglas Gregor return true; 4539d7c1a2aSDouglas Gregor } 4549d7c1a2aSDouglas Gregor 455de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 4569d7c1a2aSDouglas Gregor return llvm::sys::path::stem(M->FileName); 4579d7c1a2aSDouglas Gregor } 4589d7c1a2aSDouglas Gregor }; 4599d7c1a2aSDouglas Gregor } 4609d7c1a2aSDouglas Gregor 4619d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4629d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4639d7c1a2aSDouglas Gregor } 4649d7c1a2aSDouglas Gregor #endif 465