clang-tools  17.0.0git
Config.h
Go to the documentation of this file.
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.
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 {
58  // Absolute, native slashes, no trailing slash.
59  std::optional<std::string> FixedCDBPath;
60  };
61 
62  /// Controls how the compile command for the current file is determined.
63  struct {
64  /// Edits to apply to the compile command, in sequence.
65  std::vector<llvm::unique_function<void(std::vector<std::string> &) const>>
67  /// Where to search for compilation databases for this file's flags.
69  } CompileFlags;
70 
71  enum class BackgroundPolicy { Build, Skip };
72  /// Describes an external index configuration.
74  enum { None, File, Server } Kind = None;
75  /// This is one of:
76  /// - Address of a clangd-index-server, in the form of "ip:port".
77  /// - Absolute path to an index produced by clangd-indexer.
78  std::string Location;
79  /// Absolute path to source root this index is associated with, uses
80  /// forward-slashes.
81  std::string MountPoint;
82  };
83  /// Controls index behavior.
84  struct {
85  /// Whether this TU should be background-indexed.
88  bool StandardLibrary = true;
89  } Index;
90 
92  /// Diagnose unused includes.
95  /// The same as Strict, but using the include-cleaner library.
97  };
98  /// Controls warnings and errors when parsing code.
99  struct {
100  bool SuppressAll = false;
101  llvm::StringSet<> Suppress;
102 
103  /// Configures what clang-tidy checks to run and options to use with them.
104  struct {
105  // A comma-seperated list of globs specify which clang-tidy checks to run.
106  std::string Checks;
107  llvm::StringMap<std::string> CheckOptions;
108  } ClangTidy;
109 
111 
112  /// IncludeCleaner will not diagnose usages of these headers matched by
113  /// these regexes.
114  struct {
115  std::vector<std::function<bool(llvm::StringRef)>> IgnoreHeader;
116  } Includes;
117  } Diagnostics;
118 
119  /// Style of the codebase.
120  struct {
121  // Namespaces that should always be fully qualified, meaning no "using"
122  // declarations, always spell out the whole name (with or without leading
123  // ::). All nested namespaces are affected as well.
124  std::vector<std::string> FullyQualifiedNamespaces;
125  } Style;
126 
127  /// Configures code completion feature.
128  struct {
129  /// Whether code completion includes results that are not visible in current
130  /// scopes.
131  bool AllScopes = true;
132  } Completion;
133 
134  /// Configures hover feature.
135  struct {
136  /// Whether hover show a.k.a type.
137  bool ShowAKA = true;
138  } Hover;
139 
140  struct {
141  /// If false, inlay hints are completely disabled.
142  bool Enabled = true;
143 
144  // Whether specific categories of hints are enabled.
145  bool Parameters = true;
146  bool DeducedTypes = true;
147  bool Designators = true;
148  } InlayHints;
149 };
150 
151 } // namespace clangd
152 } // namespace clang
153 
154 namespace llvm {
155 template <> struct DenseMapInfo<clang::clangd::Config::ExternalIndexSpec> {
157  static inline ExternalIndexSpec getEmptyKey() {
158  return {ExternalIndexSpec::File, "", ""};
159  }
161  return {ExternalIndexSpec::File, "TOMB", "STONE"};
162  }
163  static unsigned getHashValue(const ExternalIndexSpec &Val) {
164  return llvm::hash_combine(Val.Kind, Val.Location, Val.MountPoint);
165  }
166  static bool isEqual(const ExternalIndexSpec &LHS,
167  const ExternalIndexSpec &RHS) {
168  return std::tie(LHS.Kind, LHS.Location, LHS.MountPoint) ==
169  std::tie(RHS.Kind, RHS.Location, RHS.MountPoint);
170  }
171 };
172 } // namespace llvm
173 
174 #endif
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:34
clang::clangd::Config::AllScopes
bool AllScopes
Whether code completion includes results that are not visible in current scopes.
Definition: Config.h:131
clang::clangd::Config::CDBSearchSpec::Policy
enum clang::clangd::Config::CDBSearchSpec::@9 Policy
clang::clangd::Config::Hover
struct clang::clangd::Config::@7 Hover
Configures hover feature.
clang::clangd::Config::Enabled
bool Enabled
If false, inlay hints are completely disabled.
Definition: Config.h:142
clang::clangd::Config::BackgroundPolicy::Skip
@ Skip
clang::clangd::Config::CDBSearchSpec::FixedDir
@ FixedDir
Definition: Config.h:57
clang::clangd::Config::Edits
std::vector< llvm::unique_function< void(std::vector< std::string > &) const > > Edits
Edits to apply to the compile command, in sequence.
Definition: Config.h:66
clang::clangd::Config::Index
struct clang::clangd::Config::@3 Index
Controls index behavior.
clang::clangd::Config::Style
struct clang::clangd::Config::@5 Style
Style of the codebase.
clang::clangd::Config::Includes
struct clang::clangd::Config::@4::@12 Includes
IncludeCleaner will not diagnose usages of these headers matched by these regexes.
clang::clangd::Config::ExternalIndexSpec::File
@ File
Definition: Config.h:74
clang::clangd::Config
Settings that express user/project preferences and control clangd behavior.
Definition: Config.h:44
clang::clangd::Config::operator=
Config & operator=(const Config &)=delete
clang::clangd::Config::ExternalIndexSpec::Kind
enum clang::clangd::Config::ExternalIndexSpec::@10 Kind
clang::clangd::Config::DeducedTypes
bool DeducedTypes
Definition: Config.h:146
clang::clangd::Config::UnusedIncludesPolicy
UnusedIncludesPolicy
Definition: Config.h:91
llvm::DenseMapInfo< clang::clangd::Config::ExternalIndexSpec >::getHashValue
static unsigned getHashValue(const ExternalIndexSpec &Val)
Definition: Config.h:163
clang::clangd::Config::CDBSearchSpec::NoCDBSearch
@ NoCDBSearch
Definition: Config.h:57
clang::clangd::Config::ClangTidy
struct clang::clangd::Config::@4::@11 ClangTidy
Configures what clang-tidy checks to run and options to use with them.
clang::clangd::Config::CDBSearchSpec
Definition: Config.h:56
clang::clangd::Config::Designators
bool Designators
Definition: Config.h:147
llvm::DenseMapInfo< clang::clangd::Config::ExternalIndexSpec >::getTombstoneKey
static ExternalIndexSpec getTombstoneKey()
Definition: Config.h:160
clang::clangd::Config::Suppress
llvm::StringSet Suppress
Definition: Config.h:101
clang::clangd::Config::Checks
std::string Checks
Definition: Config.h:106
clang::clangd::Key
Values in a Context are indexed by typed keys.
Definition: Context.h:40
clang::clangd::Config::SuppressAll
bool SuppressAll
Definition: Config.h:100
clang::clangd::Config::Config
Config()=default
clang::clangd::Config::Completion
struct clang::clangd::Config::@6 Completion
Configures code completion feature.
llvm::DenseMapInfo< clang::clangd::Config::ExternalIndexSpec >::isEqual
static bool isEqual(const ExternalIndexSpec &LHS, const ExternalIndexSpec &RHS)
Definition: Config.h:166
clang::clangd::Config::UnusedIncludes
UnusedIncludesPolicy UnusedIncludes
Definition: Config.h:110
clang::clangd::Config::ExternalIndexSpec::None
@ None
Definition: Config.h:74
clang::clangd::Config::Background
BackgroundPolicy Background
Whether this TU should be background-indexed.
Definition: Config.h:86
clang::clangd::Config::Parameters
bool Parameters
Definition: Config.h:145
clang::clangd::Config::StandardLibrary
bool StandardLibrary
Definition: Config.h:88
clang::clangd::Config::Experiment
@ Experiment
The same as Strict, but using the include-cleaner library.
Definition: Config.h:96
clang::clangd::Config::IgnoreHeader
std::vector< std::function< bool(llvm::StringRef)> > IgnoreHeader
Definition: Config.h:115
clang::clangd::Config::Strict
@ Strict
Diagnose unused includes.
Definition: Config.h:93
clang::clangd::Config::CDBSearchSpec::Ancestors
@ Ancestors
Definition: Config.h:57
clang::clangd::Config::InlayHints
struct clang::clangd::Config::@8 InlayHints
clang::clangd::Config::CDBSearchSpec::FixedCDBPath
std::optional< std::string > FixedCDBPath
Definition: Config.h:59
clang::clangd::Config::ExternalIndexSpec::MountPoint
std::string MountPoint
Absolute path to source root this index is associated with, uses forward-slashes.
Definition: Config.h:81
clang::clangd::Config::CDBSearch
CDBSearchSpec CDBSearch
Where to search for compilation databases for this file's flags.
Definition: Config.h:68
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::Config::ExternalIndexSpec
Describes an external index configuration.
Definition: Config.h:73
clang::clangd::Config::current
static const Config & current()
Returns the Config of the current Context, or an empty configuration.
Definition: Config.cpp:17
clang::clangd::Config::CheckOptions
llvm::StringMap< std::string > CheckOptions
Definition: Config.h:107
clang::clangd::Config::None
@ None
Definition: Config.h:94
clang::clangd::Config::FullyQualifiedNamespaces
std::vector< std::string > FullyQualifiedNamespaces
Definition: Config.h:124
clang::clangd::Config::Key
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
Definition: Config.h:48
clang::clangd::Config::Diagnostics
struct clang::clangd::Config::@4 Diagnostics
Controls warnings and errors when parsing code.
clang::clangd::Config::External
ExternalIndexSpec External
Definition: Config.h:87
clang::clangd::Config::ExternalIndexSpec::Location
std::string Location
This is one of:
Definition: Config.h:78
clang::clangd::Config::BackgroundPolicy::Build
@ Build
llvm::DenseMapInfo< clang::clangd::Config::ExternalIndexSpec >::getEmptyKey
static ExternalIndexSpec getEmptyKey()
Definition: Config.h:157
clang::clangd::Config::ExternalIndexSpec::Server
@ Server
Definition: Config.h:74
clang::clangd::Config::BackgroundPolicy
BackgroundPolicy
Definition: Config.h:71
clang::clangd::Config::CompileFlags
struct clang::clangd::Config::@2 CompileFlags
Controls how the compile command for the current file is determined.
clang::clangd::Config::ShowAKA
bool ShowAKA
Whether hover show a.k.a type.
Definition: Config.h:137
Context.h