[PATCH] D87272: [lld] Buffer writes when composing a single diagnostic

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 09:00:31 PDT 2020


MaskRay updated this revision to Diff 290499.
MaskRay added a comment.

Address comments


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D87272/new/

https://reviews.llvm.org/D87272

Files:
  lld/Common/ErrorHandler.cpp
  llvm/include/llvm/Support/raw_ostream.h


Index: llvm/include/llvm/Support/raw_ostream.h
===================================================================
--- llvm/include/llvm/Support/raw_ostream.h
+++ llvm/include/llvm/Support/raw_ostream.h
@@ -301,6 +301,8 @@
   // changeColor() has no effect until enable_colors(true) is called.
   virtual void enable_colors(bool enable) { ColorEnabled = enable; }
 
+  bool colors_enabled() const { return ColorEnabled; }
+
   /// Tie this stream to the specified stream. Replaces any existing tied-to
   /// stream. Specifying a nullptr unties the stream.
   void tie(raw_ostream *TieTo) { TiedStream = TieTo; }
Index: lld/Common/ErrorHandler.cpp
===================================================================
--- lld/Common/ErrorHandler.cpp
+++ lld/Common/ErrorHandler.cpp
@@ -155,8 +155,11 @@
 void ErrorHandler::log(const Twine &msg) {
   if (!verbose)
     return;
+  SmallString<256> buf;
+  raw_svector_ostream os(buf);
+  os << logName << ": " << msg << '\n';
   std::lock_guard<std::mutex> lock(mu);
-  lld::errs() << logName << ": " << msg << "\n";
+  lld::errs() << buf;
 }
 
 void ErrorHandler::message(const Twine &msg) {
@@ -171,9 +174,21 @@
     return;
   }
 
-  std::lock_guard<std::mutex> lock(mu);
-  lld::errs() << sep << getLocation(msg) << ": " << Colors::MAGENTA
-              << "warning: " << Colors::RESET << msg << "\n";
+  SmallString<256> buf;
+  raw_svector_ostream os(buf);
+  os << sep << getLocation(msg) << ": ";
+  if (lld::errs().colors_enabled()) {
+    os.enable_colors(true);
+    os << Colors::MAGENTA << "warning: " << Colors::RESET;
+  } else {
+    os << "warning: ";
+  }
+  os << msg << '\n';
+
+  {
+    std::lock_guard<std::mutex> lock(mu);
+    lld::errs() << buf;
+  }
   sep = getSeparator(msg);
 }
 
@@ -196,16 +211,25 @@
 
   bool exit = false;
   {
-    std::lock_guard<std::mutex> lock(mu);
+    if (errorLimit == 0 || errorCount <= errorLimit) {
+      SmallString<256> buf;
+      raw_svector_ostream os(buf);
+      os << sep << getLocation(msg) << ": ";
+      if (lld::errs().colors_enabled()) {
+        os.enable_colors(true);
+        os << Colors::RED << "error: " << Colors::RESET;
+      } else {
+        os << "error: ";
+      }
+      if (errorLimit == 0 || errorCount < errorLimit) {
+        os << msg << '\n';
+      } else {
+        os << errorLimitExceededMsg << '\n';
+        exit = exitEarly;
+      }
 
-    if (errorLimit == 0 || errorCount < errorLimit) {
-      lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
-                  << "error: " << Colors::RESET << msg << "\n";
-    } else if (errorCount == errorLimit) {
-      lld::errs() << sep << getLocation(msg) << ": " << Colors::RED
-                  << "error: " << Colors::RESET << errorLimitExceededMsg
-                  << "\n";
-      exit = exitEarly;
+      std::lock_guard<std::mutex> lock(mu);
+      lld::errs() << buf;
     }
 
     sep = getSeparator(msg);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D87272.290499.patch
Type: text/x-patch
Size: 2928 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200908/3da21526/attachment.bin>


More information about the llvm-commits mailing list