clang-tools 23.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 "ModulesBuilder.h"
20#include "TidyProvider.h"
21#include "index/Index.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
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.
44
45 bool ImportInsertions = false;
46
47 bool SkipPreambleBuild = false;
48};
49
50/// Information required to run clang, e.g. to parse AST or do code completion.
52 tooling::CompileCommand CompileCommand;
54 std::string Contents;
55 // Version identifier for Contents, provided by the client and opaque to us.
56 std::string Version = "null";
57 // Prevent reuse of the cached preamble/AST. Slow! Useful to workaround
58 // clangd's assumption that missing header files will stay missing.
59 bool ForceRebuild = false;
60 // Used to recover from diagnostics (e.g. find missing includes for symbol).
61 const SymbolIndex *Index = nullptr;
64 // Used to acquire ASTListeners when parsing files.
66 // Used to build and manage (C++) modules.
68};
69
70/// Clears \p CI from options that are not supported by clangd, like codegen or
71/// plugins. This should be combined with CommandMangler::adjust, which provides
72/// similar functionality for options that needs to be stripped from compile
73/// flags.
74void disableUnsupportedOptions(CompilerInvocation &CI);
75
76/// Builds compiler invocation that could be used to build AST or preamble.
77std::unique_ptr<CompilerInvocation>
78buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
79 std::vector<std::string> *CC1Args = nullptr);
80
81/// Creates a compiler instance, configured so that:
82/// - Contents of the parsed file are remapped to \p MainFile.
83/// - Preamble is overriden to use PCH passed to this function. It means the
84/// changes to the preamble headers or files included in the preamble are
85/// not visible to this compiler instance.
86/// - llvm::vfs::FileSystem is used for all underlying file accesses. The
87/// actual vfs used by the compiler may be an overlay over the passed vfs.
88/// Returns null on errors. When non-null value is returned, it is expected to
89/// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
90/// MainFile.
91std::unique_ptr<CompilerInstance> prepareCompilerInstance(
92 std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *,
93 std::unique_ptr<llvm::MemoryBuffer> MainFile,
94 IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &);
95
96/// Respect `#pragma clang __debug crash` etc, which are usually disabled.
97/// This may only be called before threads are spawned.
99
100} // namespace clangd
101} // namespace clang
102
103#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
A FeatureModuleSet is a collection of feature modules installed in clangd.
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
Definition Compiler.cpp:39
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
Definition Compiler.cpp:21
This class handles building module files for a given source file.
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition Index.h:134
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
Definition AST.cpp:44
@ Info
An information message.
Definition Protocol.h:738
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:96
llvm::function_ref< void(tidy::ClangTidyOptions &, llvm::StringRef)> TidyProviderRef
A factory to modify a tidy::ClangTidyOptions that doesn't hold any state.
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:131
void allowCrashPragmasForTest()
Respect #pragma clang __debug crash etc, which are usually disabled.
Definition Compiler.cpp:45
void disableUnsupportedOptions(CompilerInvocation &CI)
Clears CI from options that are not supported by clangd, like codegen or plugins.
Definition Compiler.cpp:47
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Information required to run clang, e.g. to parse AST or do code completion.
Definition Compiler.h:51
TidyProviderRef ClangTidyProvider
Definition Compiler.h:63
tooling::CompileCommand CompileCommand
Definition Compiler.h:52
const ThreadsafeFS * TFS
Definition Compiler.h:53
FeatureModuleSet * FeatureModules
Definition Compiler.h:65
ModulesBuilder * ModulesManager
Definition Compiler.h:67
const SymbolIndex * Index
Definition Compiler.h:61