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