Skip to content

Fix the new diagnostic formatter adding extra whitespace on Windows #66061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion lib/Driver/Compilation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@

#include <fstream>
#include <signal.h>
#if defined(_WIN32)
#include <fcntl.h>
#include <io.h>
#endif

#define DEBUG_TYPE "batch-mode"

Expand Down Expand Up @@ -727,6 +731,23 @@ namespace driver {
: TaskFinishedResponse::StopExecution;
}

#if defined(_WIN32)
struct FileBinaryModeRAII {
FileBinaryModeRAII(FILE *F) : F(F) {
PrevMode = _setmode(_fileno(F), _O_BINARY);
}
~FileBinaryModeRAII() {
_setmode(_fileno(F), PrevMode);
}
FILE *F;
int PrevMode;
};
#else
struct FileBinaryModeRAII {
FileBinaryModeRAII(FILE *) {}
};
#endif

void processOutputOfFinishedProcess(ProcessId Pid, int ReturnCode,
const Job *const FinishedCmd,
StringRef Output,
Expand All @@ -739,8 +760,14 @@ namespace driver {
case OutputLevel::Verbose:
// Send the buffered output to stderr, though only if we
// support getting buffered output.
if (TaskQueue::supportsBufferingOutput())
if (TaskQueue::supportsBufferingOutput()) {
// Temporarily change stderr to binary mode to avoid double
// LF -> CR LF conversions on the outputs from child
// processes, which have already this conversion appplied.
// This makes a difference only for Windows.
FileBinaryModeRAII F(stderr);
llvm::errs() << Output;
}
break;
case OutputLevel::Parseable:
emitParseableOutputForEachFinishedJob(Pid, ReturnCode, Output,
Expand Down
2 changes: 2 additions & 0 deletions test/diagnostics/Inputs/extra-newlines-2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Intentionally blank.

22 changes: 22 additions & 0 deletions test/diagnostics/extra-newlines.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Check that there are no extra newlines in the driver diagnostics output due to
// double LF -> CR LF conversions on child processes output on Windows.
//
// Compile more than one source files to trigger the use of the driver
// task queue and child processes.

// 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

// UNSUPPORTED: swift_swift_parser

// Check that there are no extra newlines between diagnostics lines

// CHECK: {{[=]+}} SOURCE_DIR{{[/\]+}}test{{[/\]+}}diagnostics{{[/\]+}}extra-newlines.swift:[[#LINE:]]:5
// CHECK-NEXT: [[#LINE-1]] | func foo(a: Int, b: Int) {
// CHECK-NEXT: [[#LINE]] | a + b
// CHECK-NEXT: | ~ ~
// CHECK-NEXT: | ^ warning: result of operator '+' is unused
// CHECK-NEXT: [[#LINE+1]] | }

func foo(a: Int, b: Int) {
a + b
}