blob: 356293b158f8b5c20c4f94ce520ae321d5be6544 [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
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000018#include "../clang-tidy/ClangTidyOptions.h"
Eric Liu9ef03dd2019-04-15 12:32:28 +000019#include "GlobalCompilationDatabase.h"
Eric Liudd662772019-01-28 14:01:55 +000020#include "index/Index.h"
Sam McCall98775c52017-12-04 13:49:59 +000021#include "clang/Frontend/CompilerInstance.h"
Sam McCall98775c52017-12-04 13:49:59 +000022#include "clang/Frontend/PrecompiledPreamble.h"
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000023#include "clang/Tooling/CompilationDatabase.h"
Sam McCall98775c52017-12-04 13:49:59 +000024
25namespace clang {
26namespace clangd {
27
28class IgnoreDiagnostics : public DiagnosticConsumer {
29public:
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000030 static void log(DiagnosticsEngine::Level DiagLevel,
31 const clang::Diagnostic &Info);
32
Sam McCall98775c52017-12-04 13:49:59 +000033 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
Ilya Biryukov2d4cdac2018-02-12 12:48:51 +000034 const clang::Diagnostic &Info) override;
Sam McCall98775c52017-12-04 13:49:59 +000035};
36
Eric Liudd662772019-01-28 14:01:55 +000037// Options to run clang e.g. when parsing AST.
38struct ParseOptions {
39 tidy::ClangTidyOptions ClangTidyOpts;
40 bool SuggestMissingIncludes = false;
41};
42
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000043/// Information required to run clang, e.g. to parse AST or do code completion.
44struct ParseInputs {
45 tooling::CompileCommand CompileCommand;
46 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
47 std::string Contents;
David Goldman6ff02282020-02-03 15:14:49 -050048 // Prevent reuse of the cached preamble/AST. Slow! Useful to workaround
49 // clangd's assumption that missing header files will stay missing.
50 bool ForceRebuild = false;
Eric Liudd662772019-01-28 14:01:55 +000051 // Used to recover from diagnostics (e.g. find missing includes for symbol).
52 const SymbolIndex *Index = nullptr;
53 ParseOptions Opts;
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000054};
55
56/// Builds compiler invocation that could be used to build AST or preamble.
57std::unique_ptr<CompilerInvocation>
Sam McCall407ac2e2019-11-28 19:22:50 +010058buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
59 std::vector<std::string> *CC1Args = nullptr);
Kadir Cetinkayaa65bcbf2019-01-22 09:58:53 +000060
Ilya Biryukov295c8e12018-01-18 15:17:00 +000061/// Creates a compiler instance, configured so that:
62/// - Contents of the parsed file are remapped to \p MainFile.
63/// - Preamble is overriden to use PCH passed to this function. It means the
64/// changes to the preamble headers or files included in the preamble are
65/// not visible to this compiler instance.
Jonas Devliegherefc514902018-10-10 13:27:25 +000066/// - llvm::vfs::FileSystem is used for all underlying file accesses. The
67/// actual vfs used by the compiler may be an overlay over the passed vfs.
Ilya Biryukov295c8e12018-01-18 15:17:00 +000068/// Returns null on errors. When non-null value is returned, it is expected to
69/// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
70/// MainFile.
Sam McCall98775c52017-12-04 13:49:59 +000071std::unique_ptr<CompilerInstance> prepareCompilerInstance(
72 std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *,
73 std::unique_ptr<llvm::MemoryBuffer> MainFile,
Jonas Devliegherefc514902018-10-10 13:27:25 +000074 IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &);
Sam McCall98775c52017-12-04 13:49:59 +000075
76} // namespace clangd
77} // namespace clang
78
Kirill Bobyrev8e35f1e2018-08-14 16:03:32 +000079#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H