clang-tools  11.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 "../clang-tidy/ClangTidyOptions.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 
26 namespace clang {
27 namespace clangd {
28 
30 public:
31  static void log(DiagnosticsEngine::Level DiagLevel,
32  const clang::Diagnostic &Info);
33 
34  void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
35  const clang::Diagnostic &Info) override;
36 };
37 
38 // Options to run clang e.g. when parsing AST.
39 struct ParseOptions {
41  bool SuggestMissingIncludes = false;
42  bool BuildRecoveryAST = false;
43  bool PreserveRecoveryASTType = false;
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;
59 };
60 
61 /// Builds compiler invocation that could be used to build AST or preamble.
62 std::unique_ptr<CompilerInvocation>
63 buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D,
64  std::vector<std::string> *CC1Args = nullptr);
65 
66 /// Creates a compiler instance, configured so that:
67 /// - Contents of the parsed file are remapped to \p MainFile.
68 /// - Preamble is overriden to use PCH passed to this function. It means the
69 /// changes to the preamble headers or files included in the preamble are
70 /// not visible to this compiler instance.
71 /// - llvm::vfs::FileSystem is used for all underlying file accesses. The
72 /// actual vfs used by the compiler may be an overlay over the passed vfs.
73 /// Returns null on errors. When non-null value is returned, it is expected to
74 /// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
75 /// MainFile.
76 std::unique_ptr<CompilerInstance> prepareCompilerInstance(
77  std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *,
78  std::unique_ptr<llvm::MemoryBuffer> MainFile,
79  IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &);
80 
81 } // namespace clangd
82 } // namespace clang
83 
84 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
static void log(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info)
Definition: Compiler.cpp:21
const ThreadsafeFS * TFS
Definition: Compiler.h:49
ParseInputs Inputs
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
Contains options for clang-tidy.
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:91
tidy::ClangTidyOptions ClangTidyOpts
Definition: Compiler.h:40
std::string MainFile
DiagnosticCallback Diagnostic
tooling::CompileCommand CompileCommand
Definition: Compiler.h:48
An information message.
Information required to run clang, e.g. to parse AST or do code completion.
Definition: Compiler.h:47
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Wrapper for vfs::FileSystem for use in multithreaded programs like clangd.
Definition: ThreadsafeFS.h:28
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:45
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, const clang::Diagnostic &Info) override
Definition: Compiler.cpp:39
const SymbolIndex * Index
Definition: Dexp.cpp:95