blob: 7758e03957d9b7cc284c6d231f7b037badc97fc6 [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>
44buildCompilerInvocation(const ParseInputs &Inputs) {
45 std::vector<const char *> ArgStrs;
46 for (const auto &S : Inputs.CompileCommand.CommandLine)
47 ArgStrs.push_back(S.c_str());
48
49 if (Inputs.FS->setCurrentWorkingDirectory(Inputs.CompileCommand.Directory)) {
50 log("Couldn't set working directory when creating compiler invocation.");
51 // We proceed anyway, our lit-tests rely on results for non-existing working
52 // dirs.
53 }
54
55 // FIXME(ibiryukov): store diagnostics from CommandLine when we start
56 // reporting them.
57 IgnoreDiagnostics IgnoreDiagnostics;
58 llvm::IntrusiveRefCntPtr<DiagnosticsEngine> CommandLineDiagsEngine =
59 CompilerInstance::createDiagnostics(new DiagnosticOptions,
60 &IgnoreDiagnostics, false);
61 std::unique_ptr<CompilerInvocation> CI = createInvocationFromCommandLine(
62 ArgStrs, CommandLineDiagsEngine, Inputs.FS);
63 if (!CI)
64 return nullptr;
65 // createInvocationFromCommandLine sets DisableFree.
66 CI->getFrontendOpts().DisableFree = false;
67 CI->getLangOpts()->CommentOpts.ParseAllComments = true;
68 return CI;
69}
70
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000071std::unique_ptr<CompilerInstance>
72prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
73 const PrecompiledPreamble *Preamble,
74 std::unique_ptr<llvm::MemoryBuffer> Buffer,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000075 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
76 DiagnosticConsumer &DiagsClient) {
Sam McCall98775c52017-12-04 13:49:59 +000077 assert(VFS && "VFS is null");
78 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
79 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
80 "of ContentsBuffer");
81
82 // NOTE: we use Buffer.get() when adding remapped files, so we have to make
83 // sure it will be released if no error is emitted.
84 if (Preamble) {
Ilya Biryukov295c8e12018-01-18 15:17:00 +000085 Preamble->OverridePreamble(*CI, VFS, Buffer.get());
Sam McCall98775c52017-12-04 13:49:59 +000086 } else {
87 CI->getPreprocessorOpts().addRemappedFile(
88 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
89 }
90
Sam McCall4e565022019-04-04 12:56:03 +000091 auto Clang = llvm::make_unique<CompilerInstance>(
92 std::make_shared<PCHContainerOperations>());
Sam McCall98775c52017-12-04 13:49:59 +000093 Clang->setInvocation(std::move(CI));
94 Clang->createDiagnostics(&DiagsClient, false);
95
96 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
97 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
98 VFS = VFSWithRemapping;
Duncan P. N. Exon Smith1da7eac2019-03-26 22:18:52 +000099 Clang->createFileManager(VFS);
Sam McCall98775c52017-12-04 13:49:59 +0000100
101 Clang->setTarget(TargetInfo::CreateTargetInfo(
102 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
103 if (!Clang->hasTarget())
104 return nullptr;
105
106 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
107 // release it.
108 Buffer.release();
109 return Clang;
110}
111
112} // namespace clangd
113} // namespace clang