blob: 011841c4613ff51b5d412a2405d9f9c43ccd67a8 [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- Compiler.cpp --------------------------------------------*- C++-*-===//
Sam McCall98775c52017-12-04 13:49:59 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00008//===----------------------------------------------------------------------===//
Eric Liub99d5e82017-12-14 21:22:03 +00009
Sam McCall98775c52017-12-04 13:49:59 +000010#include "Compiler.h"
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000011#include "Logger.h"
Sam McCall98775c52017-12-04 13:49:59 +000012#include "clang/Basic/TargetInfo.h"
13#include "clang/Lex/PreprocessorOptions.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
Sam McCallc008af62018-10-20 15:30:37 +000017using namespace llvm;
Sam McCall98775c52017-12-04 13:49:59 +000018namespace clang {
19namespace clangd {
20
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000021void IgnoreDiagnostics::log(DiagnosticsEngine::Level DiagLevel,
22 const clang::Diagnostic &Info) {
Sam McCall3fd25fc2018-11-02 12:51:26 +000023 // FIXME: format lazily, in case vlog is off.
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000024 SmallString<64> Message;
25 Info.FormatDiagnostic(Message);
26
27 SmallString<64> Location;
28 if (Info.hasSourceManager() && Info.getLocation().isValid()) {
29 auto &SourceMgr = Info.getSourceManager();
30 auto Loc = SourceMgr.getFileLoc(Info.getLocation());
Sam McCallc008af62018-10-20 15:30:37 +000031 raw_svector_ostream OS(Location);
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000032 Loc.print(OS, SourceMgr);
33 OS << ":";
34 }
35
Sam McCall3fd25fc2018-11-02 12:51:26 +000036 clangd::vlog("Ignored diagnostic. {0}{1}", Location, Message);
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000037}
38
39void IgnoreDiagnostics::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
40 const clang::Diagnostic &Info) {
41 IgnoreDiagnostics::log(DiagLevel, Info);
42}
43
Sam McCallc008af62018-10-20 15:30:37 +000044std::unique_ptr<CompilerInstance> prepareCompilerInstance(
45 std::unique_ptr<clang::CompilerInvocation> CI,
46 const PrecompiledPreamble *Preamble, std::unique_ptr<MemoryBuffer> Buffer,
47 std::shared_ptr<PCHContainerOperations> PCHs,
48 IntrusiveRefCntPtr<vfs::FileSystem> VFS, DiagnosticConsumer &DiagsClient) {
Sam McCall98775c52017-12-04 13:49:59 +000049 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 Biryukov295c8e12018-01-18 15:17:00 +000057 Preamble->OverridePreamble(*CI, VFS, Buffer.get());
Sam McCall98775c52017-12-04 13:49:59 +000058 } 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