clang-tools  14.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) {
31  assert(!Target.hasValue());
32  }
33 
34  CaptureProxy(const CaptureProxy &) = delete;
35  CaptureProxy &operator=(const CaptureProxy &) = delete;
36  // We need move ctor to return a value from the 'capture' helper.
37  CaptureProxy(CaptureProxy &&Other) : Target(Other.Target) {
38  Other.Target = nullptr;
39  }
40  CaptureProxy &operator=(CaptureProxy &&) = delete;
41 
42  operator llvm::unique_function<void(T)>() && {
43  assert(!Future.valid() && "conversion to callback called multiple times");
44  Future = Promise.get_future();
45  return [Promise = std::move(Promise)](T Value) mutable {
46  Promise.set_value(std::make_shared<T>(std::move(Value)));
47  };
48  }
49 
50  ~CaptureProxy() {
51  if (!Target)
52  return;
53  assert(Future.valid() && "conversion to callback was not called");
54  assert(!Target->hasValue());
55  Target->emplace(std::move(*Future.get()));
56  }
57 
58 private:
59  llvm::Optional<T> *Target;
60  // Using shared_ptr to workaround compilation errors with MSVC.
61  // MSVC only allows default-constructible and copyable objects as future<>
62  // arguments.
63  std::promise<std::shared_ptr<T>> Promise;
64  std::future<std::shared_ptr<T>> Future;
65 };
66 
67 template <typename T> CaptureProxy<T> capture(llvm::Optional<T> &Target) {
68  return CaptureProxy<T>(Target);
69 }
70 } // namespace
71 
72 llvm::Expected<CodeCompleteResult>
75  llvm::Optional<llvm::Expected<CodeCompleteResult>> Result;
76  Server.codeComplete(File, Pos, Opts, capture(Result));
77  return std::move(*Result);
78 }
79 
80 llvm::Expected<SignatureHelp> runSignatureHelp(ClangdServer &Server,
81  PathRef File, Position Pos,
82  MarkupKind DocumentationFormat) {
83  llvm::Optional<llvm::Expected<SignatureHelp>> Result;
84  Server.signatureHelp(File, Pos, DocumentationFormat, capture(Result));
85  return std::move(*Result);
86 }
87 
88 llvm::Expected<std::vector<LocatedSymbol>>
90  llvm::Optional<llvm::Expected<std::vector<LocatedSymbol>>> Result;
91  Server.locateSymbolAt(File, Pos, capture(Result));
92  return std::move(*Result);
93 }
94 
95 llvm::Expected<std::vector<DocumentHighlight>>
97  llvm::Optional<llvm::Expected<std::vector<DocumentHighlight>>> Result;
98  Server.findDocumentHighlights(File, Pos, capture(Result));
99  return std::move(*Result);
100 }
101 
102 llvm::Expected<RenameResult> runRename(ClangdServer &Server, PathRef File,
103  Position Pos, llvm::StringRef NewName,
104  const RenameOptions &RenameOpts) {
105  llvm::Optional<llvm::Expected<RenameResult>> Result;
106  Server.rename(File, Pos, NewName, RenameOpts, capture(Result));
107  return std::move(*Result);
108 }
109 
110 llvm::Expected<RenameResult>
112  llvm::Optional<std::string> NewName,
113  const RenameOptions &RenameOpts) {
114  llvm::Optional<llvm::Expected<RenameResult>> Result;
115  Server.prepareRename(File, Pos, NewName, RenameOpts, capture(Result));
116  return std::move(*Result);
117 }
118 
119 llvm::Expected<tooling::Replacements>
120 runFormatFile(ClangdServer &Server, PathRef File, llvm::Optional<Range> Rng) {
121  llvm::Optional<llvm::Expected<tooling::Replacements>> Result;
122  Server.formatFile(File, Rng, capture(Result));
123  return std::move(*Result);
124 }
125 
126 SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query) {
127  FuzzyFindRequest Req;
128  Req.Query = std::string(Query);
129  Req.AnyScope = true;
130  return runFuzzyFind(Index, Req);
131 }
132 
135  Index.fuzzyFind(Req, [&](const Symbol &Sym) { Builder.insert(Sym); });
136  return std::move(Builder).build();
137 }
138 
140  RefsRequest Req;
141  Req.IDs = {ID};
142  RefSlab::Builder Slab;
143  Index.refs(Req, [&](const Ref &S) { Slab.insert(ID, S); });
144  return std::move(Slab).build();
145 }
146 
147 llvm::Expected<std::vector<SelectionRange>>
149  const std::vector<Position> &Pos) {
150  llvm::Optional<llvm::Expected<std::vector<SelectionRange>>> Result;
151  Server.semanticRanges(File, Pos, capture(Result));
152  return std::move(*Result);
153 }
154 
155 llvm::Expected<llvm::Optional<clangd::Path>>
157  llvm::Optional<llvm::Expected<llvm::Optional<clangd::Path>>> Result;
158  Server.switchSourceHeader(File, capture(Result));
159  return std::move(*Result);
160 }
161 
162 llvm::Error runCustomAction(ClangdServer &Server, PathRef File,
163  llvm::function_ref<void(InputsAndAST)> Action) {
164  llvm::Error Result = llvm::Error::success();
165  Notification Done;
166  Server.customAction(File, "Custom", [&](llvm::Expected<InputsAndAST> AST) {
167  if (!AST)
168  Result = AST.takeError();
169  else
170  Action(*AST);
171  Done.notify();
172  });
173  Done.wait();
174  return Result;
175 }
176 
177 } // namespace clangd
178 } // namespace clang
WantDiags
WantDiagnostics WantDiags
Definition: TUScheduler.cpp:459
clang::clangd::runFuzzyFind
SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query)
Definition: SyncAPI.cpp:126
clang::clangd::InputsAndAST
Definition: TUScheduler.h:38
clang::clangd::runCodeComplete
llvm::Expected< CodeCompleteResult > runCodeComplete(ClangdServer &Server, PathRef File, Position Pos, clangd::CodeCompleteOptions Opts)
Definition: SyncAPI.cpp:73
Index.h
clang::clangd::RefSlab::Builder
RefSlab::Builder is a mutable container that can 'freeze' to RefSlab.
Definition: Ref.h:134
clang::clangd::ClangdServer
Manages a collection of source files and derived data (ASTs, indexes), and provides language-aware fe...
Definition: ClangdServer.h:62
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:110
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:27
Action
llvm::unique_function< void()> Action
Definition: TUScheduler.cpp:604
clang::clangd::runPrepareRename
llvm::Expected< RenameResult > runPrepareRename(ClangdServer &Server, PathRef File, Position Pos, llvm::Optional< std::string > NewName, const RenameOptions &RenameOpts)
Definition: SyncAPI.cpp:111
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:30
clang::clangd::WantDiagnostics
WantDiagnostics
Determines whether diagnostics should be generated for a file snapshot.
Definition: TUScheduler.h:53
clang::clangd::runSemanticRanges
llvm::Expected< std::vector< SelectionRange > > runSemanticRanges(ClangdServer &Server, PathRef File, const std::vector< Position > &Pos)
Definition: SyncAPI.cpp:148
Protocol.h
clang::clangd::getRefs
RefSlab getRefs(const SymbolIndex &Index, SymbolID ID)
Definition: SyncAPI.cpp:139
clang::clangd::Position
Definition: Protocol.h:153
clang::clangd::Notification
A threadsafe flag that is initially clear.
Definition: Threading.h:28
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:89
clang::clangd::runFormatFile
llvm::Expected< tooling::Replacements > runFormatFile(ClangdServer &Server, PathRef File, llvm::Optional< Range > Rng)
Definition: SyncAPI.cpp:120
clang::clangd::runSwitchHeaderSource
llvm::Expected< llvm::Optional< clangd::Path > > runSwitchHeaderSource(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:156
clang::clangd::RenameOptions
Definition: Rename.h:24
clang::clangd::runFindDocumentHighlights
llvm::Expected< std::vector< DocumentHighlight > > runFindDocumentHighlights(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:96
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:70
clang::clangd::runRename
llvm::Expected< RenameResult > runRename(ClangdServer &Server, PathRef File, Position Pos, llvm::StringRef NewName, const RenameOptions &RenameOpts)
Definition: SyncAPI.cpp:102
clang::clangd::CodeCompleteOptions
Definition: CodeComplete.h:45
Index
const SymbolIndex * Index
Definition: Dexp.cpp:99
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:111
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:87
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:80
clang::clangd::runCustomAction
llvm::Error runCustomAction(ClangdServer &Server, PathRef File, llvm::function_ref< void(InputsAndAST)> Action)
Definition: SyncAPI.cpp:162
clang::clangd::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:40
Pos
Position Pos
Definition: SourceCode.cpp:658
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:69
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.