From baa2aa28f56259de7fa012178ada9e5ba07ec76c Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 17 Nov 2020 12:15:42 -0500 Subject: [PATCH] lld: Add --color-diagnostic to MachO port, harmonize others This adds `--[no-]color-diagnostics[=auto,never,always]` to the MachO port and harmonizes the flag in the other ports: - Consistently use MetaVarName - Consistently document the non-eq version as alias of the eq version - Use B<> in the ports that have it (no-op, shorter) - Fix oversight in COFF port that made the --no flag have the wrong prefix Differential Revision: https://reviews.llvm.org/D91640 --- lld/COFF/Options.td | 9 +++++---- lld/ELF/Options.td | 11 ++++------- lld/MachO/Driver.cpp | 24 ++++++++++++++++++++++++ lld/MachO/Options.td | 12 ++++++++++++ lld/test/MachO/color-diagnostics.test | 21 +++++++++++++++++++++ lld/wasm/Options.td | 12 +++++------- 6 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 lld/test/MachO/color-diagnostics.test diff --git a/lld/COFF/Options.td b/lld/COFF/Options.td index a69fd7032e9e..a694c671f9cd 100644 --- a/lld/COFF/Options.td +++ b/lld/COFF/Options.td @@ -33,9 +33,12 @@ def aligncomm : P<"aligncomm", "Set common symbol alignment">; 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 opt : P<"opt", "Control optimizations">; 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 index 37d6fda03893..5171c08a8291 100644 --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -113,11 +113,11 @@ defm call_graph_profile_sort: BB<"call-graph-profile-sort", // -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 nmagic: F<"nmagic">, MetaVarName<"">, 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 index 6735611666a9..a693cf6c8033 100644 --- a/lld/MachO/Driver.cpp +++ b/lld/MachO/Driver.cpp @@ -69,6 +69,28 @@ static const opt::OptTable::Info optInfo[] = { 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 @@ opt::InputArgList MachOOptTable::parse(ArrayRef argv) { 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 index cdbdbb26702e..3695bd1e2868 100644 --- 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 index 000000000000..3bf757b7b715 --- /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 index 1a5cf3513fea..2955e6e146f9 100644 --- a/lld/wasm/Options.td +++ b/lld/wasm/Options.td @@ -20,11 +20,12 @@ multiclass B { // 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 @@ def mllvm: S<"mllvm">, HelpText<"Options to pass to LLVM">; 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<"">,