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

Michael Spencer bigcheesegs at gmail.com
Wed Dec 12 21:40:02 PST 2012


On Wed, Dec 12, 2012 at 6:38 PM, Nick Kledzik <kledzik at apple.com> wrote:
>
> 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?

It's not. It's generic for any error code. That was just an example.

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

I could add an overload of == for enable_if<is_error_code_enum<T>::value...

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

- Michael Spencer



More information about the llvm-commits mailing list