[PATCH] D49013: [Support] Allow formatv() to consume llvm::Error by-value without crashing.

Zachary Turner via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 9 15:03:44 PDT 2018


zturner added a comment.

There are two more possibilities I can think of.  Re-writing your original code sample in 2 different ways:

  // Method 1
  llvm::Expected<Foo> MaybeFoo = ...
  if (!MaybeFoo) {
    Error E = MaybeFoo.takeError();
    llvm::errs() << formatv("Error: {0}", E);
    consumeError(std::move(E));
  }



  // Method 2
  llvm::Expected<Foo> MaybeFoo = ...
  if (!MaybeFoo)
    llvm::errs() << formatv("Error: {0}", fmt_consume_error(MaybeFoo.takeError()));

The former is more verbose and may be the best solution unless you're finding that you need to format errors very very often.  It has the advantage of requiring no code change at all

The latter is a bit more verbose (perhaps we can make the name more concise), but at least it makes it explicit that the error object will be destroyed as a result of this call.

The latter could be done by modeling a `FormatAdapter` after the existing ones like `fmt_repeat`, `fmt_pad`, etc.

Thoughts?


Repository:
  rL LLVM

https://reviews.llvm.org/D49013





More information about the llvm-commits mailing list