1 //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ModuleManager class, which manages a set of loaded 11 // modules for the ASTReader. 12 // 13 //===----------------------------------------------------------------------===// 14 #include "clang/Serialization/ModuleManager.h" 15 #include "llvm/Support/MemoryBuffer.h" 16 #include "llvm/Support/raw_ostream.h" 17 #include "llvm/Support/system_error.h" 18 19 #ifndef NDEBUG 20 #include "llvm/Support/GraphWriter.h" 21 #endif 22 23 using namespace clang; 24 using namespace serialization; 25 26 ModuleFile *ModuleManager::lookup(StringRef Name) { 27 const FileEntry *Entry = FileMgr.getFile(Name); 28 return Modules[Entry]; 29 } 30 31 llvm::MemoryBuffer *ModuleManager::lookupBuffer(StringRef Name) { 32 const FileEntry *Entry = FileMgr.getFile(Name); 33 return InMemoryBuffers[Entry]; 34 } 35 36 std::pair<ModuleFile *, bool> 37 ModuleManager::addModule(StringRef FileName, ModuleKind Type, 38 ModuleFile *ImportedBy, unsigned Generation, 39 std::string &ErrorStr) { 40 const FileEntry *Entry = FileMgr.getFile(FileName); 41 if (!Entry && FileName != "-") { 42 ErrorStr = "file not found"; 43 return std::make_pair(static_cast<ModuleFile*>(0), false); 44 } 45 46 // Check whether we already loaded this module, before 47 ModuleFile *&ModuleEntry = Modules[Entry]; 48 bool NewModule = false; 49 if (!ModuleEntry) { 50 // Allocate a new module. 51 ModuleFile *New = new ModuleFile(Type, Generation); 52 New->FileName = FileName.str(); 53 New->File = Entry; 54 Chain.push_back(New); 55 NewModule = true; 56 ModuleEntry = New; 57 58 // Load the contents of the module 59 if (llvm::MemoryBuffer *Buffer = lookupBuffer(FileName)) { 60 // The buffer was already provided for us. 61 assert(Buffer && "Passed null buffer"); 62 New->Buffer.reset(Buffer); 63 } else { 64 // Open the AST file. 65 llvm::error_code ec; 66 if (FileName == "-") { 67 ec = llvm::MemoryBuffer::getSTDIN(New->Buffer); 68 if (ec) 69 ErrorStr = ec.message(); 70 } else 71 New->Buffer.reset(FileMgr.getBufferForFile(FileName, &ErrorStr)); 72 73 if (!New->Buffer) 74 return std::make_pair(static_cast<ModuleFile*>(0), false); 75 } 76 77 // Initialize the stream 78 New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(), 79 (const unsigned char *)New->Buffer->getBufferEnd()); } 80 81 if (ImportedBy) { 82 ModuleEntry->ImportedBy.insert(ImportedBy); 83 ImportedBy->Imports.insert(ModuleEntry); 84 } else { 85 ModuleEntry->DirectlyImported = true; 86 } 87 88 return std::make_pair(ModuleEntry, NewModule); 89 } 90 91 namespace { 92 /// \brief Predicate that checks whether a module file occurs within 93 /// the given set. 94 class IsInModuleFileSet : public std::unary_function<ModuleFile *, bool> { 95 llvm::SmallPtrSet<ModuleFile *, 4> &Removed; 96 97 public: 98 IsInModuleFileSet(llvm::SmallPtrSet<ModuleFile *, 4> &Removed) 99 : Removed(Removed) { } 100 101 bool operator()(ModuleFile *MF) const { 102 return Removed.count(MF); 103 } 104 }; 105 } 106 107 void ModuleManager::removeModules(ModuleIterator first, ModuleIterator last) { 108 if (first == last) 109 return; 110 111 // Collect the set of module file pointers that we'll be removing. 112 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); 113 114 // Remove any references to the now-destroyed modules. 115 IsInModuleFileSet checkInSet(victimSet); 116 for (unsigned i = 0, n = Chain.size(); i != n; ++i) { 117 Chain[i]->ImportedBy.remove_if(checkInSet); 118 } 119 120 // Delete the modules and erase them from the various structures. 121 for (ModuleIterator victim = first; victim != last; ++victim) { 122 Modules.erase((*victim)->File); 123 delete *victim; 124 } 125 126 // Remove the modules from the chain. 127 Chain.erase(first, last); 128 } 129 130 void ModuleManager::addInMemoryBuffer(StringRef FileName, 131 llvm::MemoryBuffer *Buffer) { 132 133 const FileEntry *Entry = FileMgr.getVirtualFile(FileName, 134 Buffer->getBufferSize(), 0); 135 InMemoryBuffers[Entry] = Buffer; 136 } 137 138 ModuleManager::ModuleManager(FileManager &FileMgr) : FileMgr(FileMgr) { } 139 140 ModuleManager::~ModuleManager() { 141 for (unsigned i = 0, e = Chain.size(); i != e; ++i) 142 delete Chain[e - i - 1]; 143 } 144 145 void ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), 146 void *UserData) { 147 unsigned N = size(); 148 149 // Record the number of incoming edges for each module. When we 150 // encounter a module with no incoming edges, push it into the queue 151 // to seed the queue. 152 SmallVector<ModuleFile *, 4> Queue; 153 Queue.reserve(N); 154 llvm::DenseMap<ModuleFile *, unsigned> UnusedIncomingEdges; 155 for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) { 156 if (unsigned Size = (*M)->ImportedBy.size()) 157 UnusedIncomingEdges[*M] = Size; 158 else 159 Queue.push_back(*M); 160 } 161 162 llvm::SmallPtrSet<ModuleFile *, 4> Skipped; 163 unsigned QueueStart = 0; 164 while (QueueStart < Queue.size()) { 165 ModuleFile *CurrentModule = Queue[QueueStart++]; 166 167 // Check whether this module should be skipped. 168 if (Skipped.count(CurrentModule)) 169 continue; 170 171 if (Visitor(*CurrentModule, UserData)) { 172 // The visitor has requested that cut off visitation of any 173 // module that the current module depends on. To indicate this 174 // behavior, we mark all of the reachable modules as having N 175 // incoming edges (which is impossible otherwise). 176 SmallVector<ModuleFile *, 4> Stack; 177 Stack.push_back(CurrentModule); 178 Skipped.insert(CurrentModule); 179 while (!Stack.empty()) { 180 ModuleFile *NextModule = Stack.back(); 181 Stack.pop_back(); 182 183 // For any module that this module depends on, push it on the 184 // stack (if it hasn't already been marked as visited). 185 for (llvm::SetVector<ModuleFile *>::iterator 186 M = NextModule->Imports.begin(), 187 MEnd = NextModule->Imports.end(); 188 M != MEnd; ++M) { 189 if (Skipped.insert(*M)) 190 Stack.push_back(*M); 191 } 192 } 193 continue; 194 } 195 196 // For any module that this module depends on, push it on the 197 // stack (if it hasn't already been marked as visited). 198 for (llvm::SetVector<ModuleFile *>::iterator M = CurrentModule->Imports.begin(), 199 MEnd = CurrentModule->Imports.end(); 200 M != MEnd; ++M) { 201 202 // Remove our current module as an impediment to visiting the 203 // module we depend on. If we were the last unvisited module 204 // that depends on this particular module, push it into the 205 // queue to be visited. 206 unsigned &NumUnusedEdges = UnusedIncomingEdges[*M]; 207 if (NumUnusedEdges && (--NumUnusedEdges == 0)) 208 Queue.push_back(*M); 209 } 210 } 211 } 212 213 /// \brief Perform a depth-first visit of the current module. 214 static bool visitDepthFirst(ModuleFile &M, 215 bool (*Visitor)(ModuleFile &M, bool Preorder, 216 void *UserData), 217 void *UserData, 218 llvm::SmallPtrSet<ModuleFile *, 4> &Visited) { 219 // Preorder visitation 220 if (Visitor(M, /*Preorder=*/true, UserData)) 221 return true; 222 223 // Visit children 224 for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), 225 IMEnd = M.Imports.end(); 226 IM != IMEnd; ++IM) { 227 if (!Visited.insert(*IM)) 228 continue; 229 230 if (visitDepthFirst(**IM, Visitor, UserData, Visited)) 231 return true; 232 } 233 234 // Postorder visitation 235 return Visitor(M, /*Preorder=*/false, UserData); 236 } 237 238 void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 239 void *UserData), 240 void *UserData) { 241 llvm::SmallPtrSet<ModuleFile *, 4> Visited; 242 for (unsigned I = 0, N = Chain.size(); I != N; ++I) { 243 if (!Visited.insert(Chain[I])) 244 continue; 245 246 if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited)) 247 return; 248 } 249 } 250 251 #ifndef NDEBUG 252 namespace llvm { 253 template<> 254 struct GraphTraits<ModuleManager> { 255 typedef ModuleFile NodeType; 256 typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; 257 typedef ModuleManager::ModuleConstIterator nodes_iterator; 258 259 static ChildIteratorType child_begin(NodeType *Node) { 260 return Node->Imports.begin(); 261 } 262 263 static ChildIteratorType child_end(NodeType *Node) { 264 return Node->Imports.end(); 265 } 266 267 static nodes_iterator nodes_begin(const ModuleManager &Manager) { 268 return Manager.begin(); 269 } 270 271 static nodes_iterator nodes_end(const ModuleManager &Manager) { 272 return Manager.end(); 273 } 274 }; 275 276 template<> 277 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { 278 explicit DOTGraphTraits(bool IsSimple = false) 279 : DefaultDOTGraphTraits(IsSimple) { } 280 281 static bool renderGraphFromBottomUp() { 282 return true; 283 } 284 285 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { 286 return llvm::sys::path::stem(M->FileName); 287 } 288 }; 289 } 290 291 void ModuleManager::viewGraph() { 292 llvm::ViewGraph(*this, "Modules"); 293 } 294 #endif 295