summaryrefslogtreecommitdiff
path: root/clang-tools-extra/clangd/ConfigProvider.h
blob: f268edb1df2a53f612325868915eb042e3b86179 (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
//===--- ConfigProvider.h - Loading of user configuration --------*- 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).
// The configuration system allows users to control this:
//  - in a user config file, a project config file, via LSP, or via flags
//  - specifying different settings for different files
// This file defines the structures used for this, that produce a Config.
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONFIGPROVIDER_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_CONFIGPROVIDER_H

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/SourceMgr.h"
#include <chrono>
#include <string>
#include <vector>

namespace clang {
namespace clangd {
struct Config;
class ThreadsafeFS;
namespace config {

/// Describes the context used to evaluate configuration fragments.
struct Params {
  /// Absolute path to a source file we're applying the config to. Unix slashes.
  /// Empty if not configuring a particular file.
  llvm::StringRef Path;
  /// Hint that stale data is OK to improve performance (e.g. avoid IO).
  /// FreshTime sets a bound for how old the data can be.
  /// By default, providers should validate caches against the data source.
  std::chrono::steady_clock::time_point FreshTime =
      std::chrono::steady_clock::time_point::max();
};

/// Used to report problems in parsing or interpreting a config.
/// Errors reflect structurally invalid config that should be user-visible.
/// Warnings reflect e.g. unknown properties that are recoverable.
/// Notes are used to report files and fragments.
/// (This can be used to track when previous warnings/errors have been "fixed").
using DiagnosticCallback = llvm::function_ref<void(const llvm::SMDiagnostic &)>;

/// A chunk of configuration that has been fully analyzed and is ready to apply.
/// Typically this is obtained from a Fragment by calling Fragment::compile().
///
/// Calling it updates the configuration to reflect settings from the fragment.
/// Returns true if the condition was met and the settings were used.
using CompiledFragment = std::function<bool(const Params &, Config &)>;

/// A source of configuration fragments.
/// Generally these providers reflect a fixed policy for obtaining config,
/// but return different concrete configuration over time.
/// e.g. a provider that reads config from files is responsive to file changes.
class Provider {
public:
  virtual ~Provider() = default;

  /// Reads fragments from a single YAML file with a fixed path. If non-empty,
  /// Directory will be used to resolve relative paths in the fragments.
  static std::unique_ptr<Provider> fromYAMLFile(llvm::StringRef AbsPath,
                                                llvm::StringRef Directory,
                                                const ThreadsafeFS &,
                                                bool Trusted = false);
  // Reads fragments from YAML files found relative to ancestors of Params.Path.
  //
  // All fragments that exist are returned, starting from distant ancestors.
  // For instance, given RelPath of ".clangd", then for source file /foo/bar.cc,
  // the searched fragments are [/.clangd, /foo/.clangd].
  //
  // If Params does not specify a path, no fragments are returned.
  static std::unique_ptr<Provider>
  fromAncestorRelativeYAMLFiles(llvm::StringRef RelPath, const ThreadsafeFS &,
                                bool Trusted = false);

  /// A provider that includes fragments from all the supplied providers.
  /// Order is preserved; later providers take precedence over earlier ones.
  static std::unique_ptr<Provider> combine(std::vector<const Provider *>);

  /// Build a config based on this provider.
  Config getConfig(const Params &, DiagnosticCallback) const;

private:
  /// Provide fragments that may be relevant to the file.
  /// The configuration provider is not responsible for testing conditions.
  ///
  /// Providers are expected to cache compiled fragments, and only
  /// reparse/recompile when the source data has changed.
  /// Despite the need for caching, this function must be threadsafe.
  ///
  /// When parsing/compiling, the DiagnosticCallback is used to report errors.
  virtual std::vector<CompiledFragment>
  getFragments(const Params &, DiagnosticCallback) const = 0;
};

} // namespace config
} // namespace clangd
} // namespace clang

#endif