aboutsummaryrefslogtreecommitdiff
path: root/clang-tools-extra/clangd/Config.h
blob: 2e3e0a431ab1fa924ffcd93ff2c843c9559fd1a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//===--- Config.h - User configuration of clangd behavior --------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Various clangd features have configurable behaviour (or can be disabled).
// This file defines "resolved" configuration seen by features within clangd.
// For example, settings may vary per-file, the resolved Config only contains
// settings that apply to the current file.
//
// This is distinct from how the config is specified by the user (Fragment)
// interpreted (CompiledFragment), and combined (Provider).
// ConfigFragment.h describes the steps to add a new configuration option.
//
// Because this structure is shared throughout clangd, it's a potential source
// of layering problems. Config should be expressed in terms of simple
// vocabulary types where possible.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONFIG_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONFIG_H

#include "support/Context.h"
#include "llvm/ADT/FunctionExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringSet.h"
#include <functional>
#include <optional>
#include <string>
#include <vector>

namespace clang {
namespace clangd {

/// Settings that express user/project preferences and control clangd behavior.
///
/// Generally, features should consume Config::current() and the caller is
/// responsible for setting it appropriately. In practice these callers are
/// ClangdServer, TUScheduler, and BackgroundQueue.
struct Config {
  /// Returns the Config of the current Context, or an empty configuration.
  static const Config &current();
  /// Context key which can be used to set the current Config.
  static clangd::Key<Config> Key;

  Config() = default;
  Config(const Config &) = delete;
  Config &operator=(const Config &) = delete;
  Config(Config &&) = default;
  Config &operator=(Config &&) = default;

  struct CDBSearchSpec {
    enum { Ancestors, FixedDir, NoCDBSearch } Policy = Ancestors;
    // Absolute, native slashes, no trailing slash.
    std::optional<std::string> FixedCDBPath;
  };

  enum class BuiltinHeaderPolicy { Clangd, QueryDriver };
  /// Controls how the compile command for the current file is determined.
  struct {
    /// Edits to apply to the compile command, in sequence.
    std::vector<llvm::unique_function<void(std::vector<std::string> &) const>>
        Edits;
    /// Where to search for compilation databases for this file's flags.
    CDBSearchSpec CDBSearch = {CDBSearchSpec::Ancestors, std::nullopt};

    /// Whether to use clangd's own builtin headers, or ones from the system
    /// include extractor, if available.
    BuiltinHeaderPolicy BuiltinHeaders = BuiltinHeaderPolicy::Clangd;
  } CompileFlags;

  enum class BackgroundPolicy { Build, Skip };
  /// Describes an external index configuration.
  struct ExternalIndexSpec {
    enum { None, File, Server } Kind = None;
    /// This is one of:
    /// - Address of a clangd-index-server, in the form of "ip:port".
    /// - Absolute path to an index produced by clangd-indexer.
    std::string Location;
    /// Absolute path to source root this index is associated with, uses
    /// forward-slashes.
    std::string MountPoint;
  };
  /// Controls index behavior.
  struct {
    /// Whether this TU should be background-indexed.
    BackgroundPolicy Background = BackgroundPolicy::Build;
    ExternalIndexSpec External;
    bool StandardLibrary = true;
  } Index;

  enum class IncludesPolicy {
    /// Diagnose missing and unused includes.
    Strict,
    None,
  };
  enum class FastCheckPolicy { Strict, Loose, None };
  /// Controls warnings and errors when parsing code.
  struct {
    bool SuppressAll = false;
    llvm::StringSet<> Suppress;

    /// Configures what clang-tidy checks to run and options to use with them.
    struct {
      // A comma-separated list of globs specify which clang-tidy checks to run.
      std::string Checks;
      llvm::StringMap<std::string> CheckOptions;
      FastCheckPolicy FastCheckFilter = FastCheckPolicy::Strict;
    } ClangTidy;

    IncludesPolicy UnusedIncludes = IncludesPolicy::Strict;
    IncludesPolicy MissingIncludes = IncludesPolicy::None;

    struct {
      /// IncludeCleaner will not diagnose usages of these headers matched by
      /// these regexes.
      std::vector<std::function<bool(llvm::StringRef)>> IgnoreHeader;
      bool AnalyzeAngledIncludes = false;
    } Includes;
  } Diagnostics;

  /// Style of the codebase.
  struct {
    // Namespaces that should always be fully qualified, meaning no "using"
    // declarations, always spell out the whole name (with or without leading
    // ::). All nested namespaces are affected as well.
    std::vector<std::string> FullyQualifiedNamespaces;

    // List of matcher functions for inserting certain headers with <> or "".
    std::vector<std::function<bool(llvm::StringRef)>> QuotedHeaders;
    std::vector<std::function<bool(llvm::StringRef)>> AngledHeaders;
  } Style;

  /// controls the completion options for argument lists.
  enum class ArgumentListsPolicy {
    /// nothing, no argument list and also NO Delimiters "()" or "<>".
    None,
    /// open, only opening delimiter "(" or "<".
    OpenDelimiter,
    /// empty pair of delimiters "()" or "<>".
    Delimiters,
    /// full name of both type and variable.
    FullPlaceholders,
  };

  enum class HeaderInsertionPolicy {
    IWYU,       // Include what you use
    NeverInsert // Never insert headers as part of code completion
  };

  enum class CodePatternsPolicy {
    All, // Suggest all code patterns and snippets
    None // Suggest none of the code patterns and snippets
  };

  /// Configures code completion feature.
  struct {
    /// Whether code completion includes results that are not visible in current
    /// scopes.
    bool AllScopes = true;
    /// controls the completion options for argument lists.
    ArgumentListsPolicy ArgumentLists = ArgumentListsPolicy::FullPlaceholders;
    /// Controls if headers should be inserted when completions are accepted
    HeaderInsertionPolicy HeaderInsertion = HeaderInsertionPolicy::IWYU;
    /// Enables code patterns & snippets suggestions
    CodePatternsPolicy CodePatterns = CodePatternsPolicy::All;
  } Completion;

  /// Configures hover feature.
  struct {
    /// Whether hover show a.k.a type.
    bool ShowAKA = true;
  } Hover;

  struct {
    /// If false, inlay hints are completely disabled.
    bool Enabled = true;

    // Whether specific categories of hints are enabled.
    bool Parameters = true;
    bool DeducedTypes = true;
    bool Designators = true;
    bool BlockEnd = false;
    bool DefaultArguments = false;
    // Limit the length of type names in inlay hints. (0 means no limit)
    uint32_t TypeNameLimit = 32;
  } InlayHints;

  struct {
    /// Controls highlighting kinds that are disabled.
    std::vector<std::string> DisabledKinds;
    /// Controls highlighting modifiers that are disabled.
    std::vector<std::string> DisabledModifiers;
  } SemanticTokens;

  enum class CommentFormatPolicy {
    /// Treat comments as plain text.
    PlainText,
    /// Treat comments as Markdown.
    Markdown,
    /// Treat comments as doxygen.
    Doxygen,
  };

  struct {
    CommentFormatPolicy CommentFormat = CommentFormatPolicy::PlainText;
  } Documentation;
};

} // namespace clangd
} // namespace clang

namespace llvm {
template <> struct DenseMapInfo<clang::clangd::Config::ExternalIndexSpec> {
  using ExternalIndexSpec = clang::clangd::Config::ExternalIndexSpec;
  static inline ExternalIndexSpec getEmptyKey() {
    return {ExternalIndexSpec::File, "", ""};
  }
  static inline ExternalIndexSpec getTombstoneKey() {
    return {ExternalIndexSpec::File, "TOMB", "STONE"};
  }
  static unsigned getHashValue(const ExternalIndexSpec &Val) {
    return llvm::hash_combine(Val.Kind, Val.Location, Val.MountPoint);
  }
  static bool isEqual(const ExternalIndexSpec &LHS,
                      const ExternalIndexSpec &RHS) {
    return std::tie(LHS.Kind, LHS.Location, LHS.MountPoint) ==
           std::tie(RHS.Kind, RHS.Location, RHS.MountPoint);
  }
};
} // namespace llvm

#endif