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