blob: c9c75625727b5973b28619b47927f8840e2d2199 [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.
Sam McCallcf3a5852019-09-04 07:35:00 +000010// Most callers will use this through Preamble/ParsedAST, but some features like
11// CodeComplete run their own compile actions that share these low-level pieces.
Sam McCall98775c52017-12-04 13:49:59 +000012//
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
Eric Liu9ef03dd2019-04-15 12:32:28 +000018#include "GlobalCompilationDatabase.h"
Nathan James73fdd992020-11-25 18:35:34 +000019#include "TidyProvider.h"
Eric Liudd662772019-01-28 14:01:55 +000020#include "index/Index.h"
Kadir Cetinkaya06287052020-06-17 11:53:32 +020021#include "support/ThreadsafeFS.h"
Sam McCall98775c52017-12-04 13:49:59 +000022#include "clang/Frontend/CompilerInstance.h"
Sam McCall98775c52017-12-04 13:49:59 +000023#include "clang/Frontend/PrecompiledPreamble.h"
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000024#include "clang/Tooling/CompilationDatabase.h"
Sam McCall98775c52017-12-04 13:49:59 +000025
26namespace clang {
27namespace clangd {
28
29class IgnoreDiagnostics : public DiagnosticConsumer {
30public:
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000031 static void log(DiagnosticsEngine::Level DiagLevel,
32 const clang::Diagnostic &Info);
33
Sam McCall98775c52017-12-04 13:49:59 +000034 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000035 const clang::Diagnostic &Info) override;
Sam McCall98775c52017-12-04 13:49:59 +000036};
37
Eric Liudd662772019-01-28 14:01:55 +000038// Options to run clang e.g. when parsing AST.
39struct ParseOptions {
Eric Liudd662772019-01-28 14:01:55 +000040 bool SuggestMissingIncludes = false;
Haojian Wu72439b62020-03-31 16:09:49 +020041 bool BuildRecoveryAST = false;
Haojian Wu0320ce82020-05-19 15:21:50 +020042 bool PreserveRecoveryASTType = false;
Eric Liudd662772019-01-28 14:01:55 +000043};
44
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000045/// Information required to run clang, e.g. to parse AST or do code completion.
46struct ParseInputs {
47 tooling::CompileCommand CompileCommand;
Kadir Cetinkaya8d654df2020-06-17 18:09:54 +020048 const ThreadsafeFS *TFS;
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000049 std::string Contents;
Sam McCall2cd33e62020-03-04 00:33:29 +010050 // Version identifier for Contents, provided by the client and opaque to us.
51 std::string Version = "null";
David Goldman6ff02282020-02-03 15:14:49 -050052 // Prevent reuse of the cached preamble/AST. Slow! Useful to workaround
53 // clangd's assumption that missing header files will stay missing.
54 bool ForceRebuild = false;
Eric Liudd662772019-01-28 14:01:55 +000055 // Used to recover from diagnostics (e.g. find missing includes for symbol).
56 const SymbolIndex *Index = nullptr;
Haojian Wua7534dc2020-06-03 10:34:05 +020057 ParseOptions Opts = ParseOptions();
Nathan James73fdd992020-11-25 18:35:34 +000058 TidyProviderRef ClangTidyProvider = {};
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000059};
60
61/// Builds compiler invocation that could be used to build AST or preamble.
62std::unique_ptr<CompilerInvocation>
Sam McCall407ac2e2019-11-28 19:22:50 +010063buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
64 std::vector<std::string> *CC1Args = nullptr);
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000065
Ilya Biryukov295c8e12018-01-18 15:17:00 +000066/// Creates a compiler instance, configured so that:
67/// - Contents of the parsed file are remapped to \p MainFile.
68/// - Preamble is overriden to use PCH passed to this function. It means the
69/// changes to the preamble headers or files included in the preamble are
70/// not visible to this compiler instance.
Jonas Devliegherefc514902018-10-10 13:27:25 +000071/// - llvm::vfs::FileSystem is used for all underlying file accesses. The
72/// actual vfs used by the compiler may be an overlay over the passed vfs.
Ilya Biryukov295c8e12018-01-18 15:17:00 +000073/// Returns null on errors. When non-null value is returned, it is expected to
74/// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
75/// MainFile.
Sam McCall98775c52017-12-04 13:49:59 +000076std::unique_ptr<CompilerInstance> prepareCompilerInstance(
77 std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *,
78 std::unique_ptr<llvm::MemoryBuffer> MainFile,
Jonas Devliegherefc514902018-10-10 13:27:25 +000079 IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &);
Sam McCall98775c52017-12-04 13:49:59 +000080
81} // namespace clangd
82} // namespace clang
83
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +000084#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H