clang-tools  15.0.0git
SyncAPI.cpp
Go to the documentation of this file.
1 //===--- SyncAPI.cpp - Sync version of ClangdServer's API --------*- 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 "SyncAPI.h"
10 #include "Protocol.h"
11 #include "index/Index.h"
12 
13 namespace clang {
14 namespace clangd {
15 
16 void runAddDocument(ClangdServer &Server, PathRef File,
17  llvm::StringRef Contents, llvm::StringRef Version,
18  WantDiagnostics WantDiags, bool ForceRebuild) {
19  Server.addDocument(File, Contents, Version, WantDiags, ForceRebuild);
20  if (!Server.blockUntilIdleForTest())
21  llvm_unreachable("not idle after addDocument");
22 }
23 
24 namespace {
25 /// A helper that waits for async callbacks to fire and exposes their result in
26 /// the output variable. Intended to be used in the following way:
27 /// T Result;
28 /// someAsyncFunc(Param1, Param2, /*Callback=*/capture(Result));
29 template <typename T> struct CaptureProxy {
30  CaptureProxy(llvm::Optional<T> &Target) : Target(&Target) { assert(!Target); }
31 
32  CaptureProxy(const CaptureProxy &) = delete;
33  CaptureProxy &operator=(const CaptureProxy &) = delete;
34  // We need move ctor to return a value from the 'capture' helper.
35  CaptureProxy(CaptureProxy &&Other) : Target(Other.Target) {
36  Other.Target = nullptr;
37  }
38  CaptureProxy &operator=(CaptureProxy &&) = delete;
39 
40  operator llvm::unique_function<void(T)>() && {
41  assert(!Future.valid() && "conversion to callback called multiple times");
42  Future = Promise.get_future();
43  return [Promise = std::move(Promise)](T Value) mutable {
44  Promise.set_value(std::make_shared<T>(std::move(Value)));
45  };
46  }
47 
48  ~CaptureProxy() {
49  if (!Target)
50  return;
51  assert(Future.valid() && "conversion to callback was not called");
52  assert(!Target->has_value());
53  Target->emplace(std::move(*Future.get()));
54  }
55 
56 private:
57  llvm::Optional<T> *Target;
58  // Using shared_ptr to workaround compilation errors with MSVC.
59  // MSVC only allows default-constructible and copyable objects as future<>
60  // arguments.
61  std::promise<std::shared_ptr<T>> Promise;
62  std::future<std::shared_ptr<T>> Future;
63 };
64 
65 template <typename T> CaptureProxy<T> capture(llvm::Optional<T> &Target) {
66  return CaptureProxy<T>(Target);
67 }
68 } // namespace
69 
70 llvm::Expected<CodeCompleteResult>
73  llvm::Optional<llvm::Expected<CodeCompleteResult>> Result;
74  Server.codeComplete(File, Pos, Opts, capture(Result));
75  return std::move(*Result);
76 }
77 
78 llvm::Expected<SignatureHelp> runSignatureHelp(ClangdServer &Server,
79  PathRef File, Position Pos,
80  MarkupKind DocumentationFormat) {
81  llvm::Optional<llvm::Expected<SignatureHelp>> Result;
82  Server.signatureHelp(File, Pos, DocumentationFormat, capture(Result));
83  return std::move(*Result);
84 }
85 
86 llvm::Expected<std::vector<LocatedSymbol>>
88  llvm::Optional<llvm::Expected<std::vector<LocatedSymbol>>> Result;
89  Server.locateSymbolAt(File, Pos, capture(Result));
90  return std::move(*Result);
91 }
92 
93 llvm::Expected<std::vector<DocumentHighlight>>
95  llvm::Optional<llvm::Expected<std::vector<DocumentHighlight>>> Result;
96  Server.findDocumentHighlights(File, Pos, capture(Result));
97  return std::move(*Result);
98 }
99 
100 llvm::Expected<RenameResult> runRename(ClangdServer &Server, PathRef File,
101  Position Pos, llvm::StringRef NewName,
102  const RenameOptions &RenameOpts) {
103  llvm::Optional<llvm::Expected<RenameResult>> Result;
104  Server.rename(File, Pos, NewName, RenameOpts, capture(Result));
105  return std::move(*Result);
106 }
107 
108 llvm::Expected<RenameResult>
110  llvm::Optional<std::string> NewName,
111  const RenameOptions &RenameOpts) {
112  llvm::Optional<llvm::Expected<RenameResult>> Result;
113  Server.prepareRename(File, Pos, NewName, RenameOpts, capture(Result));
114  return std::move(*Result);
115 }
116 
117 llvm::Expected<tooling::Replacements>
118 runFormatFile(ClangdServer &Server, PathRef File, llvm::Optional<Range> Rng) {
119  llvm::Optional<llvm::Expected<tooling::Replacements>> Result;
120  Server.formatFile(File, Rng, capture(Result));
121  return std::move(*Result);
122 }
123 
124 SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query) {
125  FuzzyFindRequest Req;
126  Req.Query = std::string(Query);
127  Req.AnyScope = true;
128  return runFuzzyFind(Index, Req);
129 }
130 
133  Index.fuzzyFind(Req, [&](const Symbol &Sym) { Builder.insert(Sym); });
134  return std::move(Builder).build();
135 }
136 
138  RefsRequest Req;
139  Req.IDs = {ID};
140  RefSlab::Builder Slab;
141  Index.refs(Req, [&](const Ref &S) { Slab.insert(ID, S); });
142  return std::move(Slab).build();
143 }
144 
145 llvm::Expected<std::vector<SelectionRange>>
147  const std::vector<Position> &Pos) {
148  llvm::Optional<llvm::Expected<std::vector<SelectionRange>>> Result;
149  Server.semanticRanges(File, Pos, capture(Result));
150  return std::move(*Result);
151 }
152 
153 llvm::Expected<llvm::Optional<clangd::Path>>
155  llvm::Optional<llvm::Expected<llvm::Optional<clangd::Path>>> Result;
156  Server.switchSourceHeader(File, capture(Result));
157  return std::move(*Result);
158 }
159 
160 llvm::Error runCustomAction(ClangdServer &Server, PathRef File,
161  llvm::function_ref<void(InputsAndAST)> Action) {
162  llvm::Error Result = llvm::Error::success();
163  Notification Done;
164  Server.customAction(File, "Custom", [&](llvm::Expected<InputsAndAST> AST) {
165  if (!AST)
166  Result = AST.takeError();
167  else
168  Action(*AST);
169  Done.notify();
170  });
171  Done.wait();
172  return Result;
173 }
174 
175 } // namespace clangd
176 } // namespace clang
WantDiags
WantDiagnostics WantDiags
Definition: TUScheduler.cpp:494
clang::clangd::runFuzzyFind
SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query)
Definition: SyncAPI.cpp:124
clang::clangd::InputsAndAST
Definition: TUScheduler.h:37
clang::clangd::runCodeComplete
llvm::Expected< CodeCompleteResult > runCodeComplete(ClangdServer &Server, PathRef File, Position Pos, clangd::CodeCompleteOptions Opts)
Definition: SyncAPI.cpp:71
Index.h
clang::clangd::RefSlab::Builder
RefSlab::Builder is a mutable container that can 'freeze' to RefSlab.
Definition: Ref.h:132
clang::clangd::ClangdServer
Manages a collection of source files and derived data (ASTs, indexes), and provides language-aware fe...
Definition: ClangdServer.h:58
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:108
clang::clangd::runAddDocument
void runAddDocument(ClangdServer &Server, PathRef File, llvm::StringRef Contents, llvm::StringRef Version, WantDiagnostics WantDiags, bool ForceRebuild)
Definition: SyncAPI.cpp:16
clang::clangd::FuzzyFindRequest
Definition: Index.h:26
clang::clangd::runPrepareRename
llvm::Expected< RenameResult > runPrepareRename(ClangdServer &Server, PathRef File, Position Pos, llvm::Optional< std::string > NewName, const RenameOptions &RenameOpts)
Definition: SyncAPI.cpp:109
Target
std::string Target
Definition: QueryDriverDatabase.cpp:64
clang::clangd::FuzzyFindRequest::Query
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
Pos
size_t Pos
Definition: NoLintDirectiveHandler.cpp:97
clang::clangd::WantDiagnostics
WantDiagnostics
Determines whether diagnostics should be generated for a file snapshot.
Definition: TUScheduler.h:52
clang::clangd::runSemanticRanges
llvm::Expected< std::vector< SelectionRange > > runSemanticRanges(ClangdServer &Server, PathRef File, const std::vector< Position > &Pos)
Definition: SyncAPI.cpp:146
Protocol.h
clang::clangd::getRefs
RefSlab getRefs(const SymbolIndex &Index, SymbolID ID)
Definition: SyncAPI.cpp:137
clang::clangd::Position
Definition: Protocol.h:153
clang::clangd::Notification
A threadsafe flag that is initially clear.
Definition: Threading.h:90
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:36
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
SyncAPI.h
clang::clangd::runLocateSymbolAt
llvm::Expected< std::vector< LocatedSymbol > > runLocateSymbolAt(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:87
clang::clangd::runFormatFile
llvm::Expected< tooling::Replacements > runFormatFile(ClangdServer &Server, PathRef File, llvm::Optional< Range > Rng)
Definition: SyncAPI.cpp:118
clang::clangd::runSwitchHeaderSource
llvm::Expected< llvm::Optional< clangd::Path > > runSwitchHeaderSource(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:154
clang::clangd::RenameOptions
Definition: Rename.h:22
clang::clangd::runFindDocumentHighlights
llvm::Expected< std::vector< DocumentHighlight > > runFindDocumentHighlights(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:94
clang::clangd::RefSlab::Builder::insert
void insert(const SymbolID &ID, const Ref &S)
Adds a ref to the slab. Deep copy: Strings will be owned by the slab.
Definition: Ref.cpp:36
clang::clangd::RefsRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:69
clang::clangd::runRename
llvm::Expected< RenameResult > runRename(ClangdServer &Server, PathRef File, Position Pos, llvm::StringRef NewName, const RenameOptions &RenameOpts)
Definition: SyncAPI.cpp:100
clang::clangd::CodeCompleteOptions
Definition: CodeComplete.h:41
Index
const SymbolIndex * Index
Definition: Dexp.cpp:98
clang::clangd::SymbolIndex::refs
virtual bool refs(const RefsRequest &Req, llvm::function_ref< void(const Ref &)> Callback) const =0
Finds all occurrences (e.g.
clang::clangd::SymbolIndex
Interface for symbol indexes that can be used for searching or matching symbols among a set of symbol...
Definition: Index.h:113
clang::clangd::MarkupKind
MarkupKind
Definition: Protocol.h:392
ID
static char ID
Definition: Logger.cpp:74
clang::clangd::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:85
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:29
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::runSignatureHelp
llvm::Expected< SignatureHelp > runSignatureHelp(ClangdServer &Server, PathRef File, Position Pos, MarkupKind DocumentationFormat)
Definition: SyncAPI.cpp:78
clang::clangd::runCustomAction
llvm::Error runCustomAction(ClangdServer &Server, PathRef File, llvm::function_ref< void(InputsAndAST)> Action)
Definition: SyncAPI.cpp:160
clang::clangd::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:39
clang::clangd::SymbolSlab::Builder
SymbolSlab::Builder is a mutable container that can 'freeze' to SymbolSlab.
Definition: Symbol.h:200
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::SymbolID
Definition: SymbolID.h:32
clang::clangd::RefsRequest
Definition: Index.h:68
clang::clangd::SymbolIndex::fuzzyFind
virtual bool fuzzyFind(const FuzzyFindRequest &Req, llvm::function_ref< void(const Symbol &)> Callback) const =0
Matches symbols in the index fuzzily and applies Callback on each matched symbol before returning.
Action
FieldAction Action
Definition: MemberwiseConstructor.cpp:261