Index: lld/COFF/DriverUtils.cpp =================================================================== --- lld/COFF/DriverUtils.cpp +++ lld/COFF/DriverUtils.cpp @@ -756,6 +756,28 @@ COFFOptTable::COFFOptTable() : OptTable(InfoTable, true) {} +// 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; + else if (Arg->getOption().getID() == OPT_color_diagnostics) + errorHandler().ColorDiagnostics = true; + else if (Arg->getOption().getID() == OPT_no_color_diagnostics) + errorHandler().ColorDiagnostics = false; + else { + StringRef S = Arg->getValue(); + if (S == "always") + errorHandler().ColorDiagnostics = true; + else if (S == "never") + errorHandler().ColorDiagnostics = false; + else if (S != "auto") + error("unknown option: --color-diagnostics=" + S); + } +} + static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &Args) { if (auto *Arg = Args.getLastArg(OPT_rsp_quoting)) { StringRef S = Arg->getValue(); @@ -799,6 +821,9 @@ if (MissingCount) fatal(Twine(Args.getArgString(MissingIndex)) + ": missing argument"); + + handleColorDiagnostics(Args); + for (auto *Arg : Args.filtered(OPT_UNKNOWN)) warn("ignoring unknown argument: " + Arg->getSpelling()); return Args; Index: lld/COFF/Options.td =================================================================== --- lld/COFF/Options.td +++ lld/COFF/Options.td @@ -20,6 +20,10 @@ 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">; +def color_diagnostics_eq: Joined<["--"], "color-diagnostics=">, + HelpText<"Use colors in diagnostics; one of 'always', 'never', 'auto'">; 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">; @@ -46,6 +50,8 @@ 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 pdbaltpath : P<"pdbaltpath", "PDB file path to embed in the image">; def section : P<"section", "Specify section attributes">; Index: lld/ELF/DriverUtils.cpp =================================================================== --- lld/ELF/DriverUtils.cpp +++ lld/ELF/DriverUtils.cpp @@ -70,7 +70,7 @@ else if (S == "never") errorHandler().ColorDiagnostics = false; else if (S != "auto") - error("unknown option: -color-diagnostics=" + S); + error("unknown option: --color-diagnostics=" + S); } } Index: lld/ELF/Options.td =================================================================== --- lld/ELF/Options.td +++ lld/ELF/Options.td @@ -76,7 +76,7 @@ HelpText<"Use colors in diagnostics">; def color_diagnostics_eq: J<"color-diagnostics=">, - HelpText<"Use colors in diagnostics">; + HelpText<"Use colors in diagnostics; one of 'always', 'never', 'auto'">; defm cref: B<"cref", "Output cross reference table", Index: lld/test/COFF/color-diagnostics.test =================================================================== --- /dev/null +++ lld/test/COFF/color-diagnostics.test @@ -0,0 +1,18 @@ +# Windows command prompt doesn't support ANSI escape sequences. +# REQUIRES: shell + +# RUN: not lld-link -xyz --color-diagnostics /nosuchfile 2>&1 \ +# RUN: | FileCheck -check-prefix=COLOR %s +# RUN: not lld-link -xyz --color-diagnostics=always /nosuchfile 2>&1 \ +# RUN: | FileCheck -check-prefix=COLOR %s + +# COLOR: {{lld-link: .\[0;1;35mwarning: .\[0mignoring unknown argument: -xyz}} +# COLOR: {{lld-link: .\[0;1;31merror: .\[0mcould not open /nosuchfile}} + +# RUN: not lld-link /nosuchfile 2>&1 | FileCheck -check-prefix=NOCOLOR %s +# RUN: not lld-link -color-diagnostics=never /nosuchfile 2>&1 \ +# RUN: | FileCheck -check-prefix=NOCOLOR %s +# RUN: not lld-link -color-diagnostics=always -no-color-diagnostics \ +# RUN: /nosuchfile 2>&1 | FileCheck -check-prefix=NOCOLOR %s + +# NOCOLOR: lld-link: error: could not open /nosuchfile Index: lld/wasm/Driver.cpp =================================================================== --- lld/wasm/Driver.cpp +++ lld/wasm/Driver.cpp @@ -123,7 +123,7 @@ if (S == "never") errorHandler().ColorDiagnostics = false; if (S != "auto") - error("unknown option: -color-diagnostics=" + S); + error("unknown option: --color-diagnostics=" + S); } } Index: lld/wasm/Options.td =================================================================== --- lld/wasm/Options.td +++ lld/wasm/Options.td @@ -21,7 +21,7 @@ HelpText<"Use colors in diagnostics">; def color_diagnostics_eq: J<"color-diagnostics=">, - HelpText<"Use colors in diagnostics">; + HelpText<"Use colors in diagnostics; one of 'always', 'never', 'auto'">; defm demangle: B<"demangle", "Demangle symbol names",