Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 1 | //===--- Compiler.h ----------------------------------------------*- C++-*-===// |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 6 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 8 | // |
| 9 | // Shared utilities for invoking the clang compiler. |
Sam McCall | cf3a585 | 2019-09-04 07:35:00 +0000 | [diff] [blame] | 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. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 12 | // |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 15 | #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H |
| 16 | #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H |
Eric Liu | b99d5e8 | 2017-12-14 21:22:03 +0000 | [diff] [blame] | 17 | |
Eric Liu | 9ef03dd | 2019-04-15 12:32:28 +0000 | [diff] [blame] | 18 | #include "GlobalCompilationDatabase.h" |
Nathan James | 73fdd99 | 2020-11-25 18:35:34 +0000 | [diff] [blame] | 19 | #include "TidyProvider.h" |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 20 | #include "index/Index.h" |
Kadir Cetinkaya | 0628705 | 2020-06-17 11:53:32 +0200 | [diff] [blame] | 21 | #include "support/ThreadsafeFS.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/CompilerInstance.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 23 | #include "clang/Frontend/PrecompiledPreamble.h" |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 24 | #include "clang/Tooling/CompilationDatabase.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 25 | |
| 26 | namespace clang { |
| 27 | namespace clangd { |
| 28 | |
| 29 | class IgnoreDiagnostics : public DiagnosticConsumer { |
| 30 | public: |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 31 | static void log(DiagnosticsEngine::Level DiagLevel, |
| 32 | const clang::Diagnostic &Info); |
| 33 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 34 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 35 | const clang::Diagnostic &Info) override; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 38 | // Options to run clang e.g. when parsing AST. |
| 39 | struct ParseOptions { |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 40 | bool SuggestMissingIncludes = false; |
Haojian Wu | 72439b6 | 2020-03-31 16:09:49 +0200 | [diff] [blame] | 41 | bool BuildRecoveryAST = false; |
Haojian Wu | 0320ce8 | 2020-05-19 15:21:50 +0200 | [diff] [blame] | 42 | bool PreserveRecoveryASTType = false; |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 45 | /// Information required to run clang, e.g. to parse AST or do code completion. |
| 46 | struct ParseInputs { |
| 47 | tooling::CompileCommand CompileCommand; |
Kadir Cetinkaya | 8d654df | 2020-06-17 18:09:54 +0200 | [diff] [blame] | 48 | const ThreadsafeFS *TFS; |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 49 | std::string Contents; |
Sam McCall | 2cd33e6 | 2020-03-04 00:33:29 +0100 | [diff] [blame] | 50 | // Version identifier for Contents, provided by the client and opaque to us. |
| 51 | std::string Version = "null"; |
David Goldman | 6ff0228 | 2020-02-03 15:14:49 -0500 | [diff] [blame] | 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; |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 55 | // Used to recover from diagnostics (e.g. find missing includes for symbol). |
| 56 | const SymbolIndex *Index = nullptr; |
Haojian Wu | a7534dc | 2020-06-03 10:34:05 +0200 | [diff] [blame] | 57 | ParseOptions Opts = ParseOptions(); |
Nathan James | 73fdd99 | 2020-11-25 18:35:34 +0000 | [diff] [blame] | 58 | TidyProviderRef ClangTidyProvider = {}; |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | /// Builds compiler invocation that could be used to build AST or preamble. |
| 62 | std::unique_ptr<CompilerInvocation> |
Sam McCall | 407ac2e | 2019-11-28 19:22:50 +0100 | [diff] [blame] | 63 | buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, |
| 64 | std::vector<std::string> *CC1Args = nullptr); |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 65 | |
Ilya Biryukov | 295c8e1 | 2018-01-18 15:17:00 +0000 | [diff] [blame] | 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. |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 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. |
Ilya Biryukov | 295c8e1 | 2018-01-18 15:17:00 +0000 | [diff] [blame] | 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. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 76 | std::unique_ptr<CompilerInstance> prepareCompilerInstance( |
| 77 | std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *, |
| 78 | std::unique_ptr<llvm::MemoryBuffer> MainFile, |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 79 | IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 80 | |
| 81 | } // namespace clangd |
| 82 | } // namespace clang |
| 83 | |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 84 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H |