blob: e0801433319076726f98489cde26c30cd39525ec [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- Compiler.cpp --------------------------------------------*- C++-*-===//
Sam McCall98775c52017-12-04 13:49:59 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 McCall98775c52017-12-04 13:49:59 +00006//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00007//===----------------------------------------------------------------------===//
Eric Liub99d5e82017-12-14 21:22:03 +00008
Sam McCall98775c52017-12-04 13:49:59 +00009#include "Compiler.h"
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000010#include "Logger.h"
Sam McCall98775c52017-12-04 13:49:59 +000011#include "clang/Basic/TargetInfo.h"
12#include "clang/Lex/PreprocessorOptions.h"
Sam McCall4e565022019-04-04 12:56:03 +000013#include "clang/Serialization/PCHContainerOperations.h"
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000014#include "llvm/Support/Format.h"
15#include "llvm/Support/FormatVariadic.h"
Sam McCall98775c52017-12-04 13:49:59 +000016
17namespace clang {
18namespace clangd {
19
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000020void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel,
21 const clang::Diagnostic &Info) {
Sam McCall3fd25fc2018-11-02 12:51:26 +000022 // FIXME: format lazily, in case vlog is off.
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000023 llvm::SmallString<64> Message;
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000024 Info.FormatDiagnostic(Message);
25
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000026 llvm::SmallString<64> Location;
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000027 if (Info.hasSourceManager() && Info.getLocation().isValid()) {
28 auto &SourceMgr = Info.getSourceManager();
29 auto Loc = SourceMgr.getFileLoc(Info.getLocation());
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000030 llvm::raw_svector_ostream OS(Location);
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000031 Loc.print(OS, SourceMgr);
32 OS << ":";
33 }
34
Sam McCall3fd25fc2018-11-02 12:51:26 +000035 clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message);
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000036}
37
38void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
39 const clang::Diagnostic &Info) {
40 IgnoreDiagnostics::log(DiagLevel, Info);
41}
42
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000043std::unique_ptr<CompilerInvocation>
Ilya Biryukovd73ac962019-08-28 09:24:55 +000044buildCompilerInvocation(const ParseInputs &Inputs,
45 clang::DiagnosticConsumer &D) {
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000046 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 Cetinkayaa65bcbf2019-01-22 09:58:53 +000056 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
Ilya Biryukovd73ac962019-08-28 09:24:55 +000057 CompilerInstance::createDiagnostics(new DiagnosticOptions, &D, false);
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000058 std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
Ilya Biryukov999e4c42019-08-27 10:02:18 +000059 ArgStrs, CommandLineDiagsEngine, Inputs.FS,
60 /*ShouldRecoverOnErrors=*/true);
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000061 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 Biryukovf2001aa2019-01-07 15:45:19 +000069std::unique_ptr<CompilerInstance>
70prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
71 const PrecompiledPreamble *Preamble,
72 std::unique_ptr<llvm::MemoryBuffer> Buffer,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000073 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
74 DiagnosticConsumer &DiagsClient) {
Sam McCall98775c52017-12-04 13:49:59 +000075 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 Biryukov295c8e12018-01-18 15:17:00 +000083 Preamble->OverridePreamble(*CI, VFS, Buffer.get());
Sam McCall98775c52017-12-04 13:49:59 +000084 } else {
85 CI->getPreprocessorOpts().addRemappedFile(
86 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
87 }
88
Jonas Devlieghere1c705d92019-08-14 23:52:23 +000089 auto Clang = std::make_unique<CompilerInstance>(
Sam McCall4e565022019-04-04 12:56:03 +000090 std::make_shared<PCHContainerOperations>());
Sam McCall98775c52017-12-04 13:49:59 +000091 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 Smith1da7eac2019-03-26 22:18:52 +000097 Clang->createFileManager(VFS);
Sam McCall98775c52017-12-04 13:49:59 +000098
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