clang-tools 10.0.0

Index.cpp
Go to the documentation of this file.
1 //===--- Index.cpp -----------------------------------------------*- 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 #include "Index.h"
10 #include "Logger.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/raw_ostream.h"
15 #include <limits>
16
17 namespace clang {
18 namespace clangd {
19
20 void SwapIndex::reset(std::unique_ptr<SymbolIndex> Index) {
21 // Keep the old index alive, so we don't destroy it under lock (may be slow).
22 std::shared_ptr<SymbolIndex> Pin;
23 {
24 std::lock_guard<std::mutex> Lock(Mutex);
25 Pin = std::move(this->Index);
26 this->Index = std::move(Index);
27 }
28 }
29 std::shared_ptr<SymbolIndex> SwapIndex::snapshot() const {
30 std::lock_guard<std::mutex> Lock(Mutex);
31 return Index;
32 }
33
34 bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request) {
35 llvm::json::ObjectMapper O(Parameters);
36 int64_t Limit;
37 bool OK =
38 O && O.map("Query", Request.Query) && O.map("Scopes", Request.Scopes) &&
39 O.map("AnyScope", Request.AnyScope) && O.map("Limit", Limit) &&
40 O.map("RestrictForCodeCompletion", Request.RestrictForCodeCompletion) &&
41 O.map("ProximityPaths", Request.ProximityPaths) &&
42 O.map("PreferredTypes", Request.PreferredTypes);
43 if (OK && Limit <= std::numeric_limits<uint32_t>::max())
44 Request.Limit = Limit;
45 return OK;
46 }
47
48 llvm::json::Value toJSON(const FuzzyFindRequest &Request) {
49 return llvm::json::Object{
50 {"Query", Request.Query},
51 {"Scopes", llvm::json::Array{Request.Scopes}},
52 {"AnyScope", Request.AnyScope},
53 {"Limit", Request.Limit},
54 {"RestrictForCodeCompletion", Request.RestrictForCodeCompletion},
55 {"ProximityPaths", llvm::json::Array{Request.ProximityPaths}},
56 {"PreferredTypes", llvm::json::Array{Request.PreferredTypes}},
57 };
58 }
59
60 bool SwapIndex::fuzzyFind(const FuzzyFindRequest &R,
61 llvm::function_ref<void(const Symbol &)> CB) const {
62 return snapshot()->fuzzyFind(R, CB);
63 }
64 void SwapIndex::lookup(const LookupRequest &R,
65 llvm::function_ref<void(const Symbol &)> CB) const {
66 return snapshot()->lookup(R, CB);
67 }
68 bool SwapIndex::refs(const RefsRequest &R,
69 llvm::function_ref<void(const Ref &)> CB) const {
70 return snapshot()->refs(R, CB);
71 }
72 void SwapIndex::relations(
73 const RelationsRequest &R,
74 llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const {
75 return snapshot()->relations(R, CB);
76 }
77
78 size_t SwapIndex::estimateMemoryUsage() const {
79 return snapshot()->estimateMemoryUsage();
80 }
81
82 } // namespace clangd
83 } // namespace clang
Limit
int Limit
Definition: FindSymbolsTests.cpp:71
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::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:64
clang::clangd::FuzzyFindRequest::RestrictForCodeCompletion
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:44
Index.h
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
clang::clangd::RefsRequest
Definition: Index.h:67
clang::clangd::FuzzyFindRequest
Definition: Index.h:26
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::SwapIndex::refs
bool refs(const RefsRequest &, llvm::function_ref< void(const Ref &)>) const override
Finds all occurrences (e.g.
Definition: Index.cpp:68
clang::clangd::SwapIndex::relations
void relations(const RelationsRequest &, llvm::function_ref< void(const SymbolID &, const Symbol &)>) const override
Definition: Index.cpp:72
clang::clangd::FuzzyFindRequest::Query
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
clang::clangd::fromJSON
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request)
Definition: Index.cpp:34
Logger.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::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:60
clang::clangd::SwapIndex::estimateMemoryUsage
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: Index.cpp:78
clang::clangd::FuzzyFindRequest::Limit
llvm::Optional< uint32_t > Limit
The number of top candidates to return.
Definition: Index.h:42
Parameters
std::vector< Parameter > Parameters
Definition: ExtractFunction.cpp:303
clang::clangd::SwapIndex::reset
void reset(std::unique_ptr< SymbolIndex >)
Definition: Index.cpp:20
Index
const SymbolIndex * Index
Definition: Dexp.cpp:84
clang::clangd::SymbolID
Definition: SymbolID.h:31