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