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

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 9 01:44:41 PDT 2020


jhenderson added inline comments.


================
Comment at: lld/Common/ErrorHandler.cpp:216
+    if (errorLimit == 0 || errorCount <= errorLimit) {
+      raw_ostream &errs = lld::errs();
+      SmallString<256> buf;
----------------
MaskRay wrote:
> jhenderson wrote:
> > This code is very similar to the warning code. Can you de-duplicate it with a helper function?
> `os` is used after the common part and requires a `SmallString`, so it is a bit difficult to share code.
How about something like this:

```
void ErrorHandler::reportDiagnostic(Colors c, StringRef diagKind, StringRef location, const Twine &msg) {
  SmallString<256> buf;
  raw_svector_ostream os(buf);
  os << sep << location << ": ";
  if (lld::errs().colors_enabled()) {
    os.enable_colors(true);
    os << c << diagKind << ": " << Colors::
  } else {
    os << diagKind << ": ";
  }
  os << msg << '\n';
  std::lock_guard<std::mutex> lock(mu);
  lld::errs() << buf;
}

void ErrorHandler::warn(const Twine &msg) {
  ...
  reportDiagnostic(Colors::MAGENTA, "warning", getLocation(msg), msg);
  sep = getSeparator(msg);
}

void ErrorHandler::error(const Twine &msg) {
  ...
  if (errorLimit == 0 || errorCount <= errorLimit) {
    if (errorLimit != 0 && errorCount == errorLimit) {
      reportDiagnostic(Colors::RED, "error", getLocation(msg), errorLimitExceededMsg);
      exit = exitEarly;
    } else {
      reportDiagnostic(Colors::RED, "error", getLocation(msg), msg);
    }
    sep = getSeparator(msg);
    ++errorCount;
  }
```


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D87272



More information about the llvm-commits mailing list