summaryrefslogtreecommitdiffstats
path: root/llvm/unittests/Support/ErrorTest.cpp
Commit message (Collapse)AuthorAgeFilesLines
* Add a cantFail overload for Expected-reference (Expected<T&>) types.Lang Hames2017-06-201-0/+4
| | | | llvm-svn: 305863
* [Support][Error] Add a 'cantFail' utility function for known-safe calls toLang Hames2017-02-271-0/+28
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | fallible functions. Some fallible functions (those returning Error or Expected<T>) may only fail for a subset of their inputs. For example, a "safe" square root function will succeed for all finite positive inputs: Expected<double> safeSqrt(double d) { if (d < 0 && !isnan(d) && !isinf(d)) return make_error<...>("Cannot sqrt -ve values, nans or infs"); return sqrt(d); } At a safe callsite for such a function, checking the error return value is redundant: if (auto ValOrErr = safeSqrt(42.0)) { // use *ValOrErr. } else llvm_unreachable("safeSqrt should always succeed for +ve values"); The cantFail function wraps this check and extracts the contained value, simplifying control flow: double Result = cantFail(safeSqrt(42.0)); This function should be used with care: it is a programmatic error to wrap a call with cantFail if it can in fact fail. For debug builds this will result in llvm_unreachable being called. For release builds the behavior is undefined. Use of this function is likely to be rare in library code, but more common for tool and unit-test code where inputs and mock functions may be known to be safe. llvm-svn: 296384
* Fix macro check for ABI breacking check: should use #if instead of #ifndefMehdi Amini2016-11-301-10/+10
| | | | llvm-svn: 288265
* Change Error unittest to use the LLVM_ENABLE_ABI_BREAKING_CHECKS instead of ↵Mehdi Amini2016-11-291-10/+10
| | | | | | | | | | NDEBUG This is consistent with the header (after r288087) and fixes the test for the configuration: -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ABI_BREAKING_CHECKS=FORCE_OFF llvm-svn: 288196
* Prevent at compile time converting from Error::success() to Expected<T>Mehdi Amini2016-11-111-10/+11
| | | | | | | | This would trigger an assertion at runtime otherwise. Differential Revision: https://reviews.llvm.org/D26482 llvm-svn: 286562
* Make the Error class constructor protectedMehdi Amini2016-11-111-2/+4
| | | | | | | | | This is forcing to use Error::success(), which is in a wide majority of cases a lot more readable. Differential Revision: https://reviews.llvm.org/D26481 llvm-svn: 286561
* [Support] Make ErrorAsOutParameter take an Error* rather than an Error&.Lang Hames2016-07-221-1/+1
| | | | | | | | | | | | | | | | | | | | | | This allows ErrorAsOutParameter to work better with "optional" errors. For example, consider a function where for certain input values it is known that the function can't fail. This can now be written as: Result foo(Arg X, Error *Err) { ErrorAsOutParameter EAO(Err); if (<Error Condition>) { if (Err) *Err = <report error>; else llvm_unreachable("Unexpected failure!"); } } Rather than having to construct an ErrorAsOutParameter under every conditional where Err is known to be non-null. llvm-svn: 276430
* [Support] Fix a bug in ErrorList::join / joinErrors.Lang Hames2016-06-301-0/+45
| | | | | | | | When concatenating two error lists the ErrorList::join method (which is called by joinErrors) was failing to set the checked bit on the second error, leading to a 'failure to check error' assertion. llvm-svn: 274249
* [Support] Rename unconvertibleErrorCode to inconvertibleErrorCode.Lang Hames2016-05-271-1/+1
| | | | | | | Based on a totally scientific, 30 second google search "in-" appears to be the preferred prefix. llvm-svn: 270950
* [Support] Add a StringError convenience class to Error.hLang Hames2016-05-271-0/+16
| | | | | | | | StringError can be used to represent Errors that aren't recoverable based on the error type, but that have a useful error message that can be reported to the user or logged. llvm-svn: 270948
* [Support] Add a free toString function for ErrorVedant Kumar2016-05-031-0/+19
| | | | | | | | | | toString() consumes an Error and returns a string representation of its contents. This commit also adds a message() method to ErrorInfoBase for convenience. Differential Revision: http://reviews.llvm.org/D19883 llvm-svn: 268465
* [Support] Fix latent bugs in Expected and ExitOnError that were preventing themLang Hames2016-04-251-0/+14
| | | | | | from working with reference types. llvm-svn: 267448
* [Support] Add a checked flag to Expected<T>, require checks before access orLang Hames2016-04-051-4/+46
| | | | | | | | | | destruction. This makes the Expected<T> class behave like Error, even when in success mode. Expected<T> values must be checked to see whether they contain an error prior to being dereferenced, assigned to, or destructed. llvm-svn: 265446
* [Support] Switch to RAII helper for error-as-out-parameter idiom.Lang Hames2016-03-251-4/+24
| | | | | | As discussed on the llvm-commits thread for r264467. llvm-svn: 264479
* [Support] Add Error::errorForOutParameter helper.Lang Hames2016-03-251-0/+6
| | | | | | | | This helper method creates a pre-checked Error suitable for use as an out parameter in a constructor. This avoids the need to have the constructor check a known-good error before assigning to it. llvm-svn: 264467
* Try to fix ODR violation of ErrorInfo::IDReid Kleckner2016-03-241-3/+10
| | | | | | This implements my suggestion to Lang. llvm-svn: 264360
* ErrorTest.cpp: Move instantiations out of anonymous namespace. gcc didn't ↵NAKAMURA Takumi2016-03-241-4/+3
| | | | | | complain. llvm-svn: 264297
* Define ErrorInfo::ID explicitly.NAKAMURA Takumi2016-03-241-0/+4
| | | | llvm-svn: 264293
* ErrorTest.cpp: Fix an expression, possibly typo.NAKAMURA Takumi2016-03-241-1/+1
| | | | llvm-svn: 264290
* [Support] Add conversions between Expected<T> and ErrorOr<T>.Lang Hames2016-03-241-1/+24
| | | | | | More utilities to help with std::error_code -> Error transitions. llvm-svn: 264238
* [Support] Make all Errors convertible to std::error_code.Lang Hames2016-03-231-0/+9
| | | | | | | | | | | | This is a temporary crutch to enable code that currently uses std::error_code to be incrementally moved over to Error. Requiring all Error instances be convertible enables clients to call errorToErrorCode on any error (not just ECErrors created by conversion *from* an error_code). This patch also moves code for Error from ErrorHandling.cpp into a new Error.cpp file. llvm-svn: 264221
* [Support] Refactor Error unit tests to avoid duplicating work.Lang Hames2016-03-181-294/+229
| | | | | | Suggested by Dave Blaikie in review for r263749. Thanks Dave! llvm-svn: 263768
* [Support] Add ExitOnError utility to support tools that use the exit-on-errorLang Hames2016-03-171-0/+28
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | idiom. Most LLVM tool code exits immediately when an error is encountered and prints an error message to stderr. The ExitOnError class supports this by providing two call operators - one for Errors, and one for Expected<T>s. Calls to code that can return Errors (or Expected<T>s) can use these calls to bail out on error, and otherwise continue as if the operation had succeeded. E.g. Error foo(); Expected<int> bar(); int main(int argc, char *argv[]) { ExitOnError ExitOnErr; ExitOnErr.setBanner(std::string("Error in ") + argv[0] + ":"); // Exit if foo returns an error. No need to manually check error return. ExitOnErr(foo()); // Exit if bar returns an error, otherwise unwrap the contained int and // continue. int X = ExitOnErr(bar()); // ... return 0; } llvm-svn: 263749
* [Support] Make Error::isA<T>() works on success values.Lang Hames2016-03-171-0/+3
| | | | llvm-svn: 263745
* [Support] Update Error unit test to remove implementation specific behaviour.Lang Hames2016-03-161-2/+1
| | | | llvm-svn: 263610
* [Support] Add the 'Error' class for structured error handling.Lang Hames2016-03-161-0/+458
This patch introduces the Error classs for lightweight, structured, recoverable error handling. It includes utilities for creating, manipulating and handling errors. The scheme is similar to exceptions, in that errors are described with user-defined types. Unlike exceptions however, errors are represented as ordinary return types in the API (similar to the way std::error_code is used). For usage notes see the LLVM programmer's manual, and the Error.h header. Usage examples can be found in unittests/Support/ErrorTest.cpp. Many thanks to David Blaikie, Mehdi Amini, Kevin Enderby and others on the llvm-dev and llvm-commits lists for lots of discussion and review. llvm-svn: 263609
OpenPOWER on IntegriCloud