clang-tools  14.0.0git
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 "support/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 
35  llvm::json::Path P) {
36  llvm::json::ObjectMapper O(Parameters, P);
37  int64_t Limit;
38  bool OK =
39  O && O.map("Query", Request.Query) && O.map("Scopes", Request.Scopes) &&
40  O.map("AnyScope", Request.AnyScope) && O.map("Limit", Limit) &&
41  O.map("RestrictForCodeCompletion", Request.RestrictForCodeCompletion) &&
42  O.map("ProximityPaths", Request.ProximityPaths) &&
43  O.map("PreferredTypes", Request.PreferredTypes);
44  if (OK && Limit <= std::numeric_limits<uint32_t>::max())
45  Request.Limit = Limit;
46  return OK;
47 }
48 
50  return llvm::json::Object{
51  {"Query", Request.Query},
52  {"Scopes", Request.Scopes},
53  {"AnyScope", Request.AnyScope},
54  {"Limit", Request.Limit},
55  {"RestrictForCodeCompletion", Request.RestrictForCodeCompletion},
56  {"ProximityPaths", Request.ProximityPaths},
57  {"PreferredTypes", Request.PreferredTypes},
58  };
59 }
60 
62  llvm::function_ref<void(const Symbol &)> CB) const {
63  return snapshot()->fuzzyFind(R, CB);
64 }
66  llvm::function_ref<void(const Symbol &)> CB) const {
67  return snapshot()->lookup(R, CB);
68 }
70  llvm::function_ref<void(const Ref &)> CB) const {
71  return snapshot()->refs(R, CB);
72 }
74  const RelationsRequest &R,
75  llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const {
76  return snapshot()->relations(R, CB);
77 }
78 
79 llvm::unique_function<IndexContents(llvm::StringRef) const>
81  // The index snapshot should outlive this method return value.
82  auto SnapShot = snapshot();
83  auto IndexedFiles = SnapShot->indexedFiles();
84  return [KeepAlive{std::move(SnapShot)},
85  IndexContainsFile{std::move(IndexedFiles)}](llvm::StringRef File) {
86  return IndexContainsFile(File);
87  };
88 }
89 
91  return snapshot()->estimateMemoryUsage();
92 }
93 
94 } // namespace clangd
95 } // namespace clang
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::SwapIndex::reset
void reset(std::unique_ptr< SymbolIndex >)
Definition: Index.cpp:20
Index.h
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::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::SwapIndex::relations
void relations(const RelationsRequest &, llvm::function_ref< void(const SymbolID &, const Symbol &)>) const override
Definition: Index.cpp:73
Parameters
std::vector< Parameter > Parameters
Definition: ExtractFunction.cpp:347
clang::clangd::toJSON
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
Definition: Index.cpp:49
Logger.h
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::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
Index
const SymbolIndex * Index
Definition: Dexp.cpp:99
clang::clangd::LookupRequest
Definition: Index.h:65
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:87
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::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:40
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
Value
static constexpr bool Value
Definition: SuspiciousCallArgumentCheck.cpp:72
Path
std::vector< HeaderHandle > Path
Definition: PreprocessorTracker.cpp:525