blob: 2aecadd4ed851792ed74b55b8fb5f274c97b4c9c [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) {
23 SmallString<64> Message;
24 Info.FormatDiagnostic(Message);
25
26 SmallString<64> Location;
27 if (Info.hasSourceManager() && Info.getLocation().isValid()) {
28 auto &SourceMgr = Info.getSourceManager();
29 auto Loc = SourceMgr.getFileLoc(Info.getLocation());
Sam McCallc008af62018-10-20 15:30:37 +000030 raw_svector_ostream OS(Location);
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000031 Loc.print(OS, SourceMgr);
32 OS << ":";
33 }
34
Sam McCallbed58852018-07-11 10:35:11 +000035 clangd::log("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
Sam McCallc008af62018-10-20 15:30:37 +000043std::unique_ptr<CompilerInstance> prepareCompilerInstance(
44 std::unique_ptr<clang::CompilerInvocation> CI,
45 const PrecompiledPreamble *Preamble, std::unique_ptr<MemoryBuffer> Buffer,
46 std::shared_ptr<PCHContainerOperations> PCHs,
47 IntrusiveRefCntPtr<vfs::FileSystem> VFS, DiagnosticConsumer &DiagsClient) {
Sam McCall98775c52017-12-04 13:49:59 +000048 assert(VFS && "VFS is null");
49 assert(!CI->getPreprocessorOpts().RetainRemappedFileBuffers &&
50 "Setting RetainRemappedFileBuffers to true will cause a memory leak "
51 "of ContentsBuffer");
52
53 // NOTE: we use Buffer.get() when adding remapped files, so we have to make
54 // sure it will be released if no error is emitted.
55 if (Preamble) {
Ilya Biryukov295c8e12018-01-18 15:17:00 +000056 Preamble->OverridePreamble(*CI, VFS, Buffer.get());
Sam McCall98775c52017-12-04 13:49:59 +000057 } else {
58 CI->getPreprocessorOpts().addRemappedFile(
59 CI->getFrontendOpts().Inputs[0].getFile(), Buffer.get());
60 }
61
62 auto Clang = llvm::make_unique<CompilerInstance>(PCHs);
63 Clang->setInvocation(std::move(CI));
64 Clang->createDiagnostics(&DiagsClient, false);
65
66 if (auto VFSWithRemapping = createVFSFromCompilerInvocation(
67 Clang->getInvocation(), Clang->getDiagnostics(), VFS))
68 VFS = VFSWithRemapping;
69 Clang->setVirtualFileSystem(VFS);
70
71 Clang->setTarget(TargetInfo::CreateTargetInfo(
72 Clang->getDiagnostics(), Clang->getInvocation().TargetOpts));
73 if (!Clang->hasTarget())
74 return nullptr;
75
76 // RemappedFileBuffers will handle the lifetime of the Buffer pointer,
77 // release it.
78 Buffer.release();
79 return Clang;
80}
81
82} // namespace clangd
83} // namespace clang