Skip to content

Commit 66e272c

Browse files
committed
Fix the new diagnostic formatter adding extra whitespace on Windows
The swift driver emits extra newlines because it applies the LF -> CRLF conversions again on the outpt from the child processes. Fix it by using the binary mode when outputting the output from the child processes. Fixes: #64413
1 parent 5e26417 commit 66e272c

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

lib/Driver/Compilation.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949

5050
#include <fstream>
5151
#include <signal.h>
52+
#if defined(_WIN32)
53+
#include <fcntl.h>
54+
#include <io.h>
55+
#endif
5256

5357
#define DEBUG_TYPE "batch-mode"
5458

@@ -727,6 +731,15 @@ namespace driver {
727731
: TaskFinishedResponse::StopExecution;
728732
}
729733

734+
#if defined(_WIN32)
735+
struct ChangeStderrToBinaryMode {
736+
ChangeStderrToBinaryMode() { _setmode(_fileno(stderr), _O_BINARY); }
737+
~ChangeStderrToBinaryMode() { _setmode(_fileno(stderr), _O_TEXT); }
738+
};
739+
#else
740+
struct ChangeStderrToBinaryMode {};
741+
#endif
742+
730743
void processOutputOfFinishedProcess(ProcessId Pid, int ReturnCode,
731744
const Job *const FinishedCmd,
732745
StringRef Output,
@@ -739,8 +752,14 @@ namespace driver {
739752
case OutputLevel::Verbose:
740753
// Send the buffered output to stderr, though only if we
741754
// support getting buffered output.
742-
if (TaskQueue::supportsBufferingOutput())
755+
if (TaskQueue::supportsBufferingOutput()) {
756+
// Temporarily change stderr to binary mode to avoid double
757+
// LF -> CR LF conversions on the outputs from child
758+
// processes, which have already this conversion appplied.
759+
// This makes a difference only for Windows.
760+
ChangeStderrToBinaryMode c;
743761
llvm::errs() << Output;
762+
}
744763
break;
745764
case OutputLevel::Parseable:
746765
emitParseableOutputForEachFinishedJob(Pid, ReturnCode, Output,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// Intentionally blank.
2+

test/diagnostics/extra-newlines.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Check that there are no extra newlines in the driver diagnostics output due to
2+
// double LF -> CR LF conversions on child processes output on Windows.
3+
//
4+
// Compile more than one source files to trigger the use of the driver
5+
// task queue and child processes.
6+
7+
// RUN: %line-directive %s %S/Inputs/extra-newlines-2.swift -- %swiftc_driver -c -diagnostic-style=swift -no-color-diagnostics %s %S/Inputs/extra-newlines-2.swift -module-name Foo 2>&1 | %FileCheck %s
8+
9+
// Check that there are no extra newlines between diagnostics lines
10+
11+
// CHECK: {{[=]+}} SOURCE_DIR{{[/\]+}}test{{[/\]+}}diagnostics{{[/\]+}}extra-newlines.swift:[[#LINE:]]:5
12+
// CHECK-NEXT: [[#LINE-1]] | func foo(a: Int, b: Int) {
13+
// CHECK-NEXT: [[#LINE]] | a + b
14+
// CHECK-NEXT: | ~ ~
15+
// CHECK-NEXT: | ^ warning: result of operator '+' is unused
16+
// CHECK-NEXT: [[#LINE+1]] | }
17+
18+
func foo(a: Int, b: Int) {
19+
a + b
20+
}

0 commit comments

Comments
 (0)