Debian Codegraph @

clang-tools-extra/clangd/Config.h

raw
1 //===--- Config.h - User configuration of clangd behavior --------*- 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 // Various clangd features have configurable behaviour (or can be disabled).
10 // This file defines "resolved" configuration seen by features within clangd.
11 // For example, settings may vary per-file, the resolved Config only contains
12 // settings that apply to the current file.
13 //
14 // This is distinct from how the config is specified by the user (Fragment)
15 // interpreted (CompiledFragment), and combined (Provider).
16 // ConfigFragment.h describes the steps to add a new configuration option.
17 //
18 // Because this structure is shared throughout clangd, it's a potential source
19 // of layering problems. Config should be expressed in terms of simple
20 // vocabulary types where possible.
21 //
22 //===----------------------------------------------------------------------===//
23
24 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONFIG_H
25 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONFIG_H
26
27 #include "support/Context.h"
28 #include "llvm/ADT/FunctionExtras.h"
29 #include "llvm/ADT/StringMap.h"
30 #include "llvm/ADT/StringSet.h"
31 #include <functional>
32 #include <optional>
33 #include <string>
34 #include <vector>
35
36 namespace clang {
37 namespace clangd {
38
39 /// Settings that express user/project preferences and control clangd behavior.
40 ///
41 /// Generally, features should consume Config::current() and the caller is
42 /// responsible for setting it appropriately. In practice these callers are
43 /// ClangdServer, TUScheduler, and BackgroundQueue.
44 struct Config {
45 /// Returns the Config of the current Context, or an empty configuration.
46 static const Config &current();
47 /// Context key which can be used to set the current Config.
48 static clangd::Key<Config> Key;
49
50 Config() = default;
51 Config(const Config &) = delete;
52 Config &operator=(const Config &) = delete;
53 Config(Config &&) = default;
54 Config &operator=(Config &&) = default;
55
56 struct CDBSearchSpec {
57 enum { Ancestors, FixedDir, NoCDBSearch } Policy = Ancestors;
58 // Absolute, native slashes, no trailing slash.
59 std::optional<std::string> FixedCDBPath;
60 };
61
62 enum class BuiltinHeaderPolicy { Clangd, QueryDriver };
63 /// Controls how the compile command for the current file is determined.
64 struct {
65 /// Edits to apply to the compile command, in sequence.
66 std::vector<llvm::unique_function<void(std::vector<std::string> &) const>>
67 Edits;
68 /// Where to search for compilation databases for this file's flags.
69 CDBSearchSpec CDBSearch = {CDBSearchSpec::Ancestors, std::nullopt};
70
71 /// Whether to use clangd's own builtin headers, or ones from the system
72 /// include extractor, if available.
73 BuiltinHeaderPolicy BuiltinHeaders = BuiltinHeaderPolicy::Clangd;
74 } CompileFlags;
75
76 enum class BackgroundPolicy { Build, Skip };
77 /// Describes an external index configuration.
78 struct ExternalIndexSpec {
79 enum { None, File, Server } Kind = None;
80 /// This is one of:
81 /// - Address of a clangd-index-server, in the form of "ip:port".
82 /// - Absolute path to an index produced by clangd-indexer.
83 std::string Location;
84 /// Absolute path to source root this index is associated with, uses
85 /// forward-slashes.
86 std::string MountPoint;
87 };
88 /// Controls index behavior.
89 struct {
90 /// Whether this TU should be background-indexed.
91 BackgroundPolicy Background = BackgroundPolicy::Build;
92 ExternalIndexSpec External;
93 bool StandardLibrary = true;
94 } Index;
95
96 enum class IncludesPolicy {
97 /// Diagnose missing and unused includes.
98 Strict,
99 None,
100 };
101 enum class FastCheckPolicy { Strict, Loose, None };
102 /// Controls warnings and errors when parsing code.
103 struct {
104 bool SuppressAll = false;
105 llvm::StringSet<> Suppress;
106
107 /// Configures what clang-tidy checks to run and options to use with them.
108 struct {
109 // A comma-separated list of globs specify which clang-tidy checks to run.
110 std::string Checks;
111 llvm::StringMap<std::string> CheckOptions;
112 FastCheckPolicy FastCheckFilter = FastCheckPolicy::Strict;
113 } ClangTidy;
114
115 IncludesPolicy UnusedIncludes = IncludesPolicy::Strict;
116 IncludesPolicy MissingIncludes = IncludesPolicy::None;
117
118 struct {
119 /// IncludeCleaner will not diagnose usages of these headers matched by
120 /// these regexes.
121 std::vector<std::function<bool(llvm::StringRef)>> IgnoreHeader;
122 bool AnalyzeAngledIncludes = false;
123 } Includes;
124 } Diagnostics;
125
126 /// Style of the codebase.
127 struct {
128 // Namespaces that should always be fully qualified, meaning no "using"
129 // declarations, always spell out the whole name (with or without leading
130 // ::). All nested namespaces are affected as well.
131 std::vector<std::string> FullyQualifiedNamespaces;
132
133 // List of matcher functions for inserting certain headers with <> or "".
134 std::vector<std::function<bool(llvm::StringRef)>> QuotedHeaders;
135 std::vector<std::function<bool(llvm::StringRef)>> AngledHeaders;
136 } Style;
137
138 /// controls the completion options for argument lists.
139 enum class ArgumentListsPolicy {
140 /// nothing, no argument list and also NO Delimiters "()" or "<>".
141 None,
142 /// open, only opening delimiter "(" or "<".
143 OpenDelimiter,
144 /// empty pair of delimiters "()" or "<>".
145 Delimiters,
146 /// full name of both type and variable.
147 FullPlaceholders,
148 };
149
150 enum class HeaderInsertionPolicy {
151 IWYU, // Include what you use
152 NeverInsert // Never insert headers as part of code completion
153 };
154
155 enum class CodePatternsPolicy {
156 All, // Suggest all code patterns and snippets
157 None // Suggest none of the code patterns and snippets
158 };
159
160 enum class MacroFilterPolicy {
161 ExactPrefix, // Suggest macros if the prefix matches exactly
162 FuzzyMatch, // Fuzzy-match macros if they do not have "_" as prefix or
163 // suffix
164 };
165
166 /// Configures code completion feature.
167 struct {
168 /// Whether code completion includes results that are not visible in current
169 /// scopes.
170 bool AllScopes = true;
171 /// controls the completion options for argument lists.
172 ArgumentListsPolicy ArgumentLists = ArgumentListsPolicy::FullPlaceholders;
173 /// Controls if headers should be inserted when completions are accepted
174 HeaderInsertionPolicy HeaderInsertion = HeaderInsertionPolicy::IWYU;
175 /// Enables code patterns & snippets suggestions
176 CodePatternsPolicy CodePatterns = CodePatternsPolicy::All;
177 /// Controls how macros are filtered
178 MacroFilterPolicy MacroFilter = MacroFilterPolicy::ExactPrefix;
179 } Completion;
180
181 /// Configures hover feature.
182 struct {
183 /// Whether hover show a.k.a type.
184 bool ShowAKA = true;
185 /// Limit the number of characters returned when hovering a macro;
186 /// 0 is no limit.
187 uint32_t MacroContentsLimit = 2048;
188 } Hover;
189
190 struct {
191 /// If false, inlay hints are completely disabled.
192 bool Enabled = true;
193
194 // Whether specific categories of hints are enabled.
195 bool Parameters = true;
196 bool DeducedTypes = true;
197 bool Designators = true;
198 bool BlockEnd = false;
199 bool DefaultArguments = false;
200 // Limit the length of type names in inlay hints. (0 means no limit)
201 uint32_t TypeNameLimit = 32;
202 } InlayHints;
203
204 struct {
205 /// Controls highlighting kinds that are disabled.
206 std::vector<std::string> DisabledKinds;
207 /// Controls highlighting modifiers that are disabled.
208 std::vector<std::string> DisabledModifiers;
209 } SemanticTokens;
210
211 enum class CommentFormatPolicy {
212 /// Treat comments as plain text.
213 PlainText,
214 /// Treat comments as Markdown.
215 Markdown,
216 /// Treat comments as doxygen.
217 Doxygen,
218 };
219
220 struct {
221 CommentFormatPolicy CommentFormat = CommentFormatPolicy::PlainText;
222 } Documentation;
223 };
224
225 } // namespace clangd
226 } // namespace clang
227
228 namespace llvm {
229 template <> struct DenseMapInfo<clang::clangd::Config::ExternalIndexSpec> {
230 using ExternalIndexSpec = clang::clangd::Config::ExternalIndexSpec;
231 static inline ExternalIndexSpec getEmptyKey() {
232 return {ExternalIndexSpec::File, "", ""};
233 }
234 static inline ExternalIndexSpec getTombstoneKey() {
235 return {ExternalIndexSpec::File, "TOMB", "STONE"};
236 }
237 static unsigned getHashValue(const ExternalIndexSpec &Val) {
238 return llvm::hash_combine(Val.Kind, Val.Location, Val.MountPoint);
239 }
240 static bool isEqual(const ExternalIndexSpec &LHS,
241 const ExternalIndexSpec &RHS) {
242 return std::tie(LHS.Kind, LHS.Location, LHS.MountPoint) ==
243 std::tie(RHS.Kind, RHS.Location, RHS.MountPoint);
244 }
245 };
246 } // namespace llvm
247
248 #endif