clang-tools  11.0.0
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 "index/Index.h"
11 
12 namespace clang {
13 namespace clangd {
14 
15 void runAddDocument(ClangdServer &Server, PathRef File,
16  llvm::StringRef Contents, llvm::StringRef Version,
17  WantDiagnostics WantDiags, bool ForceRebuild) {
18  Server.addDocument(File, Contents, Version, WantDiags, ForceRebuild);
19  if (!Server.blockUntilIdleForTest())
20  llvm_unreachable("not idle after addDocument");
21 }
22 
23 namespace {
24 /// A helper that waits for async callbacks to fire and exposes their result in
25 /// the output variable. Intended to be used in the following way:
26 /// T Result;
27 /// someAsyncFunc(Param1, Param2, /*Callback=*/capture(Result));
28 template <typename T> struct CaptureProxy {
29  CaptureProxy(llvm::Optional<T> &Target) : Target(&Target) {
30  assert(!Target.hasValue());
31  }
32 
33  CaptureProxy(const CaptureProxy &) = delete;
34  CaptureProxy &operator=(const CaptureProxy &) = delete;
35  // We need move ctor to return a value from the 'capture' helper.
36  CaptureProxy(CaptureProxy &&Other) : Target(Other.Target) {
37  Other.Target = nullptr;
38  }
39  CaptureProxy &operator=(CaptureProxy &&) = delete;
40 
41  operator llvm::unique_function<void(T)>() && {
42  assert(!Future.valid() && "conversion to callback called multiple times");
43  Future = Promise.get_future();
44  return [Promise = std::move(Promise)](T Value) mutable {
45  Promise.set_value(std::make_shared<T>(std::move(Value)));
46  };
47  }
48 
49  ~CaptureProxy() {
50  if (!Target)
51  return;
52  assert(Future.valid() && "conversion to callback was not called");
53  assert(!Target->hasValue());
54  Target->emplace(std::move(*Future.get()));
55  }
56 
57 private:
58  llvm::Optional<T> *Target;
59  // Using shared_ptr to workaround compilation errors with MSVC.
60  // MSVC only allows default-constructible and copyable objects as future<>
61  // arguments.
62  std::promise<std::shared_ptr<T>> Promise;
63  std::future<std::shared_ptr<T>> Future;
64 };
65 
66 template <typename T> CaptureProxy<T> capture(llvm::Optional<T> &Target) {
67  return CaptureProxy<T>(Target);
68 }
69 } // namespace
70 
71 llvm::Expected<CodeCompleteResult>
74  llvm::Optional<llvm::Expected<CodeCompleteResult>> Result;
75  Server.codeComplete(File, Pos, Opts, capture(Result));
76  return std::move(*Result);
77 }
78 
79 llvm::Expected<SignatureHelp> runSignatureHelp(ClangdServer &Server,
80  PathRef File, Position Pos) {
81  llvm::Optional<llvm::Expected<SignatureHelp>> Result;
82  Server.signatureHelp(File, Pos, 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<FileEdits> runRename(ClangdServer &Server, PathRef File,
101  Position Pos, llvm::StringRef NewName,
102  const RenameOptions &RenameOpts) {
103  llvm::Optional<llvm::Expected<FileEdits>> Result;
104  Server.rename(File, Pos, NewName, RenameOpts, capture(Result));
105  return std::move(*Result);
106 }
107 
108 llvm::Expected<tooling::Replacements>
109 runFormatFile(ClangdServer &Server, PathRef File, StringRef Code) {
110  llvm::Optional<llvm::Expected<tooling::Replacements>> Result;
111  Server.formatFile(File, Code, capture(Result));
112  return std::move(*Result);
113 }
114 
115 std::string runDumpAST(ClangdServer &Server, PathRef File) {
116  llvm::Optional<std::string> Result;
117  Server.dumpAST(File, capture(Result));
118  return std::move(*Result);
119 }
120 
121 SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query) {
122  FuzzyFindRequest Req;
123  Req.Query = std::string(Query);
124  Req.AnyScope = true;
125  return runFuzzyFind(Index, Req);
126 }
127 
130  Index.fuzzyFind(Req, [&](const Symbol &Sym) { Builder.insert(Sym); });
131  return std::move(Builder).build();
132 }
133 
135  RefsRequest Req;
136  Req.IDs = {ID};
137  RefSlab::Builder Slab;
138  Index.refs(Req, [&](const Ref &S) { Slab.insert(ID, S); });
139  return std::move(Slab).build();
140 }
141 
142 llvm::Expected<std::vector<SelectionRange>>
144  const std::vector<Position> &Pos) {
145  llvm::Optional<llvm::Expected<std::vector<SelectionRange>>> Result;
146  Server.semanticRanges(File, Pos, capture(Result));
147  return std::move(*Result);
148 }
149 
150 llvm::Expected<llvm::Optional<clangd::Path>>
152  llvm::Optional<llvm::Expected<llvm::Optional<clangd::Path>>> Result;
153  Server.switchSourceHeader(File, capture(Result));
154  return std::move(*Result);
155 }
156 
157 } // namespace clangd
158 } // namespace clang
clang::clangd::runFormatFile
llvm::Expected< tooling::Replacements > runFormatFile(ClangdServer &Server, PathRef File, StringRef Code)
Definition: SyncAPI.cpp:109
WantDiags
WantDiagnostics WantDiags
Definition: TUScheduler.cpp:323
clang::clangd::runFuzzyFind
SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query)
Definition: SyncAPI.cpp:121
clang::clangd::runCodeComplete
llvm::Expected< CodeCompleteResult > runCodeComplete(ClangdServer &Server, PathRef File, Position Pos, clangd::CodeCompleteOptions Opts)
Definition: SyncAPI.cpp:72
Index.h
clang::clangd::RefSlab::Builder
RefSlab::Builder is a mutable container that can 'freeze' to RefSlab.
Definition: Ref.h:128
Contents
llvm::StringRef Contents
Definition: DraftStoreTests.cpp:22
clang::clangd::ClangdServer
Manages a collection of source files and derived data (ASTs, indexes), and provides language-aware fe...
Definition: ClangdServer.h:63
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:104
clang::clangd::runAddDocument
void runAddDocument(ClangdServer &Server, PathRef File, llvm::StringRef Contents, llvm::StringRef Version, WantDiagnostics WantDiags, bool ForceRebuild)
Definition: SyncAPI.cpp:15
clang::clangd::FuzzyFindRequest
Definition: Index.h:26
clang::clangd::FuzzyFindRequest::Query
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
clang::clangd::WantDiagnostics
WantDiagnostics
Determines whether diagnostics should be generated for a file snapshot.
Definition: TUScheduler.h:48
clang::clangd::runSemanticRanges
llvm::Expected< std::vector< SelectionRange > > runSemanticRanges(ClangdServer &Server, PathRef File, const std::vector< Position > &Pos)
Definition: SyncAPI.cpp:143
clang::clangd::getRefs
RefSlab getRefs(const SymbolIndex &Index, SymbolID ID)
Definition: SyncAPI.cpp:134
clang::clangd::Position
Definition: Protocol.h:144
Code
std::string Code
Definition: FindTargetTests.cpp:67
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
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::runSwitchHeaderSource
llvm::Expected< llvm::Optional< clangd::Path > > runSwitchHeaderSource(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:151
clang::clangd::RenameOptions
Definition: Rename.h:29
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:68
clang::clangd::CodeCompleteOptions
Definition: CodeComplete.h:44
Index
const SymbolIndex * Index
Definition: Dexp.cpp:95
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:85
clang::clangd::runDumpAST
std::string runDumpAST(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:115
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:23
RenameOpts
RenameOptions RenameOpts
Definition: ClangdLSPServerTests.cpp:68
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::runSignatureHelp
llvm::Expected< SignatureHelp > runSignatureHelp(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:79
clang::clangd::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:39
Pos
Position Pos
Definition: SourceCode.cpp:649
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:31
clang::clangd::RefsRequest
Definition: Index.h:67
clang::clangd::CompletionItemKind::Value
clang::clangd::runRename
llvm::Expected< FileEdits > runRename(ClangdServer &Server, PathRef File, Position Pos, llvm::StringRef NewName, const RenameOptions &RenameOpts)
Definition: SyncAPI.cpp:100
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.