annotate clang/lib/Serialization/ModuleManager.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 79ff65ed7e25
children c4bab56944e8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 //===- ModuleManager.cpp - Module Manager ---------------------------------===//
anatofuz
parents:
diff changeset
2 //
anatofuz
parents:
diff changeset
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
anatofuz
parents:
diff changeset
4 // See https://llvm.org/LICENSE.txt for license information.
anatofuz
parents:
diff changeset
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
anatofuz
parents:
diff changeset
6 //
anatofuz
parents:
diff changeset
7 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
8 //
anatofuz
parents:
diff changeset
9 // This file defines the ModuleManager class, which manages a set of loaded
anatofuz
parents:
diff changeset
10 // modules for the ASTReader.
anatofuz
parents:
diff changeset
11 //
anatofuz
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
anatofuz
parents:
diff changeset
13
anatofuz
parents:
diff changeset
14 #include "clang/Serialization/ModuleManager.h"
anatofuz
parents:
diff changeset
15 #include "clang/Basic/FileManager.h"
anatofuz
parents:
diff changeset
16 #include "clang/Basic/LLVM.h"
anatofuz
parents:
diff changeset
17 #include "clang/Lex/HeaderSearch.h"
anatofuz
parents:
diff changeset
18 #include "clang/Lex/ModuleMap.h"
anatofuz
parents:
diff changeset
19 #include "clang/Serialization/GlobalModuleIndex.h"
anatofuz
parents:
diff changeset
20 #include "clang/Serialization/InMemoryModuleCache.h"
anatofuz
parents:
diff changeset
21 #include "clang/Serialization/ModuleFile.h"
anatofuz
parents:
diff changeset
22 #include "clang/Serialization/PCHContainerOperations.h"
anatofuz
parents:
diff changeset
23 #include "llvm/ADT/STLExtras.h"
anatofuz
parents:
diff changeset
24 #include "llvm/ADT/SetVector.h"
anatofuz
parents:
diff changeset
25 #include "llvm/ADT/SmallPtrSet.h"
anatofuz
parents:
diff changeset
26 #include "llvm/ADT/SmallVector.h"
anatofuz
parents:
diff changeset
27 #include "llvm/ADT/StringRef.h"
anatofuz
parents:
diff changeset
28 #include "llvm/ADT/iterator.h"
anatofuz
parents:
diff changeset
29 #include "llvm/Support/Chrono.h"
anatofuz
parents:
diff changeset
30 #include "llvm/Support/DOTGraphTraits.h"
anatofuz
parents:
diff changeset
31 #include "llvm/Support/ErrorOr.h"
anatofuz
parents:
diff changeset
32 #include "llvm/Support/GraphWriter.h"
anatofuz
parents:
diff changeset
33 #include "llvm/Support/MemoryBuffer.h"
anatofuz
parents:
diff changeset
34 #include "llvm/Support/VirtualFileSystem.h"
anatofuz
parents:
diff changeset
35 #include <algorithm>
anatofuz
parents:
diff changeset
36 #include <cassert>
anatofuz
parents:
diff changeset
37 #include <memory>
anatofuz
parents:
diff changeset
38 #include <string>
anatofuz
parents:
diff changeset
39 #include <system_error>
anatofuz
parents:
diff changeset
40
anatofuz
parents:
diff changeset
41 using namespace clang;
anatofuz
parents:
diff changeset
42 using namespace serialization;
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 ModuleFile *ModuleManager::lookupByFileName(StringRef Name) const {
anatofuz
parents:
diff changeset
45 auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
anatofuz
parents:
diff changeset
46 /*CacheFailure=*/false);
anatofuz
parents:
diff changeset
47 if (Entry)
anatofuz
parents:
diff changeset
48 return lookup(*Entry);
anatofuz
parents:
diff changeset
49
anatofuz
parents:
diff changeset
50 return nullptr;
anatofuz
parents:
diff changeset
51 }
anatofuz
parents:
diff changeset
52
anatofuz
parents:
diff changeset
53 ModuleFile *ModuleManager::lookupByModuleName(StringRef Name) const {
anatofuz
parents:
diff changeset
54 if (const Module *Mod = HeaderSearchInfo.getModuleMap().findModule(Name))
anatofuz
parents:
diff changeset
55 if (const FileEntry *File = Mod->getASTFile())
anatofuz
parents:
diff changeset
56 return lookup(File);
anatofuz
parents:
diff changeset
57
anatofuz
parents:
diff changeset
58 return nullptr;
anatofuz
parents:
diff changeset
59 }
anatofuz
parents:
diff changeset
60
anatofuz
parents:
diff changeset
61 ModuleFile *ModuleManager::lookup(const FileEntry *File) const {
anatofuz
parents:
diff changeset
62 auto Known = Modules.find(File);
anatofuz
parents:
diff changeset
63 if (Known == Modules.end())
anatofuz
parents:
diff changeset
64 return nullptr;
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 return Known->second;
anatofuz
parents:
diff changeset
67 }
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 std::unique_ptr<llvm::MemoryBuffer>
anatofuz
parents:
diff changeset
70 ModuleManager::lookupBuffer(StringRef Name) {
anatofuz
parents:
diff changeset
71 auto Entry = FileMgr.getFile(Name, /*OpenFile=*/false,
anatofuz
parents:
diff changeset
72 /*CacheFailure=*/false);
anatofuz
parents:
diff changeset
73 if (!Entry)
anatofuz
parents:
diff changeset
74 return nullptr;
anatofuz
parents:
diff changeset
75 return std::move(InMemoryBuffers[*Entry]);
anatofuz
parents:
diff changeset
76 }
anatofuz
parents:
diff changeset
77
anatofuz
parents:
diff changeset
78 static bool checkSignature(ASTFileSignature Signature,
anatofuz
parents:
diff changeset
79 ASTFileSignature ExpectedSignature,
anatofuz
parents:
diff changeset
80 std::string &ErrorStr) {
anatofuz
parents:
diff changeset
81 if (!ExpectedSignature || Signature == ExpectedSignature)
anatofuz
parents:
diff changeset
82 return false;
anatofuz
parents:
diff changeset
83
anatofuz
parents:
diff changeset
84 ErrorStr =
anatofuz
parents:
diff changeset
85 Signature ? "signature mismatch" : "could not read module signature";
anatofuz
parents:
diff changeset
86 return true;
anatofuz
parents:
diff changeset
87 }
anatofuz
parents:
diff changeset
88
anatofuz
parents:
diff changeset
89 static void updateModuleImports(ModuleFile &MF, ModuleFile *ImportedBy,
anatofuz
parents:
diff changeset
90 SourceLocation ImportLoc) {
anatofuz
parents:
diff changeset
91 if (ImportedBy) {
anatofuz
parents:
diff changeset
92 MF.ImportedBy.insert(ImportedBy);
anatofuz
parents:
diff changeset
93 ImportedBy->Imports.insert(&MF);
anatofuz
parents:
diff changeset
94 } else {
anatofuz
parents:
diff changeset
95 if (!MF.DirectlyImported)
anatofuz
parents:
diff changeset
96 MF.ImportLoc = ImportLoc;
anatofuz
parents:
diff changeset
97
anatofuz
parents:
diff changeset
98 MF.DirectlyImported = true;
anatofuz
parents:
diff changeset
99 }
anatofuz
parents:
diff changeset
100 }
anatofuz
parents:
diff changeset
101
anatofuz
parents:
diff changeset
102 ModuleManager::AddModuleResult
anatofuz
parents:
diff changeset
103 ModuleManager::addModule(StringRef FileName, ModuleKind Type,
anatofuz
parents:
diff changeset
104 SourceLocation ImportLoc, ModuleFile *ImportedBy,
anatofuz
parents:
diff changeset
105 unsigned Generation,
anatofuz
parents:
diff changeset
106 off_t ExpectedSize, time_t ExpectedModTime,
anatofuz
parents:
diff changeset
107 ASTFileSignature ExpectedSignature,
anatofuz
parents:
diff changeset
108 ASTFileSignatureReader ReadSignature,
anatofuz
parents:
diff changeset
109 ModuleFile *&Module,
anatofuz
parents:
diff changeset
110 std::string &ErrorStr) {
anatofuz
parents:
diff changeset
111 Module = nullptr;
anatofuz
parents:
diff changeset
112
anatofuz
parents:
diff changeset
113 // Look for the file entry. This only fails if the expected size or
anatofuz
parents:
diff changeset
114 // modification time differ.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
115 OptionalFileEntryRefDegradesToFileEntryPtr Entry;
150
anatofuz
parents:
diff changeset
116 if (Type == MK_ExplicitModule || Type == MK_PrebuiltModule) {
anatofuz
parents:
diff changeset
117 // If we're not expecting to pull this file out of the module cache, it
anatofuz
parents:
diff changeset
118 // might have a different mtime due to being moved across filesystems in
anatofuz
parents:
diff changeset
119 // a distributed build. The size must still match, though. (As must the
anatofuz
parents:
diff changeset
120 // contents, but we can't check that.)
anatofuz
parents:
diff changeset
121 ExpectedModTime = 0;
anatofuz
parents:
diff changeset
122 }
anatofuz
parents:
diff changeset
123 // Note: ExpectedSize and ExpectedModTime will be 0 for MK_ImplicitModule
anatofuz
parents:
diff changeset
124 // when using an ASTFileSignature.
anatofuz
parents:
diff changeset
125 if (lookupModuleFile(FileName, ExpectedSize, ExpectedModTime, Entry)) {
anatofuz
parents:
diff changeset
126 ErrorStr = "module file out of date";
anatofuz
parents:
diff changeset
127 return OutOfDate;
anatofuz
parents:
diff changeset
128 }
anatofuz
parents:
diff changeset
129
anatofuz
parents:
diff changeset
130 if (!Entry && FileName != "-") {
anatofuz
parents:
diff changeset
131 ErrorStr = "module file not found";
anatofuz
parents:
diff changeset
132 return Missing;
anatofuz
parents:
diff changeset
133 }
anatofuz
parents:
diff changeset
134
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
135 // The ModuleManager's use of FileEntry nodes as the keys for its map of
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
136 // loaded modules is less than ideal. Uniqueness for FileEntry nodes is
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
137 // maintained by FileManager, which in turn uses inode numbers on hosts
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
138 // that support that. When coupled with the module cache's proclivity for
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
139 // turning over and deleting stale PCMs, this means entries for different
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
140 // module files can wind up reusing the same underlying inode. When this
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
141 // happens, subsequent accesses to the Modules map will disagree on the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
142 // ModuleFile associated with a given file. In general, it is not sufficient
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
143 // to resolve this conundrum with a type like FileEntryRef that stores the
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
144 // name of the FileEntry node on first access because of path canonicalization
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
145 // issues. However, the paths constructed for implicit module builds are
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
146 // fully under Clang's control. We *can*, therefore, rely on their structure
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
147 // being consistent across operating systems and across subsequent accesses
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
148 // to the Modules map.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
149 auto implicitModuleNamesMatch = [](ModuleKind Kind, const ModuleFile *MF,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
150 const FileEntry *Entry) -> bool {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
151 if (Kind != MK_ImplicitModule)
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
152 return true;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
153 return Entry->getName() == MF->FileName;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
154 };
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
155
150
anatofuz
parents:
diff changeset
156 // Check whether we already loaded this module, before
anatofuz
parents:
diff changeset
157 if (ModuleFile *ModuleEntry = Modules.lookup(Entry)) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
158 if (implicitModuleNamesMatch(Type, ModuleEntry, Entry)) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
159 // Check the stored signature.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
160 if (checkSignature(ModuleEntry->Signature, ExpectedSignature, ErrorStr))
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
161 return OutOfDate;
150
anatofuz
parents:
diff changeset
162
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
163 Module = ModuleEntry;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
164 updateModuleImports(*ModuleEntry, ImportedBy, ImportLoc);
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
165 return AlreadyLoaded;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
166 }
150
anatofuz
parents:
diff changeset
167 }
anatofuz
parents:
diff changeset
168
anatofuz
parents:
diff changeset
169 // Allocate a new module.
anatofuz
parents:
diff changeset
170 auto NewModule = std::make_unique<ModuleFile>(Type, Generation);
anatofuz
parents:
diff changeset
171 NewModule->Index = Chain.size();
anatofuz
parents:
diff changeset
172 NewModule->FileName = FileName.str();
anatofuz
parents:
diff changeset
173 NewModule->File = Entry;
anatofuz
parents:
diff changeset
174 NewModule->ImportLoc = ImportLoc;
anatofuz
parents:
diff changeset
175 NewModule->InputFilesValidationTimestamp = 0;
anatofuz
parents:
diff changeset
176
anatofuz
parents:
diff changeset
177 if (NewModule->Kind == MK_ImplicitModule) {
anatofuz
parents:
diff changeset
178 std::string TimestampFilename = NewModule->getTimestampFilename();
anatofuz
parents:
diff changeset
179 llvm::vfs::Status Status;
anatofuz
parents:
diff changeset
180 // A cached stat value would be fine as well.
anatofuz
parents:
diff changeset
181 if (!FileMgr.getNoncachedStatValue(TimestampFilename, Status))
anatofuz
parents:
diff changeset
182 NewModule->InputFilesValidationTimestamp =
anatofuz
parents:
diff changeset
183 llvm::sys::toTimeT(Status.getLastModificationTime());
anatofuz
parents:
diff changeset
184 }
anatofuz
parents:
diff changeset
185
anatofuz
parents:
diff changeset
186 // Load the contents of the module
anatofuz
parents:
diff changeset
187 if (std::unique_ptr<llvm::MemoryBuffer> Buffer = lookupBuffer(FileName)) {
anatofuz
parents:
diff changeset
188 // The buffer was already provided for us.
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
189 NewModule->Buffer = &ModuleCache->addBuiltPCM(FileName, std::move(Buffer));
150
anatofuz
parents:
diff changeset
190 // Since the cached buffer is reused, it is safe to close the file
anatofuz
parents:
diff changeset
191 // descriptor that was opened while stat()ing the PCM in
anatofuz
parents:
diff changeset
192 // lookupModuleFile() above, it won't be needed any longer.
anatofuz
parents:
diff changeset
193 Entry->closeFile();
anatofuz
parents:
diff changeset
194 } else if (llvm::MemoryBuffer *Buffer =
anatofuz
parents:
diff changeset
195 getModuleCache().lookupPCM(FileName)) {
anatofuz
parents:
diff changeset
196 NewModule->Buffer = Buffer;
anatofuz
parents:
diff changeset
197 // As above, the file descriptor is no longer needed.
anatofuz
parents:
diff changeset
198 Entry->closeFile();
173
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
199 } else if (getModuleCache().shouldBuildPCM(FileName)) {
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
200 // Report that the module is out of date, since we tried (and failed) to
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
201 // import it earlier.
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
202 Entry->closeFile();
0572611fdcc8 reorgnization done
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
203 return OutOfDate;
150
anatofuz
parents:
diff changeset
204 } else {
anatofuz
parents:
diff changeset
205 // Open the AST file.
anatofuz
parents:
diff changeset
206 llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> Buf((std::error_code()));
anatofuz
parents:
diff changeset
207 if (FileName == "-") {
anatofuz
parents:
diff changeset
208 Buf = llvm::MemoryBuffer::getSTDIN();
anatofuz
parents:
diff changeset
209 } else {
anatofuz
parents:
diff changeset
210 // Get a buffer of the file and close the file descriptor when done.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
211 // The file is volatile because in a parallel build we expect multiple
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
212 // compiler processes to use the same module file rebuilding it if needed.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
213 //
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
214 // RequiresNullTerminator is false because module files don't need it, and
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
215 // this allows the file to still be mmapped.
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
216 Buf = FileMgr.getBufferForFile(NewModule->File,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
217 /*IsVolatile=*/true,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
218 /*RequiresNullTerminator=*/false);
150
anatofuz
parents:
diff changeset
219 }
anatofuz
parents:
diff changeset
220
anatofuz
parents:
diff changeset
221 if (!Buf) {
anatofuz
parents:
diff changeset
222 ErrorStr = Buf.getError().message();
anatofuz
parents:
diff changeset
223 return Missing;
anatofuz
parents:
diff changeset
224 }
anatofuz
parents:
diff changeset
225
anatofuz
parents:
diff changeset
226 NewModule->Buffer = &getModuleCache().addPCM(FileName, std::move(*Buf));
anatofuz
parents:
diff changeset
227 }
anatofuz
parents:
diff changeset
228
anatofuz
parents:
diff changeset
229 // Initialize the stream.
anatofuz
parents:
diff changeset
230 NewModule->Data = PCHContainerRdr.ExtractPCH(*NewModule->Buffer);
anatofuz
parents:
diff changeset
231
anatofuz
parents:
diff changeset
232 // Read the signature eagerly now so that we can check it. Avoid calling
anatofuz
parents:
diff changeset
233 // ReadSignature unless there's something to check though.
anatofuz
parents:
diff changeset
234 if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
anatofuz
parents:
diff changeset
235 ExpectedSignature, ErrorStr))
anatofuz
parents:
diff changeset
236 return OutOfDate;
anatofuz
parents:
diff changeset
237
anatofuz
parents:
diff changeset
238 // We're keeping this module. Store it everywhere.
anatofuz
parents:
diff changeset
239 Module = Modules[Entry] = NewModule.get();
anatofuz
parents:
diff changeset
240
anatofuz
parents:
diff changeset
241 updateModuleImports(*NewModule, ImportedBy, ImportLoc);
anatofuz
parents:
diff changeset
242
anatofuz
parents:
diff changeset
243 if (!NewModule->isModule())
anatofuz
parents:
diff changeset
244 PCHChain.push_back(NewModule.get());
anatofuz
parents:
diff changeset
245 if (!ImportedBy)
anatofuz
parents:
diff changeset
246 Roots.push_back(NewModule.get());
anatofuz
parents:
diff changeset
247
anatofuz
parents:
diff changeset
248 Chain.push_back(std::move(NewModule));
anatofuz
parents:
diff changeset
249 return NewlyLoaded;
anatofuz
parents:
diff changeset
250 }
anatofuz
parents:
diff changeset
251
anatofuz
parents:
diff changeset
252 void ModuleManager::removeModules(ModuleIterator First, ModuleMap *modMap) {
anatofuz
parents:
diff changeset
253 auto Last = end();
anatofuz
parents:
diff changeset
254 if (First == Last)
anatofuz
parents:
diff changeset
255 return;
anatofuz
parents:
diff changeset
256
anatofuz
parents:
diff changeset
257 // Explicitly clear VisitOrder since we might not notice it is stale.
anatofuz
parents:
diff changeset
258 VisitOrder.clear();
anatofuz
parents:
diff changeset
259
anatofuz
parents:
diff changeset
260 // Collect the set of module file pointers that we'll be removing.
anatofuz
parents:
diff changeset
261 llvm::SmallPtrSet<ModuleFile *, 4> victimSet(
anatofuz
parents:
diff changeset
262 (llvm::pointer_iterator<ModuleIterator>(First)),
anatofuz
parents:
diff changeset
263 (llvm::pointer_iterator<ModuleIterator>(Last)));
anatofuz
parents:
diff changeset
264
anatofuz
parents:
diff changeset
265 auto IsVictim = [&](ModuleFile *MF) {
anatofuz
parents:
diff changeset
266 return victimSet.count(MF);
anatofuz
parents:
diff changeset
267 };
anatofuz
parents:
diff changeset
268 // Remove any references to the now-destroyed modules.
anatofuz
parents:
diff changeset
269 for (auto I = begin(); I != First; ++I) {
anatofuz
parents:
diff changeset
270 I->Imports.remove_if(IsVictim);
anatofuz
parents:
diff changeset
271 I->ImportedBy.remove_if(IsVictim);
anatofuz
parents:
diff changeset
272 }
anatofuz
parents:
diff changeset
273 Roots.erase(std::remove_if(Roots.begin(), Roots.end(), IsVictim),
anatofuz
parents:
diff changeset
274 Roots.end());
anatofuz
parents:
diff changeset
275
anatofuz
parents:
diff changeset
276 // Remove the modules from the PCH chain.
anatofuz
parents:
diff changeset
277 for (auto I = First; I != Last; ++I) {
anatofuz
parents:
diff changeset
278 if (!I->isModule()) {
anatofuz
parents:
diff changeset
279 PCHChain.erase(llvm::find(PCHChain, &*I), PCHChain.end());
anatofuz
parents:
diff changeset
280 break;
anatofuz
parents:
diff changeset
281 }
anatofuz
parents:
diff changeset
282 }
anatofuz
parents:
diff changeset
283
anatofuz
parents:
diff changeset
284 // Delete the modules and erase them from the various structures.
anatofuz
parents:
diff changeset
285 for (ModuleIterator victim = First; victim != Last; ++victim) {
anatofuz
parents:
diff changeset
286 Modules.erase(victim->File);
anatofuz
parents:
diff changeset
287
anatofuz
parents:
diff changeset
288 if (modMap) {
anatofuz
parents:
diff changeset
289 StringRef ModuleName = victim->ModuleName;
anatofuz
parents:
diff changeset
290 if (Module *mod = modMap->findModule(ModuleName)) {
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
291 mod->setASTFile(None);
150
anatofuz
parents:
diff changeset
292 }
anatofuz
parents:
diff changeset
293 }
anatofuz
parents:
diff changeset
294 }
anatofuz
parents:
diff changeset
295
anatofuz
parents:
diff changeset
296 // Delete the modules.
anatofuz
parents:
diff changeset
297 Chain.erase(Chain.begin() + (First - begin()), Chain.end());
anatofuz
parents:
diff changeset
298 }
anatofuz
parents:
diff changeset
299
anatofuz
parents:
diff changeset
300 void
anatofuz
parents:
diff changeset
301 ModuleManager::addInMemoryBuffer(StringRef FileName,
anatofuz
parents:
diff changeset
302 std::unique_ptr<llvm::MemoryBuffer> Buffer) {
anatofuz
parents:
diff changeset
303 const FileEntry *Entry =
anatofuz
parents:
diff changeset
304 FileMgr.getVirtualFile(FileName, Buffer->getBufferSize(), 0);
anatofuz
parents:
diff changeset
305 InMemoryBuffers[Entry] = std::move(Buffer);
anatofuz
parents:
diff changeset
306 }
anatofuz
parents:
diff changeset
307
anatofuz
parents:
diff changeset
308 ModuleManager::VisitState *ModuleManager::allocateVisitState() {
anatofuz
parents:
diff changeset
309 // Fast path: if we have a cached state, use it.
anatofuz
parents:
diff changeset
310 if (FirstVisitState) {
anatofuz
parents:
diff changeset
311 VisitState *Result = FirstVisitState;
anatofuz
parents:
diff changeset
312 FirstVisitState = FirstVisitState->NextState;
anatofuz
parents:
diff changeset
313 Result->NextState = nullptr;
anatofuz
parents:
diff changeset
314 return Result;
anatofuz
parents:
diff changeset
315 }
anatofuz
parents:
diff changeset
316
anatofuz
parents:
diff changeset
317 // Allocate and return a new state.
anatofuz
parents:
diff changeset
318 return new VisitState(size());
anatofuz
parents:
diff changeset
319 }
anatofuz
parents:
diff changeset
320
anatofuz
parents:
diff changeset
321 void ModuleManager::returnVisitState(VisitState *State) {
anatofuz
parents:
diff changeset
322 assert(State->NextState == nullptr && "Visited state is in list?");
anatofuz
parents:
diff changeset
323 State->NextState = FirstVisitState;
anatofuz
parents:
diff changeset
324 FirstVisitState = State;
anatofuz
parents:
diff changeset
325 }
anatofuz
parents:
diff changeset
326
anatofuz
parents:
diff changeset
327 void ModuleManager::setGlobalIndex(GlobalModuleIndex *Index) {
anatofuz
parents:
diff changeset
328 GlobalIndex = Index;
anatofuz
parents:
diff changeset
329 if (!GlobalIndex) {
anatofuz
parents:
diff changeset
330 ModulesInCommonWithGlobalIndex.clear();
anatofuz
parents:
diff changeset
331 return;
anatofuz
parents:
diff changeset
332 }
anatofuz
parents:
diff changeset
333
anatofuz
parents:
diff changeset
334 // Notify the global module index about all of the modules we've already
anatofuz
parents:
diff changeset
335 // loaded.
anatofuz
parents:
diff changeset
336 for (ModuleFile &M : *this)
anatofuz
parents:
diff changeset
337 if (!GlobalIndex->loadedModuleFile(&M))
anatofuz
parents:
diff changeset
338 ModulesInCommonWithGlobalIndex.push_back(&M);
anatofuz
parents:
diff changeset
339 }
anatofuz
parents:
diff changeset
340
anatofuz
parents:
diff changeset
341 void ModuleManager::moduleFileAccepted(ModuleFile *MF) {
anatofuz
parents:
diff changeset
342 if (!GlobalIndex || GlobalIndex->loadedModuleFile(MF))
anatofuz
parents:
diff changeset
343 return;
anatofuz
parents:
diff changeset
344
anatofuz
parents:
diff changeset
345 ModulesInCommonWithGlobalIndex.push_back(MF);
anatofuz
parents:
diff changeset
346 }
anatofuz
parents:
diff changeset
347
anatofuz
parents:
diff changeset
348 ModuleManager::ModuleManager(FileManager &FileMgr,
anatofuz
parents:
diff changeset
349 InMemoryModuleCache &ModuleCache,
anatofuz
parents:
diff changeset
350 const PCHContainerReader &PCHContainerRdr,
anatofuz
parents:
diff changeset
351 const HeaderSearch &HeaderSearchInfo)
anatofuz
parents:
diff changeset
352 : FileMgr(FileMgr), ModuleCache(&ModuleCache),
anatofuz
parents:
diff changeset
353 PCHContainerRdr(PCHContainerRdr), HeaderSearchInfo(HeaderSearchInfo) {}
anatofuz
parents:
diff changeset
354
anatofuz
parents:
diff changeset
355 ModuleManager::~ModuleManager() { delete FirstVisitState; }
anatofuz
parents:
diff changeset
356
anatofuz
parents:
diff changeset
357 void ModuleManager::visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
anatofuz
parents:
diff changeset
358 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit) {
anatofuz
parents:
diff changeset
359 // If the visitation order vector is the wrong size, recompute the order.
anatofuz
parents:
diff changeset
360 if (VisitOrder.size() != Chain.size()) {
anatofuz
parents:
diff changeset
361 unsigned N = size();
anatofuz
parents:
diff changeset
362 VisitOrder.clear();
anatofuz
parents:
diff changeset
363 VisitOrder.reserve(N);
anatofuz
parents:
diff changeset
364
anatofuz
parents:
diff changeset
365 // Record the number of incoming edges for each module. When we
anatofuz
parents:
diff changeset
366 // encounter a module with no incoming edges, push it into the queue
anatofuz
parents:
diff changeset
367 // to seed the queue.
anatofuz
parents:
diff changeset
368 SmallVector<ModuleFile *, 4> Queue;
anatofuz
parents:
diff changeset
369 Queue.reserve(N);
anatofuz
parents:
diff changeset
370 llvm::SmallVector<unsigned, 4> UnusedIncomingEdges;
anatofuz
parents:
diff changeset
371 UnusedIncomingEdges.resize(size());
anatofuz
parents:
diff changeset
372 for (ModuleFile &M : llvm::reverse(*this)) {
anatofuz
parents:
diff changeset
373 unsigned Size = M.ImportedBy.size();
anatofuz
parents:
diff changeset
374 UnusedIncomingEdges[M.Index] = Size;
anatofuz
parents:
diff changeset
375 if (!Size)
anatofuz
parents:
diff changeset
376 Queue.push_back(&M);
anatofuz
parents:
diff changeset
377 }
anatofuz
parents:
diff changeset
378
anatofuz
parents:
diff changeset
379 // Traverse the graph, making sure to visit a module before visiting any
anatofuz
parents:
diff changeset
380 // of its dependencies.
anatofuz
parents:
diff changeset
381 while (!Queue.empty()) {
anatofuz
parents:
diff changeset
382 ModuleFile *CurrentModule = Queue.pop_back_val();
anatofuz
parents:
diff changeset
383 VisitOrder.push_back(CurrentModule);
anatofuz
parents:
diff changeset
384
anatofuz
parents:
diff changeset
385 // For any module that this module depends on, push it on the
anatofuz
parents:
diff changeset
386 // stack (if it hasn't already been marked as visited).
anatofuz
parents:
diff changeset
387 for (auto M = CurrentModule->Imports.rbegin(),
anatofuz
parents:
diff changeset
388 MEnd = CurrentModule->Imports.rend();
anatofuz
parents:
diff changeset
389 M != MEnd; ++M) {
anatofuz
parents:
diff changeset
390 // Remove our current module as an impediment to visiting the
anatofuz
parents:
diff changeset
391 // module we depend on. If we were the last unvisited module
anatofuz
parents:
diff changeset
392 // that depends on this particular module, push it into the
anatofuz
parents:
diff changeset
393 // queue to be visited.
anatofuz
parents:
diff changeset
394 unsigned &NumUnusedEdges = UnusedIncomingEdges[(*M)->Index];
anatofuz
parents:
diff changeset
395 if (NumUnusedEdges && (--NumUnusedEdges == 0))
anatofuz
parents:
diff changeset
396 Queue.push_back(*M);
anatofuz
parents:
diff changeset
397 }
anatofuz
parents:
diff changeset
398 }
anatofuz
parents:
diff changeset
399
anatofuz
parents:
diff changeset
400 assert(VisitOrder.size() == N && "Visitation order is wrong?");
anatofuz
parents:
diff changeset
401
anatofuz
parents:
diff changeset
402 delete FirstVisitState;
anatofuz
parents:
diff changeset
403 FirstVisitState = nullptr;
anatofuz
parents:
diff changeset
404 }
anatofuz
parents:
diff changeset
405
anatofuz
parents:
diff changeset
406 VisitState *State = allocateVisitState();
anatofuz
parents:
diff changeset
407 unsigned VisitNumber = State->NextVisitNumber++;
anatofuz
parents:
diff changeset
408
anatofuz
parents:
diff changeset
409 // If the caller has provided us with a hit-set that came from the global
anatofuz
parents:
diff changeset
410 // module index, mark every module file in common with the global module
anatofuz
parents:
diff changeset
411 // index that is *not* in that set as 'visited'.
anatofuz
parents:
diff changeset
412 if (ModuleFilesHit && !ModulesInCommonWithGlobalIndex.empty()) {
anatofuz
parents:
diff changeset
413 for (unsigned I = 0, N = ModulesInCommonWithGlobalIndex.size(); I != N; ++I)
anatofuz
parents:
diff changeset
414 {
anatofuz
parents:
diff changeset
415 ModuleFile *M = ModulesInCommonWithGlobalIndex[I];
anatofuz
parents:
diff changeset
416 if (!ModuleFilesHit->count(M))
anatofuz
parents:
diff changeset
417 State->VisitNumber[M->Index] = VisitNumber;
anatofuz
parents:
diff changeset
418 }
anatofuz
parents:
diff changeset
419 }
anatofuz
parents:
diff changeset
420
anatofuz
parents:
diff changeset
421 for (unsigned I = 0, N = VisitOrder.size(); I != N; ++I) {
anatofuz
parents:
diff changeset
422 ModuleFile *CurrentModule = VisitOrder[I];
anatofuz
parents:
diff changeset
423 // Should we skip this module file?
anatofuz
parents:
diff changeset
424 if (State->VisitNumber[CurrentModule->Index] == VisitNumber)
anatofuz
parents:
diff changeset
425 continue;
anatofuz
parents:
diff changeset
426
anatofuz
parents:
diff changeset
427 // Visit the module.
anatofuz
parents:
diff changeset
428 assert(State->VisitNumber[CurrentModule->Index] == VisitNumber - 1);
anatofuz
parents:
diff changeset
429 State->VisitNumber[CurrentModule->Index] = VisitNumber;
anatofuz
parents:
diff changeset
430 if (!Visitor(*CurrentModule))
anatofuz
parents:
diff changeset
431 continue;
anatofuz
parents:
diff changeset
432
anatofuz
parents:
diff changeset
433 // The visitor has requested that cut off visitation of any
anatofuz
parents:
diff changeset
434 // module that the current module depends on. To indicate this
anatofuz
parents:
diff changeset
435 // behavior, we mark all of the reachable modules as having been visited.
anatofuz
parents:
diff changeset
436 ModuleFile *NextModule = CurrentModule;
anatofuz
parents:
diff changeset
437 do {
anatofuz
parents:
diff changeset
438 // For any module that this module depends on, push it on the
anatofuz
parents:
diff changeset
439 // stack (if it hasn't already been marked as visited).
anatofuz
parents:
diff changeset
440 for (llvm::SetVector<ModuleFile *>::iterator
anatofuz
parents:
diff changeset
441 M = NextModule->Imports.begin(),
anatofuz
parents:
diff changeset
442 MEnd = NextModule->Imports.end();
anatofuz
parents:
diff changeset
443 M != MEnd; ++M) {
anatofuz
parents:
diff changeset
444 if (State->VisitNumber[(*M)->Index] != VisitNumber) {
anatofuz
parents:
diff changeset
445 State->Stack.push_back(*M);
anatofuz
parents:
diff changeset
446 State->VisitNumber[(*M)->Index] = VisitNumber;
anatofuz
parents:
diff changeset
447 }
anatofuz
parents:
diff changeset
448 }
anatofuz
parents:
diff changeset
449
anatofuz
parents:
diff changeset
450 if (State->Stack.empty())
anatofuz
parents:
diff changeset
451 break;
anatofuz
parents:
diff changeset
452
anatofuz
parents:
diff changeset
453 // Pop the next module off the stack.
anatofuz
parents:
diff changeset
454 NextModule = State->Stack.pop_back_val();
anatofuz
parents:
diff changeset
455 } while (true);
anatofuz
parents:
diff changeset
456 }
anatofuz
parents:
diff changeset
457
anatofuz
parents:
diff changeset
458 returnVisitState(State);
anatofuz
parents:
diff changeset
459 }
anatofuz
parents:
diff changeset
460
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
461 bool ModuleManager::lookupModuleFile(StringRef FileName, off_t ExpectedSize,
150
anatofuz
parents:
diff changeset
462 time_t ExpectedModTime,
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
463 Optional<FileEntryRef> &File) {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
464 File = None;
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
465 if (FileName == "-")
150
anatofuz
parents:
diff changeset
466 return false;
anatofuz
parents:
diff changeset
467
anatofuz
parents:
diff changeset
468 // Open the file immediately to ensure there is no race between stat'ing and
anatofuz
parents:
diff changeset
469 // opening the file.
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
470 Optional<FileEntryRef> FileOrErr =
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
471 expectedToOptional(FileMgr.getFileRef(FileName, /*OpenFile=*/true,
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
472 /*CacheFailure=*/false));
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
473 if (!FileOrErr)
150
anatofuz
parents:
diff changeset
474 return false;
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 173
diff changeset
475
150
anatofuz
parents:
diff changeset
476 File = *FileOrErr;
anatofuz
parents:
diff changeset
477
anatofuz
parents:
diff changeset
478 if ((ExpectedSize && ExpectedSize != File->getSize()) ||
anatofuz
parents:
diff changeset
479 (ExpectedModTime && ExpectedModTime != File->getModificationTime()))
anatofuz
parents:
diff changeset
480 // Do not destroy File, as it may be referenced. If we need to rebuild it,
anatofuz
parents:
diff changeset
481 // it will be destroyed by removeModules.
anatofuz
parents:
diff changeset
482 return true;
anatofuz
parents:
diff changeset
483
anatofuz
parents:
diff changeset
484 return false;
anatofuz
parents:
diff changeset
485 }
anatofuz
parents:
diff changeset
486
anatofuz
parents:
diff changeset
487 #ifndef NDEBUG
anatofuz
parents:
diff changeset
488 namespace llvm {
anatofuz
parents:
diff changeset
489
anatofuz
parents:
diff changeset
490 template<>
anatofuz
parents:
diff changeset
491 struct GraphTraits<ModuleManager> {
anatofuz
parents:
diff changeset
492 using NodeRef = ModuleFile *;
anatofuz
parents:
diff changeset
493 using ChildIteratorType = llvm::SetVector<ModuleFile *>::const_iterator;
anatofuz
parents:
diff changeset
494 using nodes_iterator = pointer_iterator<ModuleManager::ModuleConstIterator>;
anatofuz
parents:
diff changeset
495
anatofuz
parents:
diff changeset
496 static ChildIteratorType child_begin(NodeRef Node) {
anatofuz
parents:
diff changeset
497 return Node->Imports.begin();
anatofuz
parents:
diff changeset
498 }
anatofuz
parents:
diff changeset
499
anatofuz
parents:
diff changeset
500 static ChildIteratorType child_end(NodeRef Node) {
anatofuz
parents:
diff changeset
501 return Node->Imports.end();
anatofuz
parents:
diff changeset
502 }
anatofuz
parents:
diff changeset
503
anatofuz
parents:
diff changeset
504 static nodes_iterator nodes_begin(const ModuleManager &Manager) {
anatofuz
parents:
diff changeset
505 return nodes_iterator(Manager.begin());
anatofuz
parents:
diff changeset
506 }
anatofuz
parents:
diff changeset
507
anatofuz
parents:
diff changeset
508 static nodes_iterator nodes_end(const ModuleManager &Manager) {
anatofuz
parents:
diff changeset
509 return nodes_iterator(Manager.end());
anatofuz
parents:
diff changeset
510 }
anatofuz
parents:
diff changeset
511 };
anatofuz
parents:
diff changeset
512
anatofuz
parents:
diff changeset
513 template<>
anatofuz
parents:
diff changeset
514 struct DOTGraphTraits<ModuleManager> : public DefaultDOTGraphTraits {
anatofuz
parents:
diff changeset
515 explicit DOTGraphTraits(bool IsSimple = false)
anatofuz
parents:
diff changeset
516 : DefaultDOTGraphTraits(IsSimple) {}
anatofuz
parents:
diff changeset
517
anatofuz
parents:
diff changeset
518 static bool renderGraphFromBottomUp() { return true; }
anatofuz
parents:
diff changeset
519
anatofuz
parents:
diff changeset
520 std::string getNodeLabel(ModuleFile *M, const ModuleManager&) {
anatofuz
parents:
diff changeset
521 return M->ModuleName;
anatofuz
parents:
diff changeset
522 }
anatofuz
parents:
diff changeset
523 };
anatofuz
parents:
diff changeset
524
anatofuz
parents:
diff changeset
525 } // namespace llvm
anatofuz
parents:
diff changeset
526
anatofuz
parents:
diff changeset
527 void ModuleManager::viewGraph() {
anatofuz
parents:
diff changeset
528 llvm::ViewGraph(*this, "Modules");
anatofuz
parents:
diff changeset
529 }
anatofuz
parents:
diff changeset
530 #endif