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" 157211ac15SDouglas Gregor #include "clang/Serialization/GlobalModuleIndex.h" 16af04f98cSBenjamin Kramer #include "clang/Serialization/ModuleManager.h" 17d44252ecSDouglas Gregor #include "llvm/Support/MemoryBuffer.h" 18552c169eSRafael Espindola #include "llvm/Support/Path.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); 32bf7fc9c5SDouglas Gregor if (Entry) 33bf7fc9c5SDouglas Gregor return lookup(Entry); 34bf7fc9c5SDouglas Gregor 35bf7fc9c5SDouglas Gregor return 0; 36bf7fc9c5SDouglas Gregor } 37bf7fc9c5SDouglas Gregor 38bf7fc9c5SDouglas Gregor ModuleFile *ModuleManager::lookup(const FileEntry *File) { 39bf7fc9c5SDouglas Gregor llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known 40bf7fc9c5SDouglas Gregor = Modules.find(File); 41bf7fc9c5SDouglas Gregor if (Known == Modules.end()) 42bf7fc9c5SDouglas Gregor return 0; 43bf7fc9c5SDouglas Gregor 44bf7fc9c5SDouglas Gregor return Known->second; 45d44252ecSDouglas Gregor } 46d44252ecSDouglas Gregor 47d44252ecSDouglas Gregor llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) { 48dadd85dcSDouglas Gregor const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, 49dadd85dcSDouglas Gregor /*cacheFailure=*/false); 50d44252ecSDouglas Gregor return InMemoryBuffers[Entry]; 51d44252ecSDouglas Gregor } 52d44252ecSDouglas Gregor 537029ce1aSDouglas Gregor ModuleManager::AddModuleResult 54d44252ecSDouglas Gregor ModuleManager::addModule(StringRef FileName, ModuleKind Type, 556fb03aeaSDouglas Gregor SourceLocation ImportLoc, ModuleFile *ImportedBy, 567029ce1aSDouglas Gregor unsigned Generation, 577029ce1aSDouglas Gregor off_t ExpectedSize, time_t ExpectedModTime, 587029ce1aSDouglas Gregor ModuleFile *&Module, 597029ce1aSDouglas Gregor std::string &ErrorStr) { 607029ce1aSDouglas Gregor Module = 0; 617029ce1aSDouglas Gregor 627029ce1aSDouglas Gregor // Look for the file entry. This only fails if the expected size or 637029ce1aSDouglas Gregor // modification time differ. 647029ce1aSDouglas Gregor const FileEntry *Entry; 65*c27d0d5eSEli Friedman if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { 66*c27d0d5eSEli Friedman ErrorStr = "module file out of date"; 677029ce1aSDouglas Gregor return OutOfDate; 68*c27d0d5eSEli Friedman } 697029ce1aSDouglas Gregor 70d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 71*c27d0d5eSEli Friedman ErrorStr = "module file not found"; 727029ce1aSDouglas Gregor return Missing; 73d44252ecSDouglas Gregor } 74d44252ecSDouglas Gregor 75d44252ecSDouglas Gregor // Check whether we already loaded this module, before 76de3ef502SDouglas Gregor ModuleFile *&ModuleEntry = Modules[Entry]; 77d44252ecSDouglas Gregor bool NewModule = false; 78d44252ecSDouglas Gregor if (!ModuleEntry) { 79d44252ecSDouglas Gregor // Allocate a new module. 804fc9f3e8SDouglas Gregor ModuleFile *New = new ModuleFile(Type, Generation); 81bdb259d2SDouglas Gregor New->Index = Chain.size(); 82d44252ecSDouglas Gregor New->FileName = FileName.str(); 83aedf7144SArgyrios Kyrtzidis New->File = Entry; 846fb03aeaSDouglas Gregor New->ImportLoc = ImportLoc; 85d44252ecSDouglas Gregor Chain.push_back(New); 86d44252ecSDouglas Gregor NewModule = true; 87d44252ecSDouglas Gregor ModuleEntry = New; 88d44252ecSDouglas Gregor 89d44252ecSDouglas Gregor // Load the contents of the module 90d44252ecSDouglas Gregor if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) { 91d44252ecSDouglas Gregor // The buffer was already provided for us. 92d44252ecSDouglas Gregor assert(Buffer && "Passed null buffer"); 93d44252ecSDouglas Gregor New->Buffer.reset(Buffer); 94d44252ecSDouglas Gregor } else { 95d44252ecSDouglas Gregor // Open the AST file. 96d44252ecSDouglas Gregor llvm::error_code ec; 97d44252ecSDouglas Gregor if (FileName == "-") { 98d44252ecSDouglas Gregor ec = llvm::MemoryBuffer::getSTDIN(New->Buffer); 99d44252ecSDouglas Gregor if (ec) 100d44252ecSDouglas Gregor ErrorStr = ec.message(); 101d44252ecSDouglas Gregor } else 102d44252ecSDouglas Gregor New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr)); 103d44252ecSDouglas Gregor 104d44252ecSDouglas Gregor if (!New->Buffer) 1057029ce1aSDouglas Gregor return Missing; 106d44252ecSDouglas Gregor } 107d44252ecSDouglas Gregor 108d44252ecSDouglas Gregor // Initialize the stream 109d44252ecSDouglas Gregor New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(), 1107029ce1aSDouglas Gregor (const unsigned char *)New->Buffer->getBufferEnd()); 1117029ce1aSDouglas Gregor } 112d44252ecSDouglas Gregor 113d44252ecSDouglas Gregor if (ImportedBy) { 114d44252ecSDouglas Gregor ModuleEntry->ImportedBy.insert(ImportedBy); 115d44252ecSDouglas Gregor ImportedBy->Imports.insert(ModuleEntry); 116d44252ecSDouglas Gregor } else { 1176fb03aeaSDouglas Gregor if (!ModuleEntry->DirectlyImported) 1186fb03aeaSDouglas Gregor ModuleEntry->ImportLoc = ImportLoc; 1196fb03aeaSDouglas Gregor 120d44252ecSDouglas Gregor ModuleEntry->DirectlyImported = true; 121d44252ecSDouglas Gregor } 122d44252ecSDouglas Gregor 1237029ce1aSDouglas Gregor Module = ModuleEntry; 1247029ce1aSDouglas Gregor return NewModule? NewlyLoaded : AlreadyLoaded; 125d44252ecSDouglas Gregor } 126d44252ecSDouglas Gregor 127188dbef2SDouglas Gregor namespace { 128188dbef2SDouglas Gregor /// \brief Predicate that checks whether a module file occurs within 129188dbef2SDouglas Gregor /// the given set. 130188dbef2SDouglas Gregor class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> { 131188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> &Removed; 132188dbef2SDouglas Gregor 133188dbef2SDouglas Gregor public: 134188dbef2SDouglas Gregor IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed) 135188dbef2SDouglas Gregor : Removed(Removed) { } 136188dbef2SDouglas Gregor 137188dbef2SDouglas Gregor bool operator()(ModuleFile *MF) const { 138188dbef2SDouglas Gregor return Removed.count(MF); 139188dbef2SDouglas Gregor } 140188dbef2SDouglas Gregor }; 141188dbef2SDouglas Gregor } 142188dbef2SDouglas Gregor 1437029ce1aSDouglas Gregor void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last, 1447029ce1aSDouglas Gregor ModuleMap *modMap) { 145188dbef2SDouglas Gregor if (first == last) 146188dbef2SDouglas Gregor return; 147188dbef2SDouglas Gregor 148188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 149188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); 150188dbef2SDouglas Gregor 151188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 152188dbef2SDouglas Gregor IsInModuleFileSet checkInSet(victimSet); 153188dbef2SDouglas Gregor for (unsigned i = 0, n = Chain.size(); i != n; ++i) { 154188dbef2SDouglas Gregor Chain[i]->ImportedBy.remove_if(checkInSet); 155188dbef2SDouglas Gregor } 156188dbef2SDouglas Gregor 157188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 158188dbef2SDouglas Gregor for (ModuleIterator victim = first; victim != last; ++victim) { 159188dbef2SDouglas Gregor Modules.erase((*victim)->File); 1607029ce1aSDouglas Gregor 1617029ce1aSDouglas Gregor FileMgr.invalidateCache((*victim)->File); 1627029ce1aSDouglas Gregor if (modMap) { 1637029ce1aSDouglas Gregor StringRef ModuleName = llvm::sys::path::stem((*victim)->FileName); 1647029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 1657029ce1aSDouglas Gregor mod->setASTFile(0); 1667029ce1aSDouglas Gregor } 1677029ce1aSDouglas Gregor } 168188dbef2SDouglas Gregor delete *victim; 169188dbef2SDouglas Gregor } 170188dbef2SDouglas Gregor 171188dbef2SDouglas Gregor // Remove the modules from the chain. 172188dbef2SDouglas Gregor Chain.erase(first, last); 173188dbef2SDouglas Gregor } 174188dbef2SDouglas Gregor 175d44252ecSDouglas Gregor void ModuleManager::addInMemoryBuffer(StringRef FileName, 176d44252ecSDouglas Gregor llvm::MemoryBuffer *Buffer) { 177d44252ecSDouglas Gregor 178d44252ecSDouglas Gregor const FileEntry *Entry = FileMgr.getVirtualFile(FileName, 179d44252ecSDouglas Gregor Buffer->getBufferSize(), 0); 180d44252ecSDouglas Gregor InMemoryBuffers[Entry] = Buffer; 181d44252ecSDouglas Gregor } 182d44252ecSDouglas 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; 204603cd869SDouglas Gregor if (!GlobalIndex) { 205603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 206603cd869SDouglas Gregor return; 2077029ce1aSDouglas Gregor } 208603cd869SDouglas Gregor 209603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 210603cd869SDouglas Gregor // loaded. 211603cd869SDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 212603cd869SDouglas Gregor if (!GlobalIndex->loadedModuleFile(Chain[I])) { 213603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(Chain[I]); 214603cd869SDouglas Gregor } 215603cd869SDouglas Gregor } 216603cd869SDouglas Gregor } 217603cd869SDouglas Gregor 218603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 219603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 220603cd869SDouglas Gregor return; 221603cd869SDouglas Gregor 222603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 2237211ac15SDouglas Gregor } 2247211ac15SDouglas Gregor 2257211ac15SDouglas Gregor ModuleManager::ModuleManager(FileManager &FileMgr) 226e97cd90aSDouglas Gregor : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(0) { } 227d44252ecSDouglas Gregor 228d44252ecSDouglas Gregor ModuleManager::~ModuleManager() { 229d44252ecSDouglas Gregor for (unsigned i = 0, e = Chain.size(); i != e; ++i) 230d44252ecSDouglas Gregor delete Chain[e - i - 1]; 231e97cd90aSDouglas Gregor delete FirstVisitState; 232d44252ecSDouglas Gregor } 233d44252ecSDouglas Gregor 2347211ac15SDouglas Gregor void 2357211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), 2367211ac15SDouglas Gregor void *UserData, 2377029ce1aSDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) { 2387211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 239e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 240d44252ecSDouglas Gregor unsigned N = size(); 241e41d7feaSDouglas Gregor VisitOrder.clear(); 242e41d7feaSDouglas Gregor VisitOrder.reserve(N); 243d44252ecSDouglas Gregor 244d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 245d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 246d44252ecSDouglas Gregor // to seed the queue. 247de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 248d44252ecSDouglas Gregor Queue.reserve(N); 249bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 250bdb259d2SDouglas Gregor UnusedIncomingEdges.reserve(size()); 251d44252ecSDouglas Gregor for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) { 252d44252ecSDouglas Gregor if (unsigned Size = (*M)->ImportedBy.size()) 253bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(Size); 254bdb259d2SDouglas Gregor else { 255bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(0); 256d44252ecSDouglas Gregor Queue.push_back(*M); 257d44252ecSDouglas Gregor } 258bdb259d2SDouglas Gregor } 259d44252ecSDouglas Gregor 260e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 261e41d7feaSDouglas Gregor // of its dependencies. 262d44252ecSDouglas Gregor unsigned QueueStart = 0; 263d44252ecSDouglas Gregor while (QueueStart < Queue.size()) { 264de3ef502SDouglas Gregor ModuleFile *CurrentModule = Queue[QueueStart++]; 265e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 266d44252ecSDouglas Gregor 267d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 268d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 269bdb259d2SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 270bdb259d2SDouglas Gregor M = CurrentModule->Imports.begin(), 271d44252ecSDouglas Gregor MEnd = CurrentModule->Imports.end(); 272d44252ecSDouglas Gregor M != MEnd; ++M) { 273d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 274d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 275d44252ecSDouglas Gregor // that depends on this particular module, push it into the 276d44252ecSDouglas Gregor // queue to be visited. 277bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 278d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 279d44252ecSDouglas Gregor Queue.push_back(*M); 280d44252ecSDouglas Gregor } 281d44252ecSDouglas Gregor } 282e41d7feaSDouglas Gregor 283e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 2847211ac15SDouglas Gregor 285e97cd90aSDouglas Gregor delete FirstVisitState; 286e97cd90aSDouglas Gregor FirstVisitState = 0; 287e41d7feaSDouglas Gregor } 288e41d7feaSDouglas Gregor 289e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 290e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 291e41d7feaSDouglas Gregor 2927211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 2937211ac15SDouglas Gregor // module index, mark every module file in common with the global module 2947211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 2957211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 2967211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 2977211ac15SDouglas Gregor { 2987211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 2997029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 300e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 3017211ac15SDouglas Gregor } 3027211ac15SDouglas Gregor } 3037211ac15SDouglas Gregor 304e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 305e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 306e41d7feaSDouglas Gregor // Should we skip this module file? 307e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 308e41d7feaSDouglas Gregor continue; 309e41d7feaSDouglas Gregor 310e41d7feaSDouglas Gregor // Visit the module. 311e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 312e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 313e41d7feaSDouglas Gregor if (!Visitor(*CurrentModule, UserData)) 314e41d7feaSDouglas Gregor continue; 315e41d7feaSDouglas Gregor 316e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 317e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 318e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 319e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 320e41d7feaSDouglas Gregor do { 321e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 322e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 323e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 324e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 325e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 326e41d7feaSDouglas Gregor M != MEnd; ++M) { 327e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 328e97cd90aSDouglas Gregor State->Stack.push_back(*M); 329e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 330e41d7feaSDouglas Gregor } 331e41d7feaSDouglas Gregor } 332e41d7feaSDouglas Gregor 333e97cd90aSDouglas Gregor if (State->Stack.empty()) 334e41d7feaSDouglas Gregor break; 335e41d7feaSDouglas Gregor 336e41d7feaSDouglas Gregor // Pop the next module off the stack. 33725284cc9SRobert Wilhelm NextModule = State->Stack.pop_back_val(); 338e41d7feaSDouglas Gregor } while (true); 339e41d7feaSDouglas Gregor } 340e97cd90aSDouglas Gregor 341e97cd90aSDouglas Gregor returnVisitState(State); 342d44252ecSDouglas Gregor } 343d44252ecSDouglas Gregor 344d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module. 345de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M, 346de3ef502SDouglas Gregor bool (*Visitor)(ModuleFile &M, bool Preorder, 347d44252ecSDouglas Gregor void *UserData), 348d44252ecSDouglas Gregor void *UserData, 349bdb259d2SDouglas Gregor SmallVectorImpl<bool> &Visited) { 350d44252ecSDouglas Gregor // Preorder visitation 351d44252ecSDouglas Gregor if (Visitor(M, /*Preorder=*/true, UserData)) 352d44252ecSDouglas Gregor return true; 353d44252ecSDouglas Gregor 354d44252ecSDouglas Gregor // Visit children 355de3ef502SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), 356d44252ecSDouglas Gregor IMEnd = M.Imports.end(); 357d44252ecSDouglas Gregor IM != IMEnd; ++IM) { 358bdb259d2SDouglas Gregor if (Visited[(*IM)->Index]) 359d44252ecSDouglas Gregor continue; 360bdb259d2SDouglas Gregor Visited[(*IM)->Index] = true; 361d44252ecSDouglas Gregor 362d44252ecSDouglas Gregor if (visitDepthFirst(**IM, Visitor, UserData, Visited)) 363d44252ecSDouglas Gregor return true; 364d44252ecSDouglas Gregor } 365d44252ecSDouglas Gregor 366d44252ecSDouglas Gregor // Postorder visitation 367d44252ecSDouglas Gregor return Visitor(M, /*Preorder=*/false, UserData); 368d44252ecSDouglas Gregor } 369d44252ecSDouglas Gregor 370de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 371d44252ecSDouglas Gregor void *UserData), 372d44252ecSDouglas Gregor void *UserData) { 373bdb259d2SDouglas Gregor SmallVector<bool, 16> Visited(size(), false); 374d44252ecSDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 375bdb259d2SDouglas Gregor if (Visited[Chain[I]->Index]) 376d44252ecSDouglas Gregor continue; 377bdb259d2SDouglas Gregor Visited[Chain[I]->Index] = true; 378d44252ecSDouglas Gregor 379d44252ecSDouglas Gregor if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited)) 380d44252ecSDouglas Gregor return; 381d44252ecSDouglas Gregor } 382d44252ecSDouglas Gregor } 3839d7c1a2aSDouglas Gregor 3847029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 3857029ce1aSDouglas Gregor off_t ExpectedSize, 3867029ce1aSDouglas Gregor time_t ExpectedModTime, 3877029ce1aSDouglas Gregor const FileEntry *&File) { 3887029ce1aSDouglas Gregor File = FileMgr.getFile(FileName, /*openFile=*/false, /*cacheFailure=*/false); 3897029ce1aSDouglas Gregor 3907029ce1aSDouglas Gregor if (!File && FileName != "-") { 3917029ce1aSDouglas Gregor return false; 3927029ce1aSDouglas Gregor } 3937029ce1aSDouglas Gregor 3947029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 3957029ce1aSDouglas Gregor (ExpectedModTime && ExpectedModTime != File->getModificationTime())) { 3967029ce1aSDouglas Gregor return true; 3977029ce1aSDouglas Gregor } 3987029ce1aSDouglas Gregor 3997029ce1aSDouglas Gregor return false; 4007029ce1aSDouglas Gregor } 4017029ce1aSDouglas Gregor 4029d7c1a2aSDouglas Gregor #ifndef NDEBUG 4039d7c1a2aSDouglas Gregor namespace llvm { 4049d7c1a2aSDouglas Gregor template<> 4059d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 406de3ef502SDouglas Gregor typedef ModuleFile NodeType; 407de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 4089d7c1a2aSDouglas Gregor typedef ModuleManager::ModuleConstIterator nodes_iterator; 4099d7c1a2aSDouglas Gregor 4109d7c1a2aSDouglas Gregor static ChildIteratorType child_begin(NodeType *Node) { 4119d7c1a2aSDouglas Gregor return Node->Imports.begin(); 4129d7c1a2aSDouglas Gregor } 4139d7c1a2aSDouglas Gregor 4149d7c1a2aSDouglas Gregor static ChildIteratorType child_end(NodeType *Node) { 4159d7c1a2aSDouglas Gregor return Node->Imports.end(); 4169d7c1a2aSDouglas Gregor } 4179d7c1a2aSDouglas Gregor 4189d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 4199d7c1a2aSDouglas Gregor return Manager.begin(); 4209d7c1a2aSDouglas Gregor } 4219d7c1a2aSDouglas Gregor 4229d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 4239d7c1a2aSDouglas Gregor return Manager.end(); 4249d7c1a2aSDouglas Gregor } 4259d7c1a2aSDouglas Gregor }; 4269d7c1a2aSDouglas Gregor 4279d7c1a2aSDouglas Gregor template<> 4289d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4299d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4309d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4319d7c1a2aSDouglas Gregor 4329d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4339d7c1a2aSDouglas Gregor return true; 4349d7c1a2aSDouglas Gregor } 4359d7c1a2aSDouglas Gregor 436de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 4379d7c1a2aSDouglas Gregor return llvm::sys::path::stem(M->FileName); 4389d7c1a2aSDouglas Gregor } 4399d7c1a2aSDouglas Gregor }; 4409d7c1a2aSDouglas Gregor } 4419d7c1a2aSDouglas Gregor 4429d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4439d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4449d7c1a2aSDouglas Gregor } 4459d7c1a2aSDouglas Gregor #endif 446