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 | |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 18 | #include "../clang-tidy/ClangTidyOptions.h" |
Eric Liu | 9ef03dd | 2019-04-15 12:32:28 +0000 | [diff] [blame] | 19 | #include "GlobalCompilationDatabase.h" |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 20 | #include "index/Index.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CompilerInstance.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 22 | #include "clang/Frontend/PrecompiledPreamble.h" |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 23 | #include "clang/Tooling/CompilationDatabase.h" |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 24 | |
| 25 | namespace clang { |
| 26 | namespace clangd { |
| 27 | |
| 28 | class IgnoreDiagnostics : public DiagnosticConsumer { |
| 29 | public: |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 30 | static void log(DiagnosticsEngine::Level DiagLevel, |
| 31 | const clang::Diagnostic &Info); |
| 32 | |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 33 | void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel, |
Ilya Biryukov | 2d4cdac | 2018-02-12 12:48:51 +0000 | [diff] [blame] | 34 | const clang::Diagnostic &Info) override; |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 37 | // Options to run clang e.g. when parsing AST. |
| 38 | struct ParseOptions { |
| 39 | tidy::ClangTidyOptions ClangTidyOpts; |
| 40 | bool SuggestMissingIncludes = false; |
| 41 | }; |
| 42 | |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 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; |
David Goldman | 6ff0228 | 2020-02-03 15:14:49 -0500 | [diff] [blame^] | 48 | // Prevent reuse of the cached preamble/AST. Slow! Useful to workaround |
| 49 | // clangd's assumption that missing header files will stay missing. |
| 50 | bool ForceRebuild = false; |
Eric Liu | dd66277 | 2019-01-28 14:01:55 +0000 | [diff] [blame] | 51 | // Used to recover from diagnostics (e.g. find missing includes for symbol). |
| 52 | const SymbolIndex *Index = nullptr; |
| 53 | ParseOptions Opts; |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | /// Builds compiler invocation that could be used to build AST or preamble. |
| 57 | std::unique_ptr<CompilerInvocation> |
Sam McCall | 407ac2e | 2019-11-28 19:22:50 +0100 | [diff] [blame] | 58 | buildCompilerInvocation(const ParseInputs &Inputs, clang::DiagnosticConsumer &D, |
| 59 | std::vector<std::string> *CC1Args = nullptr); |
Kadir Cetinkaya | a65bcbf | 2019-01-22 09:58:53 +0000 | [diff] [blame] | 60 | |
Ilya Biryukov | 295c8e1 | 2018-01-18 15:17:00 +0000 | [diff] [blame] | 61 | /// Creates a compiler instance, configured so that: |
| 62 | /// - Contents of the parsed file are remapped to \p MainFile. |
| 63 | /// - Preamble is overriden to use PCH passed to this function. It means the |
| 64 | /// changes to the preamble headers or files included in the preamble are |
| 65 | /// not visible to this compiler instance. |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 66 | /// - llvm::vfs::FileSystem is used for all underlying file accesses. The |
| 67 | /// 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] | 68 | /// Returns null on errors. When non-null value is returned, it is expected to |
| 69 | /// be consumed by FrontendAction::BeginSourceFile to properly destroy \p |
| 70 | /// MainFile. |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 71 | std::unique_ptr<CompilerInstance> prepareCompilerInstance( |
| 72 | std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *, |
| 73 | std::unique_ptr<llvm::MemoryBuffer> MainFile, |
Jonas Devlieghere | fc51490 | 2018-10-10 13:27:25 +0000 | [diff] [blame] | 74 | IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &); |
Sam McCall | 98775c5 | 2017-12-04 13:49:59 +0000 | [diff] [blame] | 75 | |
| 76 | } // namespace clangd |
| 77 | } // namespace clang |
| 78 | |
Kirill Bobyrev | 8e35f1e | 2018-08-14 16:03:32 +0000 | [diff] [blame] | 79 | #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H |