aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/Support/ErrorTest.cpp
diff options
context:
space:
mode:
authorLang Hames <lhames@gmail.com>2018-08-15 18:42:11 +0000
committerLang Hames <lhames@gmail.com>2018-08-15 18:42:11 +0000
commit00fb14da274efc35afa4eba6019050bbcf605ead (patch)
tree4bc31d4a6a20d3091c7406ba15bafcca3d3bfc5f /llvm/unittests/Support/ErrorTest.cpp
parent5222cb601b9a04df93cc3603ede80cc0e3dc9940 (diff)
downloadllvm-00fb14da274efc35afa4eba6019050bbcf605ead.zip
llvm-00fb14da274efc35afa4eba6019050bbcf605ead.tar.gz
llvm-00fb14da274efc35afa4eba6019050bbcf605ead.tar.bz2
[Support] Add a basic C API for llvm::Error.
Summary: The C-API supports consuming errors, converting an error to a string error message, and querying an error's type. Other LLVM C APIs that wish to use llvm::Error can supply error-type-id checkers and custom error-to-structured-type converters for any custom errors they provide. Reviewers: bogner, zturner, labath, dblaikie Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D50716 llvm-svn: 339802
Diffstat (limited to 'llvm/unittests/Support/ErrorTest.cpp')
-rw-r--r--llvm/unittests/Support/ErrorTest.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/llvm/unittests/Support/ErrorTest.cpp b/llvm/unittests/Support/ErrorTest.cpp
index 66ffd23..0b1a489 100644
--- a/llvm/unittests/Support/ErrorTest.cpp
+++ b/llvm/unittests/Support/ErrorTest.cpp
@@ -8,6 +8,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/Error.h"
+#include "llvm-c/Error.h"
#include "llvm/ADT/Twine.h"
#include "llvm/Support/Errc.h"
@@ -832,4 +833,35 @@ TEST(Error, ErrorMatchers) {
" Actual: failed (CustomError {0})");
}
+TEST(Error, C_API) {
+ EXPECT_THAT_ERROR(unwrap(wrap(Error::success())), Succeeded())
+ << "Failed to round-trip Error success value via C API";
+ EXPECT_THAT_ERROR(unwrap(wrap(make_error<CustomError>(0))),
+ Failed<CustomError>())
+ << "Failed to round-trip Error failure value via C API";
+
+ auto Err =
+ wrap(make_error<StringError>("test message", inconvertibleErrorCode()));
+ EXPECT_EQ(LLVMGetErrorTypeId(Err), LLVMGetStringErrorTypeId())
+ << "Failed to match error type ids via C API";
+ char *ErrMsg = LLVMGetErrorMessage(Err);
+ EXPECT_STREQ(ErrMsg, "test message")
+ << "Failed to roundtrip StringError error message via C API";
+ LLVMDisposeErrorMessage(ErrMsg);
+
+ bool GotCSE = false;
+ bool GotCE = false;
+ handleAllErrors(
+ unwrap(wrap(joinErrors(make_error<CustomSubError>(42, 7),
+ make_error<CustomError>(42)))),
+ [&](CustomSubError &CSE) {
+ GotCSE = true;
+ },
+ [&](CustomError &CE) {
+ GotCE = true;
+ });
+ EXPECT_TRUE(GotCSE) << "Failed to round-trip ErrorList via C API";
+ EXPECT_TRUE(GotCE) << "Failed to round-trip ErrorList via C API";
+}
+
} // end anon namespace