diff --git a/lld/COFF/Options.td b/lld/COFF/Options.td --- a/lld/COFF/Options.td +++ b/lld/COFF/Options.td @@ -33,9 +33,12 @@ def alternatename : P<"alternatename", "Define weak alias">; def base : P<"base", "Base address of the program">; def color_diagnostics: Flag<["--"], "color-diagnostics">, - HelpText<"Use colors in diagnostics">; + HelpText<"Alias for --color-diagnostics=always">; +def no_color_diagnostics: Flag<["--"], "no-color-diagnostics">, + HelpText<"Alias for --color-diagnostics=never">; def color_diagnostics_eq: Joined<["--"], "color-diagnostics=">, - HelpText<"Use colors in diagnostics; one of 'always', 'never', 'auto'">; + HelpText<"Use colors in diagnostics (default: auto)">, + MetaVarName<"[auto,always,never]">; def defaultlib : P<"defaultlib", "Add the library to the list of input files">; def delayload : P<"delayload", "Delay loaded DLL name">; def entry : P<"entry", "Name of entry point symbol">; @@ -73,8 +76,6 @@ def order : P<"order", "Put functions in order">; def out : P<"out", "Path to file to write output">; def natvis : P<"natvis", "Path to natvis file to embed in the PDB">; -def no_color_diagnostics: F<"no-color-diagnostics">, - HelpText<"Do not use colors in diagnostics">; def pdb : P<"pdb", "PDB file path">; def pdbstripped : P<"pdbstripped", "Stripped PDB file path">; def pdbaltpath : P<"pdbaltpath", "PDB file path to embed in the image">; diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -113,11 +113,11 @@ // -chroot doesn't have a help text because it is an internal option. def chroot: Separate<["--", "-"], "chroot">; -def color_diagnostics: F<"color-diagnostics">, - HelpText<"Alias for --color-diagnostics=always">; - +defm color_diagnostics: B<"color-diagnostics", + "Alias for --color-diagnostics=always", + "Alias for --color-diagnostics=never">; def color_diagnostics_eq: J<"color-diagnostics=">, - HelpText<"Use colors in diagnostics">, + HelpText<"Use colors in diagnostics (default: auto)">, MetaVarName<"[auto,always,never]">; defm cref: B<"cref", @@ -274,9 +274,6 @@ def nostdlib: F<"nostdlib">, HelpText<"Only search directories specified on the command line">; -def no_color_diagnostics: F<"no-color-diagnostics">, - HelpText<"Do not use colors in diagnostics">; - def no_dynamic_linker: F<"no-dynamic-linker">, HelpText<"Inhibit output of .interp section">; diff --git a/lld/MachO/Driver.cpp b/lld/MachO/Driver.cpp --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -69,6 +69,28 @@ MachOOptTable::MachOOptTable() : OptTable(optInfo) {} +// Set color diagnostics according to --color-diagnostics={auto,always,never} +// or --no-color-diagnostics flags. +static void handleColorDiagnostics(opt::InputArgList &args) { + auto *arg = args.getLastArg(OPT_color_diagnostics, OPT_color_diagnostics_eq, + OPT_no_color_diagnostics); + if (!arg) + return; + if (arg->getOption().getID() == OPT_color_diagnostics) { + lld::errs().enable_colors(true); + } else if (arg->getOption().getID() == OPT_no_color_diagnostics) { + lld::errs().enable_colors(false); + } else { + StringRef s = arg->getValue(); + if (s == "always") + lld::errs().enable_colors(true); + else if (s == "never") + lld::errs().enable_colors(false); + else if (s != "auto") + error("unknown option: --color-diagnostics=" + s); + } +} + opt::InputArgList MachOOptTable::parse(ArrayRef argv) { // Make InputArgList from string vectors. unsigned missingIndex; @@ -80,6 +102,8 @@ if (missingCount) error(Twine(args.getArgString(missingIndex)) + ": missing argument"); + handleColorDiagnostics(args); + for (opt::Arg *arg : args.filtered(OPT_UNKNOWN)) error("unknown argument: " + arg->getSpelling()); return args; diff --git a/lld/MachO/Options.td b/lld/MachO/Options.td --- a/lld/MachO/Options.td +++ b/lld/MachO/Options.td @@ -1,8 +1,20 @@ include "llvm/Option/OptParser.td" +// Flags that lld/MachO understands but ld64 doesn't. These take +// '--' instead of '-' and use dashes instead of underscores, so +// they don't collide with the ld64 compat options. + def help : Flag<["-", "--"], "help">; def help_hidden : Flag<["--"], "help-hidden">, HelpText<"Display help for hidden options">; +def color_diagnostics: Flag<["--"], "color-diagnostics">, + HelpText<"Alias for --color-diagnostics=always">; +def no_color_diagnostics: Flag<["--"], "no-color-diagnostics">, + HelpText<"Alias for --color-diagnostics=never">; +def color_diagnostics_eq: Joined<["--"], "color-diagnostics=">, + HelpText<"Use colors in diagnostics (default: auto)">, + MetaVarName<"[auto,always,never]">; + // This is a complete Options.td compiled from Apple's ld(1) manpage // dated 2018-03-07 and cross checked with ld64 source code in repo diff --git a/lld/test/MachO/color-diagnostics.test b/lld/test/MachO/color-diagnostics.test new file mode 100644 --- /dev/null +++ b/lld/test/MachO/color-diagnostics.test @@ -0,0 +1,21 @@ +# Windows command prompt doesn't support ANSI escape sequences. +# REQUIRES: shell + +# RUN: not %lld -xyz --color-diagnostics /nosuchfile 2>&1 \ +# RUN: | FileCheck -check-prefix=COLOR %s +# RUN: not %lld -xyz --color-diagnostics=always /nosuchfile 2>&1 \ +# RUN: | FileCheck -check-prefix=COLOR %s + +# COLOR: {{lld: .\[0;31merror: .\[0munknown argument: -xyz}} +# COLOR: {{lld: .\[0;31merror: .\[0mcannot open /nosuchfile}} + +# RUN: not %lld --color-diagnostics=foobar 2>&1 | FileCheck -check-prefix=ERR %s +# ERR: unknown option: --color-diagnostics=foobar + +# RUN: not %lld /nosuchfile 2>&1 | FileCheck -check-prefix=NOCOLOR %s +# RUN: not %lld --color-diagnostics=never /nosuchfile 2>&1 \ +# RUN: | FileCheck -check-prefix=NOCOLOR %s +# RUN: not %lld --color-diagnostics=always --no-color-diagnostics \ +# RUN: /nosuchfile 2>&1 | FileCheck -check-prefix=NOCOLOR %s + +# NOCOLOR: lld: error: cannot open /nosuchfile diff --git a/lld/wasm/Options.td b/lld/wasm/Options.td --- a/lld/wasm/Options.td +++ b/lld/wasm/Options.td @@ -20,11 +20,12 @@ // The following flags are shared with the ELF linker def Bsymbolic: F<"Bsymbolic">, HelpText<"Bind defined symbols locally">; -def color_diagnostics: F<"color-diagnostics">, - HelpText<"Use colors in diagnostics">; - +defm color_diagnostics: B<"color-diagnostics", + "Alias for --color-diagnostics=always", + "Alias for --color-diagnostics=never">; def color_diagnostics_eq: J<"color-diagnostics=">, - HelpText<"Use colors in diagnostics; one of 'always', 'never', 'auto'">; + HelpText<"Use colors in diagnostics (default: auto)">, + MetaVarName<"[auto,always,never]">; def compress_relocations: F<"compress-relocations">, HelpText<"Compress the relocation targets in the code section.">; @@ -70,9 +71,6 @@ defm Map: Eq<"Map", "Print a link map to the specified file">; -def no_color_diagnostics: F<"no-color-diagnostics">, - HelpText<"Do not use colors in diagnostics">; - def no_fatal_warnings: F<"no-fatal-warnings">; def o: JoinedOrSeparate<["-"], "o">, MetaVarName<"">,