blob: 7080e20e879e76ac64604c3709191e39f0d51afd [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(
Ilya Biryukov999e4c42019-08-27 10:02:18 +000062 ArgStrs, CommandLineDiagsEngine, Inputs.FS,
63 /*ShouldRecoverOnErrors=*/true);
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000064 if (!CI)
65 return nullptr;
66 // createInvocationFromCommandLine sets DisableFree.
67 CI->getFrontendOpts().DisableFree = false;
68 CI->getLangOpts()->CommentOpts.ParseAllComments = true;
69 return CI;
70}
71
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000072std::unique_ptr<CompilerInstance>
73prepareCompilerInstance(std::unique_ptr<clang::CompilerInvocation> CI,
74 const PrecompiledPreamble *Preamble,
75 std::unique_ptr<llvm::MemoryBuffer> Buffer,
Ilya Biryukovf2001aa2019-01-07 15:45:19 +000076 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> VFS,
77 DiagnosticConsumer &DiagsClient) {
Sam McCall98775c52017-12-04 13:49:59 +000078 assert(VFS && "VFS is null");
79 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
80 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
81 "of ContentsBuffer");
82
83 // NOTE: we use Buffer.get() when adding remapped files, so we have to make
84 // sure it will be released if no error is emitted.
85 if (Preamble) {
Ilya Biryukov295c8e12018-01-18 15:17:00 +000086 Preamble->OverridePreamble(*CI, VFS, Buffer.get());
Sam McCall98775c52017-12-04 13:49:59 +000087 } else {
88 CI->getPreprocessorOpts().addRemappedFile(
89 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
90 }
91
Jonas Devlieghere1c705d92019-08-14 23:52:23 +000092 auto Clang = std::make_unique<CompilerInstance>(
Sam McCall4e565022019-04-04 12:56:03 +000093 std::make_shared<PCHContainerOperations>());
Sam McCall98775c52017-12-04 13:49:59 +000094 Clang->setInvocation(std::move(CI));
95 Clang->createDiagnostics(&DiagsClient, false);
96
97 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
98 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
99 VFS = VFSWithRemapping;
Duncan P. N. Exon Smith1da7eac2019-03-26 22:18:52 +0000100 Clang->createFileManager(VFS);
Sam McCall98775c52017-12-04 13:49:59 +0000101
102 Clang->setTarget(TargetInfo::CreateTargetInfo(
103 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
104 if (!Clang->hasTarget())
105 return nullptr;
106
107 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
108 // release it.
109 Buffer.release();
110 return Clang;
111}
112
113} // namespace clangd
114} // namespace clang