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
This commit is contained in:
Rui Ueyama 2016-11-26 15:10:01 +00:00
parent 1880bbed39
commit 72b1ee2533
3 changed files with 30 additions and 35 deletions

View File

@ -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;

View File

@ -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 <cstdlib>
@ -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<const char *> 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);

View File

@ -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 <mutex>
@ -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();