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 | //===----------------------------------------------------------------------===// |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 14 | #include "clang/Lex/HeaderSearch.h" |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 15 | #include "clang/Lex/ModuleMap.h" |
Douglas Gregor | 7211ac1 | 2013-01-25 23:32:03 +0000 | [diff] [blame] | 16 | #include "clang/Serialization/GlobalModuleIndex.h" |
Benjamin Kramer | af04f98 | 2013-08-24 13:16:22 +0000 | [diff] [blame] | 17 | #include "clang/Serialization/ModuleManager.h" |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
Rafael Espindola | 552c169 | 2013-06-11 22:15:02 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Path.h" |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | 8a8e554 | 2014-06-12 17:19:42 +0000 | [diff] [blame] | 21 | #include <system_error> |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 22 | |
Douglas Gregor | 9d7c1a2 | 2011-10-11 19:27:55 +0000 | [diff] [blame] | 23 | #ifndef NDEBUG |
| 24 | #include "llvm/Support/GraphWriter.h" |
| 25 | #endif |
| 26 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | using namespace serialization; |
| 29 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 30 | ModuleFile *ModuleManager::lookup(StringRef Name) { |
Douglas Gregor | dadd85d | 2013-02-08 21:27:45 +0000 | [diff] [blame] | 31 | const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, |
| 32 | /*cacheFailure=*/false); |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 33 | if (Entry) |
| 34 | return lookup(Entry); |
| 35 | |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 36 | return nullptr; |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | ModuleFile *ModuleManager::lookup(const FileEntry *File) { |
| 40 | llvm::DenseMap<const FileEntry *, ModuleFile *>::iterator Known |
| 41 | = Modules.find(File); |
| 42 | if (Known == Modules.end()) |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 43 | return nullptr; |
Douglas Gregor | bf7fc9c | 2013-03-27 16:47:18 +0000 | [diff] [blame] | 44 | |
| 45 | return Known->second; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Rafael Espindola | 5cd06f2 | 2014-08-18 19:16:31 +0000 | [diff] [blame] | 48 | std::unique_ptr<llvm::MemoryBuffer> |
| 49 | ModuleManager::lookupBuffer(StringRef Name) { |
Douglas Gregor | dadd85d | 2013-02-08 21:27:45 +0000 | [diff] [blame] | 50 | const FileEntry *Entry = FileMgr.getFile(Name, /*openFile=*/false, |
| 51 | /*cacheFailure=*/false); |
Rafael Espindola | 5cd06f2 | 2014-08-18 19:16:31 +0000 | [diff] [blame] | 52 | return std::move(InMemoryBuffers[Entry]); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 53 | } |
| 54 | |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 55 | ModuleManager::AddModuleResult |
Douglas Gregor | 6fb03ae | 2012-11-30 19:28:05 +0000 | [diff] [blame] | 56 | ModuleManager::addModule(StringRef FileName, ModuleKind Type, |
| 57 | SourceLocation ImportLoc, ModuleFile *ImportedBy, |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 58 | unsigned Generation, |
| 59 | off_t ExpectedSize, time_t ExpectedModTime, |
Ben Langmuir | 487ea14 | 2014-10-23 18:05:36 +0000 | [diff] [blame^] | 60 | ASTFileSignature ExpectedSignature, |
| 61 | std::function<ASTFileSignature(llvm::BitstreamReader &)> |
| 62 | ReadSignature, |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 63 | ModuleFile *&Module, |
| 64 | std::string &ErrorStr) { |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 65 | Module = nullptr; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 66 | |
| 67 | // Look for the file entry. This only fails if the expected size or |
| 68 | // modification time differ. |
| 69 | const FileEntry *Entry; |
Eli Friedman | c27d0d5 | 2013-09-05 23:50:58 +0000 | [diff] [blame] | 70 | if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) { |
| 71 | ErrorStr = "module file out of date"; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 72 | return OutOfDate; |
Eli Friedman | c27d0d5 | 2013-09-05 23:50:58 +0000 | [diff] [blame] | 73 | } |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 74 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 75 | if (!Entry && FileName != "-") { |
Eli Friedman | c27d0d5 | 2013-09-05 23:50:58 +0000 | [diff] [blame] | 76 | ErrorStr = "module file not found"; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 77 | return Missing; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 78 | } |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 79 | |
| 80 | // Check whether we already loaded this module, before |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 81 | ModuleFile *&ModuleEntry = Modules[Entry]; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 82 | bool NewModule = false; |
| 83 | if (!ModuleEntry) { |
| 84 | // Allocate a new module. |
Douglas Gregor | 4fc9f3e | 2012-01-18 20:56:22 +0000 | [diff] [blame] | 85 | ModuleFile *New = new ModuleFile(Type, Generation); |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 86 | New->Index = Chain.size(); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 87 | New->FileName = FileName.str(); |
Argyrios Kyrtzidis | aedf714 | 2012-10-03 01:58:42 +0000 | [diff] [blame] | 88 | New->File = Entry; |
Douglas Gregor | 6fb03ae | 2012-11-30 19:28:05 +0000 | [diff] [blame] | 89 | New->ImportLoc = ImportLoc; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 90 | Chain.push_back(New); |
| 91 | NewModule = true; |
| 92 | ModuleEntry = New; |
Douglas Gregor | 6fb03ae | 2012-11-30 19:28:05 +0000 | [diff] [blame] | 93 | |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 94 | New->InputFilesValidationTimestamp = 0; |
Richard Smith | e842a47 | 2014-10-22 02:05:46 +0000 | [diff] [blame] | 95 | if (New->Kind == MK_ImplicitModule) { |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 96 | std::string TimestampFilename = New->getTimestampFilename(); |
Ben Langmuir | c8130a7 | 2014-02-20 21:59:23 +0000 | [diff] [blame] | 97 | vfs::Status Status; |
Dmitri Gribenko | f430da4 | 2014-02-12 10:33:14 +0000 | [diff] [blame] | 98 | // A cached stat value would be fine as well. |
| 99 | if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status)) |
| 100 | New->InputFilesValidationTimestamp = |
| 101 | Status.getLastModificationTime().toEpochTime(); |
| 102 | } |
| 103 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 104 | // Load the contents of the module |
Rafael Espindola | 5cd06f2 | 2014-08-18 19:16:31 +0000 | [diff] [blame] | 105 | if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) { |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 106 | // The buffer was already provided for us. |
Rafael Espindola | 5cd06f2 | 2014-08-18 19:16:31 +0000 | [diff] [blame] | 107 | New->Buffer = std::move(Buffer); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 108 | } else { |
| 109 | // Open the AST file. |
Rafael Espindola | c080917 | 2014-06-12 14:02:15 +0000 | [diff] [blame] | 110 | std::error_code ec; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 111 | if (FileName == "-") { |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 112 | llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf = |
| 113 | llvm::MemoryBuffer::getSTDIN(); |
| 114 | ec = Buf.getError(); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 115 | if (ec) |
| 116 | ErrorStr = ec.message(); |
Rafael Espindola | 2d2b420 | 2014-07-06 17:43:24 +0000 | [diff] [blame] | 117 | else |
| 118 | New->Buffer = std::move(Buf.get()); |
Ben Langmuir | 9801b25 | 2014-06-20 00:24:56 +0000 | [diff] [blame] | 119 | } else { |
| 120 | // Leave the FileEntry open so if it gets read again by another |
| 121 | // ModuleManager it must be the same underlying file. |
| 122 | // FIXME: Because FileManager::getFile() doesn't guarantee that it will |
| 123 | // give us an open file, this may not be 100% reliable. |
Rafael Espindola | 6406f7b | 2014-08-26 19:54:40 +0000 | [diff] [blame] | 124 | New->Buffer = FileMgr.getBufferForFile(New->File, &ErrorStr, |
| 125 | /*IsVolatile*/ false, |
| 126 | /*ShouldClose*/ false); |
Ben Langmuir | 9801b25 | 2014-06-20 00:24:56 +0000 | [diff] [blame] | 127 | } |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 128 | |
| 129 | if (!New->Buffer) |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 130 | return Missing; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Initialize the stream |
| 134 | New->StreamFile.init((const unsigned char *)New->Buffer->getBufferStart(), |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 135 | (const unsigned char *)New->Buffer->getBufferEnd()); |
Ben Langmuir | 487ea14 | 2014-10-23 18:05:36 +0000 | [diff] [blame^] | 136 | |
| 137 | if (ExpectedSignature) { |
| 138 | New->Signature = ReadSignature(New->StreamFile); |
| 139 | if (New->Signature != ExpectedSignature) { |
| 140 | ErrorStr = New->Signature ? "signature mismatch" |
| 141 | : "could not read module signature"; |
| 142 | |
| 143 | // Remove the module file immediately, since removeModules might try to |
| 144 | // invalidate the file cache for Entry, and that is not safe if this |
| 145 | // module is *itself* up to date, but has an out-of-date importer. |
| 146 | Modules.erase(Entry); |
| 147 | Chain.pop_back(); |
| 148 | return OutOfDate; |
| 149 | } |
| 150 | } |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 151 | } |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 152 | |
| 153 | if (ImportedBy) { |
| 154 | ModuleEntry->ImportedBy.insert(ImportedBy); |
| 155 | ImportedBy->Imports.insert(ModuleEntry); |
| 156 | } else { |
Douglas Gregor | 6fb03ae | 2012-11-30 19:28:05 +0000 | [diff] [blame] | 157 | if (!ModuleEntry->DirectlyImported) |
| 158 | ModuleEntry->ImportLoc = ImportLoc; |
| 159 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 160 | ModuleEntry->DirectlyImported = true; |
| 161 | } |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 162 | |
| 163 | Module = ModuleEntry; |
| 164 | return NewModule? NewlyLoaded : AlreadyLoaded; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Ben Langmuir | 9801b25 | 2014-06-20 00:24:56 +0000 | [diff] [blame] | 167 | void ModuleManager::removeModules( |
| 168 | ModuleIterator first, ModuleIterator last, |
| 169 | llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully, |
| 170 | ModuleMap *modMap) { |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 171 | if (first == last) |
| 172 | return; |
| 173 | |
| 174 | // Collect the set of module file pointers that we'll be removing. |
| 175 | llvm::SmallPtrSet<ModuleFile *, 4> victimSet(first, last); |
| 176 | |
| 177 | // Remove any references to the now-destroyed modules. |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 178 | for (unsigned i = 0, n = Chain.size(); i != n; ++i) { |
Chandler Carruth | b55d022 | 2014-03-03 19:36:27 +0000 | [diff] [blame] | 179 | Chain[i]->ImportedBy.remove_if([&](ModuleFile *MF) { |
| 180 | return victimSet.count(MF); |
| 181 | }); |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | // Delete the modules and erase them from the various structures. |
| 185 | for (ModuleIterator victim = first; victim != last; ++victim) { |
Ben Langmuir | caea131 | 2014-05-19 17:04:28 +0000 | [diff] [blame] | 186 | Modules.erase((*victim)->File); |
Ben Langmuir | ca39214 | 2014-05-19 16:13:45 +0000 | [diff] [blame] | 187 | |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 188 | if (modMap) { |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 189 | StringRef ModuleName = (*victim)->ModuleName; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 190 | if (Module *mod = modMap->findModule(ModuleName)) { |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 191 | mod->setASTFile(nullptr); |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
Ben Langmuir | 4f05478 | 2014-05-30 21:20:54 +0000 | [diff] [blame] | 194 | |
Ben Langmuir | 9801b25 | 2014-06-20 00:24:56 +0000 | [diff] [blame] | 195 | // Files that didn't make it through ReadASTCore successfully will be |
| 196 | // rebuilt (or there was an error). Invalidate them so that we can load the |
| 197 | // new files that will be renamed over the old ones. |
| 198 | if (LoadedSuccessfully.count(*victim) == 0) |
Ben Langmuir | 4f05478 | 2014-05-30 21:20:54 +0000 | [diff] [blame] | 199 | FileMgr.invalidateCache((*victim)->File); |
| 200 | |
Douglas Gregor | 188dbef | 2012-11-07 17:46:15 +0000 | [diff] [blame] | 201 | delete *victim; |
| 202 | } |
| 203 | |
| 204 | // Remove the modules from the chain. |
| 205 | Chain.erase(first, last); |
| 206 | } |
| 207 | |
Rafael Espindola | 5cd06f2 | 2014-08-18 19:16:31 +0000 | [diff] [blame] | 208 | void |
| 209 | ModuleManager::addInMemoryBuffer(StringRef FileName, |
| 210 | std::unique_ptr<llvm::MemoryBuffer> Buffer) { |
| 211 | |
| 212 | const FileEntry *Entry = |
| 213 | FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0); |
| 214 | InMemoryBuffers[Entry] = std::move(Buffer); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 217 | ModuleManager::VisitState *ModuleManager::allocateVisitState() { |
| 218 | // Fast path: if we have a cached state, use it. |
| 219 | if (FirstVisitState) { |
| 220 | VisitState *Result = FirstVisitState; |
| 221 | FirstVisitState = FirstVisitState->NextState; |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 222 | Result->NextState = nullptr; |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 223 | return Result; |
| 224 | } |
| 225 | |
| 226 | // Allocate and return a new state. |
| 227 | return new VisitState(size()); |
| 228 | } |
| 229 | |
| 230 | void ModuleManager::returnVisitState(VisitState *State) { |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 231 | assert(State->NextState == nullptr && "Visited state is in list?"); |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 232 | State->NextState = FirstVisitState; |
| 233 | FirstVisitState = State; |
| 234 | } |
| 235 | |
Douglas Gregor | 7211ac1 | 2013-01-25 23:32:03 +0000 | [diff] [blame] | 236 | void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) { |
| 237 | GlobalIndex = Index; |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 238 | if (!GlobalIndex) { |
| 239 | ModulesInCommonWithGlobalIndex.clear(); |
| 240 | return; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 241 | } |
Douglas Gregor | 603cd86 | 2013-03-22 18:50:14 +0000 | [diff] [blame] | 242 | |
| 243 | // Notify the global module index about all of the modules we've already |
| 244 | // loaded. |
| 245 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
| 246 | if (!GlobalIndex->loadedModuleFile(Chain[I])) { |
| 247 | ModulesInCommonWithGlobalIndex.push_back(Chain[I]); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | void ModuleManager::moduleFileAccepted(ModuleFile *MF) { |
| 253 | if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF)) |
| 254 | return; |
| 255 | |
| 256 | ModulesInCommonWithGlobalIndex.push_back(MF); |
Douglas Gregor | 7211ac1 | 2013-01-25 23:32:03 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | ModuleManager::ModuleManager(FileManager &FileMgr) |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 260 | : FileMgr(FileMgr), GlobalIndex(), FirstVisitState(nullptr) {} |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 261 | |
| 262 | ModuleManager::~ModuleManager() { |
| 263 | for (unsigned i = 0, e = Chain.size(); i != e; ++i) |
| 264 | delete Chain[e - i - 1]; |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 265 | delete FirstVisitState; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Douglas Gregor | 7211ac1 | 2013-01-25 23:32:03 +0000 | [diff] [blame] | 268 | void |
| 269 | ModuleManager::visit(bool (*Visitor)(ModuleFile &M, void *UserData), |
| 270 | void *UserData, |
Craig Topper | 4dd9b43 | 2014-08-17 23:49:53 +0000 | [diff] [blame] | 271 | llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) { |
Douglas Gregor | 7211ac1 | 2013-01-25 23:32:03 +0000 | [diff] [blame] | 272 | // If the visitation order vector is the wrong size, recompute the order. |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 273 | if (VisitOrder.size() != Chain.size()) { |
| 274 | unsigned N = size(); |
| 275 | VisitOrder.clear(); |
| 276 | VisitOrder.reserve(N); |
| 277 | |
| 278 | // Record the number of incoming edges for each module. When we |
| 279 | // encounter a module with no incoming edges, push it into the queue |
| 280 | // to seed the queue. |
| 281 | SmallVector<ModuleFile *, 4> Queue; |
| 282 | Queue.reserve(N); |
| 283 | llvm::SmallVector<unsigned, 4> UnusedIncomingEdges; |
| 284 | UnusedIncomingEdges.reserve(size()); |
| 285 | for (ModuleIterator M = begin(), MEnd = end(); M != MEnd; ++M) { |
| 286 | if (unsigned Size = (*M)->ImportedBy.size()) |
| 287 | UnusedIncomingEdges.push_back(Size); |
| 288 | else { |
| 289 | UnusedIncomingEdges.push_back(0); |
| 290 | Queue.push_back(*M); |
| 291 | } |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 292 | } |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 293 | |
| 294 | // Traverse the graph, making sure to visit a module before visiting any |
| 295 | // of its dependencies. |
| 296 | unsigned QueueStart = 0; |
| 297 | while (QueueStart < Queue.size()) { |
| 298 | ModuleFile *CurrentModule = Queue[QueueStart++]; |
| 299 | VisitOrder.push_back(CurrentModule); |
| 300 | |
| 301 | // For any module that this module depends on, push it on the |
| 302 | // stack (if it hasn't already been marked as visited). |
| 303 | for (llvm::SetVector<ModuleFile *>::iterator |
| 304 | M = CurrentModule->Imports.begin(), |
| 305 | MEnd = CurrentModule->Imports.end(); |
| 306 | M != MEnd; ++M) { |
| 307 | // Remove our current module as an impediment to visiting the |
| 308 | // module we depend on. If we were the last unvisited module |
| 309 | // that depends on this particular module, push it into the |
| 310 | // queue to be visited. |
| 311 | unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index]; |
| 312 | if (NumUnusedEdges && (--NumUnusedEdges == 0)) |
| 313 | Queue.push_back(*M); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | assert(VisitOrder.size() == N && "Visitation order is wrong?"); |
Douglas Gregor | 7211ac1 | 2013-01-25 23:32:03 +0000 | [diff] [blame] | 318 | |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 319 | delete FirstVisitState; |
Craig Topper | a13603a | 2014-05-22 05:54:18 +0000 | [diff] [blame] | 320 | FirstVisitState = nullptr; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 321 | } |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 322 | |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 323 | VisitState *State = allocateVisitState(); |
| 324 | unsigned VisitNumber = State->NextVisitNumber++; |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 325 | |
Douglas Gregor | 7211ac1 | 2013-01-25 23:32:03 +0000 | [diff] [blame] | 326 | // If the caller has provided us with a hit-set that came from the global |
| 327 | // module index, mark every module file in common with the global module |
| 328 | // index that is *not* in that set as 'visited'. |
| 329 | if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) { |
| 330 | for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I) |
| 331 | { |
| 332 | ModuleFile *M = ModulesInCommonWithGlobalIndex[I]; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 333 | if (!ModuleFilesHit->count(M)) |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 334 | State->VisitNumber[M->Index] = VisitNumber; |
Douglas Gregor | 7211ac1 | 2013-01-25 23:32:03 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 338 | for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) { |
| 339 | ModuleFile *CurrentModule = VisitOrder[I]; |
| 340 | // Should we skip this module file? |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 341 | if (State->VisitNumber[CurrentModule->Index] == VisitNumber) |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 342 | continue; |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 343 | |
| 344 | // Visit the module. |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 345 | assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1); |
| 346 | State->VisitNumber[CurrentModule->Index] = VisitNumber; |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 347 | if (!Visitor(*CurrentModule, UserData)) |
| 348 | continue; |
| 349 | |
| 350 | // The visitor has requested that cut off visitation of any |
| 351 | // module that the current module depends on. To indicate this |
| 352 | // behavior, we mark all of the reachable modules as having been visited. |
| 353 | ModuleFile *NextModule = CurrentModule; |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 354 | do { |
| 355 | // For any module that this module depends on, push it on the |
| 356 | // stack (if it hasn't already been marked as visited). |
| 357 | for (llvm::SetVector<ModuleFile *>::iterator |
| 358 | M = NextModule->Imports.begin(), |
| 359 | MEnd = NextModule->Imports.end(); |
| 360 | M != MEnd; ++M) { |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 361 | if (State->VisitNumber[(*M)->Index] != VisitNumber) { |
| 362 | State->Stack.push_back(*M); |
| 363 | State->VisitNumber[(*M)->Index] = VisitNumber; |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 364 | } |
| 365 | } |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 366 | |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 367 | if (State->Stack.empty()) |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 368 | break; |
| 369 | |
| 370 | // Pop the next module off the stack. |
Robert Wilhelm | 25284cc | 2013-08-23 16:11:15 +0000 | [diff] [blame] | 371 | NextModule = State->Stack.pop_back_val(); |
Douglas Gregor | e41d7fe | 2013-01-25 22:25:23 +0000 | [diff] [blame] | 372 | } while (true); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 373 | } |
Douglas Gregor | e97cd90 | 2013-01-28 16:46:33 +0000 | [diff] [blame] | 374 | |
| 375 | returnVisitState(State); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /// \brief Perform a depth-first visit of the current module. |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 379 | static bool visitDepthFirst(ModuleFile &M, |
| 380 | bool (*Visitor)(ModuleFile &M, bool Preorder, |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 381 | void *UserData), |
| 382 | void *UserData, |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 383 | SmallVectorImpl<bool> &Visited) { |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 384 | // Preorder visitation |
| 385 | if (Visitor(M, /*Preorder=*/true, UserData)) |
| 386 | return true; |
| 387 | |
| 388 | // Visit children |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 389 | for (llvm::SetVector<ModuleFile *>::iterator IM = M.Imports.begin(), |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 390 | IMEnd = M.Imports.end(); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 391 | IM != IMEnd; ++IM) { |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 392 | if (Visited[(*IM)->Index]) |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 393 | continue; |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 394 | Visited[(*IM)->Index] = true; |
| 395 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 396 | if (visitDepthFirst(**IM, Visitor, UserData, Visited)) |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | // Postorder visitation |
| 401 | return Visitor(M, /*Preorder=*/false, UserData); |
| 402 | } |
| 403 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 404 | void ModuleManager::visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 405 | void *UserData), |
| 406 | void *UserData) { |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 407 | SmallVector<bool, 16> Visited(size(), false); |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 408 | for (unsigned I = 0, N = Chain.size(); I != N; ++I) { |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 409 | if (Visited[Chain[I]->Index]) |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 410 | continue; |
Douglas Gregor | bdb259d | 2013-01-21 20:07:12 +0000 | [diff] [blame] | 411 | Visited[Chain[I]->Index] = true; |
| 412 | |
Douglas Gregor | d44252e | 2011-08-25 20:47:51 +0000 | [diff] [blame] | 413 | if (::visitDepthFirst(*Chain[I], Visitor, UserData, Visited)) |
| 414 | return; |
| 415 | } |
| 416 | } |
Douglas Gregor | 9d7c1a2 | 2011-10-11 19:27:55 +0000 | [diff] [blame] | 417 | |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 418 | bool ModuleManager::lookupModuleFile(StringRef FileName, |
| 419 | off_t ExpectedSize, |
| 420 | time_t ExpectedModTime, |
| 421 | const FileEntry *&File) { |
Ben Langmuir | 05f82ba | 2014-05-01 03:33:36 +0000 | [diff] [blame] | 422 | // Open the file immediately to ensure there is no race between stat'ing and |
| 423 | // opening the file. |
| 424 | File = FileMgr.getFile(FileName, /*openFile=*/true, /*cacheFailure=*/false); |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 425 | |
| 426 | if (!File && FileName != "-") { |
| 427 | return false; |
| 428 | } |
| 429 | |
| 430 | if ((ExpectedSize && ExpectedSize != File->getSize()) || |
Ben Langmuir | 027731d | 2014-05-04 05:20:54 +0000 | [diff] [blame] | 431 | (ExpectedModTime && ExpectedModTime != File->getModificationTime())) |
| 432 | // Do not destroy File, as it may be referenced. If we need to rebuild it, |
| 433 | // it will be destroyed by removeModules. |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 434 | return true; |
Douglas Gregor | 7029ce1 | 2013-03-19 00:28:20 +0000 | [diff] [blame] | 435 | |
| 436 | return false; |
| 437 | } |
| 438 | |
Douglas Gregor | 9d7c1a2 | 2011-10-11 19:27:55 +0000 | [diff] [blame] | 439 | #ifndef NDEBUG |
| 440 | namespace llvm { |
| 441 | template<> |
| 442 | struct GraphTraits<ModuleManager> { |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 443 | typedef ModuleFile NodeType; |
| 444 | typedef llvm::SetVector<ModuleFile *>::const_iterator ChildIteratorType; |
Douglas Gregor | 9d7c1a2 | 2011-10-11 19:27:55 +0000 | [diff] [blame] | 445 | typedef ModuleManager::ModuleConstIterator nodes_iterator; |
| 446 | |
| 447 | static ChildIteratorType child_begin(NodeType *Node) { |
| 448 | return Node->Imports.begin(); |
| 449 | } |
| 450 | |
| 451 | static ChildIteratorType child_end(NodeType *Node) { |
| 452 | return Node->Imports.end(); |
| 453 | } |
| 454 | |
| 455 | static nodes_iterator nodes_begin(const ModuleManager &Manager) { |
| 456 | return Manager.begin(); |
| 457 | } |
| 458 | |
| 459 | static nodes_iterator nodes_end(const ModuleManager &Manager) { |
| 460 | return Manager.end(); |
| 461 | } |
| 462 | }; |
| 463 | |
| 464 | template<> |
| 465 | struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits { |
| 466 | explicit DOTGraphTraits(bool IsSimple = false) |
| 467 | : DefaultDOTGraphTraits(IsSimple) { } |
| 468 | |
| 469 | static bool renderGraphFromBottomUp() { |
| 470 | return true; |
| 471 | } |
| 472 | |
Douglas Gregor | de3ef50 | 2011-11-30 23:21:26 +0000 | [diff] [blame] | 473 | std::string getNodeLabel(ModuleFile *M, const ModuleManager&) { |
Ben Langmuir | beee15e | 2014-04-14 18:00:01 +0000 | [diff] [blame] | 474 | return M->ModuleName; |
Douglas Gregor | 9d7c1a2 | 2011-10-11 19:27:55 +0000 | [diff] [blame] | 475 | } |
| 476 | }; |
| 477 | } |
| 478 | |
| 479 | void ModuleManager::viewGraph() { |
| 480 | llvm::ViewGraph(*this, "Modules"); |
| 481 | } |
| 482 | #endif |