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; 65c27d0d5eSEli Friedman if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { 66c27d0d5eSEli Friedman ErrorStr = "module file out of date"; 677029ce1aSDouglas Gregor return OutOfDate; 68c27d0d5eSEli Friedman } 697029ce1aSDouglas Gregor 70d44252ecSDouglas Gregor if (!Entry && FileName != "-") { 71c27d0d5eSEli 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 89*f430da4dSDmitri Gribenko New->InputFilesValidationTimestamp = 0; 90*f430da4dSDmitri Gribenko if (New->Kind == MK_Module) { 91*f430da4dSDmitri Gribenko std::string TimestampFilename = New->getTimestampFilename(); 92*f430da4dSDmitri Gribenko llvm::sys::fs::file_status Status; 93*f430da4dSDmitri Gribenko // A cached stat value would be fine as well. 94*f430da4dSDmitri Gribenko if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) 95*f430da4dSDmitri Gribenko New->InputFilesValidationTimestamp = 96*f430da4dSDmitri Gribenko Status.getLastModificationTime().toEpochTime(); 97*f430da4dSDmitri Gribenko } 98*f430da4dSDmitri Gribenko 99d44252ecSDouglas Gregor // Load the contents of the module 100d44252ecSDouglas Gregor if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) { 101d44252ecSDouglas Gregor // The buffer was already provided for us. 102d44252ecSDouglas Gregor assert(Buffer && "Passed null buffer"); 103d44252ecSDouglas Gregor New->Buffer.reset(Buffer); 104d44252ecSDouglas Gregor } else { 105d44252ecSDouglas Gregor // Open the AST file. 106d44252ecSDouglas Gregor llvm::error_code ec; 107d44252ecSDouglas Gregor if (FileName == "-") { 108d44252ecSDouglas Gregor ec = llvm::MemoryBuffer::getSTDIN(New->Buffer); 109d44252ecSDouglas Gregor if (ec) 110d44252ecSDouglas Gregor ErrorStr = ec.message(); 111d44252ecSDouglas Gregor } else 112d44252ecSDouglas Gregor New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr)); 113d44252ecSDouglas Gregor 114d44252ecSDouglas Gregor if (!New->Buffer) 1157029ce1aSDouglas Gregor return Missing; 116d44252ecSDouglas Gregor } 117d44252ecSDouglas Gregor 118d44252ecSDouglas Gregor // Initialize the stream 119d44252ecSDouglas Gregor New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(), 1207029ce1aSDouglas Gregor (const unsigned char *)New->Buffer->getBufferEnd()); 1217029ce1aSDouglas Gregor } 122d44252ecSDouglas Gregor 123d44252ecSDouglas Gregor if (ImportedBy) { 124d44252ecSDouglas Gregor ModuleEntry->ImportedBy.insert(ImportedBy); 125d44252ecSDouglas Gregor ImportedBy->Imports.insert(ModuleEntry); 126d44252ecSDouglas Gregor } else { 1276fb03aeaSDouglas Gregor if (!ModuleEntry->DirectlyImported) 1286fb03aeaSDouglas Gregor ModuleEntry->ImportLoc = ImportLoc; 1296fb03aeaSDouglas Gregor 130d44252ecSDouglas Gregor ModuleEntry->DirectlyImported = true; 131d44252ecSDouglas Gregor } 132d44252ecSDouglas Gregor 1337029ce1aSDouglas Gregor Module = ModuleEntry; 1347029ce1aSDouglas Gregor return NewModule? NewlyLoaded : AlreadyLoaded; 135d44252ecSDouglas Gregor } 136d44252ecSDouglas Gregor 137188dbef2SDouglas Gregor namespace { 138188dbef2SDouglas Gregor /// \brief Predicate that checks whether a module file occurs within 139188dbef2SDouglas Gregor /// the given set. 140188dbef2SDouglas Gregor class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> { 141188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> &Removed; 142188dbef2SDouglas Gregor 143188dbef2SDouglas Gregor public: 144188dbef2SDouglas Gregor IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed) 145188dbef2SDouglas Gregor : Removed(Removed) { } 146188dbef2SDouglas Gregor 147188dbef2SDouglas Gregor bool operator()(ModuleFile *MF) const { 148188dbef2SDouglas Gregor return Removed.count(MF); 149188dbef2SDouglas Gregor } 150188dbef2SDouglas Gregor }; 151188dbef2SDouglas Gregor } 152188dbef2SDouglas Gregor 1537029ce1aSDouglas Gregor void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last, 1547029ce1aSDouglas Gregor ModuleMap *modMap) { 155188dbef2SDouglas Gregor if (first == last) 156188dbef2SDouglas Gregor return; 157188dbef2SDouglas Gregor 158188dbef2SDouglas Gregor // Collect the set of module file pointers that we'll be removing. 159188dbef2SDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); 160188dbef2SDouglas Gregor 161188dbef2SDouglas Gregor // Remove any references to the now-destroyed modules. 162188dbef2SDouglas Gregor IsInModuleFileSet checkInSet(victimSet); 163188dbef2SDouglas Gregor for (unsigned i = 0, n = Chain.size(); i != n; ++i) { 164188dbef2SDouglas Gregor Chain[i]->ImportedBy.remove_if(checkInSet); 165188dbef2SDouglas Gregor } 166188dbef2SDouglas Gregor 167188dbef2SDouglas Gregor // Delete the modules and erase them from the various structures. 168188dbef2SDouglas Gregor for (ModuleIterator victim = first; victim != last; ++victim) { 169188dbef2SDouglas Gregor Modules.erase((*victim)->File); 1707029ce1aSDouglas Gregor 1717029ce1aSDouglas Gregor FileMgr.invalidateCache((*victim)->File); 1727029ce1aSDouglas Gregor if (modMap) { 1737029ce1aSDouglas Gregor StringRef ModuleName = llvm::sys::path::stem((*victim)->FileName); 1747029ce1aSDouglas Gregor if (Module *mod = modMap->findModule(ModuleName)) { 1757029ce1aSDouglas Gregor mod->setASTFile(0); 1767029ce1aSDouglas Gregor } 1777029ce1aSDouglas Gregor } 178188dbef2SDouglas Gregor delete *victim; 179188dbef2SDouglas Gregor } 180188dbef2SDouglas Gregor 181188dbef2SDouglas Gregor // Remove the modules from the chain. 182188dbef2SDouglas Gregor Chain.erase(first, last); 183188dbef2SDouglas Gregor } 184188dbef2SDouglas Gregor 185d44252ecSDouglas Gregor void ModuleManager::addInMemoryBuffer(StringRef FileName, 186d44252ecSDouglas Gregor llvm::MemoryBuffer *Buffer) { 187d44252ecSDouglas Gregor 188d44252ecSDouglas Gregor const FileEntry *Entry = FileMgr.getVirtualFile(FileName, 189d44252ecSDouglas Gregor Buffer->getBufferSize(), 0); 190d44252ecSDouglas Gregor InMemoryBuffers[Entry] = Buffer; 191d44252ecSDouglas Gregor } 192d44252ecSDouglas Gregor 193e97cd90aSDouglas Gregor ModuleManager::VisitState *ModuleManager::allocateVisitState() { 194e97cd90aSDouglas Gregor // Fast path: if we have a cached state, use it. 195e97cd90aSDouglas Gregor if (FirstVisitState) { 196e97cd90aSDouglas Gregor VisitState *Result = FirstVisitState; 197e97cd90aSDouglas Gregor FirstVisitState = FirstVisitState->NextState; 198e97cd90aSDouglas Gregor Result->NextState = 0; 199e97cd90aSDouglas Gregor return Result; 200e97cd90aSDouglas Gregor } 201e97cd90aSDouglas Gregor 202e97cd90aSDouglas Gregor // Allocate and return a new state. 203e97cd90aSDouglas Gregor return new VisitState(size()); 204e97cd90aSDouglas Gregor } 205e97cd90aSDouglas Gregor 206e97cd90aSDouglas Gregor void ModuleManager::returnVisitState(VisitState *State) { 207e97cd90aSDouglas Gregor assert(State->NextState == 0 && "Visited state is in list?"); 208e97cd90aSDouglas Gregor State->NextState = FirstVisitState; 209e97cd90aSDouglas Gregor FirstVisitState = State; 210e97cd90aSDouglas Gregor } 211e97cd90aSDouglas Gregor 2127211ac15SDouglas Gregor void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { 2137211ac15SDouglas Gregor GlobalIndex = Index; 214603cd869SDouglas Gregor if (!GlobalIndex) { 215603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.clear(); 216603cd869SDouglas Gregor return; 2177029ce1aSDouglas Gregor } 218603cd869SDouglas Gregor 219603cd869SDouglas Gregor // Notify the global module index about all of the modules we've already 220603cd869SDouglas Gregor // loaded. 221603cd869SDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 222603cd869SDouglas Gregor if (!GlobalIndex->loadedModuleFile(Chain[I])) { 223603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(Chain[I]); 224603cd869SDouglas Gregor } 225603cd869SDouglas Gregor } 226603cd869SDouglas Gregor } 227603cd869SDouglas Gregor 228603cd869SDouglas Gregor void ModuleManager::moduleFileAccepted(ModuleFile *MF) { 229603cd869SDouglas Gregor if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) 230603cd869SDouglas Gregor return; 231603cd869SDouglas Gregor 232603cd869SDouglas Gregor ModulesInCommonWithGlobalIndex.push_back(MF); 2337211ac15SDouglas Gregor } 2347211ac15SDouglas Gregor 2357211ac15SDouglas Gregor ModuleManager::ModuleManager(FileManager &FileMgr) 236e97cd90aSDouglas Gregor : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(0) { } 237d44252ecSDouglas Gregor 238d44252ecSDouglas Gregor ModuleManager::~ModuleManager() { 239d44252ecSDouglas Gregor for (unsigned i = 0, e = Chain.size(); i != e; ++i) 240d44252ecSDouglas Gregor delete Chain[e - i - 1]; 241e97cd90aSDouglas Gregor delete FirstVisitState; 242d44252ecSDouglas Gregor } 243d44252ecSDouglas Gregor 2447211ac15SDouglas Gregor void 2457211ac15SDouglas Gregor ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), 2467211ac15SDouglas Gregor void *UserData, 2477029ce1aSDouglas Gregor llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit) { 2487211ac15SDouglas Gregor // If the visitation order vector is the wrong size, recompute the order. 249e41d7feaSDouglas Gregor if (VisitOrder.size() != Chain.size()) { 250d44252ecSDouglas Gregor unsigned N = size(); 251e41d7feaSDouglas Gregor VisitOrder.clear(); 252e41d7feaSDouglas Gregor VisitOrder.reserve(N); 253d44252ecSDouglas Gregor 254d44252ecSDouglas Gregor // Record the number of incoming edges for each module. When we 255d44252ecSDouglas Gregor // encounter a module with no incoming edges, push it into the queue 256d44252ecSDouglas Gregor // to seed the queue. 257de3ef502SDouglas Gregor SmallVector<ModuleFile *, 4> Queue; 258d44252ecSDouglas Gregor Queue.reserve(N); 259bdb259d2SDouglas Gregor llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; 260bdb259d2SDouglas Gregor UnusedIncomingEdges.reserve(size()); 261d44252ecSDouglas Gregor for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) { 262d44252ecSDouglas Gregor if (unsigned Size = (*M)->ImportedBy.size()) 263bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(Size); 264bdb259d2SDouglas Gregor else { 265bdb259d2SDouglas Gregor UnusedIncomingEdges.push_back(0); 266d44252ecSDouglas Gregor Queue.push_back(*M); 267d44252ecSDouglas Gregor } 268bdb259d2SDouglas Gregor } 269d44252ecSDouglas Gregor 270e41d7feaSDouglas Gregor // Traverse the graph, making sure to visit a module before visiting any 271e41d7feaSDouglas Gregor // of its dependencies. 272d44252ecSDouglas Gregor unsigned QueueStart = 0; 273d44252ecSDouglas Gregor while (QueueStart < Queue.size()) { 274de3ef502SDouglas Gregor ModuleFile *CurrentModule = Queue[QueueStart++]; 275e41d7feaSDouglas Gregor VisitOrder.push_back(CurrentModule); 276d44252ecSDouglas Gregor 277d44252ecSDouglas Gregor // For any module that this module depends on, push it on the 278d44252ecSDouglas Gregor // stack (if it hasn't already been marked as visited). 279bdb259d2SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 280bdb259d2SDouglas Gregor M = CurrentModule->Imports.begin(), 281d44252ecSDouglas Gregor MEnd = CurrentModule->Imports.end(); 282d44252ecSDouglas Gregor M != MEnd; ++M) { 283d44252ecSDouglas Gregor // Remove our current module as an impediment to visiting the 284d44252ecSDouglas Gregor // module we depend on. If we were the last unvisited module 285d44252ecSDouglas Gregor // that depends on this particular module, push it into the 286d44252ecSDouglas Gregor // queue to be visited. 287bdb259d2SDouglas Gregor unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; 288d44252ecSDouglas Gregor if (NumUnusedEdges && (--NumUnusedEdges == 0)) 289d44252ecSDouglas Gregor Queue.push_back(*M); 290d44252ecSDouglas Gregor } 291d44252ecSDouglas Gregor } 292e41d7feaSDouglas Gregor 293e41d7feaSDouglas Gregor assert(VisitOrder.size() == N && "Visitation order is wrong?"); 2947211ac15SDouglas Gregor 295e97cd90aSDouglas Gregor delete FirstVisitState; 296e97cd90aSDouglas Gregor FirstVisitState = 0; 297e41d7feaSDouglas Gregor } 298e41d7feaSDouglas Gregor 299e97cd90aSDouglas Gregor VisitState *State = allocateVisitState(); 300e97cd90aSDouglas Gregor unsigned VisitNumber = State->NextVisitNumber++; 301e41d7feaSDouglas Gregor 3027211ac15SDouglas Gregor // If the caller has provided us with a hit-set that came from the global 3037211ac15SDouglas Gregor // module index, mark every module file in common with the global module 3047211ac15SDouglas Gregor // index that is *not* in that set as 'visited'. 3057211ac15SDouglas Gregor if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { 3067211ac15SDouglas Gregor for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) 3077211ac15SDouglas Gregor { 3087211ac15SDouglas Gregor ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; 3097029ce1aSDouglas Gregor if (!ModuleFilesHit->count(M)) 310e97cd90aSDouglas Gregor State->VisitNumber[M->Index] = VisitNumber; 3117211ac15SDouglas Gregor } 3127211ac15SDouglas Gregor } 3137211ac15SDouglas Gregor 314e41d7feaSDouglas Gregor for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { 315e41d7feaSDouglas Gregor ModuleFile *CurrentModule = VisitOrder[I]; 316e41d7feaSDouglas Gregor // Should we skip this module file? 317e97cd90aSDouglas Gregor if (State->VisitNumber[CurrentModule->Index] == VisitNumber) 318e41d7feaSDouglas Gregor continue; 319e41d7feaSDouglas Gregor 320e41d7feaSDouglas Gregor // Visit the module. 321e97cd90aSDouglas Gregor assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); 322e97cd90aSDouglas Gregor State->VisitNumber[CurrentModule->Index] = VisitNumber; 323e41d7feaSDouglas Gregor if (!Visitor(*CurrentModule, UserData)) 324e41d7feaSDouglas Gregor continue; 325e41d7feaSDouglas Gregor 326e41d7feaSDouglas Gregor // The visitor has requested that cut off visitation of any 327e41d7feaSDouglas Gregor // module that the current module depends on. To indicate this 328e41d7feaSDouglas Gregor // behavior, we mark all of the reachable modules as having been visited. 329e41d7feaSDouglas Gregor ModuleFile *NextModule = CurrentModule; 330e41d7feaSDouglas Gregor do { 331e41d7feaSDouglas Gregor // For any module that this module depends on, push it on the 332e41d7feaSDouglas Gregor // stack (if it hasn't already been marked as visited). 333e41d7feaSDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator 334e41d7feaSDouglas Gregor M = NextModule->Imports.begin(), 335e41d7feaSDouglas Gregor MEnd = NextModule->Imports.end(); 336e41d7feaSDouglas Gregor M != MEnd; ++M) { 337e97cd90aSDouglas Gregor if (State->VisitNumber[(*M)->Index] != VisitNumber) { 338e97cd90aSDouglas Gregor State->Stack.push_back(*M); 339e97cd90aSDouglas Gregor State->VisitNumber[(*M)->Index] = VisitNumber; 340e41d7feaSDouglas Gregor } 341e41d7feaSDouglas Gregor } 342e41d7feaSDouglas Gregor 343e97cd90aSDouglas Gregor if (State->Stack.empty()) 344e41d7feaSDouglas Gregor break; 345e41d7feaSDouglas Gregor 346e41d7feaSDouglas Gregor // Pop the next module off the stack. 34725284cc9SRobert Wilhelm NextModule = State->Stack.pop_back_val(); 348e41d7feaSDouglas Gregor } while (true); 349e41d7feaSDouglas Gregor } 350e97cd90aSDouglas Gregor 351e97cd90aSDouglas Gregor returnVisitState(State); 352d44252ecSDouglas Gregor } 353d44252ecSDouglas Gregor 354d44252ecSDouglas Gregor /// \brief Perform a depth-first visit of the current module. 355de3ef502SDouglas Gregor static bool visitDepthFirst(ModuleFile &M, 356de3ef502SDouglas Gregor bool (*Visitor)(ModuleFile &M, bool Preorder, 357d44252ecSDouglas Gregor void *UserData), 358d44252ecSDouglas Gregor void *UserData, 359bdb259d2SDouglas Gregor SmallVectorImpl<bool> &Visited) { 360d44252ecSDouglas Gregor // Preorder visitation 361d44252ecSDouglas Gregor if (Visitor(M, /*Preorder=*/true, UserData)) 362d44252ecSDouglas Gregor return true; 363d44252ecSDouglas Gregor 364d44252ecSDouglas Gregor // Visit children 365de3ef502SDouglas Gregor for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), 366d44252ecSDouglas Gregor IMEnd = M.Imports.end(); 367d44252ecSDouglas Gregor IM != IMEnd; ++IM) { 368bdb259d2SDouglas Gregor if (Visited[(*IM)->Index]) 369d44252ecSDouglas Gregor continue; 370bdb259d2SDouglas Gregor Visited[(*IM)->Index] = true; 371d44252ecSDouglas Gregor 372d44252ecSDouglas Gregor if (visitDepthFirst(**IM, Visitor, UserData, Visited)) 373d44252ecSDouglas Gregor return true; 374d44252ecSDouglas Gregor } 375d44252ecSDouglas Gregor 376d44252ecSDouglas Gregor // Postorder visitation 377d44252ecSDouglas Gregor return Visitor(M, /*Preorder=*/false, UserData); 378d44252ecSDouglas Gregor } 379d44252ecSDouglas Gregor 380de3ef502SDouglas Gregor void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 381d44252ecSDouglas Gregor void *UserData), 382d44252ecSDouglas Gregor void *UserData) { 383bdb259d2SDouglas Gregor SmallVector<bool, 16> Visited(size(), false); 384d44252ecSDouglas Gregor for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 385bdb259d2SDouglas Gregor if (Visited[Chain[I]->Index]) 386d44252ecSDouglas Gregor continue; 387bdb259d2SDouglas Gregor Visited[Chain[I]->Index] = true; 388d44252ecSDouglas Gregor 389d44252ecSDouglas Gregor if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited)) 390d44252ecSDouglas Gregor return; 391d44252ecSDouglas Gregor } 392d44252ecSDouglas Gregor } 3939d7c1a2aSDouglas Gregor 3947029ce1aSDouglas Gregor bool ModuleManager::lookupModuleFile(StringRef FileName, 3957029ce1aSDouglas Gregor off_t ExpectedSize, 3967029ce1aSDouglas Gregor time_t ExpectedModTime, 3977029ce1aSDouglas Gregor const FileEntry *&File) { 3987029ce1aSDouglas Gregor File = FileMgr.getFile(FileName, /*openFile=*/false, /*cacheFailure=*/false); 3997029ce1aSDouglas Gregor 4007029ce1aSDouglas Gregor if (!File && FileName != "-") { 4017029ce1aSDouglas Gregor return false; 4027029ce1aSDouglas Gregor } 4037029ce1aSDouglas Gregor 4047029ce1aSDouglas Gregor if ((ExpectedSize && ExpectedSize != File->getSize()) || 4057029ce1aSDouglas Gregor (ExpectedModTime && ExpectedModTime != File->getModificationTime())) { 4067029ce1aSDouglas Gregor return true; 4077029ce1aSDouglas Gregor } 4087029ce1aSDouglas Gregor 4097029ce1aSDouglas Gregor return false; 4107029ce1aSDouglas Gregor } 4117029ce1aSDouglas Gregor 4129d7c1a2aSDouglas Gregor #ifndef NDEBUG 4139d7c1a2aSDouglas Gregor namespace llvm { 4149d7c1a2aSDouglas Gregor template<> 4159d7c1a2aSDouglas Gregor struct GraphTraits<ModuleManager> { 416de3ef502SDouglas Gregor typedef ModuleFile NodeType; 417de3ef502SDouglas Gregor typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 4189d7c1a2aSDouglas Gregor typedef ModuleManager::ModuleConstIterator nodes_iterator; 4199d7c1a2aSDouglas Gregor 4209d7c1a2aSDouglas Gregor static ChildIteratorType child_begin(NodeType *Node) { 4219d7c1a2aSDouglas Gregor return Node->Imports.begin(); 4229d7c1a2aSDouglas Gregor } 4239d7c1a2aSDouglas Gregor 4249d7c1a2aSDouglas Gregor static ChildIteratorType child_end(NodeType *Node) { 4259d7c1a2aSDouglas Gregor return Node->Imports.end(); 4269d7c1a2aSDouglas Gregor } 4279d7c1a2aSDouglas Gregor 4289d7c1a2aSDouglas Gregor static nodes_iterator nodes_begin(const ModuleManager &Manager) { 4299d7c1a2aSDouglas Gregor return Manager.begin(); 4309d7c1a2aSDouglas Gregor } 4319d7c1a2aSDouglas Gregor 4329d7c1a2aSDouglas Gregor static nodes_iterator nodes_end(const ModuleManager &Manager) { 4339d7c1a2aSDouglas Gregor return Manager.end(); 4349d7c1a2aSDouglas Gregor } 4359d7c1a2aSDouglas Gregor }; 4369d7c1a2aSDouglas Gregor 4379d7c1a2aSDouglas Gregor template<> 4389d7c1a2aSDouglas Gregor struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 4399d7c1a2aSDouglas Gregor explicit DOTGraphTraits(bool IsSimple = false) 4409d7c1a2aSDouglas Gregor : DefaultDOTGraphTraits(IsSimple) { } 4419d7c1a2aSDouglas Gregor 4429d7c1a2aSDouglas Gregor static bool renderGraphFromBottomUp() { 4439d7c1a2aSDouglas Gregor return true; 4449d7c1a2aSDouglas Gregor } 4459d7c1a2aSDouglas Gregor 446de3ef502SDouglas Gregor std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 4479d7c1a2aSDouglas Gregor return llvm::sys::path::stem(M->FileName); 4489d7c1a2aSDouglas Gregor } 4499d7c1a2aSDouglas Gregor }; 4509d7c1a2aSDouglas Gregor } 4519d7c1a2aSDouglas Gregor 4529d7c1a2aSDouglas Gregor void ModuleManager::viewGraph() { 4539d7c1a2aSDouglas Gregor llvm::ViewGraph(*this, "Modules"); 4549d7c1a2aSDouglas Gregor } 4559d7c1a2aSDouglas Gregor #endif 456