clang-tools  16.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 "llvm/ADT/StringRef.h"
11 #include <limits>
12 
13 namespace clang {
14 namespace clangd {
15 
16 void SwapIndex::reset(std::unique_ptr<SymbolIndex> Index) {
17  // Keep the old index alive, so we don't destroy it under lock (may be slow).
18  std::shared_ptr<SymbolIndex> Pin;
19  {
20  std::lock_guard<std::mutex> Lock(Mutex);
21  Pin = std::move(this->Index);
22  this->Index = std::move(Index);
23  }
24 }
25 std::shared_ptr<SymbolIndex> SwapIndex::snapshot() const {
26  std::lock_guard<std::mutex> Lock(Mutex);
27  return Index;
28 }
29 
31  llvm::json::Path P) {
32  llvm::json::ObjectMapper O(Parameters, P);
33  int64_t Limit;
34  bool OK =
35  O && O.map("Query", Request.Query) && O.map("Scopes", Request.Scopes) &&
36  O.map("AnyScope", Request.AnyScope) && O.map("Limit", Limit) &&
37  O.map("RestrictForCodeCompletion", Request.RestrictForCodeCompletion) &&
38  O.map("ProximityPaths", Request.ProximityPaths) &&
39  O.map("PreferredTypes", Request.PreferredTypes);
40  if (OK && Limit <= std::numeric_limits<uint32_t>::max())
41  Request.Limit = Limit;
42  return OK;
43 }
44 
46  return llvm::json::Object{
47  {"Query", Request.Query},
48  {"Scopes", Request.Scopes},
49  {"AnyScope", Request.AnyScope},
50  {"Limit", Request.Limit},
51  {"RestrictForCodeCompletion", Request.RestrictForCodeCompletion},
52  {"ProximityPaths", Request.ProximityPaths},
53  {"PreferredTypes", Request.PreferredTypes},
54  };
55 }
56 
58  llvm::function_ref<void(const Symbol &)> CB) const {
59  return snapshot()->fuzzyFind(R, CB);
60 }
62  llvm::function_ref<void(const Symbol &)> CB) const {
63  return snapshot()->lookup(R, CB);
64 }
66  llvm::function_ref<void(const Ref &)> CB) const {
67  return snapshot()->refs(R, CB);
68 }
70  const RelationsRequest &R,
71  llvm::function_ref<void(const SymbolID &, const Symbol &)> CB) const {
72  return snapshot()->relations(R, CB);
73 }
74 
75 llvm::unique_function<IndexContents(llvm::StringRef) const>
77  // The index snapshot should outlive this method return value.
78  auto SnapShot = snapshot();
79  auto IndexedFiles = SnapShot->indexedFiles();
80  return [KeepAlive{std::move(SnapShot)},
81  IndexContainsFile{std::move(IndexedFiles)}](llvm::StringRef File) {
82  return IndexContainsFile(File);
83  };
84 }
85 
87  return snapshot()->estimateMemoryUsage();
88 }
89 
90 } // namespace clangd
91 } // namespace clang
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::SwapIndex::reset
void reset(std::unique_ptr< SymbolIndex >)
Definition: Index.cpp:16
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:36
clang::clangd::FuzzyFindRequest
Definition: Index.h:26
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:155
clang::clangd::FuzzyFindRequest::Query
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
clang::clangd::SwapIndex::relations
void relations(const RelationsRequest &, llvm::function_ref< void(const SymbolID &, const Symbol &)>) const override
Definition: Index.cpp:69
clang::clangd::toJSON
llvm::json::Value toJSON(const FuzzyFindRequest &Request)
Definition: Index.cpp:45
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:30
clang::clangd::IndexContents
IndexContents
Describes what data is covered by an index.
Definition: Index.h:93
clang::clangd::SwapIndex::refs
bool refs(const RefsRequest &, llvm::function_ref< void(const Ref &)>) const override
Finds all occurrences (e.g.
Definition: Index.cpp:65
clang::clangd::SwapIndex::indexedFiles
llvm::unique_function< IndexContents(llvm::StringRef) const > indexedFiles() const override
Definition: Index.cpp:76
clang::clangd::FuzzyFindRequest::Limit
llvm::Optional< uint32_t > Limit
The number of top candidates to return.
Definition: Index.h:42
Index
const SymbolIndex * Index
Definition: Dexp.cpp:98
clang::clangd::LookupRequest
Definition: Index.h:64
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:85
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:86
clang::clangd::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:39
Parameters
ArrayRef< const ParmVarDecl * > Parameters
Definition: AST.cpp:782
clang::clangd::RelationsRequest
Definition: Index.h:80
clang::clangd::FuzzyFindRequest::RestrictForCodeCompletion
bool RestrictForCodeCompletion
If set to true, only symbols for completion support will be considered.
Definition: Index.h:44
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:57
clang::clangd::RefsRequest
Definition: Index.h:68
clang::clangd::FuzzyFindRequest::ProximityPaths
std::vector< std::string > ProximityPaths
Contextually relevant files (e.g.
Definition: Index.h:47
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:61
Value
static constexpr bool Value
Definition: SuspiciousCallArgumentCheck.cpp:72
Path
std::vector< HeaderHandle > Path
Definition: PreprocessorTracker.cpp:525