diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 7c0ea982beb0..18b7d28c0609 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -34,6 +34,9 @@ enum ELFKind { // For --build-id. enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid }; +// For --color-diagnostics. +enum class ColorPolicy { Auto, Always, Never }; + // For --discard-{all,locals,none}. enum class DiscardPolicy { Default, All, Locals, None }; @@ -135,6 +138,7 @@ struct Configuration { bool ZRelro; bool ExitEarly; bool ZWxneeded; + ColorPolicy ColorDiagnostics = ColorPolicy::Auto; DiscardPolicy Discard; SortSectionPolicy SortSection; StripPolicy Strip = StripPolicy::None; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 27914282cdd1..13d01b608774 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -392,6 +392,24 @@ static bool getArg(opt::InputArgList &Args, unsigned K1, unsigned K2, return Default; } +// Parse -color-diagnostics={auto,always,never} or -no-color-diagnostics. +static ColorPolicy getColorDiagnostics(opt::InputArgList &Args) { + auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_no_color_diagnostics); + if (!Arg) + return ColorPolicy::Auto; + if (Arg->getOption().getID() == OPT_no_color_diagnostics) + return ColorPolicy::Never; + + StringRef S = Arg->getValue(); + if (S == "auto") + return ColorPolicy::Auto; + if (S == "always") + return ColorPolicy::Always; + if (S != "never") + error("unknown -color-diagnostics value: " + S); + return ColorPolicy::Never; +} + static DiscardPolicy getDiscardOption(opt::InputArgList &Args) { auto *Arg = Args.getLastArg(OPT_discard_all, OPT_discard_locals, OPT_discard_none); @@ -486,6 +504,7 @@ void LinkerDriver::readConfigs(opt::InputArgList &Args) { Config->AllowMultipleDefinition = Args.hasArg(OPT_allow_multiple_definition); Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic); Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_functions); + Config->ColorDiagnostics = getColorDiagnostics(Args); Config->Demangle = getArg(Args, OPT_demangle, OPT_no_demangle, true); Config->DisableVerify = Args.hasArg(OPT_disable_verify); Config->Discard = getDiscardOption(Args); diff --git a/lld/ELF/Error.cpp b/lld/ELF/Error.cpp index a230bf6dadde..2b5cfe9bc70d 100644 --- a/lld/ELF/Error.cpp +++ b/lld/ELF/Error.cpp @@ -13,6 +13,7 @@ #include "llvm/ADT/Twine.h" #include "llvm/Support/Error.h" #include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/Process.h" #include "llvm/Support/raw_ostream.h" #include @@ -20,6 +21,7 @@ #include #endif +using namespace lld::elf; using namespace llvm; namespace lld { @@ -32,6 +34,26 @@ StringRef elf::Argv0; // but outs() or errs() are not thread-safe. We protect them using a mutex. static std::mutex Mu; +static bool useColor() { + if (Config->ColorDiagnostics == ColorPolicy::Always) + return true; + if (Config->ColorDiagnostics == ColorPolicy::Never) + return false; + return ErrorOS == &errs() && sys::Process::StandardErrHasColors(); +} + +static void print(StringRef S, raw_ostream::Colors C) { + if (useColor()) { + ErrorOS->changeColor(raw_ostream::WHITE, /*Bold=*/true); + *ErrorOS << Argv0 + ": "; + ErrorOS->changeColor(C, true); + *ErrorOS << S; + ErrorOS->resetColor(); + } else { + *ErrorOS << Argv0 + ": " << S; + } +} + void elf::log(const Twine &Msg) { std::lock_guard Lock(Mu); if (Config->Verbose) @@ -44,16 +66,19 @@ void elf::warn(const Twine &Msg) { return; } std::lock_guard Lock(Mu); - *ErrorOS << Argv0 << ": warning: " << Msg << "\n"; + print("warning: ", raw_ostream::MAGENTA); + *ErrorOS << Msg << "\n"; } void elf::error(const Twine &Msg) { std::lock_guard Lock(Mu); if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) { - *ErrorOS << Argv0 << ": error: " << Msg << "\n"; + print("error: ", raw_ostream::RED); + *ErrorOS << Msg << "\n"; } else if (ErrorCount == Config->ErrorLimit) { - *ErrorOS << Argv0 << ": error: too many errors emitted, stopping now" + print("error: ", raw_ostream::RED); + *ErrorOS << "too many errors emitted, stopping now" << " (use -error-limit=0 to see all errors)\n"; if (Config->ExitEarly) exitLld(1); @@ -79,7 +104,8 @@ void elf::exitLld(int Val) { void elf::fatal(const Twine &Msg) { std::lock_guard Lock(Mu); - *ErrorOS << Argv0 << ": error: " << Msg << "\n"; + print("error: ", raw_ostream::RED); + *ErrorOS << Msg << "\n"; exitLld(1); } diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td index a4eca161f25e..b3feded3531b 100644 --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -39,6 +39,9 @@ def allow_multiple_definition: F<"allow-multiple-definition">, def as_needed: F<"as-needed">, HelpText<"Only set DT_NEEDED for shared libraries if used">; +def color_diagnostics: S<"color-diagnostics">, + HelpText<"Use colors in diagnostics">; + def disable_new_dtags: F<"disable-new-dtags">, HelpText<"Disable new dynamic tags">; @@ -121,6 +124,9 @@ def nostdlib: F<"nostdlib">, def no_as_needed: F<"no-as-needed">, HelpText<"Always DT_NEEDED for shared libraries">; +def no_color_diagnostics: F<"no-color-diagnostics">, + HelpText<"Do not use colors in diagnostics">; + def no_demangle: F<"no-demangle">, HelpText<"Do not demangle symbol names">; @@ -238,6 +244,7 @@ def alias_Bstatic_dn: F<"dn">, Alias; def alias_Bstatic_non_shared: F<"non_shared">, Alias; def alias_Bstatic_static: F<"static">, Alias; def alias_L__library_path: J<"library-path=">, Alias; +def alias_color_diagnostics: J<"color-diagnostics=">, Alias; def alias_discard_all_x: Flag<["-"], "x">, Alias; def alias_discard_locals_X: Flag<["-"], "X">, Alias; def alias_dynamic_list: J<"dynamic-list=">, Alias; diff --git a/lld/test/ELF/color-diagnostics.test b/lld/test/ELF/color-diagnostics.test new file mode 100644 index 000000000000..5a6a1ec1a009 --- /dev/null +++ b/lld/test/ELF/color-diagnostics.test @@ -0,0 +1,12 @@ +# Windows command prompt doesn't support ANSI escape sequences. +# REQUIRES: shell + +# RUN: not ld.lld -color-diagnostics=always /nosuchfile 2>&1 \ +# RUN: | FileCheck -check-prefix=COLOR %s + +# COLOR: {{^.\[0;1;37m.*/ld.lld: .\[0;1;31merror: .\[0mcannot open /nosuchfile}} + +# RUN: not ld.lld -color-diagnostics=always -no-color-diagnostics /nosuchfile 2>&1 \ +# RUN: | FileCheck -check-prefix=NOCOLOR %s + +# NOCOLOR: ld.lld: error: cannot open /nosuchfile