[llvm] 45ef0d4 - Add llvm::Error C API, LLVMCantFail

David Blaikie via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 31 09:40:33 PDT 2024


Author: David Blaikie
Date: 2024-07-31T16:40:26Z
New Revision: 45ef0d492f7b61613d0833fbf7eafdd41e137139

URL: https://github.com/llvm/llvm-project/commit/45ef0d492f7b61613d0833fbf7eafdd41e137139
DIFF: https://github.com/llvm/llvm-project/commit/45ef0d492f7b61613d0833fbf7eafdd41e137139.diff

LOG: 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.

Added: 
    

Modified: 
    llvm/include/llvm-c/Error.h
    llvm/lib/Support/Error.cpp
    llvm/unittests/Support/ErrorTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm-c/Error.h b/llvm/include/llvm-c/Error.h
index c3baaf65186aa..874bbcfe8f21a 100644
--- a/llvm/include/llvm-c/Error.h
+++ b/llvm/include/llvm-c/Error.h
@@ -51,6 +51,14 @@ LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err);
  */
 void LLVMConsumeError(LLVMErrorRef Err);
 
+/**
+ * Report a fatal error if Err is a failure value.
+ *
+ * This function can be used to wrap calls to fallible functions ONLY when it is
+ * known that the Error will always be a success value.
+ */
+void LLVMCantFail(LLVMErrorRef Err);
+
 /**
  * Returns the given string's error message. This operation consumes the error,
  * and the given LLVMErrorRef value is not usable once this call returns.

diff  --git a/llvm/lib/Support/Error.cpp b/llvm/lib/Support/Error.cpp
index 93481ca916d2a..baa3c322e9dae 100644
--- a/llvm/lib/Support/Error.cpp
+++ b/llvm/lib/Support/Error.cpp
@@ -182,6 +182,12 @@ LLVMErrorTypeId LLVMGetErrorTypeId(LLVMErrorRef Err) {
 
 void LLVMConsumeError(LLVMErrorRef Err) { consumeError(unwrap(Err)); }
 
+
+
+void LLVMCantFail(LLVMErrorRef Err) {
+  cantFail(unwrap(Err));
+}
+
 char *LLVMGetErrorMessage(LLVMErrorRef Err) {
   std::string Tmp = toString(unwrap(Err));
   char *ErrMsg = new char[Tmp.size() + 1];

diff  --git a/llvm/unittests/Support/ErrorTest.cpp b/llvm/unittests/Support/ErrorTest.cpp
index bd098a4988dc5..4cd9896c3b08e 100644
--- a/llvm/unittests/Support/ErrorTest.cpp
+++ b/llvm/unittests/Support/ErrorTest.cpp
@@ -930,6 +930,8 @@ TEST(Error, C_API) {
     });
   EXPECT_TRUE(GotCSE) << "Failed to round-trip ErrorList via C API";
   EXPECT_TRUE(GotCE) << "Failed to round-trip ErrorList via C API";
+
+  LLVMCantFail(wrap(Error::success()));
 }
 
 TEST(Error, FileErrorTest) {


        


More information about the llvm-commits mailing list