[llvm] [nfc] Common utility macros for `Error` / `Expect<T>` (PR #92150)
David Blaikie via llvm-commits
llvm-commits at lists.llvm.org
Mon May 20 14:43:41 PDT 2024
================
@@ -1420,6 +1420,32 @@ inline Error unwrap(LLVMErrorRef ErrRef) {
reinterpret_cast<ErrorInfoBase *>(ErrRef)));
}
+/// Common scenarios macros for handling Error or Expect<T> returns
+/// Forward Error.
+#ifndef RETURN_IF_ERROR
+#define RETURN_IF_ERROR(Expr) \
+ if (auto Err = (Expr)) \
+ return Err;
+#endif
+
+/// Assign Var as Expect<T>, and forward the Error. The user decides how to
+/// consume the contained value - move it, or take a reference.
+#ifndef ASSIGN_OR_RETURN
+#define ASSIGN_OR_RETURN(Var, Expr) \
----------------
dwblaikie wrote:
The name is a bit misleading, since this doesn't involve the assignment operator - wonder if there's a better name for it? Looks like this version in proto ( https://github.com/protocolbuffers/protobuf/blob/main/src/google/protobuf/stubs/status_macros.h#L52 ) does support assignment (essentially takes the "auto Var" part as a template parameter - so it can declare a new variable with `=` initialization, or can be used to assign to a pre-existing variable. That seems suitable to be named `ASSIGN_OR_RETURN`, even if some uses of it don't really use assignment, but at least some can/do.
Probably would be good to support that kind of usage too, with assignment? But I understand it's a more complex implementation.
Oh, and it assigns to the value type, not Expected<Value> - which is nice...
https://github.com/llvm/llvm-project/pull/92150
More information about the llvm-commits
mailing list