Skip to content

Commit 45ef0d4

Browse files
committed
Add llvm::Error C API, LLVMCantFail
It's barely testable - the test does exercise the code, but wouldn't fail on an empty implementation. It would cause a memory leak though (because the error handle wouldn't be unwrapped/reowned) which could be detected by asan and other leak detectors.
1 parent 3715035 commit 45ef0d4

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

llvm/include/llvm-c/Error.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err);
5151
*/
5252
void LLVMConsumeError(LLVMErrorRef Err);
5353

54+
/**
55+
* Report a fatal error if Err is a failure value.
56+
*
57+
* This function can be used to wrap calls to fallible functions ONLY when it is
58+
* known that the Error will always be a success value.
59+
*/
60+
void LLVMCantFail(LLVMErrorRef Err);
61+
5462
/**
5563
* Returns the given string's error message. This operation consumes the error,
5664
* and the given LLVMErrorRef value is not usable once this call returns.

llvm/lib/Support/Error.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,12 @@ LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err) {
182182

183183
void LLVMConsumeError(LLVMErrorRef Err) { consumeError(unwrap(Err)); }
184184

185+
186+
187+
void LLVMCantFail(LLVMErrorRef Err) {
188+
cantFail(unwrap(Err));
189+
}
190+
185191
char *LLVMGetErrorMessage(LLVMErrorRef Err) {
186192
std::string Tmp = toString(unwrap(Err));
187193
char *ErrMsg = new char[Tmp.size() + 1];

llvm/unittests/Support/ErrorTest.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,8 @@ TEST(Error, C_API) {
930930
});
931931
EXPECT_TRUE(GotCSE) << "Failed to round-trip ErrorList via C API";
932932
EXPECT_TRUE(GotCE) << "Failed to round-trip ErrorList via C API";
933+
934+
LLVMCantFail(wrap(Error::success()));
933935
}
934936

935937
TEST(Error, FileErrorTest) {

0 commit comments

Comments
 (0)