llvm::ErrorAsOutParameter

Helper for Errors used as out-parameters.

Synopsis

Declared in <llvm/Support/Error.h>

class ErrorAsOutParameter;

Description

This helper is for use with the Error-as-out-parameter idiom, where an error is passed to a function or method by reference, rather than being returned. In such cases it is helpful to set the checked bit on entry to the function so that the error can be written to (unchecked Errors abort on assignment) and clear the checked bit on exit so that clients cannot accidentally forget to check the result. This helper performs these actions automatically using RAII:


Result foo(Error &Err) {
  ErrorAsOutParameter ErrAsOutParam(&Err); // 'Checked' flag set
  // <body of foo>
  // <- 'Checked' flag auto-cleared when ErrAsOutParam is destructed.
}

ErrorAsOutParameter takes an Error* rather than Error& so that it can be used with optional Errors (Error pointers that are allowed to be null). If ErrorAsOutParameter took an Error reference, an instance would have to be created inside every condition that verified that Error was non-null. By taking an Error pointer we can just create one instance at the top of the function.

Member Functions

NameDescription
ErrorAsOutParameter [constructor]Constructors
~ErrorAsOutParameter [destructor]Clear the checked bit on destruction if Err still holds success.