[lld] r292240 - COFF: add error() and warn() to Error.{cpp,h}

Bob Haarman via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 17 11:07:45 PST 2017


Author: inglorion
Date: Tue Jan 17 13:07:42 2017
New Revision: 292240

URL: http://llvm.org/viewvc/llvm-project?rev=292240&view=rev
Log:
COFF: add error() and warn() to Error.{cpp,h}

Summary: This copies over some functionality we have in ELF/Error.{cpp,h} and makes it available in COFF/Error.{cpp,h}

Reviewers: pcc, rafael, ruiu

Subscribers:

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

Modified:
    lld/trunk/COFF/Config.h
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/Error.cpp
    lld/trunk/COFF/Error.h
    lld/trunk/include/lld/Driver/Driver.h

Modified: lld/trunk/COFF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Config.h?rev=292240&r1=292239&r2=292240&view=diff
==============================================================================
--- lld/trunk/COFF/Config.h (original)
+++ lld/trunk/COFF/Config.h Tue Jan 17 13:07:42 2017
@@ -80,8 +80,10 @@ struct Configuration {
   SymbolBody *Entry = nullptr;
   bool NoEntry = false;
   std::string OutputFile;
+  bool ColorDiagnostics;
   bool DoGC = true;
   bool DoICF = true;
+  uint64_t ErrorLimit = 20;
   bool Relocatable = true;
   bool Force = false;
   bool Debug = false;

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=292240&r1=292239&r2=292240&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Tue Jan 17 13:07:42 2017
@@ -55,8 +55,13 @@ BumpPtrAllocator BAlloc;
 StringSaver Saver{BAlloc};
 std::vector<SpecificAllocBase *> SpecificAllocBase::Instances;
 
-bool link(ArrayRef<const char *> Args) {
+bool link(ArrayRef<const char *> Args, raw_ostream &Diag) {
+  ErrorCount = 0;
+  ErrorOS = &Diag;
+  Argv0 = Args[0];
   Config = make<Configuration>();
+  Config->ColorDiagnostics =
+      (ErrorOS == &llvm::errs() && Process::StandardErrHasColors());
   Driver = make<LinkerDriver>();
   Driver->link(Args);
   return true;

Modified: lld/trunk/COFF/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Error.cpp?rev=292240&r1=292239&r2=292240&view=diff
==============================================================================
--- lld/trunk/COFF/Error.cpp (original)
+++ lld/trunk/COFF/Error.cpp Tue Jan 17 13:07:42 2017
@@ -8,11 +8,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "Error.h"
+#include "Config.h"
 
 #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>
 
 #if !defined(_MSC_VER) && !defined(__MINGW32__)
 #include <unistd.h>
@@ -21,10 +24,55 @@
 using namespace llvm;
 
 namespace lld {
+// The functions defined in this file can be called from multiple threads,
+// but outs() or errs() are not thread-safe. We protect them using a mutex.
+static std::mutex Mu;
+
 namespace coff {
+StringRef Argv0;
+uint64_t ErrorCount;
+raw_ostream *ErrorOS;
+
+static LLVM_ATTRIBUTE_NORETURN void exitLld(int Val) {
+  // Dealloc/destroy ManagedStatic variables before calling
+  // _exit(). In a non-LTO build, this is a nop. In an LTO
+  // build allows us to get the output of -time-passes.
+  llvm_shutdown();
+
+  outs().flush();
+  errs().flush();
+  _exit(Val);
+}
+
+static void print(StringRef S, raw_ostream::Colors C) {
+  *ErrorOS << Argv0 + ": ";
+  if (Config->ColorDiagnostics) {
+    ErrorOS->changeColor(C, true);
+    *ErrorOS << S;
+    ErrorOS->resetColor();
+  } else {
+    *ErrorOS << S;
+  }
+}
+
+void error(const Twine &Msg) {
+  std::lock_guard<std::mutex> Lock(Mu);
+
+  if (Config->ErrorLimit == 0 || ErrorCount < Config->ErrorLimit) {
+    print("error: ", raw_ostream::RED);
+    *ErrorOS << Msg << "\n";
+  } else if (ErrorCount == Config->ErrorLimit) {
+    print("error: ", raw_ostream::RED);
+    *ErrorOS << "too many errors emitted, stopping now"
+             << " (use -error-limit=0 to see all errors)\n";
+    exitLld(1);
+  }
+
+  ++ErrorCount;
+}
 
 void fatal(const Twine &Msg) {
-  if (sys::Process::StandardErrHasColors()) {
+  if (Config->ColorDiagnostics) {
     errs().changeColor(raw_ostream::RED, /*bold=*/true);
     errs() << "error: ";
     errs().resetColor();
@@ -32,10 +80,7 @@ void fatal(const Twine &Msg) {
     errs() << "error: ";
   }
   errs() << Msg << "\n";
-
-  outs().flush();
-  errs().flush();
-  _exit(1);
+  exitLld(1);
 }
 
 void fatal(std::error_code EC, const Twine &Msg) {
@@ -46,5 +91,11 @@ void fatal(llvm::Error &Err, const Twine
   fatal(errorToErrorCode(std::move(Err)), Msg);
 }
 
+void warn(const Twine &Msg) {
+  std::lock_guard<std::mutex> Lock(Mu);
+  print("warning: ", raw_ostream::MAGENTA);
+  *ErrorOS << Msg << "\n";
+}
+
 } // namespace coff
 } // namespace lld

Modified: lld/trunk/COFF/Error.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Error.h?rev=292240&r1=292239&r2=292240&view=diff
==============================================================================
--- lld/trunk/COFF/Error.h (original)
+++ lld/trunk/COFF/Error.h Tue Jan 17 13:07:42 2017
@@ -16,6 +16,12 @@
 namespace lld {
 namespace coff {
 
+extern uint64_t ErrorCount;
+extern llvm::raw_ostream *ErrorOS;
+extern llvm::StringRef Argv0;
+
+void warn(const Twine &Msg);
+void error(const Twine &Msg);
 LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
 LLVM_ATTRIBUTE_NORETURN void fatal(std::error_code EC, const Twine &Prefix);
 LLVM_ATTRIBUTE_NORETURN void fatal(llvm::Error &Err, const Twine &Prefix);

Modified: lld/trunk/include/lld/Driver/Driver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Driver/Driver.h?rev=292240&r1=292239&r2=292240&view=diff
==============================================================================
--- lld/trunk/include/lld/Driver/Driver.h (original)
+++ lld/trunk/include/lld/Driver/Driver.h Tue Jan 17 13:07:42 2017
@@ -15,7 +15,8 @@
 
 namespace lld {
 namespace coff {
-bool link(llvm::ArrayRef<const char *> Args);
+bool link(llvm::ArrayRef<const char *> Args,
+          llvm::raw_ostream &Diag = llvm::errs());
 }
 
 namespace elf {




More information about the llvm-commits mailing list