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