Skip to content

Commit 7e2da79

Browse files
committed
Add support for gcc's spelling of -fcolor-diagnostics.
See http://gcc.gnu.org/onlinedocs/gcc/Language-Independent-Options.html llvm-svn: 179728
1 parent b32f6bf commit 7e2da79

3 files changed

Lines changed: 92 additions & 3 deletions

File tree

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,8 @@ def fcatch_undefined_behavior : Flag<["-"], "fcatch-undefined-behavior">, Group<
333333
def fclasspath_EQ : Joined<["-"], "fclasspath=">, Group<f_Group>;
334334
def fcolor_diagnostics : Flag<["-"], "fcolor-diagnostics">, Group<f_Group>, Flags<[CC1Option]>,
335335
HelpText<"Use colors in diagnostics">;
336+
def fdiagnostics_color : Flag<["-"], "fdiagnostics-color">, Group<f_Group>;
337+
def fdiagnostics_color_EQ : Joined<["-"], "fdiagnostics-color=">, Group<f_Group>;
336338
def fcomment_block_commands : CommaJoined<["-"], "fcomment-block-commands=">, Group<f_clang_Group>, Flags<[CC1Option]>,
337339
HelpText<"Treat each comma separated argument in <arg> as a documentation comment block command">,
338340
MetaVarName<"<arg>">;
@@ -539,6 +541,7 @@ def fno_builtin : Flag<["-"], "fno-builtin">, Group<f_Group>, Flags<[CC1Option]>
539541
def fno_caret_diagnostics : Flag<["-"], "fno-caret-diagnostics">, Group<f_Group>,
540542
Flags<[CC1Option]>;
541543
def fno_color_diagnostics : Flag<["-"], "fno-color-diagnostics">, Group<f_Group>;
544+
def fno_diagnostics_color : Flag<["-"], "fno-diagnostics-color">, Group<f_Group>;
542545
def fno_common : Flag<["-"], "fno-common">, Group<f_Group>, Flags<[CC1Option]>,
543546
HelpText<"Compile common globals like normal definitions">;
544547
def fno_constant_cfstrings : Flag<["-"], "fno-constant-cfstrings">, Group<f_Group>,

clang/lib/Driver/Tools.cpp

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,9 +3200,42 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
32003200

32013201
// Color diagnostics are the default, unless the terminal doesn't support
32023202
// them.
3203-
if (Args.hasFlag(options::OPT_fcolor_diagnostics,
3204-
options::OPT_fno_color_diagnostics,
3205-
llvm::sys::Process::StandardErrHasColors()))
3203+
// Support both clang's -f[no-]color-diagnostics and gcc's
3204+
// -f[no-]diagnostics-colors[=never|always|auto].
3205+
enum { Colors_On, Colors_Off, Colors_Auto } ShowColors = Colors_Auto;
3206+
for (ArgList::const_iterator it = Args.begin(), ie = Args.end();
3207+
it != ie; ++it) {
3208+
const Option &O = (*it)->getOption();
3209+
if (!O.matches(options::OPT_fcolor_diagnostics) &&
3210+
!O.matches(options::OPT_fdiagnostics_color) &&
3211+
!O.matches(options::OPT_fno_color_diagnostics) &&
3212+
!O.matches(options::OPT_fno_diagnostics_color) &&
3213+
!O.matches(options::OPT_fdiagnostics_color_EQ))
3214+
continue;
3215+
3216+
(*it)->claim();
3217+
if (O.matches(options::OPT_fcolor_diagnostics) ||
3218+
O.matches(options::OPT_fdiagnostics_color)) {
3219+
ShowColors = Colors_On;
3220+
} else if (O.matches(options::OPT_fno_color_diagnostics) ||
3221+
O.matches(options::OPT_fno_diagnostics_color)) {
3222+
ShowColors = Colors_Off;
3223+
} else {
3224+
assert(O.matches(options::OPT_fdiagnostics_color_EQ));
3225+
StringRef value((*it)->getValue());
3226+
if (value == "always")
3227+
ShowColors = Colors_On;
3228+
else if (value == "never")
3229+
ShowColors = Colors_Off;
3230+
else if (value == "auto")
3231+
ShowColors = Colors_Auto;
3232+
else
3233+
getToolChain().getDriver().Diag(diag::err_drv_clang_unsupported)
3234+
<< ("-fdiagnostics-color=" + value).str();
3235+
}
3236+
}
3237+
if (ShowColors == Colors_On ||
3238+
(ShowColors == Colors_Auto && llvm::sys::Process::StandardErrHasColors()))
32063239
CmdArgs.push_back("-fcolor-diagnostics");
32073240

32083241
if (!Args.hasFlag(options::OPT_fshow_source_location,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// RUN: %clang -fcolor-diagnostics -### -c %s 2>&1 \
2+
// RUN: | FileCheck --check-prefix=CD %s
3+
// CHECK-CD: clang{{.*}}" "-fcolor-diagnostics"
4+
5+
// RUN: %clang -fno-color-diagnostics -### -c %s 2>&1 \
6+
// RUN: | FileCheck --check-prefix=NCD %s
7+
// CHECK-NCD-NOT: clang{{.*}}" "-fcolor-diagnostics"
8+
9+
// RUN: %clang -fdiagnostics-color -### -c %s 2>&1 \
10+
// RUN: | FileCheck --check-prefix=DC %s
11+
// CHECK-DC: clang{{.*}}" "-fcolor-diagnostics"
12+
13+
// RUN: %clang -fno-diagnostics-color -### -c %s 2>&1 \
14+
// RUN: | FileCheck --check-prefix=NDC %s
15+
// CHECK-NDC-NOT: clang{{.*}}" "-fcolor-diagnostics"
16+
17+
// RUN: %clang -fdiagnostics-color=always -### -c %s 2>&1 \
18+
// RUN: | FileCheck --check-prefix=DCE_A %s
19+
// CHECK-DCE_A: clang{{.*}}" "-fcolor-diagnostics"
20+
21+
// RUN: %clang -fdiagnostics-color=never -### -c %s 2>&1 \
22+
// RUN: | FileCheck --check-prefix=DCE_N %s
23+
// CHECK-DCE_N-NOT: clang{{.*}}" "-fcolor-diagnostics"
24+
25+
// The test doesn't run in a PTY, so "auto" defaults to off.
26+
// RUN: %clang -fdiagnostics-color=auto -### -c %s 2>&1 \
27+
// RUN: | FileCheck --check-prefix=DCE_AUTO %s
28+
// CHECK-DCE_AUTO-NOT: clang{{.*}}" "-fcolor-diagnostics"
29+
30+
// RUN: %clang -fdiagnostics-color=foo -### -c %s 2>&1 \
31+
// RUN: | FileCheck --check-prefix=DCE_FOO %s
32+
// CHECK-DCE_FOO: error: the clang compiler does not support '-fdiagnostics-color=foo'
33+
34+
// Check that the last flag wins.
35+
// RUN: %clang -fno-color-diagnostics -fdiagnostics-color -### -c %s 2>&1 \
36+
// RUN: | FileCheck --check-prefix=NCD_DC_S %s
37+
// CHECK-NCD_DC_S: clang{{.*}}" "-fcolor-diagnostics"
38+
39+
// RUN: %clang -fcolor-diagnostics -fno-diagnostics-color -### -c %s 2>&1 \
40+
// RUN: | FileCheck --check-prefix=CD_NDC_S %s
41+
// CHECK-CD_NDC_S-NOT: clang{{.*}}" "-fcolor-diagnostics"
42+
43+
// RUN: %clang -fdiagnostics-color -fno-color-diagnostics -### -c %s 2>&1 \
44+
// RUN: | FileCheck --check-prefix=DC_NCD_S %s
45+
// CHECK-DC_NCD_S-NOT: clang{{.*}}" "-fcolor-diagnostics"
46+
47+
// RUN: %clang -fno-diagnostics-color -fcolor-diagnostics -### -c %s 2>&1 \
48+
// RUN: | FileCheck --check-prefix=NDC_CD_S %s
49+
// CHECK-NDC_CD_S: clang{{.*}}" "-fcolor-diagnostics"
50+
51+
// RUN: %clang -fcolor-diagnostics -fdiagnostics-color=auto -### -c %s 2>&1 \
52+
// RUN: | FileCheck --check-prefix=CD_DCE_AUTO_S %s
53+
// CHECK-CD_DCE_AUTO_S-NOT: clang{{.*}}" "-fcolor-diagnostics"

0 commit comments

Comments
 (0)