1//===--- Compiler.h ----------------------------------------------*- C++-*-===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// Shared utilities for invoking the clang compiler.
10// 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.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
16#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
17
18#include "FeatureModule.h"
19#include "ModulesBuilder.h"
20#include "TidyProvider.h"
21#include "index/Index.h"
22#include "support/ThreadsafeFS.h"
23#include "clang/Frontend/CompilerInstance.h"
24#include "clang/Frontend/PrecompiledPreamble.h"
25#include "clang/Tooling/CompilationDatabase.h"
26#include <memory>
27#include <vector>
28
29namespace clang {
30namespace clangd {
31
32class IgnoreDiagnostics : public DiagnosticConsumer {
33public:
34 static void log(DiagnosticsEngine::Level DiagLevel,
35 const clang::Diagnostic &Info);
36
37 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
38 const clang::Diagnostic &Info) override;
39};
40
41// Options to run clang e.g. when parsing AST.
42struct ParseOptions {
43 bool PreambleParseForwardingFunctions = true;
44
45 bool ImportInsertions = false;
46};
47
48/// Information required to run clang, e.g. to parse AST or do code completion.
49struct ParseInputs {
50 tooling::CompileCommand CompileCommand;
51 const ThreadsafeFS *TFS;
52 std::string Contents;
53 // Version identifier for Contents, provided by the client and opaque to us.
54 std::string Version = "null";
55 // Prevent reuse of the cached preamble/AST. Slow! Useful to workaround
56 // clangd's assumption that missing header files will stay missing.
57 bool ForceRebuild = false;
58 // Used to recover from diagnostics (e.g. find missing includes for symbol).
59 const SymbolIndex *Index = nullptr;
60 ParseOptions Opts = ParseOptions();
61 TidyProviderRef ClangTidyProvider = {};
62 // Used to acquire ASTListeners when parsing files.
63 FeatureModuleSet *FeatureModules = nullptr;
64 // Used to build and manage (C++) modules.
65 ModulesBuilder *ModulesManager = nullptr;
66};
67
68/// Clears \p CI from options that are not supported by clangd, like codegen or
69/// plugins. This should be combined with CommandMangler::adjust, which provides
70/// similar functionality for options that needs to be stripped from compile
71/// flags.
72void disableUnsupportedOptions(CompilerInvocation &CI);
73
74/// Builds compiler invocation that could be used to build AST or preamble.
75std::unique_ptr<CompilerInvocation>
76buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
77 std::vector<std::string> *CC1Args = nullptr);
78
79/// Creates a compiler instance, configured so that:
80/// - Contents of the parsed file are remapped to \p MainFile.
81/// - Preamble is overriden to use PCH passed to this function. It means the
82/// changes to the preamble headers or files included in the preamble are
83/// not visible to this compiler instance.
84/// - llvm::vfs::FileSystem is used for all underlying file accesses. The
85/// actual vfs used by the compiler may be an overlay over the passed vfs.
86/// Returns null on errors. When non-null value is returned, it is expected to
87/// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
88/// MainFile.
89std::unique_ptr<CompilerInstance> prepareCompilerInstance(
90 std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *,
91 std::unique_ptr<llvm::MemoryBuffer> MainFile,
92 IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &);
93
94/// Respect `#pragma clang __debug crash` etc, which are usually disabled.
95/// This may only be called before threads are spawned.
96void allowCrashPragmasForTest();
97
98} // namespace clangd
99} // namespace clang
100
101#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
102

source code of clang-tools-extra/clangd/Compiler.h