clang-tools  14.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 // vocubulary 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/Optional.h"
30 #include "llvm/ADT/StringMap.h"
31 #include "llvm/ADT/StringSet.h"
32 #include <string>
33 #include <vector>
34 
35 namespace clang {
36 namespace clangd {
37 
38 /// Settings that express user/project preferences and control clangd behavior.
39 ///
40 /// Generally, features should consume Config::current() and the caller is
41 /// responsible for setting it appropriately. In practice these callers are
42 /// ClangdServer, TUScheduler, and BackgroundQueue.
43 struct Config {
44  /// Returns the Config of the current Context, or an empty configuration.
45  static const Config &current();
46  /// Context key which can be used to set the current Config.
48 
49  Config() = default;
50  Config(const Config &) = delete;
51  Config &operator=(const Config &) = delete;
52  Config(Config &&) = default;
53  Config &operator=(Config &&) = default;
54 
55  struct CDBSearchSpec {
57  // Absolute, native slashes, no trailing slash.
58  llvm::Optional<std::string> FixedCDBPath;
59  };
60 
61  /// Controls how the compile command for the current file is determined.
62  struct {
63  /// Edits to apply to the compile command, in sequence.
64  std::vector<llvm::unique_function<void(std::vector<std::string> &) const>>
66  /// Where to search for compilation databases for this file's flags.
68  } CompileFlags;
69 
70  enum class BackgroundPolicy { Build, Skip };
71  /// Describes an external index configuration.
73  enum { None, File, Server } Kind = None;
74  /// This is one of:
75  /// - Address of a clangd-index-server, in the form of "ip:port".
76  /// - Absolute path to an index produced by clangd-indexer.
77  std::string Location;
78  /// Absolute path to source root this index is associated with, uses
79  /// forward-slashes.
80  std::string MountPoint;
81  };
82  /// Controls background-index behavior.
83  struct {
84  /// Whether this TU should be indexed.
87  } Index;
88 
89  /// Controls warnings and errors when parsing code.
90  struct {
91  bool SuppressAll = false;
92  llvm::StringSet<> Suppress;
93 
94  /// Configures what clang-tidy checks to run and options to use with them.
95  struct {
96  // A comma-seperated list of globs specify which clang-tidy checks to run.
97  std::string Checks;
98  llvm::StringMap<std::string> CheckOptions;
99  } ClangTidy;
100  } Diagnostics;
101 
102  /// Style of the codebase.
103  struct {
104  // Namespaces that should always be fully qualified, meaning no "using"
105  // declarations, always spell out the whole name (with or without leading
106  // ::). All nested namespaces are affected as well.
107  std::vector<std::string> FullyQualifiedNamespaces;
108  } Style;
109 
110  /// Configures code completion feature.
111  struct {
112  /// Whether code completion includes results that are not visible in current
113  /// scopes.
114  bool AllScopes = true;
115  } Completion;
116 };
117 
118 } // namespace clangd
119 } // namespace clang
120 
121 namespace llvm {
122 template <> struct DenseMapInfo<clang::clangd::Config::ExternalIndexSpec> {
124  static inline ExternalIndexSpec getEmptyKey() {
125  return {ExternalIndexSpec::File, "", ""};
126  }
128  return {ExternalIndexSpec::File, "TOMB", "STONE"};
129  }
130  static unsigned getHashValue(const ExternalIndexSpec &Val) {
131  return llvm::hash_combine(Val.Kind, Val.Location, Val.MountPoint);
132  }
133  static bool isEqual(const ExternalIndexSpec &LHS,
134  const ExternalIndexSpec &RHS) {
135  return std::tie(LHS.Kind, LHS.Location, LHS.MountPoint) ==
136  std::tie(RHS.Kind, RHS.Location, RHS.MountPoint);
137  }
138 };
139 } // namespace llvm
140 
141 #endif
llvm
Some operations such as code completion produce a set of candidates.
Definition: YAMLGenerator.cpp:28
clang::clangd::Config::AllScopes
bool AllScopes
Whether code completion includes results that are not visible in current scopes.
Definition: Config.h:114
clang::clangd::Config::BackgroundPolicy::Skip
@ Skip
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:65
clang::clangd::Config::Index
struct clang::clangd::Config::@3 Index
Controls background-index behavior.
clang::clangd::Config::Style
struct clang::clangd::Config::@5 Style
Style of the codebase.
clang::clangd::Config
Settings that express user/project preferences and control clangd behavior.
Definition: Config.h:43
clang::clangd::Config::CDBSearchSpec::Ancestors
@ Ancestors
Definition: Config.h:56
clang::clangd::Config::operator=
Config & operator=(const Config &)=delete
clang::clangd::Config::ExternalIndexSpec::File
@ File
Definition: Config.h:73
clang::clangd::Config::CDBSearchSpec::FixedDir
@ FixedDir
Definition: Config.h:56
llvm::DenseMapInfo< clang::clangd::Config::ExternalIndexSpec >::getHashValue
static unsigned getHashValue(const ExternalIndexSpec &Val)
Definition: Config.h:130
clang::clangd::Config::ExternalIndexSpec::Kind
enum clang::clangd::Config::ExternalIndexSpec::@8 Kind
clang::clangd::Config::CDBSearchSpec
Definition: Config.h:55
clang::clangd::Config::CDBSearchSpec::NoCDBSearch
@ NoCDBSearch
Definition: Config.h:56
llvm::DenseMapInfo< clang::clangd::Config::ExternalIndexSpec >::getTombstoneKey
static ExternalIndexSpec getTombstoneKey()
Definition: Config.h:127
clang::clangd::Config::Suppress
llvm::StringSet Suppress
Definition: Config.h:92
clang::clangd::Config::ExternalIndexSpec::None
@ None
Definition: Config.h:73
clang::clangd::Config::Checks
std::string Checks
Definition: Config.h:97
clang::clangd::Config::ClangTidy
struct clang::clangd::Config::@4::@9 ClangTidy
Configures what clang-tidy checks to run and options to use with them.
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:91
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:133
clang::clangd::Config::Background
BackgroundPolicy Background
Whether this TU should be indexed.
Definition: Config.h:85
clang::clangd::Config::ExternalIndexSpec::Server
@ Server
Definition: Config.h:73
clang::clangd::Config::ExternalIndexSpec::MountPoint
std::string MountPoint
Absolute path to source root this index is associated with, uses forward-slashes.
Definition: Config.h:80
clang::clangd::Config::CDBSearch
CDBSearchSpec CDBSearch
Where to search for compilation databases for this file's flags.
Definition: Config.h:67
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::Config::ExternalIndexSpec
Describes an external index configuration.
Definition: Config.h:72
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:98
clang::clangd::Config::FullyQualifiedNamespaces
std::vector< std::string > FullyQualifiedNamespaces
Definition: Config.h:107
clang::clangd::Config::Key
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
Definition: Config.h:47
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:86
clang::clangd::Config::CDBSearchSpec::FixedCDBPath
llvm::Optional< std::string > FixedCDBPath
Definition: Config.h:58
clang::clangd::Config::ExternalIndexSpec::Location
std::string Location
This is one of:
Definition: Config.h:77
clang::clangd::Config::BackgroundPolicy::Build
@ Build
llvm::DenseMapInfo< clang::clangd::Config::ExternalIndexSpec >::getEmptyKey
static ExternalIndexSpec getEmptyKey()
Definition: Config.h:124
clang::clangd::Config::BackgroundPolicy
BackgroundPolicy
Definition: Config.h:70
clang::clangd::Config::CompileFlags
struct clang::clangd::Config::@2 CompileFlags
Controls how the compile command for the current file is determined.
clang::clangd::Config::CDBSearchSpec::Policy
enum clang::clangd::Config::CDBSearchSpec::@7 Policy
Context.h