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