blob: 13b499c860dc8b10ace47fa19bb8aa815557dfee [file] [log] [blame]
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +00001//===--- Compiler.h ----------------------------------------------*- 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//===----------------------------------------------------------------------===//
Sam McCall98775c52017-12-04 13:49:59 +00008//
9// Shared utilities for invoking the clang compiler.
10// ClangdUnit takes care of much of this, but some features like CodeComplete
11// run their own compile actions that share logic.
12//
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +000013//===----------------------------------------------------------------------===//
14
Sam McCall98775c52017-12-04 13:49:59 +000015#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
16#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
Eric Liub99d5e82017-12-14 21:22:03 +000017
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000018#include "../clang-tidy/ClangTidyOptions.h"
Sam McCall98775c52017-12-04 13:49:59 +000019#include "clang/Frontend/CompilerInstance.h"
20#include "clang/Frontend/CompilerInvocation.h"
21#include "clang/Frontend/PrecompiledPreamble.h"
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000022#include "clang/Tooling/CompilationDatabase.h"
Sam McCall98775c52017-12-04 13:49:59 +000023
24namespace clang {
25namespace clangd {
26
27class IgnoreDiagnostics : public DiagnosticConsumer {
28public:
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000029 static void log(DiagnosticsEngine::Level DiagLevel,
30 const clang::Diagnostic &Info);
31
Sam McCall98775c52017-12-04 13:49:59 +000032 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000033 const clang::Diagnostic &Info) override;
Sam McCall98775c52017-12-04 13:49:59 +000034};
35
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000036/// Information required to run clang, e.g. to parse AST or do code completion.
37struct ParseInputs {
38 tooling::CompileCommand CompileCommand;
39 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
40 std::string Contents;
41 tidy::ClangTidyOptions ClangTidyOpts;
42};
43
44/// Builds compiler invocation that could be used to build AST or preamble.
45std::unique_ptr<CompilerInvocation>
46buildCompilerInvocation(const ParseInputs &Inputs);
47
Ilya Biryukov295c8e12018-01-18 15:17:00 +000048/// Creates a compiler instance, configured so that:
49/// - Contents of the parsed file are remapped to \p MainFile.
50/// - Preamble is overriden to use PCH passed to this function. It means the
51/// changes to the preamble headers or files included in the preamble are
52/// not visible to this compiler instance.
Jonas Devliegherefc514902018-10-10 13:27:25 +000053/// - llvm::vfs::FileSystem is used for all underlying file accesses. The
54/// actual vfs used by the compiler may be an overlay over the passed vfs.
Ilya Biryukov295c8e12018-01-18 15:17:00 +000055/// Returns null on errors. When non-null value is returned, it is expected to
56/// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
57/// MainFile.
Sam McCall98775c52017-12-04 13:49:59 +000058std::unique_ptr<CompilerInstance> prepareCompilerInstance(
59 std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *,
60 std::unique_ptr<llvm::MemoryBuffer> MainFile,
61 std::shared_ptr<PCHContainerOperations>,
Jonas Devliegherefc514902018-10-10 13:27:25 +000062 IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &);
Sam McCall98775c52017-12-04 13:49:59 +000063
64} // namespace clangd
65} // namespace clang
66
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +000067#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H