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" |
Sam McCall | 4e56502 | 2019-04-04 12:56:03 +0000 | [diff] [blame] | 13 | #include "clang/Serialization/PCHContainerOperations.h" |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Format.h" |
| 15 | #include "llvm/Support/FormatVariadic.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 16 | |
| 17 | namespace clang { |
| 18 | namespace clangd { |
| 19 | |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 20 | void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel, |
| 21 | const clang::Diagnostic &Info) { |
Sam McCall | 3fd25fc | 2018-11-02 12:51:26 +0000 | [diff] [blame] | 22 | // FIXME: format lazily, in case vlog is off. |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 23 | llvm::SmallString<64> Message; |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 24 | Info.FormatDiagnostic(Message); |
| 25 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 26 | llvm::SmallString<64> Location; |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 27 | if (Info.hasSourceManager() && Info.getLocation().isValid()) { |
| 28 | auto &SourceMgr = Info.getSourceManager(); |
| 29 | auto Loc = SourceMgr.getFileLoc(Info.getLocation()); |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 30 | llvm::raw_svector_ostream OS(Location); |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 31 | Loc.print(OS, SourceMgr); |
| 32 | OS << ":"; |
| 33 | } |
| 34 | |
Sam McCall | 3fd25fc | 2018-11-02 12:51:26 +0000 | [diff] [blame] | 35 | clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message); |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
| 39 | const clang::Diagnostic &Info) { |
| 40 | IgnoreDiagnostics::log(DiagLevel, Info); |
| 41 | } |
| 42 | |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 43 | std::unique_ptr<CompilerInvocation> |
Ilya Biryukov | d73ac96 | 2019-08-28 09:24:55 +0000 | [diff] [blame] | 44 | buildCompilerInvocation(const ParseInputs &Inputs, |
| 45 | clang::DiagnosticConsumer &D) { |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 46 | std::vector<const char *> ArgStrs; |
| 47 | for (const auto &S : Inputs.CompileCommand.CommandLine) |
| 48 | ArgStrs.push_back(S.c_str()); |
| 49 | |
| 50 | if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) { |
| 51 | log("Couldn't set working directory when creating compiler invocation."); |
| 52 | // We proceed anyway, our lit-tests rely on results for non-existing working |
| 53 | // dirs. |
| 54 | } |
| 55 | |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 56 | llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine = |
Ilya Biryukov | d73ac96 | 2019-08-28 09:24:55 +0000 | [diff] [blame] | 57 | CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false); |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 58 | std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine( |
Ilya Biryukov | 999e4c4 | 2019-08-27 10:02:18 +0000 | [diff] [blame] | 59 | ArgStrs, CommandLineDiagsEngine, Inputs.FS, |
| 60 | /*ShouldRecoverOnErrors=*/true); |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 61 | if (!CI) |
| 62 | return nullptr; |
| 63 | // createInvocationFromCommandLine sets DisableFree. |
| 64 | CI->getFrontendOpts().DisableFree = false; |
| 65 | CI->getLangOpts()->CommentOpts.ParseAllComments = true; |
| 66 | return CI; |
| 67 | } |
| 68 | |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 69 | std::unique_ptr<CompilerInstance> |
| 70 | prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI, |
| 71 | const PrecompiledPreamble *Preamble, |
| 72 | std::unique_ptr<llvm::MemoryBuffer> Buffer, |
Ilya Biryukov | f2001aa | 2019-01-07 15:45:19 +0000 | [diff] [blame] | 73 | llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS, |
| 74 | DiagnosticConsumer &DiagsClient) { |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 75 | assert(VFS && "VFS is null"); |
| 76 | assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers && |
| 77 | "Setting RetainRemappedFileBuffers to true will cause a memory leak " |
| 78 | "of ContentsBuffer"); |
| 79 | |
| 80 | // NOTE: we use Buffer.get() when adding remapped files, so we have to make |
| 81 | // sure it will be released if no error is emitted. |
| 82 | if (Preamble) { |
Ilya Biryukov | 295c8e1 | 2018-01-18 15:17:00 +0000 | [diff] [blame] | 83 | Preamble->OverridePreamble(*CI, VFS, Buffer.get()); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 84 | } else { |
| 85 | CI->getPreprocessorOpts().addRemappedFile( |
| 86 | CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get()); |
| 87 | } |
| 88 | |
Jonas Devlieghere | 1c705d9 | 2019-08-14 23:52:23 +0000 | [diff] [blame] | 89 | auto Clang = std::make_unique<CompilerInstance>( |
Sam McCall | 4e56502 | 2019-04-04 12:56:03 +0000 | [diff] [blame] | 90 | std::make_shared<PCHContainerOperations>()); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 91 | Clang->setInvocation(std::move(CI)); |
| 92 | Clang->createDiagnostics(&DiagsClient, false); |
| 93 | |
| 94 | if (auto VFSWithRemapping = createVFSFromCompilerInvocation( |
| 95 | Clang->getInvocation(), Clang->getDiagnostics(), VFS)) |
| 96 | VFS = VFSWithRemapping; |
Duncan P. N. Exon Smith | 1da7eac | 2019-03-26 22:18:52 +0000 | [diff] [blame] | 97 | Clang->createFileManager(VFS); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 98 | |
| 99 | Clang->setTarget(TargetInfo::CreateTargetInfo( |
| 100 | Clang->getDiagnostics(), Clang->getInvocation().TargetOpts)); |
| 101 | if (!Clang->hasTarget()) |
| 102 | return nullptr; |
| 103 | |
| 104 | // RemappedFileBuffers will handle the lifetime of the Buffer pointer, |
| 105 | // release it. |
| 106 | Buffer.release(); |
| 107 | return Clang; |
| 108 | } |
| 109 | |
| 110 | } // namespace clangd |
| 111 | } // namespace clang |