Skip to content

Commit fb26447

Browse files
committed
[SourceKit] Add an option to cancel async requests
1 parent 7d5ee83 commit fb26447

File tree

5 files changed

+49
-5
lines changed

5 files changed

+49
-5
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Check that we can cancel requests.
2+
// We need to wait a little bit after request scheduling and cancellation to make sure we are not cancelling the request before it got scheduled.
3+
4+
// RUN: not %sourcekitd-test -req=cursor -id=slow -async -pos=10:3 %s -- %s == \
5+
// RUN: -shell -- sleep 1 == \
6+
// RUN: -cancel=slow 2>&1 \
7+
// RUN: | %FileCheck %s
8+
9+
func foo(x: Invalid1, y: Invalid2) {
10+
x / y / x / y / x / y / x / y
11+
}
12+
13+
// CHECK: error response (Request Cancelled)

tools/SourceKit/tools/sourcekitd-test/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ def req : Separate<["-"], "req">,
1616
HelpText<"Request kind">;
1717
def req_EQ : Joined<["-"], "req=">, Alias<req>;
1818

19+
def id : Separate<["-"], "id">,
20+
HelpText<"If this is an async request, an ID that can be used to cancel the request">;
21+
def id_EQ : Joined<["-"], "id=">, Alias<id>;
22+
1923
def offset : Separate<["-"], "offset">,
2024
HelpText<"byte offset">;
2125
def offset_EQ : Joined<["-"], "offset=">, Alias<offset>;
@@ -156,6 +160,10 @@ def module_cache_path_EQ : Joined<["-"], "module-cache-path=">, Alias<module_cac
156160
def shell: Flag<["-"], "shell">,
157161
HelpText<"Run shell command">;
158162

163+
def cancel : Separate<["-"], "cancel">,
164+
HelpText<"Cancel all async requests with the given ID">;
165+
def cancel_EQ : Joined<["-"], "cancel=">, Alias<cancel>;
166+
159167
def help : Flag<["-", "--"], "help">,
160168
HelpText<"Display available options">;
161169

tools/SourceKit/tools/sourcekitd-test/TestOptions.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
274274
ModuleGroupName = InputArg->getValue();
275275
break;
276276

277+
case OPT_id:
278+
RequestId = InputArg->getValue();
279+
break;
280+
277281
case OPT_interested_usr:
278282
InterestedUSR = InputArg->getValue();
279283
break;
@@ -420,6 +424,10 @@ bool TestOptions::parseArgs(llvm::ArrayRef<const char *> Args) {
420424
ShellExecution = true;
421425
break;
422426

427+
case OPT_cancel:
428+
CancelRequest = InputArg->getValue();
429+
break;
430+
423431
case OPT_disable_implicit_concurrency_module_import:
424432
DisableImplicitConcurrencyModuleImport = true;
425433
break;

tools/SourceKit/tools/sourcekitd-test/TestOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ struct TestOptions {
103103
std::string ObjCName;
104104
std::string ObjCSelector;
105105
std::string Name;
106+
/// An ID that can be used to cancel this request.
107+
std::string RequestId;
108+
/// If not empty, all requests with this ID should be cancelled.
109+
std::string CancelRequest;
106110
bool CheckInterfaceIsASCII = false;
107111
bool UsedSema = false;
108112
bool PrintRequest = true;

tools/SourceKit/tools/sourcekitd-test/sourcekitd-test.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ struct AsyncResponseInfo {
143143
TestOptions options;
144144
std::string sourceFilename;
145145
std::unique_ptr<llvm::MemoryBuffer> sourceBuffer;
146+
sourcekitd_request_handle_t requestHandle;
146147
};
147148
} // end anonymous namespace
148149

@@ -480,6 +481,15 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
480481
if (Opts.ShellExecution)
481482
return performShellExecution(Opts.CompilerArgs);
482483

484+
if (!Opts.CancelRequest.empty()) {
485+
for (auto &asyncResponse : asyncResponses) {
486+
if (asyncResponse.options.RequestId == Opts.CancelRequest) {
487+
sourcekitd_cancel_request(asyncResponse.requestHandle);
488+
}
489+
}
490+
return 0;
491+
}
492+
483493
assert(Opts.repeatRequest >= 1);
484494
for (unsigned i = 0; i < Opts.repeatRequest; ++i) {
485495
if (int ret = handleTestInvocation(Opts, InitOpts)) {
@@ -1196,11 +1206,12 @@ static int handleTestInvocation(TestOptions Opts, TestOptions &InitOpts) {
11961206
if (Opts.PrintRequest)
11971207
sourcekitd_request_description_dump(Req);
11981208

1199-
sourcekitd_send_request(Req, nullptr, ^(sourcekitd_response_t resp) {
1200-
auto &info = asyncResponses[respIndex];
1201-
info.response = resp;
1202-
info.semaphore.signal(); // Ready to be handled!
1203-
});
1209+
sourcekitd_send_request(Req, &asyncResponses[respIndex].requestHandle,
1210+
^(sourcekitd_response_t resp) {
1211+
auto &info = asyncResponses[respIndex];
1212+
info.response = resp;
1213+
info.semaphore.signal(); // Ready to be handled!
1214+
});
12041215

12051216
#else
12061217
llvm::report_fatal_error(

0 commit comments

Comments
 (0)