1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #include "clang/Frontend/PrecompiledPreamble.h" |
14 | #include "clang/AST/DeclObjC.h" |
15 | #include "clang/Basic/TargetInfo.h" |
16 | #include "clang/Frontend/CompilerInstance.h" |
17 | #include "clang/Frontend/CompilerInvocation.h" |
18 | #include "clang/Frontend/FrontendActions.h" |
19 | #include "clang/Frontend/FrontendOptions.h" |
20 | #include "clang/Lex/Lexer.h" |
21 | #include "clang/Lex/Preprocessor.h" |
22 | #include "clang/Lex/PreprocessorOptions.h" |
23 | #include "clang/Serialization/ASTWriter.h" |
24 | #include "llvm/ADT/StringExtras.h" |
25 | #include "llvm/ADT/StringSet.h" |
26 | #include "llvm/Config/llvm-config.h" |
27 | #include "llvm/Support/CrashRecoveryContext.h" |
28 | #include "llvm/Support/FileSystem.h" |
29 | #include "llvm/Support/Mutex.h" |
30 | #include "llvm/Support/MutexGuard.h" |
31 | #include "llvm/Support/Process.h" |
32 | #include "llvm/Support/VirtualFileSystem.h" |
33 | #include <limits> |
34 | #include <utility> |
35 | |
36 | using namespace clang; |
37 | |
38 | namespace { |
39 | |
40 | StringRef getInMemoryPreamblePath() { |
41 | #if defined(LLVM_ON_UNIX) |
42 | return "/__clang_tmp/___clang_inmemory_preamble___"; |
43 | #elif defined(_WIN32) |
44 | return "C:\\__clang_tmp\\___clang_inmemory_preamble___"; |
45 | #else |
46 | #warning "Unknown platform. Defaulting to UNIX-style paths for in-memory PCHs" |
47 | return "/__clang_tmp/___clang_inmemory_preamble___"; |
48 | #endif |
49 | } |
50 | |
51 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> |
52 | createVFSOverlayForPreamblePCH(StringRef PCHFilename, |
53 | std::unique_ptr<llvm::MemoryBuffer> PCHBuffer, |
54 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS) { |
55 | |
56 | |
57 | IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> PCHFS( |
58 | new llvm::vfs::InMemoryFileSystem()); |
59 | PCHFS->addFile(PCHFilename, 0, std::move(PCHBuffer)); |
60 | IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> Overlay( |
61 | new llvm::vfs::OverlayFileSystem(VFS)); |
62 | Overlay->pushOverlay(PCHFS); |
63 | return Overlay; |
64 | } |
65 | |
66 | class PreambleDependencyCollector : public DependencyCollector { |
67 | public: |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | bool needSystemDependencies() override { return true; } |
74 | }; |
75 | |
76 | |
77 | class TemporaryFiles { |
78 | public: |
79 | |
80 | static TemporaryFiles &getInstance(); |
81 | |
82 | private: |
83 | |
84 | TemporaryFiles() = default; |
85 | |
86 | TemporaryFiles(const TemporaryFiles &) = delete; |
87 | |
88 | public: |
89 | ~TemporaryFiles(); |
90 | |
91 | |
92 | void addFile(StringRef File); |
93 | |
94 | |
95 | void removeFile(StringRef File); |
96 | |
97 | private: |
98 | llvm::sys::SmartMutex<false> Mutex; |
99 | llvm::StringSet<> Files; |
100 | }; |
101 | |
102 | TemporaryFiles &TemporaryFiles::getInstance() { |
103 | static TemporaryFiles Instance; |
104 | return Instance; |
105 | } |
106 | |
107 | TemporaryFiles::~TemporaryFiles() { |
108 | llvm::MutexGuard Guard(Mutex); |
109 | for (const auto &File : Files) |
110 | llvm::sys::fs::remove(File.getKey()); |
111 | } |
112 | |
113 | void TemporaryFiles::addFile(StringRef File) { |
114 | llvm::MutexGuard Guard(Mutex); |
115 | auto IsInserted = Files.insert(File).second; |
116 | (void)IsInserted; |
117 | (0) . __assert_fail ("IsInserted && \"File has already been added\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 117, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IsInserted && "File has already been added"); |
118 | } |
119 | |
120 | void TemporaryFiles::removeFile(StringRef File) { |
121 | llvm::MutexGuard Guard(Mutex); |
122 | auto WasPresent = Files.erase(File); |
123 | (void)WasPresent; |
124 | (0) . __assert_fail ("WasPresent && \"File was not tracked\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 124, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(WasPresent && "File was not tracked"); |
125 | llvm::sys::fs::remove(File); |
126 | } |
127 | |
128 | class PrecompilePreambleAction : public ASTFrontendAction { |
129 | public: |
130 | PrecompilePreambleAction(std::string *InMemStorage, |
131 | PreambleCallbacks &Callbacks) |
132 | : InMemStorage(InMemStorage), Callbacks(Callbacks) {} |
133 | |
134 | std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, |
135 | StringRef InFile) override; |
136 | |
137 | bool hasEmittedPreamblePCH() const { return HasEmittedPreamblePCH; } |
138 | |
139 | void setEmittedPreamblePCH(ASTWriter &Writer) { |
140 | this->HasEmittedPreamblePCH = true; |
141 | Callbacks.AfterPCHEmitted(Writer); |
142 | } |
143 | |
144 | bool shouldEraseOutputFiles() override { return !hasEmittedPreamblePCH(); } |
145 | bool hasCodeCompletionSupport() const override { return false; } |
146 | bool hasASTFileSupport() const override { return false; } |
147 | TranslationUnitKind getTranslationUnitKind() override { return TU_Prefix; } |
148 | |
149 | private: |
150 | friend class PrecompilePreambleConsumer; |
151 | |
152 | bool HasEmittedPreamblePCH = false; |
153 | std::string *InMemStorage; |
154 | PreambleCallbacks &Callbacks; |
155 | }; |
156 | |
157 | class PrecompilePreambleConsumer : public PCHGenerator { |
158 | public: |
159 | PrecompilePreambleConsumer(PrecompilePreambleAction &Action, |
160 | const Preprocessor &PP, |
161 | InMemoryModuleCache &ModuleCache, |
162 | StringRef isysroot, |
163 | std::unique_ptr<raw_ostream> Out) |
164 | : PCHGenerator(PP, ModuleCache, "", isysroot, |
165 | std::make_shared<PCHBuffer>(), |
166 | ArrayRef<std::shared_ptr<ModuleFileExtension>>(), |
167 | ), |
168 | Action(Action), Out(std::move(Out)) {} |
169 | |
170 | bool HandleTopLevelDecl(DeclGroupRef DG) override { |
171 | Action.Callbacks.HandleTopLevelDecl(DG); |
172 | return true; |
173 | } |
174 | |
175 | void HandleTranslationUnit(ASTContext &Ctx) override { |
176 | PCHGenerator::HandleTranslationUnit(Ctx); |
177 | if (!hasEmittedPCH()) |
178 | return; |
179 | |
180 | |
181 | *Out << getPCH(); |
182 | |
183 | Out->flush(); |
184 | |
185 | llvm::SmallVector<char, 0> Empty; |
186 | getPCH() = std::move(Empty); |
187 | |
188 | Action.setEmittedPreamblePCH(getWriter()); |
189 | } |
190 | |
191 | private: |
192 | PrecompilePreambleAction &Action; |
193 | std::unique_ptr<raw_ostream> Out; |
194 | }; |
195 | |
196 | std::unique_ptr<ASTConsumer> |
197 | PrecompilePreambleAction::CreateASTConsumer(CompilerInstance &CI, |
198 | StringRef InFile) { |
199 | std::string Sysroot; |
200 | if (!GeneratePCHAction::ComputeASTConsumerArguments(CI, Sysroot)) |
201 | return nullptr; |
202 | |
203 | std::unique_ptr<llvm::raw_ostream> OS; |
204 | if (InMemStorage) { |
205 | OS = llvm::make_unique<llvm::raw_string_ostream>(*InMemStorage); |
206 | } else { |
207 | std::string OutputFile; |
208 | OS = GeneratePCHAction::CreateOutputFile(CI, InFile, OutputFile); |
209 | } |
210 | if (!OS) |
211 | return nullptr; |
212 | |
213 | if (!CI.getFrontendOpts().RelocatablePCH) |
214 | Sysroot.clear(); |
215 | |
216 | return llvm::make_unique<PrecompilePreambleConsumer>( |
217 | *this, CI.getPreprocessor(), CI.getModuleCache(), Sysroot, std::move(OS)); |
218 | } |
219 | |
220 | template <class T> bool moveOnNoError(llvm::ErrorOr<T> Val, T &Output) { |
221 | if (!Val) |
222 | return false; |
223 | Output = std::move(*Val); |
224 | return true; |
225 | } |
226 | |
227 | } |
228 | |
229 | PreambleBounds clang::ComputePreambleBounds(const LangOptions &LangOpts, |
230 | llvm::MemoryBuffer *Buffer, |
231 | unsigned MaxLines) { |
232 | return Lexer::ComputePreamble(Buffer->getBuffer(), LangOpts, MaxLines); |
233 | } |
234 | |
235 | llvm::ErrorOr<PrecompiledPreamble> PrecompiledPreamble::Build( |
236 | const CompilerInvocation &Invocation, |
237 | const llvm::MemoryBuffer *MainFileBuffer, PreambleBounds Bounds, |
238 | DiagnosticsEngine &Diagnostics, |
239 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, |
240 | std::shared_ptr<PCHContainerOperations> PCHContainerOps, bool StoreInMemory, |
241 | PreambleCallbacks &Callbacks) { |
242 | (0) . __assert_fail ("VFS && \"VFS is null\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 242, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(VFS && "VFS is null"); |
243 | |
244 | auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation); |
245 | FrontendOptions &FrontendOpts = PreambleInvocation->getFrontendOpts(); |
246 | PreprocessorOptions &PreprocessorOpts = |
247 | PreambleInvocation->getPreprocessorOpts(); |
248 | |
249 | llvm::Optional<TempPCHFile> TempFile; |
250 | if (!StoreInMemory) { |
251 | |
252 | |
253 | llvm::ErrorOr<PrecompiledPreamble::TempPCHFile> PreamblePCHFile = |
254 | PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile(); |
255 | if (!PreamblePCHFile) |
256 | return BuildPreambleError::CouldntCreateTempFile; |
257 | TempFile = std::move(*PreamblePCHFile); |
258 | } |
259 | |
260 | PCHStorage Storage = StoreInMemory ? PCHStorage(InMemoryPreamble()) |
261 | : PCHStorage(std::move(*TempFile)); |
262 | |
263 | |
264 | |
265 | std::vector<char> PreambleBytes(MainFileBuffer->getBufferStart(), |
266 | MainFileBuffer->getBufferStart() + |
267 | Bounds.Size); |
268 | bool PreambleEndsAtStartOfLine = Bounds.PreambleEndsAtStartOfLine; |
269 | |
270 | |
271 | FrontendOpts.ProgramAction = frontend::GeneratePCH; |
272 | FrontendOpts.OutputFile = StoreInMemory ? getInMemoryPreamblePath() |
273 | : Storage.asFile().getFilePath(); |
274 | PreprocessorOpts.PrecompiledPreambleBytes.first = 0; |
275 | PreprocessorOpts.PrecompiledPreambleBytes.second = false; |
276 | |
277 | PreprocessorOpts.GeneratePreamble = true; |
278 | |
279 | |
280 | std::unique_ptr<CompilerInstance> Clang( |
281 | new CompilerInstance(std::move(PCHContainerOps))); |
282 | |
283 | |
284 | llvm::CrashRecoveryContextCleanupRegistrar<CompilerInstance> CICleanup( |
285 | Clang.get()); |
286 | |
287 | Clang->setInvocation(std::move(PreambleInvocation)); |
288 | Clang->setDiagnostics(&Diagnostics); |
289 | |
290 | |
291 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
292 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
293 | if (!Clang->hasTarget()) |
294 | return BuildPreambleError::CouldntCreateTargetInfo; |
295 | |
296 | |
297 | |
298 | |
299 | |
300 | Clang->getTarget().adjust(Clang->getLangOpts()); |
301 | |
302 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs.size() == 1 && \"Invocation must have exactly one source file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 303, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs.size() == 1 && |
303 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs.size() == 1 && \"Invocation must have exactly one source file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 303, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invocation must have exactly one source file!"); |
304 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 306, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == |
305 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 306, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InputKind::Source && |
306 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getFormat() == InputKind..Source && \"FIXME. AST inputs not yet supported here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 306, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "FIXME: AST inputs not yet supported here!"); |
307 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 309, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != |
308 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 309, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> InputKind::LLVM_IR && |
309 | (0) . __assert_fail ("Clang->getFrontendOpts().Inputs[0].getKind().getLanguage() != InputKind..LLVM_IR && \"IR inputs not support here!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 309, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "IR inputs not support here!"); |
310 | |
311 | |
312 | Diagnostics.Reset(); |
313 | ProcessWarningOptions(Diagnostics, Clang->getDiagnosticOpts()); |
314 | |
315 | VFS = |
316 | createVFSFromCompilerInvocation(Clang->getInvocation(), Diagnostics, VFS); |
317 | |
318 | |
319 | Clang->setFileManager(new FileManager(Clang->getFileSystemOpts(), VFS)); |
320 | |
321 | |
322 | Clang->setSourceManager( |
323 | new SourceManager(Diagnostics, Clang->getFileManager())); |
324 | |
325 | auto PreambleDepCollector = std::make_shared<PreambleDependencyCollector>(); |
326 | Clang->addDependencyCollector(PreambleDepCollector); |
327 | |
328 | |
329 | StringRef MainFilePath = FrontendOpts.Inputs[0].getFile(); |
330 | auto PreambleInputBuffer = llvm::MemoryBuffer::getMemBufferCopy( |
331 | MainFileBuffer->getBuffer().slice(0, Bounds.Size), MainFilePath); |
332 | if (PreprocessorOpts.RetainRemappedFileBuffers) { |
333 | |
334 | PreprocessorOpts.addRemappedFile(MainFilePath, PreambleInputBuffer.get()); |
335 | } else { |
336 | |
337 | |
338 | PreprocessorOpts.addRemappedFile(MainFilePath, |
339 | PreambleInputBuffer.release()); |
340 | } |
341 | |
342 | std::unique_ptr<PrecompilePreambleAction> Act; |
343 | Act.reset(new PrecompilePreambleAction( |
344 | StoreInMemory ? &Storage.asMemory().Data : nullptr, Callbacks)); |
345 | Callbacks.BeforeExecute(*Clang); |
346 | if (!Act->BeginSourceFile(*Clang.get(), Clang->getFrontendOpts().Inputs[0])) |
347 | return BuildPreambleError::BeginSourceFileFailed; |
348 | |
349 | std::unique_ptr<PPCallbacks> DelegatedPPCallbacks = |
350 | Callbacks.createPPCallbacks(); |
351 | if (DelegatedPPCallbacks) |
352 | Clang->getPreprocessor().addPPCallbacks(std::move(DelegatedPPCallbacks)); |
353 | if (auto CommentHandler = Callbacks.getCommentHandler()) |
354 | Clang->getPreprocessor().addCommentHandler(CommentHandler); |
355 | |
356 | Act->Execute(); |
357 | |
358 | |
359 | Callbacks.AfterExecute(*Clang); |
360 | |
361 | Act->EndSourceFile(); |
362 | |
363 | if (!Act->hasEmittedPreamblePCH()) |
364 | return BuildPreambleError::CouldntEmitPCH; |
365 | |
366 | |
367 | |
368 | llvm::StringMap<PrecompiledPreamble::PreambleFileHash> FilesInPreamble; |
369 | |
370 | SourceManager &SourceMgr = Clang->getSourceManager(); |
371 | for (auto &Filename : PreambleDepCollector->getDependencies()) { |
372 | const FileEntry *File = Clang->getFileManager().getFile(Filename); |
373 | if (!File || File == SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())) |
374 | continue; |
375 | if (time_t ModTime = File->getModificationTime()) { |
376 | FilesInPreamble[File->getName()] = |
377 | PrecompiledPreamble::PreambleFileHash::createForFile(File->getSize(), |
378 | ModTime); |
379 | } else { |
380 | llvm::MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File); |
381 | FilesInPreamble[File->getName()] = |
382 | PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer(Buffer); |
383 | } |
384 | } |
385 | |
386 | return PrecompiledPreamble(std::move(Storage), std::move(PreambleBytes), |
387 | PreambleEndsAtStartOfLine, |
388 | std::move(FilesInPreamble)); |
389 | } |
390 | |
391 | PreambleBounds PrecompiledPreamble::getBounds() const { |
392 | return PreambleBounds(PreambleBytes.size(), PreambleEndsAtStartOfLine); |
393 | } |
394 | |
395 | std::size_t PrecompiledPreamble::getSize() const { |
396 | switch (Storage.getKind()) { |
397 | case PCHStorage::Kind::Empty: |
398 | (0) . __assert_fail ("false && \"Calling getSize() on invalid PrecompiledPreamble. \" \"Was it std..moved?\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 399, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(false && "Calling getSize() on invalid PrecompiledPreamble. " |
399 | (0) . __assert_fail ("false && \"Calling getSize() on invalid PrecompiledPreamble. \" \"Was it std..moved?\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 399, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Was it std::moved?"); |
400 | return 0; |
401 | case PCHStorage::Kind::InMemory: |
402 | return Storage.asMemory().Data.size(); |
403 | case PCHStorage::Kind::TempFile: { |
404 | uint64_t Result; |
405 | if (llvm::sys::fs::file_size(Storage.asFile().getFilePath(), Result)) |
406 | return 0; |
407 | |
408 | (0) . __assert_fail ("Result <= std..numeric_limits..max() && \"file size did not fit into size_t\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 409, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Result <= std::numeric_limits<std::size_t>::max() && |
409 | (0) . __assert_fail ("Result <= std..numeric_limits..max() && \"file size did not fit into size_t\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 409, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "file size did not fit into size_t"); |
410 | return Result; |
411 | } |
412 | } |
413 | llvm_unreachable("Unhandled storage kind"); |
414 | } |
415 | |
416 | bool PrecompiledPreamble::CanReuse(const CompilerInvocation &Invocation, |
417 | const llvm::MemoryBuffer *MainFileBuffer, |
418 | PreambleBounds Bounds, |
419 | llvm::vfs::FileSystem *VFS) const { |
420 | |
421 | (0) . __assert_fail ("Bounds.Size <= MainFileBuffer->getBufferSize() && \"Buffer is too large. Bounds were calculated from a different buffer?\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 423, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert( |
422 | (0) . __assert_fail ("Bounds.Size <= MainFileBuffer->getBufferSize() && \"Buffer is too large. Bounds were calculated from a different buffer?\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 423, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> Bounds.Size <= MainFileBuffer->getBufferSize() && |
423 | (0) . __assert_fail ("Bounds.Size <= MainFileBuffer->getBufferSize() && \"Buffer is too large. Bounds were calculated from a different buffer?\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 423, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Buffer is too large. Bounds were calculated from a different buffer?"); |
424 | |
425 | auto PreambleInvocation = std::make_shared<CompilerInvocation>(Invocation); |
426 | PreprocessorOptions &PreprocessorOpts = |
427 | PreambleInvocation->getPreprocessorOpts(); |
428 | |
429 | |
430 | |
431 | |
432 | |
433 | if (PreambleBytes.size() != Bounds.Size || |
434 | PreambleEndsAtStartOfLine != Bounds.PreambleEndsAtStartOfLine || |
435 | !std::equal(PreambleBytes.begin(), PreambleBytes.end(), |
436 | MainFileBuffer->getBuffer().begin())) |
437 | return false; |
438 | |
439 | |
440 | |
441 | |
442 | |
443 | |
444 | std::map<llvm::sys::fs::UniqueID, PreambleFileHash> OverriddenFiles; |
445 | for (const auto &R : PreprocessorOpts.RemappedFiles) { |
446 | llvm::vfs::Status Status; |
447 | if (!moveOnNoError(VFS->status(R.second), Status)) { |
448 | |
449 | |
450 | return false; |
451 | } |
452 | |
453 | OverriddenFiles[Status.getUniqueID()] = PreambleFileHash::createForFile( |
454 | Status.getSize(), llvm::sys::toTimeT(Status.getLastModificationTime())); |
455 | } |
456 | |
457 | for (const auto &RB : PreprocessorOpts.RemappedFileBuffers) { |
458 | llvm::vfs::Status Status; |
459 | if (!moveOnNoError(VFS->status(RB.first), Status)) |
460 | return false; |
461 | |
462 | OverriddenFiles[Status.getUniqueID()] = |
463 | PreambleFileHash::createForMemoryBuffer(RB.second); |
464 | } |
465 | |
466 | |
467 | for (const auto &F : FilesInPreamble) { |
468 | llvm::vfs::Status Status; |
469 | if (!moveOnNoError(VFS->status(F.first()), Status)) { |
470 | |
471 | return false; |
472 | } |
473 | |
474 | std::map<llvm::sys::fs::UniqueID, PreambleFileHash>::iterator Overridden = |
475 | OverriddenFiles.find(Status.getUniqueID()); |
476 | if (Overridden != OverriddenFiles.end()) { |
477 | |
478 | |
479 | if (Overridden->second != F.second) |
480 | return false; |
481 | continue; |
482 | } |
483 | |
484 | |
485 | if (Status.getSize() != uint64_t(F.second.Size) || |
486 | llvm::sys::toTimeT(Status.getLastModificationTime()) != |
487 | F.second.ModTime) |
488 | return false; |
489 | } |
490 | return true; |
491 | } |
492 | |
493 | void PrecompiledPreamble::AddImplicitPreamble( |
494 | CompilerInvocation &CI, IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS, |
495 | llvm::MemoryBuffer *MainFileBuffer) const { |
496 | PreambleBounds Bounds(PreambleBytes.size(), PreambleEndsAtStartOfLine); |
497 | configurePreamble(Bounds, CI, VFS, MainFileBuffer); |
498 | } |
499 | |
500 | void PrecompiledPreamble::OverridePreamble( |
501 | CompilerInvocation &CI, IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS, |
502 | llvm::MemoryBuffer *MainFileBuffer) const { |
503 | auto Bounds = ComputePreambleBounds(*CI.getLangOpts(), MainFileBuffer, 0); |
504 | configurePreamble(Bounds, CI, VFS, MainFileBuffer); |
505 | } |
506 | |
507 | PrecompiledPreamble::PrecompiledPreamble( |
508 | PCHStorage Storage, std::vector<char> PreambleBytes, |
509 | bool PreambleEndsAtStartOfLine, |
510 | llvm::StringMap<PreambleFileHash> FilesInPreamble) |
511 | : Storage(std::move(Storage)), FilesInPreamble(std::move(FilesInPreamble)), |
512 | PreambleBytes(std::move(PreambleBytes)), |
513 | PreambleEndsAtStartOfLine(PreambleEndsAtStartOfLine) { |
514 | Storage.getKind() != PCHStorage..Kind..Empty", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 514, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(this->Storage.getKind() != PCHStorage::Kind::Empty); |
515 | } |
516 | |
517 | llvm::ErrorOr<PrecompiledPreamble::TempPCHFile> |
518 | PrecompiledPreamble::TempPCHFile::CreateNewPreamblePCHFile() { |
519 | |
520 | |
521 | |
522 | const char *TmpFile = ::getenv("CINDEXTEST_PREAMBLE_FILE"); |
523 | if (TmpFile) |
524 | return TempPCHFile::createFromCustomPath(TmpFile); |
525 | return TempPCHFile::createInSystemTempDir("preamble", "pch"); |
526 | } |
527 | |
528 | llvm::ErrorOr<PrecompiledPreamble::TempPCHFile> |
529 | PrecompiledPreamble::TempPCHFile::createInSystemTempDir(const Twine &Prefix, |
530 | StringRef Suffix) { |
531 | llvm::SmallString<64> File; |
532 | |
533 | |
534 | |
535 | int FD; |
536 | auto EC = llvm::sys::fs::createTemporaryFile(Prefix, Suffix, FD, File); |
537 | if (EC) |
538 | return EC; |
539 | |
540 | llvm::sys::Process::SafelyCloseFileDescriptor(FD); |
541 | return TempPCHFile(std::move(File).str()); |
542 | } |
543 | |
544 | llvm::ErrorOr<PrecompiledPreamble::TempPCHFile> |
545 | PrecompiledPreamble::TempPCHFile::createFromCustomPath(const Twine &Path) { |
546 | return TempPCHFile(Path.str()); |
547 | } |
548 | |
549 | PrecompiledPreamble::TempPCHFile::TempPCHFile(std::string FilePath) |
550 | : FilePath(std::move(FilePath)) { |
551 | TemporaryFiles::getInstance().addFile(*this->FilePath); |
552 | } |
553 | |
554 | PrecompiledPreamble::TempPCHFile::TempPCHFile(TempPCHFile &&Other) { |
555 | FilePath = std::move(Other.FilePath); |
556 | Other.FilePath = None; |
557 | } |
558 | |
559 | PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::TempPCHFile:: |
560 | operator=(TempPCHFile &&Other) { |
561 | RemoveFileIfPresent(); |
562 | |
563 | FilePath = std::move(Other.FilePath); |
564 | Other.FilePath = None; |
565 | return *this; |
566 | } |
567 | |
568 | PrecompiledPreamble::TempPCHFile::~TempPCHFile() { RemoveFileIfPresent(); } |
569 | |
570 | void PrecompiledPreamble::TempPCHFile::RemoveFileIfPresent() { |
571 | if (FilePath) { |
572 | TemporaryFiles::getInstance().removeFile(*FilePath); |
573 | FilePath = None; |
574 | } |
575 | } |
576 | |
577 | llvm::StringRef PrecompiledPreamble::TempPCHFile::getFilePath() const { |
578 | (0) . __assert_fail ("FilePath && \"TempPCHFile doesn't have a FilePath. Had it been moved?\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/PrecompiledPreamble.cpp", 578, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FilePath && "TempPCHFile doesn't have a FilePath. Had it been moved?"); |
579 | return *FilePath; |
580 | } |
581 | |
582 | PrecompiledPreamble::PCHStorage::PCHStorage(TempPCHFile File) |
583 | : StorageKind(Kind::TempFile) { |
584 | new (&asFile()) TempPCHFile(std::move(File)); |
585 | } |
586 | |
587 | PrecompiledPreamble::PCHStorage::PCHStorage(InMemoryPreamble Memory) |
588 | : StorageKind(Kind::InMemory) { |
589 | new (&asMemory()) InMemoryPreamble(std::move(Memory)); |
590 | } |
591 | |
592 | PrecompiledPreamble::PCHStorage::PCHStorage(PCHStorage &&Other) : PCHStorage() { |
593 | *this = std::move(Other); |
594 | } |
595 | |
596 | PrecompiledPreamble::PCHStorage &PrecompiledPreamble::PCHStorage:: |
597 | operator=(PCHStorage &&Other) { |
598 | destroy(); |
599 | |
600 | StorageKind = Other.StorageKind; |
601 | switch (StorageKind) { |
602 | case Kind::Empty: |
603 | |
604 | break; |
605 | case Kind::TempFile: |
606 | new (&asFile()) TempPCHFile(std::move(Other.asFile())); |
607 | break; |
608 | case Kind::InMemory: |
609 | new (&asMemory()) InMemoryPreamble(std::move(Other.asMemory())); |
610 | break; |
611 | } |
612 | |
613 | Other.setEmpty(); |
614 | return *this; |
615 | } |
616 | |
617 | PrecompiledPreamble::PCHStorage::~PCHStorage() { destroy(); } |
618 | |
619 | PrecompiledPreamble::PCHStorage::Kind |
620 | PrecompiledPreamble::PCHStorage::getKind() const { |
621 | return StorageKind; |
622 | } |
623 | |
624 | PrecompiledPreamble::TempPCHFile &PrecompiledPreamble::PCHStorage::asFile() { |
625 | assert(getKind() == Kind::TempFile); |
626 | return *reinterpret_cast<TempPCHFile *>(Storage.buffer); |
627 | } |
628 | |
629 | const PrecompiledPreamble::TempPCHFile & |
630 | PrecompiledPreamble::PCHStorage::asFile() const { |
631 | return const_cast<PCHStorage *>(this)->asFile(); |
632 | } |
633 | |
634 | PrecompiledPreamble::InMemoryPreamble & |
635 | PrecompiledPreamble::PCHStorage::asMemory() { |
636 | assert(getKind() == Kind::InMemory); |
637 | return *reinterpret_cast<InMemoryPreamble *>(Storage.buffer); |
638 | } |
639 | |
640 | const PrecompiledPreamble::InMemoryPreamble & |
641 | PrecompiledPreamble::PCHStorage::asMemory() const { |
642 | return const_cast<PCHStorage *>(this)->asMemory(); |
643 | } |
644 | |
645 | void PrecompiledPreamble::PCHStorage::destroy() { |
646 | switch (StorageKind) { |
647 | case Kind::Empty: |
648 | return; |
649 | case Kind::TempFile: |
650 | asFile().~TempPCHFile(); |
651 | return; |
652 | case Kind::InMemory: |
653 | asMemory().~InMemoryPreamble(); |
654 | return; |
655 | } |
656 | } |
657 | |
658 | void PrecompiledPreamble::PCHStorage::setEmpty() { |
659 | destroy(); |
660 | StorageKind = Kind::Empty; |
661 | } |
662 | |
663 | PrecompiledPreamble::PreambleFileHash |
664 | PrecompiledPreamble::PreambleFileHash::createForFile(off_t Size, |
665 | time_t ModTime) { |
666 | PreambleFileHash Result; |
667 | Result.Size = Size; |
668 | Result.ModTime = ModTime; |
669 | Result.MD5 = {}; |
670 | return Result; |
671 | } |
672 | |
673 | PrecompiledPreamble::PreambleFileHash |
674 | PrecompiledPreamble::PreambleFileHash::createForMemoryBuffer( |
675 | const llvm::MemoryBuffer *Buffer) { |
676 | PreambleFileHash Result; |
677 | Result.Size = Buffer->getBufferSize(); |
678 | Result.ModTime = 0; |
679 | |
680 | llvm::MD5 MD5Ctx; |
681 | MD5Ctx.update(Buffer->getBuffer().data()); |
682 | MD5Ctx.final(Result.MD5); |
683 | |
684 | return Result; |
685 | } |
686 | |
687 | void PrecompiledPreamble::configurePreamble( |
688 | PreambleBounds Bounds, CompilerInvocation &CI, |
689 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS, |
690 | llvm::MemoryBuffer *MainFileBuffer) const { |
691 | assert(VFS); |
692 | |
693 | auto &PreprocessorOpts = CI.getPreprocessorOpts(); |
694 | |
695 | |
696 | auto MainFilePath = CI.getFrontendOpts().Inputs[0].getFile(); |
697 | PreprocessorOpts.addRemappedFile(MainFilePath, MainFileBuffer); |
698 | |
699 | |
700 | PreprocessorOpts.PrecompiledPreambleBytes.first = Bounds.Size; |
701 | PreprocessorOpts.PrecompiledPreambleBytes.second = |
702 | Bounds.PreambleEndsAtStartOfLine; |
703 | PreprocessorOpts.DisablePCHValidation = true; |
704 | |
705 | setupPreambleStorage(Storage, PreprocessorOpts, VFS); |
706 | } |
707 | |
708 | void PrecompiledPreamble::setupPreambleStorage( |
709 | const PCHStorage &Storage, PreprocessorOptions &PreprocessorOpts, |
710 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> &VFS) { |
711 | if (Storage.getKind() == PCHStorage::Kind::TempFile) { |
712 | const TempPCHFile &PCHFile = Storage.asFile(); |
713 | PreprocessorOpts.ImplicitPCHInclude = PCHFile.getFilePath(); |
714 | |
715 | |
716 | IntrusiveRefCntPtr<llvm::vfs::FileSystem> RealFS = |
717 | llvm::vfs::getRealFileSystem(); |
718 | auto PCHPath = PCHFile.getFilePath(); |
719 | if (VFS == RealFS || VFS->exists(PCHPath)) |
720 | return; |
721 | auto Buf = RealFS->getBufferForFile(PCHPath); |
722 | if (!Buf) { |
723 | |
724 | |
725 | |
726 | return; |
727 | } |
728 | |
729 | |
730 | |
731 | VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(*Buf), VFS); |
732 | } else { |
733 | assert(Storage.getKind() == PCHStorage::Kind::InMemory); |
734 | |
735 | |
736 | StringRef PCHPath = getInMemoryPreamblePath(); |
737 | PreprocessorOpts.ImplicitPCHInclude = PCHPath; |
738 | |
739 | auto Buf = llvm::MemoryBuffer::getMemBuffer(Storage.asMemory().Data); |
740 | VFS = createVFSOverlayForPreamblePCH(PCHPath, std::move(Buf), VFS); |
741 | } |
742 | } |
743 | |
744 | void PreambleCallbacks::BeforeExecute(CompilerInstance &CI) {} |
745 | void PreambleCallbacks::AfterExecute(CompilerInstance &CI) {} |
746 | void PreambleCallbacks::AfterPCHEmitted(ASTWriter &Writer) {} |
747 | void PreambleCallbacks::HandleTopLevelDecl(DeclGroupRef DG) {} |
748 | std::unique_ptr<PPCallbacks> PreambleCallbacks::createPPCallbacks() { |
749 | return nullptr; |
750 | } |
751 | CommentHandler *PreambleCallbacks::getCommentHandler() { return nullptr; } |
752 | |
753 | static llvm::ManagedStatic<BuildPreambleErrorCategory> BuildPreambleErrCategory; |
754 | |
755 | std::error_code clang::make_error_code(BuildPreambleError Error) { |
756 | return std::error_code(static_cast<int>(Error), *BuildPreambleErrCategory); |
757 | } |
758 | |
759 | const char *BuildPreambleErrorCategory::name() const noexcept { |
760 | return "build-preamble.error"; |
761 | } |
762 | |
763 | std::string BuildPreambleErrorCategory::message(int condition) const { |
764 | switch (static_cast<BuildPreambleError>(condition)) { |
765 | case BuildPreambleError::CouldntCreateTempFile: |
766 | return "Could not create temporary file for PCH"; |
767 | case BuildPreambleError::CouldntCreateTargetInfo: |
768 | return "CreateTargetInfo() return null"; |
769 | case BuildPreambleError::BeginSourceFileFailed: |
770 | return "BeginSourceFile() return an error"; |
771 | case BuildPreambleError::CouldntEmitPCH: |
772 | return "Could not emit PCH"; |
773 | } |
774 | llvm_unreachable("unexpected BuildPreambleError"); |
775 | } |
776 | |