[llvm-commits] [PATCH] [lld][ErrorOr] Add support for user data.

Nick Kledzik kledzik at apple.com
Wed Dec 12 18:38:04 PST 2012


On Dec 12, 2012, at 4:45 PM, Michael Spencer wrote:

> Hi jordan_rose, silvas,
> 
> This adds support for user data to ErrorOr<T>.
> 
> struct InvalidArgError {
>  InvalidArgError() {}
>  InvalidArgError(std::string S) : ArgName(S) {}
>  std::string ArgName;
> };
> 
> namespace lld {
> template<>
> struct ErrorOrUserDataTraits<InvalidArgError> : std::true_type {
>  static error_code error() {
>    return make_error_code(errc::invalid_argument);
>  }
> };
> } // end namespace lld
> 
> using namespace lld;
> 
> ErrorOr<int> foo() {
>  return InvalidArgError("adena");
> }
> 
> int main() {
>  auto a = foo();
>  if (!a && error_code(a) == errc::invalid_argument)
>    llvm::errs() << a.getError<InvalidArgError>().ArgName << "\n";
> }

I like what this is trying to accomplish!

ArgName seems a strange name when used above.  Perhaps "message" would read better as an ivar name. Or have a conversion operator to convert it to a StringRef, then no name would be needed by clients.

Why is this limited to just errc::invalid_argument?  What if I wanted to return a string along with a different error code?  

The check for the existence of the string:
   error_code(a) == errc::invalid_argument
seems cumbersome can that be simplified?

Or what if the an optional message string was added to ErrorOr<> along side the error_code??

-Nick





More information about the llvm-commits mailing list