[PATCH] Add ErrorOr<>::getOrDie() which uses report_fatal_error instead of assert.

Justin Bogner mail at justinbogner.com
Thu Oct 9 12:44:29 PDT 2014


Alexander Kornienko <alexfh at google.com> writes:
> This method allows writing code that consistently fails in case of
> dereferencing an ErrorOr<> containing no value rather than crashing in
> debug and using uninitialized data in release build.

I don't see why this would be better than doing this check in the
caller, for two reasons:

1. Situations where report_fatal_error is the right thing are fairly
   rare, so I wouldn't expect it to be a lot of boilerplate.

2. The caller generally can and should report a more specific error
   message.

> http://reviews.llvm.org/D5706
>
> Files:
>   include/llvm/Support/ErrorOr.h
>
> Index: include/llvm/Support/ErrorOr.h
> ===================================================================
> --- include/llvm/Support/ErrorOr.h
> +++ include/llvm/Support/ErrorOr.h
> @@ -18,6 +18,7 @@
>  
>  #include "llvm/ADT/PointerIntPair.h"
>  #include "llvm/Support/AlignOf.h"
> +#include "llvm/Support/ErrorHandling.h"
>  #include <cassert>
>  #include <system_error>
>  #include <type_traits>
> @@ -175,6 +176,15 @@
>    reference get() { return *getStorage(); }
>    const_reference get() const { return const_cast<ErrorOr<T> >(this)->get(); }
>  
> +  reference getOrDie() {
> +    if (HasError)
> +      report_fatal_error("Cannot get value when an error exists!", false);
> +    return *getStorage();
> +  }
> +  const_reference getOrDie() const {
> +    return const_cast<ErrorOr<T>>(this)->getOrDie();
> +  }
> +
>    std::error_code getError() const {
>      return HasError ? *getErrorStorage() : std::error_code();
>    }
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits



More information about the llvm-commits mailing list