Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | 9d7c1a2 | 2011-10-11 19:27:55 +0000 | [diff] [blame] | 19 | #ifndef NDEBUG |
| 20 | #include "llvm/Support/GraphWriter.h" |
| 21 | #endif |
| 22 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | using namespace serialization; |
| 25 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 26 | ModuleFile *ModuleManager::lookup(StringRef Name) { |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 36 | std::pair<ModuleFile *, bool> |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 37 | ModuleManager::addModule(StringRef FileName, ModuleKind Type, |
Douglas Gregor | 4fc9f3e | 2012-01-18 20:56:22 +0000 | [diff] [blame] | 38 | ModuleFile *ImportedBy, unsigned Generation, |
| 39 | std::string &ErrorStr) { |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 40 | const FileEntry *Entry = FileMgr.getFile(FileName); |
| 41 | if (!Entry && FileName != "-") { |
| 42 | ErrorStr = "file not found"; |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 43 | return std::make_pair(static_cast<ModuleFile*>(0), false); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | // Check whether we already loaded this module, before |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 47 | ModuleFile *&ModuleEntry = Modules[Entry]; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 48 | bool NewModule = false; |
| 49 | if (!ModuleEntry) { |
| 50 | // Allocate a new module. |
Douglas Gregor | 4fc9f3e | 2012-01-18 20:56:22 +0000 | [diff] [blame] | 51 | ModuleFile *New = new ModuleFile(Type, Generation); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 52 | New->FileName = FileName.str(); |
Argyrios Kyrtzidis | aedf714 | 2012-10-03 01:58:42 +0000 | [diff] [blame] | 53 | New->File = Entry; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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) |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 74 | return std::make_pair(static_cast<ModuleFile*>(0), false); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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 | |
Argyrios Kyrtzidis | aedf714 | 2012-10-03 01:58:42 +0000 | [diff] [blame] | 138 | ModuleManager::ModuleManager(FileManager &FileMgr) : FileMgr(FileMgr) { } |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 139 | |
| 140 | ModuleManager::~ModuleManager() { |
| 141 | for (unsigned i = 0, e = Chain.size(); i != e; ++i) |
| 142 | delete Chain[e - i - 1]; |
| 143 | } |
| 144 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 145 | void ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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. |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 152 | SmallVector<ModuleFile *, 4> Queue; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 153 | Queue.reserve(N); |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 154 | llvm::DenseMap<ModuleFile *, unsigned> UnusedIncomingEdges; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 162 | llvm::SmallPtrSet<ModuleFile *, 4> Skipped; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 163 | unsigned QueueStart = 0; |
| 164 | while (QueueStart < Queue.size()) { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 165 | ModuleFile *CurrentModule = Queue[QueueStart++]; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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). |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 176 | SmallVector<ModuleFile *, 4> Stack; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 177 | Stack.push_back(CurrentModule); |
| 178 | Skipped.insert(CurrentModule); |
| 179 | while (!Stack.empty()) { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 180 | ModuleFile *NextModule = Stack.back(); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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). |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 185 | for (llvm::SetVector<ModuleFile *>::iterator |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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). |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 198 | for (llvm::SetVector<ModuleFile *>::iterator M = CurrentModule->Imports.begin(), |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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. |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 214 | static bool visitDepthFirst(ModuleFile &M, |
| 215 | bool (*Visitor)(ModuleFile &M, bool Preorder, |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 216 | void *UserData), |
| 217 | void *UserData, |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 218 | llvm::SmallPtrSet<ModuleFile *, 4> &Visited) { |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 219 | // Preorder visitation |
| 220 | if (Visitor(M, /*Preorder=*/true, UserData)) |
| 221 | return true; |
| 222 | |
| 223 | // Visit children |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 224 | for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 238 | void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 239 | void *UserData), |
| 240 | void *UserData) { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 241 | llvm::SmallPtrSet<ModuleFile *, 4> Visited; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 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 | } |
Douglas Gregor | 9d7c1a2 | 2011-10-11 19:27:55 +0000 | [diff] [blame] | 250 | |
| 251 | #ifndef NDEBUG |
| 252 | namespace llvm { |
| 253 | template<> |
| 254 | struct GraphTraits<ModuleManager> { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 255 | typedef ModuleFile NodeType; |
| 256 | typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; |
Douglas Gregor | 9d7c1a2 | 2011-10-11 19:27:55 +0000 | [diff] [blame] | 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 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 285 | std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { |
Douglas Gregor | 9d7c1a2 | 2011-10-11 19:27:55 +0000 | [diff] [blame] | 286 | return llvm::sys::path::stem(M->FileName); |
| 287 | } |
| 288 | }; |
| 289 | } |
| 290 | |
| 291 | void ModuleManager::viewGraph() { |
| 292 | llvm::ViewGraph(*this, "Modules"); |
| 293 | } |
| 294 | #endif |