clang-tools  14.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"
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 
29 namespace clang {
30 namespace clangd {
31 
33 public:
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.
42 struct ParseOptions {
43  // (empty at present, formerly controlled recovery AST, include-fixer etc)
44 };
45 
46 /// Information required to run clang, e.g. to parse AST or do code completion.
47 struct ParseInputs {
48  tooling::CompileCommand CompileCommand;
49  const ThreadsafeFS *TFS;
50  std::string Contents;
51  // Version identifier for Contents, provided by the client and opaque to us.
52  std::string Version = "null";
53  // Prevent reuse of the cached preamble/AST. Slow! Useful to workaround
54  // clangd's assumption that missing header files will stay missing.
55  bool ForceRebuild = false;
56  // Used to recover from diagnostics (e.g. find missing includes for symbol).
57  const SymbolIndex *Index = nullptr;
60  // Used to acquire ASTListeners when parsing files.
62 };
63 
64 /// Clears \p CI from options that are not supported by clangd, like codegen or
65 /// plugins. This should be combined with CommandMangler::adjust, which provides
66 /// similar functionality for options that needs to be stripped from compile
67 /// flags.
68 void disableUnsupportedOptions(CompilerInvocation &CI);
69 
70 /// Builds compiler invocation that could be used to build AST or preamble.
71 std::unique_ptr<CompilerInvocation>
72 buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
73  std::vector<std::string> *CC1Args = nullptr);
74 
75 /// Creates a compiler instance, configured so that:
76 /// - Contents of the parsed file are remapped to \p MainFile.
77 /// - Preamble is overriden to use PCH passed to this function. It means the
78 /// changes to the preamble headers or files included in the preamble are
79 /// not visible to this compiler instance.
80 /// - llvm::vfs::FileSystem is used for all underlying file accesses. The
81 /// actual vfs used by the compiler may be an overlay over the passed vfs.
82 /// Returns null on errors. When non-null value is returned, it is expected to
83 /// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
84 /// MainFile.
85 std::unique_ptr<CompilerInstance> prepareCompilerInstance(
86  std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *,
87  std::unique_ptr<llvm::MemoryBuffer> MainFile,
88  IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &);
89 
90 } // namespace clangd
91 } // namespace clang
92 
93 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
clang::clangd::ParseInputs::Version
std::string Version
Definition: Compiler.h:52
clang::clangd::FeatureModuleSet
A FeatureModuleSet is a collection of feature modules installed in clangd.
Definition: FeatureModule.h:145
clang::clangd::ParseInputs::FeatureModules
FeatureModuleSet * FeatureModules
Definition: Compiler.h:61
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:110
clang::clangd::ParseInputs::ForceRebuild
bool ForceRebuild
Definition: Compiler.h:55
clang::clangd::ParseInputs::Contents
std::string Contents
Definition: Compiler.h:50
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:45
CI
std::unique_ptr< CompilerInvocation > CI
Definition: TUScheduler.cpp:450
clang::clangd::ParseInputs::CompileCommand
tooling::CompileCommand CompileCommand
Definition: Compiler.h:48
ThreadsafeFS.h
Inputs
ParseInputs Inputs
Definition: TUScheduler.cpp:451
clang::clangd::ParseInputs::Opts
ParseOptions Opts
Definition: Compiler.h:58
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:47
ns1::ns2::D
@ D
Definition: CategoricalFeature.h:3
Diagnostic
DiagnosticCallback Diagnostic
Definition: ConfigCompile.cpp:101
clang::clangd::IgnoreDiagnostics::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
Definition: Compiler.cpp:40
clang::clangd::ParseInputs::TFS
const ThreadsafeFS * TFS
Definition: Compiler.h:49
GlobalCompilationDatabase.h
TidyProvider.h
clang::clangd::ParseInputs::ClangTidyProvider
TidyProviderRef ClangTidyProvider
Definition: Compiler.h:59
clang::clangd::ParseInputs::Index
const SymbolIndex * Index
Definition: Compiler.h:57
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:84
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:111
FeatureModule.h
Info
FunctionInfo Info
Definition: FunctionSizeCheck.cpp:120
clang::clangd::ThreadsafeFS
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
Definition: ThreadsafeFS.h:28
clang::clangd::ParseOptions
Definition: Compiler.h:42
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
MainFile
std::string MainFile
Definition: HeadersTests.cpp:125
clang::clangd::IgnoreDiagnostics::log
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
Definition: Compiler.cpp:22
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:29
clang::clangd::IgnoreDiagnostics
Definition: Compiler.h:32