[lld] r290157 - Set Config->ColorDiagnostics early so that all error messages are colored.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 19 18:17:25 PST 2016


Author: ruiu
Date: Mon Dec 19 20:17:24 2016
New Revision: 290157

URL: http://llvm.org/viewvc/llvm-project?rev=290157&view=rev
Log:
Set Config->ColorDiagnostics early so that all error messages are colored.

Previously, some errors that were checked before we set to
Config->ColorDiagnostics weren't colored. This patch moves the code
to set the variable so that such error messages are colored just like
other error messages.

Modified:
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/DriverUtils.cpp
    lld/trunk/test/ELF/color-diagnostics.test

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=290157&r1=290156&r2=290157&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Mon Dec 19 20:17:24 2016
@@ -25,7 +25,6 @@
 #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>
@@ -284,33 +283,12 @@ static uint64_t getZOptionValue(opt::Inp
   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.
+  // Interpret this flag 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)) {

Modified: lld/trunk/ELF/DriverUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/DriverUtils.cpp?rev=290157&r1=290156&r2=290157&view=diff
==============================================================================
--- lld/trunk/ELF/DriverUtils.cpp (original)
+++ lld/trunk/ELF/DriverUtils.cpp Mon Dec 19 20:17:24 2016
@@ -26,6 +26,7 @@
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Path.h"
+#include "llvm/Support/Process.h"
 
 using namespace llvm;
 using namespace llvm::sys;
@@ -51,6 +52,26 @@ static const opt::OptTable::Info OptInfo
 
 ELFOptTable::ELFOptTable() : OptTable(OptInfo) {}
 
+// 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;
+}
+
 static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &Args) {
   if (auto *Arg = Args.getLastArg(OPT_rsp_quoting)) {
     StringRef S = Arg->getValue();
@@ -77,11 +98,14 @@ opt::InputArgList ELFOptTable::parse(Arr
   // --rsp-quoting.
   opt::InputArgList Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
 
-  // Expand response files. '@<filename>' is replaced by the file's contents.
+  // Expand response files (arguments in the form of @<filename>)
+  // and then parse the argument again.
   cl::ExpandResponseFiles(Saver, getQuotingStyle(Args), Vec);
-
-  // Parse options and then do error checking.
   Args = this->ParseArgs(Vec, MissingIndex, MissingCount);
+
+  // Interpret -color-diagnostics early so that error messages
+  // for unknown flags are colored.
+  Config->ColorDiagnostics = getColorDiagnostics(Args);
   if (MissingCount)
     error(Twine(Args.getArgString(MissingIndex)) + ": missing argument");
 

Modified: lld/trunk/test/ELF/color-diagnostics.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/color-diagnostics.test?rev=290157&r1=290156&r2=290157&view=diff
==============================================================================
--- lld/trunk/test/ELF/color-diagnostics.test (original)
+++ lld/trunk/test/ELF/color-diagnostics.test Mon Dec 19 20:17:24 2016
@@ -1,9 +1,10 @@
 # Windows command prompt doesn't support ANSI escape sequences.
 # REQUIRES: shell
 
-# RUN: not ld.lld -color-diagnostics=always /nosuchfile 2>&1 \
+# RUN: not ld.lld -xyz -color-diagnostics=always /nosuchfile 2>&1 \
 # RUN:   | FileCheck -check-prefix=COLOR %s
 
+# COLOR: {{ld.lld: .\[0;1;31merror: .\[0munknown argument: -xyz}}
 # COLOR: {{ld.lld: .\[0;1;31merror: .\[0mcannot open /nosuchfile}}
 
 # RUN: not ld.lld -color-diagnostics=always -no-color-diagnostics /nosuchfile 2>&1 \




More information about the llvm-commits mailing list