1 //===--- Compiler.h ----------------------------------------------*- C++-*-===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
16 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
18 #include "FeatureModule.h"
19 #include "TidyProvider.h"
20 #include "index/Index.h"
21 #include "support/ThreadsafeFS.h"
22 #include "clang/Frontend/CompilerInstance.h"
23 #include "clang/Frontend/PrecompiledPreamble.h"
24 #include "clang/Tooling/CompilationDatabase.h"
31 class IgnoreDiagnostics
: public DiagnosticConsumer
{
33 static void log(DiagnosticsEngine::Level DiagLevel
,
34 const clang::Diagnostic
&Info
);
36 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel
,
37 const clang::Diagnostic
&Info
) override
;
40 // Options to run clang e.g. when parsing AST.
42 bool PreambleParseForwardingFunctions
= false;
45 /// Information required to run clang, e.g. to parse AST or do code completion.
47 tooling::CompileCommand CompileCommand
;
48 const ThreadsafeFS
*TFS
;
50 // Version identifier for Contents, provided by the client and opaque to us.
51 std::string Version
= "null";
52 // 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;
55 // Used to recover from diagnostics (e.g. find missing includes for symbol).
56 const SymbolIndex
*Index
= nullptr;
57 ParseOptions Opts
= ParseOptions();
58 TidyProviderRef ClangTidyProvider
= {};
59 // Used to acquire ASTListeners when parsing files.
60 FeatureModuleSet
*FeatureModules
= nullptr;
63 /// Clears \p CI from options that are not supported by clangd, like codegen or
64 /// plugins. This should be combined with CommandMangler::adjust, which provides
65 /// similar functionality for options that needs to be stripped from compile
67 void disableUnsupportedOptions(CompilerInvocation
&CI
);
69 /// Builds compiler invocation that could be used to build AST or preamble.
70 std::unique_ptr
<CompilerInvocation
>
71 buildCompilerInvocation(const ParseInputs
&Inputs
, clang::DiagnosticConsumer
&D
,
72 std::vector
<std::string
> *CC1Args
= nullptr);
74 /// Creates a compiler instance, configured so that:
75 /// - Contents of the parsed file are remapped to \p MainFile.
76 /// - Preamble is overriden to use PCH passed to this function. It means the
77 /// changes to the preamble headers or files included in the preamble are
78 /// not visible to this compiler instance.
79 /// - llvm::vfs::FileSystem is used for all underlying file accesses. The
80 /// actual vfs used by the compiler may be an overlay over the passed vfs.
81 /// Returns null on errors. When non-null value is returned, it is expected to
82 /// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
84 std::unique_ptr
<CompilerInstance
> prepareCompilerInstance(
85 std::unique_ptr
<clang::CompilerInvocation
>, const PrecompiledPreamble
*,
86 std::unique_ptr
<llvm::MemoryBuffer
> MainFile
,
87 IntrusiveRefCntPtr
<llvm::vfs::FileSystem
>, DiagnosticConsumer
&);
89 /// Respect `#pragma clang __debug crash` etc, which are usually disabled.
90 /// This may only be called before threads are spawned.
91 void allowCrashPragmasForTest();
96 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H