From 72b1ee2533f6e7f574fd183f91cb2fc72463a415 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Sat, 26 Nov 2016 15:10:01 +0000 Subject: [PATCH] Make getColorDiagnostics return a boolean value instead of an enum. Config->ColorDiagnostics was of type enum before. Now it is just a boolean flag. Thanks Rafael for suggestion. llvm-svn: 287978 --- lld/ELF/Config.h | 7 ++----- lld/ELF/Driver.cpp | 47 +++++++++++++++++++++++++++-------------------- lld/ELF/Error.cpp | 11 +---------- 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h index 072eea6..2f05afa 100644 --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -34,9 +34,6 @@ 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 }; @@ -100,6 +97,7 @@ struct Configuration { bool AsNeeded = false; bool Bsymbolic; bool BsymbolicFunctions; + bool ColorDiagnostics = false; bool Demangle = true; bool DisableVerify; bool EhFrameHdr; @@ -138,7 +136,6 @@ struct Configuration { bool ZRelro; bool ExitEarly; bool ZWxneeded; - ColorPolicy ColorDiagnostics = ColorPolicy::Auto; DiscardPolicy Discard; SortSectionPolicy SortSection; StripPolicy Strip = StripPolicy::None; @@ -149,7 +146,7 @@ struct Configuration { uint16_t DefaultSymbolVersion = llvm::ELF::VER_NDX_GLOBAL; uint16_t EMachine = llvm::ELF::EM_NONE; uint64_t EntryAddr = 0; - uint64_t ErrorLimit = 20; // initialize it early so that error() won't complain + uint64_t ErrorLimit = 20; uint64_t ImageBase; uint64_t MaxPageSize; uint64_t ZStackSize; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 5259082..315e678 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -25,6 +25,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/CommandLine.h" +#include "llvm/Support/Process.h" #include "llvm/Support/TargetSelect.h" #include "llvm/Support/raw_ostream.h" #include @@ -285,9 +286,35 @@ static uint64_t getZOptionValue(opt::InputArgList &Args, StringRef Key, return Default; } +// Parse -color-diagnostics={auto,always,never} or -no-color-diagnostics. +static bool getColorDiagnostics(opt::InputArgList &Args) { + bool Default = (ErrorOS == &errs() && Process::StandardErrHasColors()); + + auto *Arg = Args.getLastArg(OPT_color_diagnostics, OPT_no_color_diagnostics); + if (!Arg) + return Default; + if (Arg->getOption().getID() == OPT_no_color_diagnostics) + return false; + + StringRef S = Arg->getValue(); + if (S == "auto") + return Default; + if (S == "always") + return true; + if (S != "never") + error("unknown -color-diagnostics value: " + S); + return false; +} + void LinkerDriver::main(ArrayRef ArgsArr, bool CanExitEarly) { ELFOptTable Parser; opt::InputArgList Args = Parser.parse(ArgsArr.slice(1)); + + // Read some flags early because error() depends on them. + Config->ErrorLimit = getInteger(Args, OPT_error_limit, 20); + Config->ColorDiagnostics = getColorDiagnostics(Args); + + // Handle -help if (Args.hasArg(OPT_help)) { printHelp(ArgsArr[0]); return; @@ -392,24 +419,6 @@ 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); @@ -504,13 +513,11 @@ 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); Config->EhFrameHdr = Args.hasArg(OPT_eh_frame_hdr); Config->EnableNewDtags = !Args.hasArg(OPT_disable_new_dtags); - Config->ErrorLimit = getInteger(Args, OPT_error_limit, 20); Config->ExportDynamic = Args.hasArg(OPT_export_dynamic); Config->FatalWarnings = Args.hasArg(OPT_fatal_warnings); Config->GcSections = getArg(Args, OPT_gc_sections, OPT_no_gc_sections, false); diff --git a/lld/ELF/Error.cpp b/lld/ELF/Error.cpp index 1a66048..6e30f08 100644 --- a/lld/ELF/Error.cpp +++ b/lld/ELF/Error.cpp @@ -13,7 +13,6 @@ #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 @@ -34,17 +33,9 @@ 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) { *ErrorOS << Argv0 + ": "; - if (useColor()) { + if (Config->ColorDiagnostics) { ErrorOS->changeColor(C, true); *ErrorOS << S; ErrorOS->resetColor(); -- 2.7.4