clang-tools 10.0.0

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/Optional.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/JSON.h"
20 #include <mutex>
21 #include <string>
22
23 namespace clang {
24 namespace clangd {
25
26 struct FuzzyFindRequest {
27 /// A query string for the fuzzy find. This is matched against symbols'
28 /// un-qualified identifiers and should not contain qualifiers like "::".
29 std::string Query;
30 /// If this is non-empty, symbols must be in at least one of the scopes
31 /// (e.g. namespaces) excluding nested scopes. For example, if a scope "xyz::"
32 /// is provided, the matched symbols must be defined in namespace xyz but not
33 /// namespace xyz::abc.
34 ///
35 /// The global scope is "", a top level scope is "foo::", etc.
36 std::vector<std::string> Scopes;
37 /// If set to true, allow symbols from any scope. Scopes explicitly listed
38 /// above will be ranked higher.
39 bool AnyScope = false;
40 /// The number of top candidates to return. The index may choose to
41 /// return more than this, e.g. if it doesn't know which candidates are best.
42 llvm::Optional<uint32_t> Limit;
43 /// If set to true, only symbols for completion support will be considered.
44 bool RestrictForCodeCompletion = false;
45 /// Contextually relevant files (e.g. the file we're code-completing in).
46 /// Paths should be absolute.
47 std::vector<std::string> ProximityPaths;
48 /// Preferred types of symbols. These are raw representation of `OpaqueType`.
49 std::vector<std::string> PreferredTypes;
50
51 bool operator==(const FuzzyFindRequest &Req) const {
52 return std::tie(Query, Scopes, Limit, RestrictForCodeCompletion,
53 ProximityPaths, PreferredTypes) ==
54 std::tie(Req.Query, Req.Scopes, Req.Limit,
55 Req.RestrictForCodeCompletion, Req.ProximityPaths,
56 Req.PreferredTypes);
57 }
58 bool operator!=(const FuzzyFindRequest &Req) const { return !(*this == Req); }
59 };
60 bool fromJSON(const llvm::json::Value &Value, FuzzyFindRequest &Request);
61 llvm::json::Value toJSON(const FuzzyFindRequest &Request);
62
63 struct LookupRequest {
64 llvm::DenseSet<SymbolID> IDs;
65 };
66
67 struct RefsRequest {
68 llvm::DenseSet<SymbolID> IDs;
69 RefKind Filter = RefKind::All;
70 /// If set, limit the number of refers returned from the index. The index may
71 /// choose to return less than this, e.g. it tries to avoid returning stale
72 /// results.
73 llvm::Optional<uint32_t> Limit;
74 };
75
76 struct RelationsRequest {
77 llvm::DenseSet<SymbolID> Subjects;
78 RelationKind Predicate;
79 /// If set, limit the number of relations returned from the index.
80 llvm::Optional<uint32_t> Limit;
81 };
82
83 /// Interface for symbol indexes that can be used for searching or
84 /// matching symbols among a set of symbols based on names or unique IDs.
85 class SymbolIndex {
86 public:
87 virtual ~SymbolIndex() = default;
88
89 /// Matches symbols in the index fuzzily and applies \p Callback on
90 /// each matched symbol before returning.
91 /// If returned Symbols are used outside Callback, they must be deep-copied!
92 ///
93 /// Returns true if there may be more results (limited by Req.Limit).
94 virtual bool
95 fuzzyFind(const FuzzyFindRequest &Req,
96 llvm::function_ref<void(const Symbol &)> Callback) const = 0;
97
98 /// Looks up symbols with any of the given symbol IDs and applies \p Callback
99 /// on each matched symbol.
100 /// The returned symbol must be deep-copied if it's used outside Callback.
101 virtual void
102 lookup(const LookupRequest &Req,
103 llvm::function_ref<void(const Symbol &)> Callback) const = 0;
104
105 /// Finds all occurrences (e.g. references, declarations, definitions) of a
106 /// symbol and applies \p Callback on each result.
107 ///
108 /// Results should be returned in arbitrary order.
109 /// The returned result must be deep-copied if it's used outside Callback.
110 ///
111 /// Returns true if there will be more results (limited by Req.Limit);
112 virtual bool refs(const RefsRequest &Req,
113 llvm::function_ref<void(const Ref &)> Callback) const = 0;
114
115 /// Finds all relations (S, P, O) stored in the index such that S is among
116 /// Req.Subjects and P is Req.Predicate, and invokes \p Callback for (S, O) in
117 /// each.
118 virtual void relations(
119 const RelationsRequest &Req,
120 llvm::function_ref<void(const SymbolID &Subject, const Symbol &Object)>
121 Callback) const = 0;
122
123 /// Returns estimated size of index (in bytes).
124 virtual size_t estimateMemoryUsage() const = 0;
125 };
126
127 // Delegating implementation of SymbolIndex whose delegate can be swapped out.
128 class SwapIndex : public SymbolIndex {
129 public:
130 // If an index is not provided, reset() must be called.
131 SwapIndex(std::unique_ptr<SymbolIndex> Index = nullptr)
132 : Index(std::move(Index)) {}
133 void reset(std::unique_ptr<SymbolIndex>);
134
135 // SymbolIndex methods delegate to the current index, which is kept alive
136 // until the call returns (even if reset() is called).
137 bool fuzzyFind(const FuzzyFindRequest &,
138 llvm::function_ref<void(const Symbol &)>) const override;
139 void lookup(const LookupRequest &,
140 llvm::function_ref<void(const Symbol &)>) const override;
141 bool refs(const RefsRequest &,
142 llvm::function_ref<void(const Ref &)>) const override;
143 void relations(const RelationsRequest &,
144 llvm::function_ref<void(const SymbolID &, const Symbol &)>)
145 const override;
146
147 size_t estimateMemoryUsage() const override;
148
149 private:
150 std::shared_ptr<SymbolIndex> snapshot() const;
151 mutable std::mutex Mutex;
152 std::shared_ptr<SymbolIndex> Index;
153 };
154
155 } // namespace clangd
156 } // namespace clang
157
158 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_INDEX_INDEX_H
clang::clangd::RefsRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:68
clang::clangd::toJSON
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
Definition: Index.cpp:48
clang::clangd::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:39
clang::clangd::SwapIndex::SwapIndex
SwapIndex(std::unique_ptr< SymbolIndex > Index=nullptr)
Definition: Index.h:131
clang::clangd::FuzzyFindRequest::RestrictForCodeCompletion
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:44
clang::clangd::RelationKind
RelationKind
Definition: Relation.h:22
clang::clangd::RelationsRequest::Limit
llvm::Optional< uint32_t > Limit
If set, limit the number of relations returned from the index.
Definition: Index.h:80
Symbol.h
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:85
clang::clangd::LookupRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:64
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
clang::clangd::RefsRequest
Definition: Index.h:67
clang::clangd::RefKind::All
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::FuzzyFindRequest
Definition: Index.h:26
clang::clangd::FuzzyFindRequest::operator==
bool operator==(const FuzzyFindRequest &Req) const
Definition: Index.h:51
clang::clangd::FuzzyFindRequest::PreferredTypes
std::vector< std::string > PreferredTypes
Preferred types of symbols. These are raw representation of OpaqueType.
Definition: Index.h:49
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:36
clang::clangd::SymbolKind::Object
clang::clangd::lookup
std::vector< std::string > lookup(const SymbolIndex &I, llvm::ArrayRef< SymbolID > IDs)
Definition: TestIndex.cpp:106
Relation.h
clang::clangd::FuzzyFindRequest::Query
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
clang::clangd::RefKind
RefKind
Describes the kind of a cross-reference.
Definition: Ref.h:28
clang::clangd::FuzzyFindRequest::operator!=
bool operator!=(const FuzzyFindRequest &Req) const
Definition: Index.h:58
clang::clangd::RelationsRequest::Subjects
llvm::DenseSet< SymbolID > Subjects
Definition: Index.h:77
clang::clangd::fromJSON
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request)
Definition: Index.cpp:34
clang::clangd::SwapIndex
Definition: Index.h:128
Ref.h
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang::clangd::LookupRequest
Definition: Index.h:63
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::FuzzyFindRequest::ProximityPaths
std::vector< std::string > ProximityPaths
Contextually relevant files (e.g.
Definition: Index.h:47
clang::clangd::RelationsRequest
Definition: Index.h:76
clang::clangd::RefsRequest::Limit
llvm::Optional< uint32_t > Limit
If set, limit the number of refers returned from the index.
Definition: Index.h:73
clang::clangd::FuzzyFindRequest::Limit
llvm::Optional< uint32_t > Limit
The number of top candidates to return.
Definition: Index.h:42
clang::clangd::CompletionItemKind::Value
SymbolID.h
clang::clangd::RelationsRequest::Predicate
RelationKind Predicate
Definition: Index.h:78
Index
const SymbolIndex * Index
Definition: Dexp.cpp:84
clang::clangd::SymbolID
Definition: SymbolID.h:31