clang-tools  9.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 
61  llvm::function_ref<void(const Symbol &)> CB) const {
62  return snapshot()->fuzzyFind(R, CB);
63 }
65  llvm::function_ref<void(const Symbol &)> CB) const {
66  return snapshot()->lookup(R, CB);
67 }
69  llvm::function_ref<void(const Ref &)> CB) const {
70  return snapshot()->refs(R, CB);
71 }
73  const RelationsRequest &R,
74  llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const {
75  return snapshot()->relations(R, CB);
76 }
77 
79  return snapshot()->estimateMemoryUsage();
80 }
81 
82 } // namespace clangd
83 } // namespace clang
int Limit
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
Definition: Index.cpp:48
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:39
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
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:44
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
std::vector< std::string > PreferredTypes
Preferred types of symbols. These are raw representation of OpaqueType.
Definition: Index.h:49
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
void refs(const RefsRequest &, llvm::function_ref< void(const Ref &)>) const override
Finds all occurrences (e.g.
Definition: Index.cpp:68
void relations(const RelationsRequest &, llvm::function_ref< void(const SymbolID &, const Symbol &)>) const override
Definition: Index.cpp:72
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
bool fromJSON(const llvm::json::Value &Parameters, FuzzyFindRequest &Request)
Definition: Index.cpp:34
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::vector< std::string > ProximityPaths
Contextually relevant files (e.g.
Definition: Index.h:47
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
size_t estimateMemoryUsage() const override
Returns estimated size of index (in bytes).
Definition: Index.cpp:78
llvm::Optional< uint32_t > Limit
The number of top candidates to return.
Definition: Index.h:42
void reset(std::unique_ptr< SymbolIndex >)
Definition: Index.cpp:20
const SymbolIndex * Index
Definition: Dexp.cpp:84