Add -fansi-escape-codes option
Some build systems use pipes for stdin/stderr. On nix-ish platforms colored
output can be forced by -fcolor-diagnostics. On Windows this option has
no effect in these cases because LLVM uses the console API (which only
operates on the console buffer) even if a console wrapper capable of
interpreting ANSI escape codes is used.
The -fansi-escape-codes option allows switching from the console API to
ANSI escape codes. It has no effect on other platforms.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190464 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/docs/UsersManual.rst b/docs/UsersManual.rst
index 2dc6068..73f6a67 100644
--- a/docs/UsersManual.rst
+++ b/docs/UsersManual.rst
@@ -235,6 +235,11 @@
^
//
+**-fansi-escape-codes**
+ Controls whether ANSI escape codes are used instead of the Windows Console
+ API to output colored diagnostics. This option is only used on Windows and
+ defaults to off.
+
.. option:: -fdiagnostics-format=clang/msvc/vi
Changes diagnostic output format to better match IDEs and command line tools.
diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td
index 26a6e21..90e9fac 100644
--- a/include/clang/Driver/Options.td
+++ b/include/clang/Driver/Options.td
@@ -370,6 +370,8 @@
HelpText<"Use colors in diagnostics">;
def fdiagnostics_color : Flag<["-"], "fdiagnostics-color">, Group<f_Group>;
def fdiagnostics_color_EQ : Joined<["-"], "fdiagnostics-color=">, Group<f_Group>;
+def fansi_escape_codes : Flag<["-"], "fansi-escape-codes">, Group<f_Group>, Flags<[CC1Option]>,
+ HelpText<"Use ANSI escape codes for diagnostics">;
def fcomment_block_commands : CommaJoined<["-"], "fcomment-block-commands=">, Group<f_clang_Group>, Flags<[CC1Option]>,
HelpText<"Treat each comma separated argument in <arg> as a documentation comment block command">,
MetaVarName<"<arg>">;
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index 79cdb04..d72358d 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -3370,6 +3370,9 @@
(ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()))
CmdArgs.push_back("-fcolor-diagnostics");
+ if (Args.hasArg(options::OPT_fansi_escape_codes))
+ CmdArgs.push_back("-fansi-escape-codes");
+
if (!Args.hasFlag(options::OPT_fshow_source_location,
options::OPT_fno_show_source_location))
CmdArgs.push_back("-fno-show-source-location");
diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp
index ce4fdc3..669ea5d 100644
--- a/lib/Frontend/CompilerInvocation.cpp
+++ b/lib/Frontend/CompilerInvocation.cpp
@@ -32,6 +32,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/Host.h"
#include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
#include "llvm/Support/system_error.h"
#include <sys/stat.h>
using namespace clang;
@@ -543,6 +544,8 @@
Opts.ShowLocation = !Args.hasArg(OPT_fno_show_source_location);
Opts.ShowOptionNames = Args.hasArg(OPT_fdiagnostics_show_option);
+ llvm::sys::Process::UseANSIEscapeCodes(Args.hasArg(OPT_fansi_escape_codes));
+
// Default behavior is to not to show note include stacks.
Opts.ShowNoteIncludeStack = false;
if (Arg *A = Args.getLastArg(OPT_fdiagnostics_show_note_include_stack,
diff --git a/test/Driver/color-diagnostics.c b/test/Driver/color-diagnostics.c
index af869af..ebf614e 100644
--- a/test/Driver/color-diagnostics.c
+++ b/test/Driver/color-diagnostics.c
@@ -51,3 +51,7 @@
// RUN: %clang -fcolor-diagnostics -fdiagnostics-color=auto -### -c %s 2>&1 \
// RUN: | FileCheck --check-prefix=CHECK-CD_DCE_AUTO_S %s
// CHECK-CD_DCE_AUTO_S-NOT: clang{{.*}}" "-fcolor-diagnostics"
+
+// RUN: %clang -fansi-escape-codes -### -c %s 2>&1 \
+// RUN: | FileCheck --check-prefix=CHECK-AEC %s
+// CHECK-AEC: clang{{.*}}" "-fansi-escape-codes"