clang-tools 10.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, WantDiagnostics WantDiags) {
17 Server.addDocument(File, Contents, WantDiags);
18 if (!Server.blockUntilIdleForTest())
19 llvm_unreachable("not idle after addDocument");
20 }
21
22 namespace {
23 /// A helper that waits for async callbacks to fire and exposes their result in
24 /// the output variable. Intended to be used in the following way:
25 /// T Result;
26 /// someAsyncFunc(Param1, Param2, /*Callback=*/capture(Result));
27 template <typename T> struct CaptureProxy {
28 CaptureProxy(llvm::Optional<T> &Target) : Target(&Target) {
29 assert(!Target.hasValue());
30 }
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->hasValue());
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>
71 runCodeComplete(ClangdServer &Server, PathRef File, Position Pos,
72 clangd::CodeCompleteOptions Opts) {
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 llvm::Optional<llvm::Expected<SignatureHelp>> Result;
81 Server.signatureHelp(File, Pos, capture(Result));
82 return std::move(*Result);
83 }
84
85 llvm::Expected<std::vector<LocatedSymbol>>
86 runLocateSymbolAt(ClangdServer &Server, PathRef File, Position Pos) {
87 llvm::Optional<llvm::Expected<std::vector<LocatedSymbol>>> Result;
88 Server.locateSymbolAt(File, Pos, capture(Result));
89 return std::move(*Result);
90 }
91
92 llvm::Expected<std::vector<DocumentHighlight>>
93 runFindDocumentHighlights(ClangdServer &Server, PathRef File, Position Pos) {
94 llvm::Optional<llvm::Expected<std::vector<DocumentHighlight>>> Result;
95 Server.findDocumentHighlights(File, Pos, capture(Result));
96 return std::move(*Result);
97 }
98
99 llvm::Expected<FileEdits> runRename(ClangdServer &Server, PathRef File,
100 Position Pos, llvm::StringRef NewName) {
101 llvm::Optional<llvm::Expected<FileEdits>> Result;
102 Server.rename(File, Pos, NewName, /*WantFormat=*/false, capture(Result));
103 return std::move(*Result);
104 }
105
106 std::string runDumpAST(ClangdServer &Server, PathRef File) {
107 llvm::Optional<std::string> Result;
108 Server.dumpAST(File, capture(Result));
109 return std::move(*Result);
110 }
111
112 llvm::Expected<std::vector<SymbolInformation>>
113 runWorkspaceSymbols(ClangdServer &Server, llvm::StringRef Query, int Limit) {
114 llvm::Optional<llvm::Expected<std::vector<SymbolInformation>>> Result;
115 Server.workspaceSymbols(Query, Limit, capture(Result));
116 return std::move(*Result);
117 }
118
119 llvm::Expected<std::vector<DocumentSymbol>>
120 runDocumentSymbols(ClangdServer &Server, PathRef File) {
121 llvm::Optional<llvm::Expected<std::vector<DocumentSymbol>>> Result;
122 Server.documentSymbols(File, capture(Result));
123 return std::move(*Result);
124 }
125
126 SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query) {
127 FuzzyFindRequest Req;
128 Req.Query = Query;
129 Req.AnyScope = true;
130 return runFuzzyFind(Index, Req);
131 }
132
133 SymbolSlab runFuzzyFind(const SymbolIndex &Index, const FuzzyFindRequest &Req) {
134 SymbolSlab::Builder Builder;
135 Index.fuzzyFind(Req, [&](const Symbol &Sym) { Builder.insert(Sym); });
136 return std::move(Builder).build();
137 }
138
139 RefSlab getRefs(const SymbolIndex &Index, SymbolID ID) {
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<Range>>
148 runSemanticRanges(ClangdServer &Server, PathRef File, Position Pos) {
149 llvm::Optional<llvm::Expected<std::vector<Range>>> Result;
150 Server.semanticRanges(File, Pos, capture(Result));
151 return std::move(*Result);
152 }
153
154 llvm::Expected<llvm::Optional<clangd::Path>>
155 runSwitchHeaderSource(ClangdServer &Server, PathRef File) {
156 llvm::Optional<llvm::Expected<llvm::Optional<clangd::Path>>> Result;
157 Server.switchSourceHeader(File, capture(Result));
158 return std::move(*Result);
159 }
160
161 } // namespace clangd
162 } // namespace clang
Limit
int Limit
Definition: FindSymbolsTests.cpp:71
clang::clangd::WantDiagnostics
WantDiagnostics
Determines whether diagnostics should be generated for a file snapshot.
Definition: TUScheduler.h:47
clang::clangd::SymbolSlab
An immutable symbol container that stores a set of symbols.
Definition: Symbol.h:177
clang::clangd::RefsRequest::IDs
llvm::DenseSet< SymbolID > IDs
Definition: Index.h:68
clang::clangd::ClangdServer::addDocument
void addDocument(PathRef File, StringRef Contents, WantDiagnostics WD=WantDiagnostics::Auto)
Add a File to the list of tracked C++ files or update the contents if File is already tracked...
Definition: ClangdServer.cpp:157
clang::clangd::FuzzyFindRequest::AnyScope
bool AnyScope
If set to true, allow symbols from any scope.
Definition: Index.h:39
clang::clangd::runSemanticRanges
llvm::Expected< std::vector< Range > > runSemanticRanges(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:148
Contents
llvm::StringRef Contents
Definition: CodeComplete.cpp:1021
clang::clangd::ClangdServer::codeComplete
void codeComplete(PathRef File, Position Pos, const clangd::CodeCompleteOptions &Opts, Callback< CodeCompleteResult > CB)
Run code completion for File at Pos.
Definition: ClangdServer.cpp:186
clang::clangd::runCodeComplete
llvm::Expected< CodeCompleteResult > runCodeComplete(ClangdServer &Server, PathRef File, Position Pos, clangd::CodeCompleteOptions Opts)
Definition: SyncAPI.cpp:71
clang::clangd::runDumpAST
std::string runDumpAST(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:106
clang::clangd::RefSlab
An efficient structure of storing large set of symbol references in memory.
Definition: Ref.h:69
Index.h
clang::clangd::runSignatureHelp
llvm::Expected< SignatureHelp > runSignatureHelp(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:78
clang::clangd::runDocumentSymbols
llvm::Expected< std::vector< DocumentSymbol > > runDocumentSymbols(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:120
clang::clangd::runLocateSymbolAt
llvm::Expected< std::vector< LocatedSymbol > > runLocateSymbolAt(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:86
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::Ref
Represents a symbol occurrence in the source file.
Definition: Ref.h:52
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...
clang::clangd::RefsRequest
Definition: Index.h:67
clang::clangd::Position
Definition: Protocol.h:127
clang::clangd::PathRef
llvm::StringRef PathRef
A typedef to represent a ref to file path.
Definition: Path.h:23
clang::clangd::SymbolSlab::Builder::insert
void insert(const Symbol &S)
Adds a symbol, overwriting any existing one with the same ID.
Definition: Symbol.cpp:50
clang::clangd::ClangdServer::blockUntilIdleForTest
LLVM_NODISCARD bool blockUntilIdleForTest(llvm::Optional< double > TimeoutSeconds=10)
Definition: ClangdServer.cpp:631
clang::clangd::FuzzyFindRequest
Definition: Index.h:26
clang::clangd::ClangdServer::findDocumentHighlights
void findDocumentHighlights(PathRef File, Position Pos, Callback< std::vector< DocumentHighlight >> CB)
Get document highlights for a given position.
Definition: ClangdServer.cpp:505
clang::clangd::runSwitchHeaderSource
llvm::Expected< llvm::Optional< clangd::Path > > runSwitchHeaderSource(ClangdServer &Server, PathRef File)
Definition: SyncAPI.cpp:155
clang::clangd::SymbolSlab::Builder
SymbolSlab::Builder is a mutable container that can &#39;freeze&#39; to SymbolSlab.
Definition: Symbol.h:199
clang::clangd::runFuzzyFind
SymbolSlab runFuzzyFind(const SymbolIndex &Index, llvm::StringRef Query)
Definition: SyncAPI.cpp:126
clang::clangd::FuzzyFindRequest::Query
std::string Query
A query string for the fuzzy find.
Definition: Index.h:29
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::CodeCompleteOptions
Definition: CodeComplete.h:39
clang::clangd::ClangdServer::switchSourceHeader
void switchSourceHeader(PathRef Path, Callback< llvm::Optional< clangd::Path >> CB)
Switch to a corresponding source file when given a header file, and vice versa.
Definition: ClangdServer.cpp:467
clang::clangd::ClangdServer::dumpAST
void dumpAST(PathRef File, llvm::unique_function< void(std::string)> Callback)
Only for testing purposes.
Definition: ClangdServer.cpp:435
clang::clangd::ClangdServer::documentSymbols
void documentSymbols(StringRef File, Callback< std::vector< DocumentSymbol >> CB)
Retrieve the symbols within the specified file.
Definition: ClangdServer.cpp:568
Pos
Position Pos
Definition: SourceCode.cpp:772
clang::clangd::RefSlab::Builder
RefSlab::Builder is a mutable container that can &#39;freeze&#39; to RefSlab.
Definition: Ref.h:93
Builder
CodeCompletionBuilder Builder
Definition: CodeCompletionStringsTests.cpp:35
clang::clangd::Symbol
The class presents a C++ symbol, e.g.
Definition: Symbol.h:36
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::runWorkspaceSymbols
llvm::Expected< std::vector< SymbolInformation > > runWorkspaceSymbols(ClangdServer &Server, llvm::StringRef Query, int Limit)
Definition: SyncAPI.cpp:113
Server
ClangdServer Server
Definition: FindSymbolsTests.cpp:70
clang::clangd::runFindDocumentHighlights
llvm::Expected< std::vector< DocumentHighlight > > runFindDocumentHighlights(ClangdServer &Server, PathRef File, Position Pos)
Definition: SyncAPI.cpp:93
clang::clangd::runRename
llvm::Expected< FileEdits > runRename(ClangdServer &Server, PathRef File, Position Pos, llvm::StringRef NewName)
Definition: SyncAPI.cpp:99
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:35
SyncAPI.h
clang::clangd::ClangdServer::locateSymbolAt
void locateSymbolAt(PathRef File, Position Pos, Callback< std::vector< LocatedSymbol >> CB)
Find declaration/definition locations of symbol at a specified position.
Definition: ClangdServer.cpp:455
clang::clangd::ClangdServer
Manages a collection of source files and derived data (ASTs, indexes), and provides language-aware fe...
Definition: ClangdServer.h:80
clang::clangd::ClangdServer::semanticRanges
void semanticRanges(PathRef File, Position Pos, Callback< std::vector< Range >> CB)
Get semantic ranges around a specified position in a file.
Definition: ClangdServer.cpp:603
clang::clangd::ClangdServer::rename
void rename(PathRef File, Position Pos, llvm::StringRef NewName, bool WantFormat, Callback< FileEdits > CB)
Rename all occurrences of the symbol at the Pos in File to NewName.
Definition: ClangdServer.cpp:340
clang::clangd::CompletionItemKind::File
clang::clangd::ClangdServer::workspaceSymbols
void workspaceSymbols(StringRef Query, int Limit, Callback< std::vector< SymbolInformation >> CB)
Retrieve the top symbols from the workspace matching a query.
Definition: ClangdServer.cpp:557
clang::clangd::CompletionItemKind::Value
clang::clangd::ClangdServer::signatureHelp
void signatureHelp(PathRef File, Position Pos, Callback< SignatureHelp > CB)
Provide signature help for File at Pos.
Definition: ClangdServer.cpp:247
clang::clangd::getRefs
RefSlab getRefs(const SymbolIndex &Index, SymbolID ID)
Definition: SyncAPI.cpp:139
clang::clangd::runAddDocument
void runAddDocument(ClangdServer &Server, PathRef File, llvm::StringRef Contents, WantDiagnostics WantDiags)
Definition: SyncAPI.cpp:15
Index
const SymbolIndex * Index
Definition: Dexp.cpp:84
clang::clangd::SymbolID
Definition: SymbolID.h:31