Skip to content

Commit 90fd254

Browse files
authored
Merge pull request #66061 from hjyamauchi/newlines
Fix the new diagnostic formatter adding extra whitespace on Windows
2 parents 2bb08e3 + b419f68 commit 90fd254

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

lib/Driver/Compilation.cpp

Lines changed: 28 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,23 @@ namespace driver {
727731
: TaskFinishedResponse::StopExecution;
728732
}
729733

734+
#if defined(_WIN32)
735+
struct FileBinaryModeRAII {
736+
FileBinaryModeRAII(FILE *F) : F(F) {
737+
PrevMode = _setmode(_fileno(F), _O_BINARY);
738+
}
739+
~FileBinaryModeRAII() {
740+
_setmode(_fileno(F), PrevMode);
741+
}
742+
FILE *F;
743+
int PrevMode;
744+
};
745+
#else
746+
struct FileBinaryModeRAII {
747+
FileBinaryModeRAII(FILE *) {}
748+
};
749+
#endif
750+
730751
void processOutputOfFinishedProcess(ProcessId Pid, int ReturnCode,
731752
const Job *const FinishedCmd,
732753
StringRef Output,
@@ -739,8 +760,14 @@ namespace driver {
739760
case OutputLevel::Verbose:
740761
// Send the buffered output to stderr, though only if we
741762
// support getting buffered output.
742-
if (TaskQueue::supportsBufferingOutput())
763+
if (TaskQueue::supportsBufferingOutput()) {
764+
// Temporarily change stderr to binary mode to avoid double
765+
// LF -> CR LF conversions on the outputs from child
766+
// processes, which have already this conversion appplied.
767+
// This makes a difference only for Windows.
768+
FileBinaryModeRAII F(stderr);
743769
llvm::errs() << Output;
770+
}
744771
break;
745772
case OutputLevel::Parseable:
746773
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
// UNSUPPORTED: swift_swift_parser
10+
11+
// Check that there are no extra newlines between diagnostics lines
12+
13+
// CHECK: {{[=]+}} SOURCE_DIR{{[/\]+}}test{{[/\]+}}diagnostics{{[/\]+}}extra-newlines.swift:[[#LINE:]]:5
14+
// CHECK-NEXT: [[#LINE-1]] | func foo(a: Int, b: Int) {
15+
// CHECK-NEXT: [[#LINE]] | a + b
16+
// CHECK-NEXT: | ~ ~
17+
// CHECK-NEXT: | ^ warning: result of operator '+' is unused
18+
// CHECK-NEXT: [[#LINE+1]] | }
19+
20+
func foo(a: Int, b: Int) {
21+
a + b
22+
}

0 commit comments

Comments
 (0)