clang-tools  14.0.0git
Index.h
Go to the documentation of this file.
1 //===--- Index.h -------------------------------------------------*- 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 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H
10 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H
11 
12 #include "Ref.h"
13 #include "Relation.h"
14 #include "Symbol.h"
15 #include "SymbolID.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/FunctionExtras.h"
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/StringExtras.h"
20 #include "llvm/Support/JSON.h"
21 #include <mutex>
22 #include <string>
23 
24 namespace clang {
25 namespace clangd {
26 
28  /// A query string for the fuzzy find. This is matched against symbols'
29  /// un-qualified identifiers and should not contain qualifiers like "::".
30  std::string Query;
31  /// If this is non-empty, symbols must be in at least one of the scopes
32  /// (e.g. namespaces) excluding nested scopes. For example, if a scope "xyz::"
33  /// is provided, the matched symbols must be defined in namespace xyz but not
34  /// namespace xyz::abc.
35  ///
36  /// The global scope is "", a top level scope is "foo::", etc.
37  std::vector<std::string> Scopes;
38  /// If set to true, allow symbols from any scope. Scopes explicitly listed
39  /// above will be ranked higher.
40  bool AnyScope = false;
41  /// The number of top candidates to return. The index may choose to
42  /// return more than this, e.g. if it doesn't know which candidates are best.
43  llvm::Optional<uint32_t> Limit;
44  /// If set to true, only symbols for completion support will be considered.
46  /// Contextually relevant files (e.g. the file we're code-completing in).
47  /// Paths should be absolute.
48  std::vector<std::string> ProximityPaths;
49  /// Preferred types of symbols. These are raw representation of `OpaqueType`.
50  std::vector<std::string> PreferredTypes;
51 
52  bool operator==(const FuzzyFindRequest &Req) const {
53  return std::tie(Query, Scopes, Limit, RestrictForCodeCompletion,
55  std::tie(Req.Query, Req.Scopes, Req.Limit,
57  Req.PreferredTypes);
58  }
59  bool operator!=(const FuzzyFindRequest &Req) const { return !(*this == Req); }
60 };
61 bool fromJSON(const llvm::json::Value &Value, FuzzyFindRequest &Request,
63 llvm::json::Value toJSON(const FuzzyFindRequest &Request);
64 
65 struct LookupRequest {
66  llvm::DenseSet<SymbolID> IDs;
67 };
68 
69 struct RefsRequest {
70  llvm::DenseSet<SymbolID> IDs;
72  /// If set, limit the number of refers returned from the index. The index may
73  /// choose to return less than this, e.g. it tries to avoid returning stale
74  /// results.
75  llvm::Optional<uint32_t> Limit;
76 };
77 
79  llvm::DenseSet<SymbolID> Subjects;
81  /// If set, limit the number of relations returned from the index.
82  llvm::Optional<uint32_t> Limit;
83 };
84 
85 /// Describes what data is covered by an index.
86 ///
87 /// Indexes may contain symbols but not references from a file, etc.
88 /// This affects merging: if a staler index contains a reference but a fresher
89 /// one does not, we want to trust the fresher index *only* if it actually
90 /// includes references in general.
91 enum class IndexContents : uint8_t {
92  None = 0,
93  Symbols = 1 << 1,
94  References = 1 << 2,
95  Relations = 1 << 3,
97 };
98 
100  return static_cast<IndexContents>(static_cast<uint8_t>(L) &
101  static_cast<uint8_t>(R));
102 }
103 
105  return static_cast<IndexContents>(static_cast<uint8_t>(L) |
106  static_cast<uint8_t>(R));
107 }
108 
109 /// Interface for symbol indexes that can be used for searching or
110 /// matching symbols among a set of symbols based on names or unique IDs.
111 class SymbolIndex {
112 public:
113  virtual ~SymbolIndex() = default;
114 
115  /// Matches symbols in the index fuzzily and applies \p Callback on
116  /// each matched symbol before returning.
117  /// If returned Symbols are used outside Callback, they must be deep-copied!
118  ///
119  /// Returns true if there may be more results (limited by Req.Limit).
120  virtual bool
121  fuzzyFind(const FuzzyFindRequest &Req,
122  llvm::function_ref<void(const Symbol &)> Callback) const = 0;
123 
124  /// Looks up symbols with any of the given symbol IDs and applies \p Callback
125  /// on each matched symbol.
126  /// The returned symbol must be deep-copied if it's used outside Callback.
127  virtual void
128  lookup(const LookupRequest &Req,
129  llvm::function_ref<void(const Symbol &)> Callback) const = 0;
130 
131  /// Finds all occurrences (e.g. references, declarations, definitions) of
132  /// symbols and applies \p Callback on each result.
133  ///
134  /// Results should be returned in arbitrary order.
135  /// The returned result must be deep-copied if it's used outside Callback.
136  /// FIXME: there's no indication which result references which symbol.
137  ///
138  /// Returns true if there will be more results (limited by Req.Limit);
139  virtual bool refs(const RefsRequest &Req,
140  llvm::function_ref<void(const Ref &)> Callback) const = 0;
141 
142  /// Finds all relations (S, P, O) stored in the index such that S is among
143  /// Req.Subjects and P is Req.Predicate, and invokes \p Callback for (S, O) in
144  /// each.
145  virtual void relations(
146  const RelationsRequest &Req,
147  llvm::function_ref<void(const SymbolID &Subject, const Symbol &Object)>
148  Callback) const = 0;
149 
150  /// Returns function which checks if the specified file was used to build this
151  /// index or not. The function must only be called while the index is alive.
152  using IndexedFiles =
153  llvm::unique_function<IndexContents(llvm::StringRef) const>;
154  virtual IndexedFiles indexedFiles() const = 0;
155 
156  /// Returns estimated size of index (in bytes).
157  virtual size_t estimateMemoryUsage() const = 0;
158 };
159 
160 // Delegating implementation of SymbolIndex whose delegate can be swapped out.
161 class SwapIndex : public SymbolIndex {
162 public:
163  // If an index is not provided, reset() must be called.
164  SwapIndex(std::unique_ptr<SymbolIndex> Index = nullptr)
165  : Index(std::move(Index)) {}
166  void reset(std::unique_ptr<SymbolIndex>);
167 
168  // SymbolIndex methods delegate to the current index, which is kept alive
169  // until the call returns (even if reset() is called).
170  bool fuzzyFind(const FuzzyFindRequest &,
171  llvm::function_ref<void(const Symbol &)>) const override;
172  void lookup(const LookupRequest &,
173  llvm::function_ref<void(const Symbol &)>) const override;
174  bool refs(const RefsRequest &,
175  llvm::function_ref<void(const Ref &)>) const override;
176  void relations(const RelationsRequest &,
177  llvm::function_ref<void(const SymbolID &, const Symbol &)>)
178  const override;
179 
180  llvm::unique_function<IndexContents(llvm::StringRef) const>
181  indexedFiles() const override;
182 
183  size_t estimateMemoryUsage() const override;
184 
185 private:
186  std::shared_ptr<SymbolIndex> snapshot() const;
187  mutable std::mutex Mutex;
188  std::shared_ptr<SymbolIndex> Index;
189 };
190 
191 } // namespace clangd
192 } // namespace clang
193 
194 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H
SymbolID.h
clang::clangd::RefKind::All
@ All
clang::clangd::SwapIndex::SwapIndex
SwapIndex(std::unique_ptr< SymbolIndex > Index=nullptr)
Definition: Index.h:164
clang::clangd::FuzzyFindRequest::PreferredTypes
std::vector< std::string > PreferredTypes
Preferred types of symbols. These are raw representation of OpaqueType.
Definition: Index.h:50
clang::clangd::FuzzyFindRequest::operator==
bool operator==(const FuzzyFindRequest &Req) const
Definition: Index.h:52
clang::clangd::RelationsRequest::Limit
llvm::Optional< uint32_t > Limit
If set, limit the number of relations returned from the index.
Definition: Index.h:82
clang::clangd::SymbolIndex::indexedFiles
virtual IndexedFiles indexedFiles() const =0
clang::clangd::SymbolKind::Object
@ Object
clang::clangd::SwapIndex::reset
void reset(std::unique_ptr< SymbolIndex >)
Definition: Index.cpp:20
clang::clangd::IndexContents::Relations
@ Relations
clang::clangd::RefKind
RefKind
Describes the kind of a cross-reference.
Definition: Ref.h:30
clang::clangd::SwapIndex
Definition: Index.h:161
clang::clangd::SymbolIndex::~SymbolIndex
virtual ~SymbolIndex()=default
clang::clangd::FuzzyFindRequest::Scopes
std::vector< std::string > Scopes
If this is non-empty, symbols must be in at least one of the scopes (e.g.
Definition: Index.h:37
clang::clangd::IndexContents::Symbols
@ Symbols
clang::clangd::RelationsRequest::Predicate
RelationKind Predicate
Definition: Index.h:80
clang::clangd::FuzzyFindRequest
Definition: Index.h:27
clang::clangd::SymbolIndex::IndexedFiles
llvm::unique_function< IndexContents(llvm::StringRef) const > IndexedFiles
Returns function which checks if the specified file was used to build this index or not.
Definition: Index.h:153
clang::clangd::FuzzyFindRequest::Query
std::string Query
A query string for the fuzzy find.
Definition: Index.h:30
clang::clangd::operator&
DeclRelationSet operator&(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:214
clang::clangd::SymbolIndex::estimateMemoryUsage
virtual size_t estimateMemoryUsage() const =0
Returns estimated size of index (in bytes).
Relation.h
clang::clangd::FuzzyFindRequest::operator!=
bool operator!=(const FuzzyFindRequest &Req) const
Definition: Index.h:59
clang::clangd::SymbolIndex::lookup
virtual void lookup(const LookupRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Looks up symbols with any of the given symbol IDs and applies Callback on each matched symbol.
clang::clangd::RelationsRequest::Subjects
llvm::DenseSet< SymbolID > Subjects
Definition: Index.h:79
clang::clangd::SymbolIndex::relations
virtual void relations(const RelationsRequest &Req, llvm::function_ref< void(const SymbolID &Subject, const Symbol &Object)> Callback) const =0
Finds all relations (S, P, O) stored in the index such that S is among Req.Subjects and P is Req....
clang::clangd::SwapIndex::relations
void relations(const RelationsRequest &, llvm::function_ref< void(const SymbolID &, const Symbol &)>) const override
Definition: Index.cpp:73
clang::clangd::toJSON
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
Definition: Index.cpp:49
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang::clangd::fromJSON
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request, llvm::json::Path P)
Definition: Index.cpp:34
clang::clangd::IndexContents
IndexContents
Describes what data is covered by an index.
Definition: Index.h:91
clang::clangd::SwapIndex::refs
bool refs(const RefsRequest &, llvm::function_ref< void(const Ref &)>) const override
Finds all occurrences (e.g.
Definition: Index.cpp:69
clang::clangd::RefsRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:70
clang::clangd::SwapIndex::indexedFiles
llvm::unique_function< IndexContents(llvm::StringRef) const > indexedFiles() const override
Definition: Index.cpp:80
clang::clangd::FuzzyFindRequest::Limit
llvm::Optional< uint32_t > Limit
The number of top candidates to return.
Definition: Index.h:43
Symbol.h
clang::clangd::IndexContents::All
@ All
clang::clangd::LookupRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:66
clang::clangd::SymbolIndex::refs
virtual bool refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const =0
Finds all occurrences (e.g.
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:111
clang::clangd::RefsRequest::Filter
RefKind Filter
Definition: Index.h:71
clang::clangd::LookupRequest
Definition: Index.h:65
clang::clangd::IndexContents::References
@ References
clang::clangd::IndexContents::None
@ None
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:87
Ref.h
clang::clangd::operator|
DeclRelationSet operator|(DeclRelation L, DeclRelation R)
Definition: FindTarget.h:211
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::SwapIndex::estimateMemoryUsage
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: Index.cpp:90
clang::clangd::RefsRequest::Limit
llvm::Optional< uint32_t > Limit
If set, limit the number of refers returned from the index.
Definition: Index.h:75
clang::clangd::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:40
clang::clangd::Callback
llvm::unique_function< void(llvm::Expected< T >)> Callback
A Callback<T> is a void function that accepts Expected<T>.
Definition: Function.h:28
clang::clangd::RelationsRequest
Definition: Index.h:78
clang::clangd::FuzzyFindRequest::RestrictForCodeCompletion
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:45
clang::clangd::SymbolID
Definition: SymbolID.h:32
clang::clangd::SwapIndex::fuzzyFind
bool fuzzyFind(const FuzzyFindRequest &, llvm::function_ref< void(const Symbol &)>) const override
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning.
Definition: Index.cpp:61
clang::clangd::RefsRequest
Definition: Index.h:69
clang::clangd::FuzzyFindRequest::ProximityPaths
std::vector< std::string > ProximityPaths
Contextually relevant files (e.g.
Definition: Index.h:48
clang::clangd::SwapIndex::lookup
void lookup(const LookupRequest &, llvm::function_ref< void(const Symbol &)>) const override
Looks up symbols with any of the given symbol IDs and applies Callback on each matched symbol.
Definition: Index.cpp:65
clang::clangd::SymbolIndex::fuzzyFind
virtual bool fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning.
Value
static constexpr bool Value
Definition: SuspiciousCallArgumentCheck.cpp:72
Path
std::vector< HeaderHandle > Path
Definition: PreprocessorTracker.cpp:525
clang::clangd::RelationKind
RelationKind
Definition: Relation.h:22