Skip to content

Commit 3f0e69f

Browse files
authored
Merge pull request #25992 from broadwaylamb/no-color-diagnostics
[Driver] Implement -no-color-diagnostics flag
2 parents b72301c + f5ec348 commit 3f0e69f

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

include/swift/Option/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ def serialize_diagnostics_path_EQ: Joined<["-"], "serialize-diagnostics-path=">,
303303
def color_diagnostics : Flag<["-"], "color-diagnostics">,
304304
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
305305
HelpText<"Print diagnostics in color">;
306+
def no_color_diagnostics : Flag<["-"], "no-color-diagnostics">,
307+
Flags<[FrontendOption, DoesNotAffectIncrementalBuild]>,
308+
HelpText<"Do not print diagnostics in color">;
306309

307310
def module_cache_path : Separate<["-"], "module-cache-path">,
308311
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,

lib/Driver/ToolChains.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -172,12 +172,18 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
172172
arguments.push_back(inputArgs.MakeArgString(OI.SDKPath));
173173
}
174174

175+
if (llvm::sys::Process::StandardErrHasColors()) {
176+
arguments.push_back("-color-diagnostics");
177+
}
178+
175179
inputArgs.AddAllArgs(arguments, options::OPT_I);
176180
inputArgs.AddAllArgs(arguments, options::OPT_F, options::OPT_Fsystem);
177181

178182
inputArgs.AddLastArg(arguments, options::OPT_AssertConfig);
179183
inputArgs.AddLastArg(arguments, options::OPT_autolink_force_load);
180-
inputArgs.AddLastArg(arguments, options::OPT_color_diagnostics);
184+
inputArgs.AddLastArg(arguments,
185+
options::OPT_color_diagnostics,
186+
options::OPT_no_color_diagnostics);
181187
inputArgs.AddLastArg(arguments, options::OPT_fixit_all);
182188
inputArgs.AddLastArg(arguments,
183189
options::OPT_warn_swift3_objc_inference_minimal,
@@ -260,9 +266,6 @@ static void addCommonFrontendArgs(const ToolChain &TC, const OutputInfo &OI,
260266
// Pass through any subsystem flags.
261267
inputArgs.AddAllArgs(arguments, options::OPT_Xllvm);
262268
inputArgs.AddAllArgs(arguments, options::OPT_Xcc);
263-
264-
if (llvm::sys::Process::StandardErrHasColors())
265-
arguments.push_back("-color-diagnostics");
266269
}
267270

268271
static void addRuntimeLibraryFlags(const OutputInfo &OI,

lib/Frontend/CompilerInvocation.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -26,6 +26,7 @@
2626
#include "llvm/Support/FileSystem.h"
2727
#include "llvm/Support/LineIterator.h"
2828
#include "llvm/Support/Path.h"
29+
#include "llvm/Support/Process.h"
2930

3031
using namespace swift;
3132
using namespace llvm::opt;
@@ -659,7 +660,10 @@ static bool ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
659660
Opts.SkipDiagnosticPasses |= Args.hasArg(OPT_disable_diagnostic_passes);
660661
Opts.ShowDiagnosticsAfterFatalError |=
661662
Args.hasArg(OPT_show_diagnostics_after_fatal);
662-
Opts.UseColor |= Args.hasArg(OPT_color_diagnostics);
663+
Opts.UseColor |=
664+
Args.hasFlag(OPT_color_diagnostics,
665+
OPT_no_color_diagnostics,
666+
/*Default=*/llvm::sys::Process::StandardErrHasColors());
663667
Opts.FixitCodeForAllDiagnostics |= Args.hasArg(OPT_fixit_all);
664668
Opts.SuppressWarnings |= Args.hasArg(OPT_suppress_warnings);
665669
Opts.WarningsAsErrors |= Args.hasArg(OPT_warnings_as_errors);

lib/Frontend/PrintingDiagnosticConsumer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -112,9 +112,9 @@ void PrintingDiagnosticConsumer::handleDiagnostic(
112112
llvm::raw_svector_ostream Out(Text);
113113
DiagnosticEngine::formatDiagnosticText(Out, FormatString, FormatArgs);
114114
}
115-
115+
116116
auto Msg = SM.GetMessage(Loc, SMKind, Text, Ranges, FixIts);
117-
rawSM.PrintMessage(out, Msg);
117+
rawSM.PrintMessage(out, Msg, ForceColors);
118118
}
119119

120120
llvm::SMDiagnostic

test/Driver/color-diagnostics.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
// RUN: not %target-swiftc_driver -color-diagnostics -emit-executable -o %t %s 2>&1 | %FileCheck %s
1+
// RUN: not %target-swiftc_driver -color-diagnostics -emit-executable -o %t %s 2>&1 \
2+
// RUN: | %FileCheck -check-prefix=CHECK-CD %s
3+
// CHECK-CD: [0m1 = 2{{$}}
4+
5+
// RUN: not %target-swiftc_driver -no-color-diagnostics -emit-executable -o %t %s 2>&1 \
6+
// RUN: | %FileCheck -check-prefix=CHECK-NCD --match-full-lines %s
7+
// CHECK-NCD: 1 = 2
28

3-
// CHECK: [0m1 = 2{{$}}
49
1 = 2

0 commit comments

Comments
 (0)