Skip to content

Commit e2ebd2b

Browse files
committed
[SourceKit] Flush llvm::outs() before printing to STDOUT_FILENO in soucekitd-test
Otherwise, we get non-deterministic ordering of results printed through `llvm::outs()` and `STDOUT_FILENO`.
1 parent 2f19d18 commit e2ebd2b

File tree

3 files changed

+33
-27
lines changed

3 files changed

+33
-27
lines changed

test/SourceKit/CompileNotifications/cursor-info.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: %sourcekitd-test -req=track-compiles == -req=cursor %s -offset=0 -- %s | %FileCheck %s -check-prefix=COMPILE_1 --enable-yaml-compatibility
2+
// COMPILE_1: <empty cursor info; internal diagnostic: "Unable to resolve cursor info.">
23
// COMPILE_1: {
34
// COMPILE_1: key.notification: source.notification.compile-will-start,
45
// COMPILE_1: key.filepath: "SOURCE_DIR{{.*}}cursor-info.swift",
@@ -8,6 +9,5 @@
89
// COMPILE_1: key.notification: source.notification.compile-did-finish,
910
// COMPILE_1: key.compileid: [[CID1]]
1011
// COMPILE_1: }
11-
// COMPILE_1: <empty cursor info; internal diagnostic: "Unable to resolve cursor info.">
1212
// COMPILE_1-NOT: compile-will-start
1313
// COMPILE_1-NOT: compile-did-finish

tools/SourceKit/tools/complete-test/complete-test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,7 @@ class ResponsePrinter {
568568
static void printResponse(sourcekitd_response_t resp, bool raw, bool structure,
569569
unsigned indentation) {
570570
if (raw) {
571+
llvm::outs().flush();
571572
sourcekitd_response_description_dump_filedesc(resp, STDOUT_FILENO);
572573
return;
573574
}

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

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,16 @@ struct NotificationBuffer {
170170
};
171171
static NotificationBuffer notificationBuffer;
172172

173+
static void printRawResponse(sourcekitd_response_t resp) {
174+
llvm::outs().flush();
175+
sourcekitd_response_description_dump_filedesc(resp, STDOUT_FILENO);
176+
}
177+
178+
static void printRawVariant(sourcekitd_variant_t obj) {
179+
llvm::outs().flush();
180+
sourcekitd_variant_description_dump_filedesc(obj, STDOUT_FILENO);
181+
}
182+
173183
static void syncNotificationsWithService() {
174184
// Send TestNotification request, then wait for the notification. This ensures
175185
// that all notifications previously posted on the service side have been
@@ -192,9 +202,8 @@ static void printBufferedNotifications(bool syncWithService = true) {
192202
if (syncWithService) {
193203
syncNotificationsWithService();
194204
}
195-
notificationBuffer.handleNotifications([](sourcekitd_response_t note) {
196-
sourcekitd_response_description_dump_filedesc(note, STDOUT_FILENO);
197-
});
205+
notificationBuffer.handleNotifications(
206+
[](sourcekitd_response_t note) { printRawResponse(note); });
198207
}
199208

200209
struct skt_args {
@@ -443,7 +452,7 @@ static int handleJsonRequestPath(StringRef QueryPath, const TestOptions &Opts) {
443452
sourcekitd_response_t Resp = sendRequestSync(Req, Opts);
444453
auto Error = sourcekitd_response_is_error(Resp);
445454
if (Opts.PrintResponse) {
446-
sourcekitd_response_description_dump_filedesc(Resp, STDOUT_FILENO);
455+
printRawResponse(Resp);
447456
}
448457
return Error ? 1 : 0;
449458
}
@@ -1256,7 +1265,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
12561265
free(json);
12571266

12581267
} else if (Opts.PrintRawResponse) {
1259-
sourcekitd_response_description_dump_filedesc(Resp, STDOUT_FILENO);
1268+
printRawResponse(Resp);
12601269

12611270
} else {
12621271
sourcekitd_variant_t Info = sourcekitd_response_get_value(Resp);
@@ -1279,7 +1288,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
12791288
case SourceKitRequest::Edit:
12801289
if (Opts.Length == 0 && Opts.ReplaceText->empty()) {
12811290
// Length=0, replace="" is a nop and will not trigger sema.
1282-
sourcekitd_response_description_dump_filedesc(Resp, STDOUT_FILENO);
1291+
printRawResponse(Resp);
12831292
} else {
12841293
getSemanticInfo(Info, SourceFile);
12851294
KeepResponseAlive = true;
@@ -1309,7 +1318,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
13091318
case SourceKitRequest::ConformingMethodList:
13101319
case SourceKitRequest::DependencyUpdated:
13111320
case SourceKitRequest::Diagnostics:
1312-
sourcekitd_response_description_dump_filedesc(Resp, STDOUT_FILENO);
1321+
printRawResponse(Resp);
13131322
break;
13141323

13151324
case SourceKitRequest::RelatedIdents:
@@ -1380,7 +1389,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
13801389
}
13811390
case SourceKitRequest::SyntaxMap:
13821391
case SourceKitRequest::Structure:
1383-
sourcekitd_response_description_dump_filedesc(Resp, STDOUT_FILENO);
1392+
printRawResponse(Resp);
13841393
if (Opts.ReplaceText.hasValue()) {
13851394
unsigned Offset =
13861395
resolveFromLineCol(Opts.Line, Opts.Col, SourceFile, Opts.VFSFiles);
@@ -1405,7 +1414,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
14051414
!Opts.UsedSema);
14061415

14071416
sourcekitd_response_t EdResp = sendRequestSync(EdReq, Opts);
1408-
sourcekitd_response_description_dump_filedesc(EdResp, STDOUT_FILENO);
1417+
printRawResponse(EdResp);
14091418
sourcekitd_response_dispose(EdResp);
14101419
sourcekitd_request_release(EdReq);
14111420
}
@@ -1440,7 +1449,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
14401449
}
14411450

14421451
sourcekitd_response_t FmtResp = sendRequestSync(Fmt, Opts);
1443-
sourcekitd_response_description_dump_filedesc(FmtResp, STDOUT_FILENO);
1452+
printRawResponse(FmtResp);
14441453
sourcekitd_response_dispose(FmtResp);
14451454
sourcekitd_request_release(Fmt);
14461455
}
@@ -1449,7 +1458,7 @@ static bool handleResponse(sourcekitd_response_t Resp, const TestOptions &Opts,
14491458
case SourceKitRequest::ExpandPlaceholder:
14501459
if (Opts.Length) {
14511460
// Single placeholder by location.
1452-
sourcekitd_response_description_dump_filedesc(Resp, STDOUT_FILENO);
1461+
printRawResponse(Resp);
14531462
} else {
14541463
// Expand all placeholders.
14551464
expandPlaceholders(SourceBuf.get(), llvm::outs());
@@ -1523,13 +1532,12 @@ static void getSemanticInfo(sourcekitd_variant_t Info, StringRef Filename) {
15231532
}
15241533

15251534
static int printAnnotations() {
1526-
sourcekitd_variant_description_dump_filedesc(LatestSemaAnnotations,
1527-
STDOUT_FILENO);
1535+
printRawVariant(LatestSemaAnnotations);
15281536
return 0;
15291537
}
15301538

15311539
static int printDiags() {
1532-
sourcekitd_variant_description_dump_filedesc(LatestSemaDiags, STDOUT_FILENO);
1540+
printRawVariant(LatestSemaDiags);
15331541
return 0;
15341542
}
15351543

@@ -2106,16 +2114,14 @@ static void printFoundUSR(sourcekitd_variant_t Info,
21062114
static void printNormalizedDocComment(sourcekitd_variant_t Info) {
21072115
sourcekitd_variant_t Source =
21082116
sourcekitd_variant_dictionary_get_value(Info, KeySourceText);
2109-
sourcekitd_variant_description_dump_filedesc(Source, STDOUT_FILENO);
2117+
printRawVariant(Source);
21102118
}
21112119

21122120
static void printDocInfo(sourcekitd_variant_t Info, StringRef Filename) {
21132121
const char *text =
21142122
sourcekitd_variant_dictionary_get_string(Info, KeySourceText);
2115-
llvm::raw_fd_ostream OS(STDOUT_FILENO, /*shouldClose=*/false);
21162123
if (text) {
2117-
OS << text << '\n';
2118-
OS.flush();
2124+
llvm::outs() << text << '\n';
21192125
}
21202126

21212127
sourcekitd_variant_t annotations =
@@ -2126,11 +2132,11 @@ static void printDocInfo(sourcekitd_variant_t Info, StringRef Filename) {
21262132
sourcekitd_variant_t diags =
21272133
sourcekitd_variant_dictionary_get_value(Info, KeyDiagnostics);
21282134

2129-
sourcekitd_variant_description_dump_filedesc(annotations, STDOUT_FILENO);
2130-
sourcekitd_variant_description_dump_filedesc(entities, STDOUT_FILENO);
2135+
printRawVariant(annotations);
2136+
printRawVariant(entities);
21312137

21322138
if (sourcekitd_variant_get_type(diags) != SOURCEKITD_VARIANT_TYPE_NULL)
2133-
sourcekitd_variant_description_dump_filedesc(diags, STDOUT_FILENO);
2139+
printRawVariant(diags);
21342140
}
21352141

21362142
static void checkTextIsASCII(const char *Text) {
@@ -2278,8 +2284,7 @@ static void printInterfaceGen(sourcekitd_variant_t Info, bool CheckASCII) {
22782284
sourcekitd_variant_dictionary_get_string(Info, KeySourceText);
22792285

22802286
if (text) {
2281-
llvm::raw_fd_ostream OS(STDOUT_FILENO, /*shouldClose=*/false);
2282-
OS << text << '\n';
2287+
llvm::outs() << text << '\n';
22832288
}
22842289

22852290
if (CheckASCII) {
@@ -2288,13 +2293,13 @@ static void printInterfaceGen(sourcekitd_variant_t Info, bool CheckASCII) {
22882293

22892294
sourcekitd_variant_t syntaxmap =
22902295
sourcekitd_variant_dictionary_get_value(Info, KeySyntaxMap);
2291-
sourcekitd_variant_description_dump_filedesc(syntaxmap, STDOUT_FILENO);
2296+
printRawVariant(syntaxmap);
22922297
sourcekitd_variant_t annotations =
22932298
sourcekitd_variant_dictionary_get_value(Info, KeyAnnotations);
2294-
sourcekitd_variant_description_dump_filedesc(annotations, STDOUT_FILENO);
2299+
printRawVariant(annotations);
22952300
sourcekitd_variant_t structure =
22962301
sourcekitd_variant_dictionary_get_value(Info, KeySubStructure);
2297-
sourcekitd_variant_description_dump_filedesc(structure, STDOUT_FILENO);
2302+
printRawVariant(structure);
22982303
}
22992304

23002305
static void printRelatedIdents(sourcekitd_variant_t Info, StringRef Filename,

0 commit comments

Comments
 (0)