Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 1 | //===--- Compiler.cpp --------------------------------------------*- C++-*-===// |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +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 |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 6 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Eric Liu | b99d5e8 | 2017-12-14 21:22:03 +0000 | [diff] [blame] | 8 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 9 | #include "Compiler.h" |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 10 | #include "Logger.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 11 | #include "clang/Basic/TargetInfo.h" |
| 12 | #include "clang/Lex/PreprocessorOptions.h" |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Format.h" |
| 14 | #include "llvm/Support/FormatVariadic.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 15 | |
| 16 | namespace clang { |
| 17 | namespace clangd { |
| 18 | |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 19 | void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel, |
| 20 | const clang::Diagnostic &Info) { |
Sam McCall | 3fd25fc | 2018-11-02 12:51:26 +0000 | [diff] [blame] | 21 | // FIXME: format lazily, in case vlog is off. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 22 | llvm::SmallString<64> Message; |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 23 | Info.FormatDiagnostic(Message); |
| 24 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 25 | llvm::SmallString<64> Location; |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 26 | if (Info.hasSourceManager() && Info.getLocation().isValid()) { |
| 27 | auto &SourceMgr = Info.getSourceManager(); |
| 28 | auto Loc = SourceMgr.getFileLoc(Info.getLocation()); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 29 | llvm::raw_svector_ostream OS(Location); |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 30 | Loc.print(OS, SourceMgr); |
| 31 | OS << ":"; |
| 32 | } |
| 33 | |
Sam McCall | 3fd25fc | 2018-11-02 12:51:26 +0000 | [diff] [blame] | 34 | clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message); |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 38 | const clang::Diagnostic &Info) { |
| 39 | IgnoreDiagnostics::log(DiagLevel, Info); |
| 40 | } |
| 41 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 42 | std::unique_ptr<CompilerInstance> |
| 43 | prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI, |
| 44 | const PrecompiledPreamble *Preamble, |
| 45 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
| 46 | std::shared_ptr<PCHContainerOperations> PCHs, |
| 47 | llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, |
| 48 | DiagnosticConsumer &DiagsClient) { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 49 | assert(VFS && "VFS is null"); |
| 50 | assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers && |
| 51 | "Setting RetainRemappedFileBuffers to true will cause a memory leak " |
| 52 | "of ContentsBuffer"); |
| 53 | |
| 54 | // NOTE: we use Buffer.get() when adding remapped files, so we have to make |
| 55 | // sure it will be released if no error is emitted. |
| 56 | if (Preamble) { |
Ilya Biryukov | 295c8e1 | 2018-01-18 15:17:00 +0000 | [diff] [blame] | 57 | Preamble->OverridePreamble(*CI, VFS, Buffer.get()); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 58 | } else { |
| 59 | CI->getPreprocessorOpts().addRemappedFile( |
| 60 | CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get()); |
| 61 | } |
| 62 | |
| 63 | auto Clang = llvm::make_unique<CompilerInstance>(PCHs); |
| 64 | Clang->setInvocation(std::move(CI)); |
| 65 | Clang->createDiagnostics(&DiagsClient, false); |
| 66 | |
| 67 | if (auto VFSWithRemapping = createVFSFromCompilerInvocation( |
| 68 | Clang->getInvocation(), Clang->getDiagnostics(), VFS)) |
| 69 | VFS = VFSWithRemapping; |
| 70 | Clang->setVirtualFileSystem(VFS); |
| 71 | |
| 72 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
| 73 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
| 74 | if (!Clang->hasTarget()) |
| 75 | return nullptr; |
| 76 | |
| 77 | // RemappedFileBuffers will handle the lifetime of the Buffer pointer, |
| 78 | // release it. |
| 79 | Buffer.release(); |
| 80 | return Clang; |
| 81 | } |
| 82 | |
| 83 | } // namespace clangd |
| 84 | } // namespace clang |