<div dir="ltr">Good point. Done in r287978.</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Nov 25, 2016 at 11:27 PM, Rafael Espíndola <span dir="ltr"><<a href="mailto:rafael.espindola@gmail.com" target="_blank">rafael.espindola@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">By calling sys::Process::<wbr>StandardErrHasColors() early Config can have<br>
just a bool, no?<br>
<br>
Cheers,<br>
Rafael<br>
<br>
<br>
On 25 November 2016 at 15:27, Rui Ueyama via llvm-commits<br>
<div class="HOEnZb"><div class="h5"><<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br>
> Author: ruiu<br>
> Date: Fri Nov 25 14:27:32 2016<br>
> New Revision: 287949<br>
><br>
> URL: <a href="http://llvm.org/viewvc/llvm-project?rev=287949&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project?rev=287949&view=rev</a><br>
> Log:<br>
> Support -color-diagnostics={auto,<wbr>always,never}.<br>
><br>
> -color-diagnostics=auto is default because that's the same as<br>
> Clang's default. When color is enabled, error or warning messages<br>
> are colored like this.<br>
><br>
>   error:<br>
>   <bold>ld.lld</bold> <red>error:</red> foo.o: no such file<br>
><br>
>   warning:<br>
>   <bold>ld.lld</bold> <magenta>warning:</magenta> foo.o: no such file<br>
><br>
> Differential Revision: <a href="https://reviews.llvm.org/D27117" rel="noreferrer" target="_blank">https://reviews.llvm.org/<wbr>D27117</a><br>
><br>
> Added:<br>
>     lld/trunk/test/ELF/color-<wbr>diagnostics.test<br>
> Modified:<br>
>     lld/trunk/ELF/Config.h<br>
>     lld/trunk/ELF/Driver.cpp<br>
>     lld/trunk/ELF/Error.cpp<br>
>     lld/trunk/ELF/Options.td<br>
><br>
> Modified: lld/trunk/ELF/Config.h<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Config.h?rev=287949&r1=287948&r2=287949&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/Config.<wbr>h?rev=287949&r1=287948&r2=<wbr>287949&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- lld/trunk/ELF/Config.h (original)<br>
> +++ lld/trunk/ELF/Config.h Fri Nov 25 14:27:32 2016<br>
> @@ -34,6 +34,9 @@ enum ELFKind {<br>
>  // For --build-id.<br>
>  enum class BuildIdKind { None, Fast, Md5, Sha1, Hexstring, Uuid };<br>
><br>
> +// For --color-diagnostics.<br>
> +enum class ColorPolicy { Auto, Always, Never };<br>
> +<br>
>  // For --discard-{all,locals,none}.<br>
>  enum class DiscardPolicy { Default, All, Locals, None };<br>
><br>
> @@ -135,6 +138,7 @@ struct Configuration {<br>
>    bool ZRelro;<br>
>    bool ExitEarly;<br>
>    bool ZWxneeded;<br>
> +  ColorPolicy ColorDiagnostics = ColorPolicy::Auto;<br>
>    DiscardPolicy Discard;<br>
>    SortSectionPolicy SortSection;<br>
>    StripPolicy Strip = StripPolicy::None;<br>
><br>
> Modified: lld/trunk/ELF/Driver.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=287949&r1=287948&r2=287949&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/Driver.<wbr>cpp?rev=287949&r1=287948&r2=<wbr>287949&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- lld/trunk/ELF/Driver.cpp (original)<br>
> +++ lld/trunk/ELF/Driver.cpp Fri Nov 25 14:27:32 2016<br>
> @@ -392,6 +392,24 @@ static bool getArg(opt::InputArgList &Ar<br>
>    return Default;<br>
>  }<br>
><br>
> +// Parse -color-diagnostics={auto,<wbr>always,never} or -no-color-diagnostics.<br>
> +static ColorPolicy getColorDiagnostics(opt::<wbr>InputArgList &Args) {<br>
> +  auto *Arg = Args.getLastArg(OPT_color_<wbr>diagnostics, OPT_no_color_diagnostics);<br>
> +  if (!Arg)<br>
> +    return ColorPolicy::Auto;<br>
> +  if (Arg->getOption().getID() == OPT_no_color_diagnostics)<br>
> +    return ColorPolicy::Never;<br>
> +<br>
> +  StringRef S = Arg->getValue();<br>
> +  if (S == "auto")<br>
> +    return ColorPolicy::Auto;<br>
> +  if (S == "always")<br>
> +    return ColorPolicy::Always;<br>
> +  if (S != "never")<br>
> +    error("unknown -color-diagnostics value: " + S);<br>
> +  return ColorPolicy::Never;<br>
> +}<br>
> +<br>
>  static DiscardPolicy getDiscardOption(opt::<wbr>InputArgList &Args) {<br>
>    auto *Arg =<br>
>        Args.getLastArg(OPT_discard_<wbr>all, OPT_discard_locals, OPT_discard_none);<br>
> @@ -486,6 +504,7 @@ void LinkerDriver::readConfigs(opt:<wbr>:Inpu<br>
>    Config-><wbr>AllowMultipleDefinition = Args.hasArg(OPT_allow_<wbr>multiple_definition);<br>
>    Config->Bsymbolic = Args.hasArg(OPT_Bsymbolic);<br>
>    Config->BsymbolicFunctions = Args.hasArg(OPT_Bsymbolic_<wbr>functions);<br>
> +  Config->ColorDiagnostics = getColorDiagnostics(Args);<br>
>    Config->Demangle = getArg(Args, OPT_demangle, OPT_no_demangle, true);<br>
>    Config->DisableVerify = Args.hasArg(OPT_disable_<wbr>verify);<br>
>    Config->Discard = getDiscardOption(Args);<br>
><br>
> Modified: lld/trunk/ELF/Error.cpp<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Error.cpp?rev=287949&r1=287948&r2=287949&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/Error.<wbr>cpp?rev=287949&r1=287948&r2=<wbr>287949&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- lld/trunk/ELF/Error.cpp (original)<br>
> +++ lld/trunk/ELF/Error.cpp Fri Nov 25 14:27:32 2016<br>
> @@ -13,6 +13,7 @@<br>
>  #include "llvm/ADT/Twine.h"<br>
>  #include "llvm/Support/Error.h"<br>
>  #include "llvm/Support/ManagedStatic.h"<br>
> +#include "llvm/Support/Process.h"<br>
>  #include "llvm/Support/raw_ostream.h"<br>
>  #include <mutex><br>
><br>
> @@ -20,6 +21,7 @@<br>
>  #include <unistd.h><br>
>  #endif<br>
><br>
> +using namespace lld::elf;<br>
>  using namespace llvm;<br>
><br>
>  namespace lld {<br>
> @@ -32,6 +34,26 @@ StringRef elf::Argv0;<br>
>  // but outs() or errs() are not thread-safe. We protect them using a mutex.<br>
>  static std::mutex Mu;<br>
><br>
> +static bool useColor() {<br>
> +  if (Config->ColorDiagnostics == ColorPolicy::Always)<br>
> +    return true;<br>
> +  if (Config->ColorDiagnostics == ColorPolicy::Never)<br>
> +    return false;<br>
> +  return ErrorOS == &errs() && sys::Process::<wbr>StandardErrHasColors();<br>
> +}<br>
> +<br>
> +static void print(StringRef S, raw_ostream::Colors C) {<br>
> +  if (useColor()) {<br>
> +    ErrorOS->changeColor(raw_<wbr>ostream::WHITE, /*Bold=*/true);<br>
> +    *ErrorOS << Argv0 + ": ";<br>
> +    ErrorOS->changeColor(C, true);<br>
> +    *ErrorOS << S;<br>
> +    ErrorOS->resetColor();<br>
> +  } else {<br>
> +    *ErrorOS << Argv0 + ": " << S;<br>
> +  }<br>
> +}<br>
> +<br>
>  void elf::log(const Twine &Msg) {<br>
>    std::lock_guard<std::mutex> Lock(Mu);<br>
>    if (Config->Verbose)<br>
> @@ -44,16 +66,19 @@ void elf::warn(const Twine &Msg) {<br>
>      return;<br>
>    }<br>
>    std::lock_guard<std::mutex> Lock(Mu);<br>
> -  *ErrorOS << Argv0 << ": warning: " << Msg << "\n";<br>
> +  print("warning: ", raw_ostream::MAGENTA);<br>
> +  *ErrorOS << Msg << "\n";<br>
>  }<br>
><br>
>  void elf::error(const Twine &Msg) {<br>
>    std::lock_guard<std::mutex> Lock(Mu);<br>
><br>
>    if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {<br>
> -    *ErrorOS << Argv0 << ": error: " << Msg << "\n";<br>
> +    print("error: ", raw_ostream::RED);<br>
> +    *ErrorOS << Msg << "\n";<br>
>    } else if (ErrorCount == Config->ErrorLimit) {<br>
> -    *ErrorOS << Argv0 << ": error: too many errors emitted, stopping now"<br>
> +    print("error: ", raw_ostream::RED);<br>
> +    *ErrorOS << "too many errors emitted, stopping now"<br>
>               << " (use -error-limit=0 to see all errors)\n";<br>
>      if (Config->ExitEarly)<br>
>        exitLld(1);<br>
> @@ -79,7 +104,8 @@ void elf::exitLld(int Val) {<br>
><br>
>  void elf::fatal(const Twine &Msg) {<br>
>    std::lock_guard<std::mutex> Lock(Mu);<br>
> -  *ErrorOS << Argv0 << ": error: " << Msg << "\n";<br>
> +  print("error: ", raw_ostream::RED);<br>
> +  *ErrorOS << Msg << "\n";<br>
>    exitLld(1);<br>
>  }<br>
><br>
><br>
> Modified: lld/trunk/ELF/Options.td<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Options.td?rev=287949&r1=287948&r2=287949&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/ELF/Options.<wbr>td?rev=287949&r1=287948&r2=<wbr>287949&view=diff</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- lld/trunk/ELF/Options.td (original)<br>
> +++ lld/trunk/ELF/Options.td Fri Nov 25 14:27:32 2016<br>
> @@ -39,6 +39,9 @@ def allow_multiple_definition: F<"allow-<br>
>  def as_needed: F<"as-needed">,<br>
>    HelpText<"Only set DT_NEEDED for shared libraries if used">;<br>
><br>
> +def color_diagnostics: S<"color-diagnostics">,<br>
> +  HelpText<"Use colors in diagnostics">;<br>
> +<br>
>  def disable_new_dtags: F<"disable-new-dtags">,<br>
>    HelpText<"Disable new dynamic tags">;<br>
><br>
> @@ -121,6 +124,9 @@ def nostdlib: F<"nostdlib">,<br>
>  def no_as_needed: F<"no-as-needed">,<br>
>    HelpText<"Always DT_NEEDED for shared libraries">;<br>
><br>
> +def no_color_diagnostics: F<"no-color-diagnostics">,<br>
> +  HelpText<"Do not use colors in diagnostics">;<br>
> +<br>
>  def no_demangle: F<"no-demangle">,<br>
>    HelpText<"Do not demangle symbol names">;<br>
><br>
> @@ -238,6 +244,7 @@ def alias_Bstatic_dn: F<"dn">, Alias<Bst<br>
>  def alias_Bstatic_non_shared: F<"non_shared">, Alias<Bstatic>;<br>
>  def alias_Bstatic_static: F<"static">, Alias<Bstatic>;<br>
>  def alias_L__library_path: J<"library-path=">, Alias<L>;<br>
> +def alias_color_diagnostics: J<"color-diagnostics=">, Alias<color_diagnostics>;<br>
>  def alias_discard_all_x: Flag<["-"], "x">, Alias<discard_all>;<br>
>  def alias_discard_locals_X: Flag<["-"], "X">, Alias<discard_locals>;<br>
>  def alias_dynamic_list: J<"dynamic-list=">, Alias<dynamic_list>;<br>
><br>
> Added: lld/trunk/test/ELF/color-<wbr>diagnostics.test<br>
> URL: <a href="http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/color-diagnostics.test?rev=287949&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-<wbr>project/lld/trunk/test/ELF/<wbr>color-diagnostics.test?rev=<wbr>287949&view=auto</a><br>
> ==============================<wbr>==============================<wbr>==================<br>
> --- lld/trunk/test/ELF/color-<wbr>diagnostics.test (added)<br>
> +++ lld/trunk/test/ELF/color-<wbr>diagnostics.test Fri Nov 25 14:27:32 2016<br>
> @@ -0,0 +1,12 @@<br>
> +# Windows command prompt doesn't support ANSI escape sequences.<br>
> +# REQUIRES: shell<br>
> +<br>
> +# RUN: not ld.lld -color-diagnostics=always /nosuchfile 2>&1 \<br>
> +# RUN:   | FileCheck -check-prefix=COLOR %s<br>
> +<br>
> +# COLOR: {{^.\[0;1;37m.*/ld.lld: .\[0;1;31merror: .\[0mcannot open /nosuchfile}}<br>
> +<br>
> +# RUN: not ld.lld -color-diagnostics=always -no-color-diagnostics /nosuchfile 2>&1 \<br>
> +# RUN:   | FileCheck -check-prefix=NOCOLOR %s<br>
> +<br>
> +# NOCOLOR: ld.lld: error: cannot open /nosuchfile<br>
><br>
><br>
> ______________________________<wbr>_________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</div></div></blockquote></div><br></div>