From: Nico Rieck <nico.rieck@gmail.com>
Date: Wed, 11 Sep 2013 00:38:02 +0000 (+0000)
Subject: Add -fansi-escape-codes option
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=2956ef43da9c0e7bce8fae4a015255a2c19458bb;p=clang

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 2dc6068dca..73f6a67560 100644
--- a/docs/UsersManual.rst
+++ b/docs/UsersManual.rst
@@ -235,6 +235,11 @@ output format of the diagnostics that it generates.
                 ^
                 //
 
+**-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 26a6e216ea..90e9faca22 100644
--- a/include/clang/Driver/Options.td
+++ b/include/clang/Driver/Options.td
@@ -370,6 +370,8 @@ def fcolor_diagnostics : Flag<["-"], "fcolor-diagnostics">, Group<f_Group>, Flag
   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 79cdb042a9..d72358dca0 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -3370,6 +3370,9 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
       (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 ce4fdc32d2..669ea5de3e 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 @@ bool clang::ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args,
   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 af869af86c..ebf614eeb1 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"