From: Nico Weber Date: Wed, 17 Apr 2013 21:52:44 +0000 (+0000) Subject: Add support for gcc's spelling of -fcolor-diagnostics. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9753d462fabba1db75a977cb1f75ea66e9081120;p=clang Add support for gcc's spelling of -fcolor-diagnostics. See http://gcc.gnu.org/onlinedocs/gcc/Language-Independent-Options.html git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@179728 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Driver/Options.td b/include/clang/Driver/Options.td index f79a5badc6..3a5358a7e3 100644 --- a/include/clang/Driver/Options.td +++ b/include/clang/Driver/Options.td @@ -333,6 +333,8 @@ def fcatch_undefined_behavior : Flag<["-"], "fcatch-undefined-behavior">, Group< def fclasspath_EQ : Joined<["-"], "fclasspath=">, Group; def fcolor_diagnostics : Flag<["-"], "fcolor-diagnostics">, Group, Flags<[CC1Option]>, HelpText<"Use colors in diagnostics">; +def fdiagnostics_color : Flag<["-"], "fdiagnostics-color">, Group; +def fdiagnostics_color_EQ : Joined<["-"], "fdiagnostics-color=">, Group; def fcomment_block_commands : CommaJoined<["-"], "fcomment-block-commands=">, Group, Flags<[CC1Option]>, HelpText<"Treat each comma separated argument in as a documentation comment block command">, MetaVarName<"">; @@ -539,6 +541,7 @@ def fno_builtin : Flag<["-"], "fno-builtin">, Group, Flags<[CC1Option]> def fno_caret_diagnostics : Flag<["-"], "fno-caret-diagnostics">, Group, Flags<[CC1Option]>; def fno_color_diagnostics : Flag<["-"], "fno-color-diagnostics">, Group; +def fno_diagnostics_color : Flag<["-"], "fno-diagnostics-color">, Group; def fno_common : Flag<["-"], "fno-common">, Group, Flags<[CC1Option]>, HelpText<"Compile common globals like normal definitions">; def fno_constant_cfstrings : Flag<["-"], "fno-constant-cfstrings">, Group, diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 0ecd6f4146..1ea7c43efc 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -3200,9 +3200,42 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, // Color diagnostics are the default, unless the terminal doesn't support // them. - if (Args.hasFlag(options::OPT_fcolor_diagnostics, - options::OPT_fno_color_diagnostics, - llvm::sys::Process::StandardErrHasColors())) + // Support both clang's -f[no-]color-diagnostics and gcc's + // -f[no-]diagnostics-colors[=never|always|auto]. + enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto; + for (ArgList::const_iterator it = Args.begin(), ie = Args.end(); + it != ie; ++it) { + const Option &O = (*it)->getOption(); + if (!O.matches(options::OPT_fcolor_diagnostics) && + !O.matches(options::OPT_fdiagnostics_color) && + !O.matches(options::OPT_fno_color_diagnostics) && + !O.matches(options::OPT_fno_diagnostics_color) && + !O.matches(options::OPT_fdiagnostics_color_EQ)) + continue; + + (*it)->claim(); + if (O.matches(options::OPT_fcolor_diagnostics) || + O.matches(options::OPT_fdiagnostics_color)) { + ShowColors = Colors_On; + } else if (O.matches(options::OPT_fno_color_diagnostics) || + O.matches(options::OPT_fno_diagnostics_color)) { + ShowColors = Colors_Off; + } else { + assert(O.matches(options::OPT_fdiagnostics_color_EQ)); + StringRef value((*it)->getValue()); + if (value == "always") + ShowColors = Colors_On; + else if (value == "never") + ShowColors = Colors_Off; + else if (value == "auto") + ShowColors = Colors_Auto; + else + getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported) + << ("-fdiagnostics-color=" + value).str(); + } + } + if (ShowColors == Colors_On || + (ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors())) CmdArgs.push_back("-fcolor-diagnostics"); if (!Args.hasFlag(options::OPT_fshow_source_location, diff --git a/test/Driver/color-diagnostics.c b/test/Driver/color-diagnostics.c new file mode 100644 index 0000000000..deff5119a0 --- /dev/null +++ b/test/Driver/color-diagnostics.c @@ -0,0 +1,53 @@ +// RUN: %clang -fcolor-diagnostics -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=CD %s +// CHECK-CD: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fno-color-diagnostics -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=NCD %s +// CHECK-NCD-NOT: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fdiagnostics-color -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=DC %s +// CHECK-DC: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fno-diagnostics-color -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=NDC %s +// CHECK-NDC-NOT: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fdiagnostics-color=always -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=DCE_A %s +// CHECK-DCE_A: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fdiagnostics-color=never -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=DCE_N %s +// CHECK-DCE_N-NOT: clang{{.*}}" "-fcolor-diagnostics" + +// The test doesn't run in a PTY, so "auto" defaults to off. +// RUN: %clang -fdiagnostics-color=auto -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=DCE_AUTO %s +// CHECK-DCE_AUTO-NOT: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fdiagnostics-color=foo -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=DCE_FOO %s +// CHECK-DCE_FOO: error: the clang compiler does not support '-fdiagnostics-color=foo' + +// Check that the last flag wins. +// RUN: %clang -fno-color-diagnostics -fdiagnostics-color -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=NCD_DC_S %s +// CHECK-NCD_DC_S: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fcolor-diagnostics -fno-diagnostics-color -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=CD_NDC_S %s +// CHECK-CD_NDC_S-NOT: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fdiagnostics-color -fno-color-diagnostics -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=DC_NCD_S %s +// CHECK-DC_NCD_S-NOT: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fno-diagnostics-color -fcolor-diagnostics -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=NDC_CD_S %s +// CHECK-NDC_CD_S: clang{{.*}}" "-fcolor-diagnostics" + +// RUN: %clang -fcolor-diagnostics -fdiagnostics-color=auto -### -c %s 2>&1 \ +// RUN: | FileCheck --check-prefix=CD_DCE_AUTO_S %s +// CHECK-CD_DCE_AUTO_S-NOT: clang{{.*}}" "-fcolor-diagnostics"