Skip to content

Commit

Permalink
Add support for gcc's spelling of -fcolor-diagnostics.
Browse files Browse the repository at this point in the history
  • Loading branch information
nico committed Apr 17, 2013
1 parent b32f6bf commit 7e2da79
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ def fcatch_undefined_behavior : Flag<["-"], "fcatch-undefined-behavior">, Group<
def fclasspath_EQ : Joined<["-"], "fclasspath=">, Group<f_Group>;
def fcolor_diagnostics : Flag<["-"], "fcolor-diagnostics">, Group<f_Group>, Flags<[CC1Option]>,
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 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>">;
Expand Down Expand Up @@ -539,6 +541,7 @@ def fno_builtin : Flag<["-"], "fno-builtin">, Group<f_Group>, Flags<[CC1Option]>
def fno_caret_diagnostics : Flag<["-"], "fno-caret-diagnostics">, Group<f_Group>,
Flags<[CC1Option]>;
def fno_color_diagnostics : Flag<["-"], "fno-color-diagnostics">, Group<f_Group>;
def fno_diagnostics_color : Flag<["-"], "fno-diagnostics-color">, Group<f_Group>;
def fno_common : Flag<["-"], "fno-common">, Group<f_Group>, Flags<[CC1Option]>,
HelpText<"Compile common globals like normal definitions">;
def fno_constant_cfstrings : Flag<["-"], "fno-constant-cfstrings">, Group<f_Group>,
Expand Down
39 changes: 36 additions & 3 deletions clang/lib/Driver/Tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
53 changes: 53 additions & 0 deletions clang/test/Driver/color-diagnostics.c
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 7e2da79

Please sign in to comment.