[lld] d3fec7f - LLD: Don't use the stderrOS stream in link before it's reassigned.

James Y Knight via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 21 07:55:29 PST 2019


Author: James Y Knight
Date: 2019-11-21T10:55:03-05:00
New Revision: d3fec7fb456138c83b84e38ce785ea6bfa59c30b

URL: https://github.com/llvm/llvm-project/commit/d3fec7fb456138c83b84e38ce785ea6bfa59c30b
DIFF: https://github.com/llvm/llvm-project/commit/d3fec7fb456138c83b84e38ce785ea6bfa59c30b.diff

LOG: LLD: Don't use the stderrOS stream in link before it's reassigned.

Remove the lld::enableColors function, as it just obscures which
stream it's affecting, and replace with explicit calls to the stream's
enable_colors.

Also, assign the stderrOS and stdoutOS globals first in link function,
just to ensure nothing might use them.

(Either change individually fixes the issue of using the old
stream, but both together seems best.)

Follow-up to b11386f9be9b2dc7276a758d64f66833da10bdea.

Differential Revision: https://reviews.llvm.org/D70492

Added: 
    

Modified: 
    lld/COFF/Driver.cpp
    lld/COFF/DriverUtils.cpp
    lld/Common/ErrorHandler.cpp
    lld/ELF/Driver.cpp
    lld/ELF/DriverUtils.cpp
    lld/MinGW/Driver.cpp
    lld/include/lld/Common/ErrorHandler.h
    lld/lib/Driver/DarwinLdDriver.cpp
    lld/wasm/Driver.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 8a9ba92899cc..f770fff80bcb 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -64,15 +64,15 @@ LinkerDriver *driver;
 
 bool link(ArrayRef<const char *> args, bool canExitEarly, raw_ostream &stdoutOS,
           raw_ostream &stderrOS) {
+  lld::stdoutOS = &stdoutOS;
+  lld::stderrOS = &stderrOS;
+
   errorHandler().logName = args::getFilenameWithoutExe(args[0]);
   errorHandler().errorLimitExceededMsg =
       "too many errors emitted, stopping now"
       " (use /errorlimit:0 to see all errors)";
   errorHandler().exitEarly = canExitEarly;
-  enableColors(stderrOS.has_colors());
-
-  lld::stdoutOS = &stdoutOS;
-  lld::stderrOS = &stderrOS;
+  stderrOS.enable_colors(stderrOS.has_colors());
 
   config = make<Configuration>();
   symtab = make<SymbolTable>();

diff  --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp
index cb23c9bf7349..301a5c5efa8a 100644
--- a/lld/COFF/DriverUtils.cpp
+++ b/lld/COFF/DriverUtils.cpp
@@ -773,15 +773,15 @@ static void handleColorDiagnostics(opt::InputArgList &args) {
   if (!arg)
     return;
   if (arg->getOption().getID() == OPT_color_diagnostics) {
-    enableColors(true);
+    lld::errs().enable_colors(true);
   } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
-    enableColors(false);
+    lld::errs().enable_colors(false);
   } else {
     StringRef s = arg->getValue();
     if (s == "always")
-      enableColors(true);
+      lld::errs().enable_colors(true);
     else if (s == "never")
-      enableColors(false);
+      lld::errs().enable_colors(false);
     else if (s != "auto")
       error("unknown option: --color-diagnostics=" + s);
   }

diff  --git a/lld/Common/ErrorHandler.cpp b/lld/Common/ErrorHandler.cpp
index e26d78c28715..91ea6799907e 100644
--- a/lld/Common/ErrorHandler.cpp
+++ b/lld/Common/ErrorHandler.cpp
@@ -51,8 +51,6 @@ ErrorHandler &lld::errorHandler() {
   return handler;
 }
 
-void lld::enableColors(bool enable) { lld::errs().enable_colors(enable); }
-
 void lld::exitLld(int val) {
   // Delete any temporary file, while keeping the memory mapping open.
   if (errorHandler().outputBuffer)

diff  --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 25a9b2920547..f7ce7358bb78 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -77,15 +77,15 @@ static void readConfigs(opt::InputArgList &args);
 
 bool link(ArrayRef<const char *> args, bool canExitEarly, raw_ostream &stdoutOS,
           raw_ostream &stderrOS) {
+  lld::stdoutOS = &stdoutOS;
+  lld::stderrOS = &stderrOS;
+
   errorHandler().logName = args::getFilenameWithoutExe(args[0]);
   errorHandler().errorLimitExceededMsg =
       "too many errors emitted, stopping now (use "
       "-error-limit=0 to see all errors)";
   errorHandler().exitEarly = canExitEarly;
-  enableColors(stderrOS.has_colors());
-
-  lld::stdoutOS = &stdoutOS;
-  lld::stderrOS = &stderrOS;
+  stderrOS.enable_colors(stderrOS.has_colors());
 
   inputSections.clear();
   outputSections.clear();

diff  --git a/lld/ELF/DriverUtils.cpp b/lld/ELF/DriverUtils.cpp
index 53134d0cbfe1..9fcb36e81676 100644
--- a/lld/ELF/DriverUtils.cpp
+++ b/lld/ELF/DriverUtils.cpp
@@ -59,15 +59,15 @@ static void handleColorDiagnostics(opt::InputArgList &args) {
   if (!arg)
     return;
   if (arg->getOption().getID() == OPT_color_diagnostics) {
-    enableColors(true);
+    lld::errs().enable_colors(true);
   } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
-    enableColors(false);
+    lld::errs().enable_colors(false);
   } else {
     StringRef s = arg->getValue();
     if (s == "always")
-      enableColors(true);
+      lld::errs().enable_colors(true);
     else if (s == "never")
-      enableColors(false);
+      lld::errs().enable_colors(false);
     else if (s != "auto")
       error("unknown option: --color-diagnostics=" + s);
   }

diff  --git a/lld/MinGW/Driver.cpp b/lld/MinGW/Driver.cpp
index 950156a8a8f5..1a256b44efb8 100644
--- a/lld/MinGW/Driver.cpp
+++ b/lld/MinGW/Driver.cpp
@@ -161,10 +161,11 @@ searchLibrary(StringRef name, ArrayRef<StringRef> searchPaths, bool bStatic) {
 // then call coff::link.
 bool mingw::link(ArrayRef<const char *> argsArr, bool canExitEarly,
                  raw_ostream &stdoutOS, raw_ostream &stderrOS) {
-  enableColors(stderrOS.has_colors());
   lld::stdoutOS = &stdoutOS;
   lld::stderrOS = &stderrOS;
 
+  stderrOS.enable_colors(stderrOS.has_colors());
+
   MinGWOptTable parser;
   opt::InputArgList args = parser.parse(argsArr.slice(1));
 

diff  --git a/lld/include/lld/Common/ErrorHandler.h b/lld/include/lld/Common/ErrorHandler.h
index ac6f93fc088b..8eed38af8c88 100644
--- a/lld/include/lld/Common/ErrorHandler.h
+++ b/lld/include/lld/Common/ErrorHandler.h
@@ -117,8 +117,6 @@ class ErrorHandler {
 /// Returns the default error handler.
 ErrorHandler &errorHandler();
 
-void enableColors(bool enable);
-
 inline void error(const Twine &msg) { errorHandler().error(msg); }
 inline LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &msg) {
   errorHandler().fatal(msg);

diff  --git a/lld/lib/Driver/DarwinLdDriver.cpp b/lld/lib/Driver/DarwinLdDriver.cpp
index d372e6c94c59..10e0aa3d9fee 100644
--- a/lld/lib/Driver/DarwinLdDriver.cpp
+++ b/lld/lib/Driver/DarwinLdDriver.cpp
@@ -1145,15 +1145,15 @@ static void createFiles(MachOLinkingContext &ctx, bool Implicit) {
 /// This is where the link is actually performed.
 bool link(llvm::ArrayRef<const char *> args, bool CanExitEarly,
           raw_ostream &StdoutOS, raw_ostream &StderrOS) {
+  lld::stdoutOS = &StdoutOS;
+  lld::stderrOS = &StderrOS;
+
   errorHandler().logName = args::getFilenameWithoutExe(args[0]);
   errorHandler().errorLimitExceededMsg =
       "too many errors emitted, stopping now (use "
       "'-error-limit 0' to see all errors)";
   errorHandler().exitEarly = CanExitEarly;
-  enableColors(StderrOS.has_colors());
-
-  lld::stdoutOS = &StdoutOS;
-  lld::stderrOS = &StderrOS;
+  StderrOS.enable_colors(StderrOS.has_colors());
 
   MachOLinkingContext ctx;
   if (!parse(args, ctx))

diff  --git a/lld/wasm/Driver.cpp b/lld/wasm/Driver.cpp
index d1e81caf2c56..80a717a533c7 100644
--- a/lld/wasm/Driver.cpp
+++ b/lld/wasm/Driver.cpp
@@ -80,14 +80,14 @@ class LinkerDriver {
 
 bool link(ArrayRef<const char *> args, bool canExitEarly, raw_ostream &stdoutOS,
           raw_ostream &stderrOS) {
+  lld::stdoutOS = &stdoutOS;
+  lld::stderrOS = &stderrOS;
+
   errorHandler().logName = args::getFilenameWithoutExe(args[0]);
   errorHandler().errorLimitExceededMsg =
       "too many errors emitted, stopping now (use "
       "-error-limit=0 to see all errors)";
-  enableColors(stderrOS.has_colors());
-
-  lld::stdoutOS = &stdoutOS;
-  lld::stderrOS = &stderrOS;
+  stderrOS.enable_colors(stderrOS.has_colors());
 
   config = make<Configuration>();
   symtab = make<SymbolTable>();
@@ -135,15 +135,15 @@ static void handleColorDiagnostics(opt::InputArgList &args) {
   if (!arg)
     return;
   if (arg->getOption().getID() == OPT_color_diagnostics) {
-    enableColors(true);
+    lld::errs().enable_colors(true);
   } else if (arg->getOption().getID() == OPT_no_color_diagnostics) {
-    enableColors(false);
+    lld::errs().enable_colors(false);
   } else {
     StringRef s = arg->getValue();
     if (s == "always")
-      enableColors(true);
+      lld::errs().enable_colors(true);
     else if (s == "never")
-      enableColors(false);
+      lld::errs().enable_colors(false);
     else if (s != "auto")
       error("unknown option: --color-diagnostics=" + s);
   }


        


More information about the llvm-commits mailing list