clang-tools 10.0.0

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 "../clang-tidy/ClangTidyOptions.h"
19 #include "GlobalCompilationDatabase.h"
20 #include "index/Index.h"
21 #include "clang/Frontend/CompilerInstance.h"
22 #include "clang/Frontend/PrecompiledPreamble.h"
23 #include "clang/Tooling/CompilationDatabase.h"
24
25 namespace clang {
26 namespace clangd {
27
28 class IgnoreDiagnostics : public DiagnosticConsumer {
29 public:
30 static void log(DiagnosticsEngine::Level DiagLevel,
31 const clang::Diagnostic &Info);
32
33 void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
34 const clang::Diagnostic &Info) override;
35 };
36
37 // Options to run clang e.g. when parsing AST.
38 struct ParseOptions {
39 tidy::ClangTidyOptions ClangTidyOpts;
40 bool SuggestMissingIncludes = false;
41 };
42
43 /// Information required to run clang, e.g. to parse AST or do code completion.
44 struct ParseInputs {
45 tooling::CompileCommand CompileCommand;
46 IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
47 std::string Contents;
48 // Used to recover from diagnostics (e.g. find missing includes for symbol).
49 const SymbolIndex *Index = nullptr;
50 ParseOptions Opts;
51 };
52
53 /// Builds compiler invocation that could be used to build AST or preamble.
54 std::unique_ptr<CompilerInvocation>
55 buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
56 std::vector<std::string> *CC1Args = nullptr);
57
58 /// Creates a compiler instance, configured so that:
59 /// - Contents of the parsed file are remapped to \p MainFile.
60 /// - Preamble is overriden to use PCH passed to this function. It means the
61 /// changes to the preamble headers or files included in the preamble are
62 /// not visible to this compiler instance.
63 /// - llvm::vfs::FileSystem is used for all underlying file accesses. The
64 /// actual vfs used by the compiler may be an overlay over the passed vfs.
65 /// Returns null on errors. When non-null value is returned, it is expected to
66 /// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
67 /// MainFile.
68 std::unique_ptr<CompilerInstance> prepareCompilerInstance(
69 std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *,
70 std::unique_ptr<llvm::MemoryBuffer> MainFile,
71 IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &);
72
73 } // namespace clangd
74 } // namespace clang
75
76 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
DiagnosticConsumer
clang::clangd::IgnoreDiagnostics::log
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
Definition: Compiler.cpp:20
Index.h
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
clang::tidy::ClangTidyOptions
Contains options for clang-tidy.
Definition: ClangTidyOptions.h:50
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:72
clang::clangd::ParseOptions::ClangTidyOpts
tidy::ClangTidyOptions ClangTidyOpts
Definition: Compiler.h:39
clang::clangd::ParseInputs::Contents
std::string Contents
Definition: Compiler.h:47
GlobalCompilationDatabase.h
MainFile
std::string MainFile
Definition: HeadersTests.cpp:121
clang::clangd::ParseOptions
Definition: Compiler.h:38
clang::clangd::ParseInputs::Opts
ParseOptions Opts
Definition: Compiler.h:50
clang::clangd::ParseInputs::CompileCommand
tooling::CompileCommand CompileCommand
Definition: Compiler.h:45
clang::clangd::IgnoreDiagnostics
Definition: Compiler.h:28
clang::clangd::MessageType::Info
An information message.
clang::clangd::ParseInputs
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:44
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
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:44
clang::clangd::IgnoreDiagnostics::HandleDiagnostic
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
Definition: Compiler.cpp:38
clang::clangd::ParseInputs::FS
IntrusiveRefCntPtr< llvm::vfs::FileSystem > FS
Definition: Compiler.h:46
Index
const SymbolIndex * Index
Definition: Dexp.cpp:84