clang-tools  15.0.0git
Compiler.h
Go to the documentation of this file.
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 "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"
25 #include <memory>
26 #include <vector>
27 
28 namespace clang {
29 namespace clangd {
30 
32 public:
33  static void log(DiagnosticsEngine::Level DiagLevel,
34  const clang::Diagnostic &Info);
35 
36  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
37  const clang::Diagnostic &Info) override;
38 };
39 
40 // Options to run clang e.g. when parsing AST.
41 struct ParseOptions {
43 };
44 
45 /// Information required to run clang, e.g. to parse AST or do code completion.
46 struct ParseInputs {
47  tooling::CompileCommand CompileCommand;
48  const ThreadsafeFS *TFS;
49  std::string Contents;
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;
59  // Used to acquire ASTListeners when parsing files.
61 };
62 
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
66 /// flags.
67 void disableUnsupportedOptions(CompilerInvocation &CI);
68 
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);
73 
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
83 /// MainFile.
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 &);
88 
89 /// Respect `#pragma clang __debug crash` etc, which are usually disabled.
90 /// This may only be called before threads are spawned.
92 
93 } // namespace clangd
94 } // namespace clang
95 
96 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
clang::clangd::ParseInputs::Version
std::string Version
Definition: Compiler.h:51
clang::clangd::FeatureModuleSet
A FeatureModuleSet is a collection of feature modules installed in clangd.
Definition: FeatureModule.h:150
clang::clangd::ParseInputs::FeatureModules
FeatureModuleSet * FeatureModules
Definition: Compiler.h:60
clang::clangd::prepareCompilerInstance
std::unique_ptr< CompilerInstance > prepareCompilerInstance(std::unique_ptr< clang::CompilerInvocation > CI, const PrecompiledPreamble *Preamble, std::unique_ptr< llvm::MemoryBuffer > Buffer, llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS, DiagnosticConsumer &DiagsClient)
Definition: Compiler.cpp:120
clang::clangd::ParseInputs::ForceRebuild
bool ForceRebuild
Definition: Compiler.h:54
clang::clangd::ParseInputs::Contents
std::string Contents
Definition: Compiler.h:49
DiagnosticConsumer
Index.h
clang::clangd::disableUnsupportedOptions
void disableUnsupportedOptions(CompilerInvocation &CI)
Clears CI from options that are not supported by clangd, like codegen or plugins.
Definition: Compiler.cpp:46
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:491
clang::clangd::ParseInputs::CompileCommand
tooling::CompileCommand CompileCommand
Definition: Compiler.h:47
ThreadsafeFS.h
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:492
clang::clangd::ParseInputs::Opts
ParseOptions Opts
Definition: Compiler.h:57
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:46
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:100
clang::clangd::IgnoreDiagnostics::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
Definition: Compiler.cpp:38
clang::clangd::ParseInputs::TFS
const ThreadsafeFS * TFS
Definition: Compiler.h:48
TidyProvider.h
clang::clangd::ParseInputs::ClangTidyProvider
TidyProviderRef ClangTidyProvider
Definition: Compiler.h:58
clang::clangd::ParseOptions::PreambleParseForwardingFunctions
bool PreambleParseForwardingFunctions
Definition: Compiler.h:42
clang::clangd::ParseInputs::Index
const SymbolIndex * Index
Definition: Compiler.h:56
clang::clangd::buildCompilerInvocation
std::unique_ptr< CompilerInvocation > buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, std::vector< std::string > *CC1Args)
Builds compiler invocation that could be used to build AST or preamble.
Definition: Compiler.cpp:86
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:113
FeatureModule.h
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:121
clang::clangd::ThreadsafeFS
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
Definition: ThreadsafeFS.h:27
clang::clangd::allowCrashPragmasForTest
void allowCrashPragmasForTest()
Respect #pragma clang __debug crash etc, which are usually disabled.
Definition: Compiler.cpp:44
clang::clangd::ParseOptions
Definition: Compiler.h:41
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
MainFile
std::string MainFile
Definition: HeadersTests.cpp:134
clang::clangd::IgnoreDiagnostics::log
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
Definition: Compiler.cpp:20
clang::clangd::TidyProviderRef
llvm::function_ref< void(tidy::ClangTidyOptions &, llvm::StringRef)> TidyProviderRef
A factory to modify a tidy::ClangTidyOptions that doesn't hold any state.
Definition: TidyProvider.h:28
clang::clangd::IgnoreDiagnostics
Definition: Compiler.h:31