25
25
#include " llvm/Support/raw_ostream.h"
26
26
#include " llvm/Support/Signals.h"
27
27
#include " llvm/Support/FileSystem.h"
28
+ #include " llvm/Support/FormatVariadic.h"
28
29
#include < fstream>
29
30
#include < unistd.h>
30
31
#include < sys/param.h>
@@ -274,7 +275,35 @@ static bool readPopularAPIList(StringRef filename,
274
275
return false ;
275
276
}
276
277
277
- static int handleJsonRequestPath (StringRef QueryPath) {
278
+ namespace {
279
+ class PrintingTimer {
280
+ std::string desc;
281
+ llvm::sys::TimePoint<> start;
282
+ llvm::raw_ostream &OS;
283
+ public:
284
+ PrintingTimer (std::string desc, llvm::raw_ostream &OS = llvm::errs())
285
+ : desc(std::move(desc)), start(std::chrono::system_clock::now()), OS(OS) {
286
+ }
287
+ ~PrintingTimer () {
288
+ std::chrono::duration<float , std::milli> delta (
289
+ std::chrono::system_clock::now () - start);
290
+ OS << desc << " : " << llvm::formatv (" {0:ms+f3}" , delta) << " \n " ;
291
+ }
292
+ };
293
+ }
294
+
295
+ // / Wrapper for sourcekitd_send_request_sync that handles printing options.
296
+ static sourcekitd_response_t sendRequestSync (sourcekitd_object_t req,
297
+ const TestOptions &opts) {
298
+ if (opts.PrintRequest )
299
+ sourcekitd_request_description_dump (req);
300
+ Optional<PrintingTimer> timer;
301
+ if (opts.timeRequest )
302
+ timer.emplace (" request time" );
303
+ return sourcekitd_send_request_sync (req);
304
+ }
305
+
306
+ static int handleJsonRequestPath (StringRef QueryPath, const TestOptions &Opts) {
278
307
auto Buffer = getBufferForFilename (QueryPath)->getBuffer ();
279
308
char *Err = nullptr ;
280
309
auto Req = sourcekitd_request_create_from_yaml (Buffer.data (), &Err);
@@ -284,13 +313,17 @@ static int handleJsonRequestPath(StringRef QueryPath) {
284
313
free (Err);
285
314
return 1 ;
286
315
}
287
- sourcekitd_request_description_dump (Req);
288
- sourcekitd_response_t Resp = sourcekitd_send_request_sync (Req);
316
+
317
+ sourcekitd_response_t Resp = sendRequestSync (Req, Opts );
289
318
auto Error = sourcekitd_response_is_error (Resp);
290
- sourcekitd_response_description_dump_filedesc (Resp, STDOUT_FILENO);
319
+ if (Opts.PrintResponse ) {
320
+ sourcekitd_response_description_dump_filedesc (Resp, STDOUT_FILENO);
321
+ }
291
322
return Error ? 1 : 0 ;
292
323
}
293
324
325
+ static int handleTestInvocation (TestOptions Opts, TestOptions &InitOpts);
326
+
294
327
static int handleTestInvocation (ArrayRef<const char *> Args,
295
328
TestOptions &InitOpts) {
296
329
@@ -305,12 +338,23 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
305
338
if (Opts.parseArgs (Args.slice (0 , Optargc)))
306
339
return 1 ;
307
340
308
- if (!Opts.JsonRequestPath .empty ())
309
- return handleJsonRequestPath (Opts.JsonRequestPath );
310
-
311
341
if (Optargc < Args.size ())
312
342
Opts.CompilerArgs = Args.slice (Optargc+1 );
313
343
344
+ assert (Opts.repeatRequest >= 1 );
345
+ for (unsigned i = 0 ; i < Opts.repeatRequest ; ++i) {
346
+ if (int ret = handleTestInvocation (Opts, InitOpts)) {
347
+ return ret;
348
+ }
349
+ }
350
+ return 0 ;
351
+ }
352
+
353
+ static int handleTestInvocation (TestOptions Opts, TestOptions &InitOpts) {
354
+
355
+ if (!Opts.JsonRequestPath .empty ())
356
+ return handleJsonRequestPath (Opts.JsonRequestPath , Opts);
357
+
314
358
if (Opts.Request == SourceKitRequest::DemangleNames ||
315
359
Opts.Request == SourceKitRequest::MangleSimpleClasses)
316
360
Opts.SourceFile .clear ();
@@ -526,7 +570,7 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
526
570
}
527
571
StringRef AllArgs = Text.substr (ArgStart + 1 , ArgEnd - ArgStart - 1 );
528
572
AllArgs.split (ArgPieces, ' :' );
529
- if (!Args .empty ()) {
573
+ if (!ArgPieces .empty ()) {
530
574
if (!ArgPieces.back ().empty ()) {
531
575
llvm::errs () << " Swift name is malformed.\n " ;
532
576
return 1 ;
@@ -804,11 +848,9 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
804
848
}
805
849
}
806
850
807
- if (Opts.PrintRequest )
808
- sourcekitd_request_description_dump (Req);
809
851
810
852
if (!Opts.isAsyncRequest ) {
811
- sourcekitd_response_t Resp = sourcekitd_send_request_sync (Req);
853
+ sourcekitd_response_t Resp = sendRequestSync (Req, Opts );
812
854
sourcekitd_request_release (Req);
813
855
return handleResponse (Resp, Opts, SourceFile, std::move (SourceBuf),
814
856
&InitOpts)
@@ -823,6 +865,9 @@ static int handleTestInvocation(ArrayRef<const char *> Args,
823
865
unsigned respIndex = asyncResponses.size ();
824
866
asyncResponses.push_back (std::move (info));
825
867
868
+ if (Opts.PrintRequest )
869
+ sourcekitd_request_description_dump (Req);
870
+
826
871
sourcekitd_send_request (Req, nullptr , ^(sourcekitd_response_t resp) {
827
872
auto &info = asyncResponses[respIndex];
828
873
info.response = resp;
@@ -848,6 +893,8 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
848
893
if (IsError) {
849
894
sourcekitd_response_description_dump (Resp);
850
895
896
+ } else if (!Opts.PrintResponse ) {
897
+ // Nothing.
851
898
} else if (Opts.PrintResponseAsJSON ) {
852
899
sourcekitd_variant_t Info = sourcekitd_response_get_value (Resp);
853
900
char *json = sourcekitd_variant_json_description_copy (Info);
@@ -988,7 +1035,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
988
1035
sourcekitd_request_dictionary_set_int64 (EdReq, KeySyntacticOnly,
989
1036
!Opts.UsedSema );
990
1037
991
- sourcekitd_response_t EdResp = sourcekitd_send_request_sync (EdReq);
1038
+ sourcekitd_response_t EdResp = sendRequestSync (EdReq, Opts );
992
1039
sourcekitd_response_description_dump_filedesc (EdResp, STDOUT_FILENO);
993
1040
sourcekitd_response_dispose (EdResp);
994
1041
sourcekitd_request_release (EdReq);
@@ -1023,7 +1070,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
1023
1070
sourcekitd_request_release (FO);
1024
1071
}
1025
1072
1026
- sourcekitd_response_t FmtResp = sourcekitd_send_request_sync (Fmt);
1073
+ sourcekitd_response_t FmtResp = sendRequestSync (Fmt, Opts );
1027
1074
sourcekitd_response_description_dump_filedesc (FmtResp, STDOUT_FILENO);
1028
1075
sourcekitd_response_dispose (FmtResp);
1029
1076
sourcekitd_request_release (Fmt);
0 commit comments