[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
diff --git a/llvm/lib/Support/Error.cpp b/llvm/lib/Support/Error.cpp
index 83345bf..d66e0e5 100644
--- a/llvm/lib/Support/Error.cpp
+++ b/llvm/lib/Support/Error.cpp
@@ -126,6 +126,26 @@
report_fatal_error(ErrMsg);
}
+} // end namespace llvm
+
+LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err) {
+ return reinterpret_cast<ErrorInfoBase *>(Err)->dynamicClassID();
+}
+
+void LLVMConsumeError(LLVMErrorRef Err) { consumeError(unwrap(Err)); }
+
+char *LLVMGetErrorMessage(LLVMErrorRef Err) {
+ std::string Tmp = toString(unwrap(Err));
+ char *ErrMsg = new char[Tmp.size() + 1];
+ memcpy(ErrMsg, Tmp.data(), Tmp.size());
+ ErrMsg[Tmp.size()] = '\0';
+ return ErrMsg;
+}
+
+void LLVMDisposeErrorMessage(char *ErrMsg) { delete[] ErrMsg; }
+
+LLVMErrorTypeId LLVMGetStringErrorTypeId() {
+ return reinterpret_cast<void *>(&StringError::ID);
}
#ifndef _MSC_VER