[llvm] [llvm-c] Add C API methods to match size_t ConstantDataArray C++ API signatures (PR #84433)

via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 7 23:05:33 PST 2024


https://github.com/erer1243 updated https://github.com/llvm/llvm-project/pull/84433

>From 6a05fd079050bacd10a0e6978ce499f5fa0a9ad2 Mon Sep 17 00:00:00 2001
From: erer1243 <erer1243 at users.noreply.github.com>
Date: Fri, 8 Mar 2024 00:15:39 -0500
Subject: [PATCH 1/2] [llvm-c] Add C API methods to match size_t
 ConstantDataArray C++ API signatures

---
 llvm/bindings/ocaml/llvm/llvm_ocaml.c |  8 ++++----
 llvm/include/llvm-c/Core.h            | 25 +++++++++++++++++++++++++
 llvm/lib/IR/Core.cpp                  | 16 ++++++++++++++++
 3 files changed, 45 insertions(+), 4 deletions(-)

diff --git a/llvm/bindings/ocaml/llvm/llvm_ocaml.c b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
index d74d8030cea0de..55679f218b307e 100644
--- a/llvm/bindings/ocaml/llvm/llvm_ocaml.c
+++ b/llvm/bindings/ocaml/llvm/llvm_ocaml.c
@@ -1043,14 +1043,14 @@ value llvm_const_float_of_string(value RealTy, value S) {
 
 /* llcontext -> string -> llvalue */
 value llvm_const_string(value Context, value Str) {
-  return to_val(LLVMConstStringInContext(Context_val(Context), String_val(Str),
-                                         caml_string_length(Str), 1));
+  return to_val(LLVMConstStringInContext2(Context_val(Context), String_val(Str),
+                                          caml_string_length(Str), 1));
 }
 
 /* llcontext -> string -> llvalue */
 value llvm_const_stringz(value Context, value Str) {
-  return to_val(LLVMConstStringInContext(Context_val(Context), String_val(Str),
-                                         caml_string_length(Str), 0));
+  return to_val(LLVMConstStringInContext2(Context_val(Context), String_val(Str),
+                                          caml_string_length(Str), 0));
 }
 
 /* lltype -> llvalue array -> llvalue */
diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 09746bdaf0c94e..6ac0b77b4f2ce8 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -2165,23 +2165,48 @@ double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *losesInfo);
 /**
  * Create a ConstantDataSequential and initialize it with a string.
  *
+ * @deprecated LLVMConstStringInContext is deprecated in favor of the API accurate
+ * LLVMConstStringInContext2
  * @see llvm::ConstantDataArray::getString()
  */
 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
                                       unsigned Length, LLVMBool DontNullTerminate);
 
+/**
+ * Create a ConstantDataSequential and initialize it with a string.
+ *
+ * @see llvm::ConstantDataArray::getString()
+ */
+LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
+                                       size_t Length, LLVMBool DontNullTerminate);
+
 /**
  * Create a ConstantDataSequential with string content in the global context.
  *
  * This is the same as LLVMConstStringInContext except it operates on the
  * global context.
  *
+ * @deprecated LLVMConstString is deprecated in favor of the API accurate
+ * LLVMConstString2
  * @see LLVMConstStringInContext()
  * @see llvm::ConstantDataArray::getString()
  */
 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
                              LLVMBool DontNullTerminate);
 
+/**
+ * Create a ConstantDataSequential with string content in the global context.
+ *
+ * This is the same as LLVMConstStringInContext2 except it operates on the
+ * global context.
+ *
+ * @see LLVMConstStringInContext2()
+ * @see llvm::ConstantDataArray::getString()
+ */
+LLVMValueRef LLVMConstString2(const char *Str, unsigned Length,
+                              LLVMBool DontNullTerminate);
+
+
 /**
  * Returns true if the specified constant is an array of i8.
  *
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index d6d159ab8b9e83..bb9d404790e77e 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -1522,12 +1522,28 @@ LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
                                            DontNullTerminate == 0));
 }
 
+LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
+                                       size_t Length,
+                                       LLVMBool DontNullTerminate) {
+  /* Inverted the sense of AddNull because ', 0)' is a
+     better mnemonic for null termination than ', 1)'. */
+  return wrap(ConstantDataArray::getString(*unwrap(C), StringRef(Str, Length),
+                                           DontNullTerminate == 0));
+}
+
+
 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
                              LLVMBool DontNullTerminate) {
   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,
                                   DontNullTerminate);
 }
 
+LLVMValueRef LLVMConstString2(const char *Str, size_t Length,
+                              LLVMBool DontNullTerminate) {
+  return LLVMConstStringInContext2(LLVMGetGlobalContext(), Str, Length,
+                                   DontNullTerminate);
+}
+
 LLVMValueRef LLVMGetAggregateElement(LLVMValueRef C, unsigned Idx) {
   return wrap(unwrap<Constant>(C)->getAggregateElement(Idx));
 }

>From 6f4c2e040b634d43fa938abc1bade5034289f983 Mon Sep 17 00:00:00 2001
From: erer1243 <erer1243 at users.noreply.github.com>
Date: Fri, 8 Mar 2024 02:05:23 -0500
Subject: [PATCH 2/2] format

---
 llvm/include/llvm-c/Core.h | 8 ++++----
 llvm/lib/IR/Core.cpp       | 1 -
 2 files changed, 4 insertions(+), 5 deletions(-)

diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 6ac0b77b4f2ce8..b21ce1679f314f 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -2165,8 +2165,8 @@ double LLVMConstRealGetDouble(LLVMValueRef ConstantVal, LLVMBool *losesInfo);
 /**
  * Create a ConstantDataSequential and initialize it with a string.
  *
- * @deprecated LLVMConstStringInContext is deprecated in favor of the API accurate
- * LLVMConstStringInContext2
+ * @deprecated LLVMConstStringInContext is deprecated in favor of the API
+ * accurate LLVMConstStringInContext2
  * @see llvm::ConstantDataArray::getString()
  */
 LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
@@ -2178,7 +2178,8 @@ LLVMValueRef LLVMConstStringInContext(LLVMContextRef C, const char *Str,
  * @see llvm::ConstantDataArray::getString()
  */
 LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
-                                       size_t Length, LLVMBool DontNullTerminate);
+                                       size_t Length,
+                                       LLVMBool DontNullTerminate);
 
 /**
  * Create a ConstantDataSequential with string content in the global context.
@@ -2206,7 +2207,6 @@ LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
 LLVMValueRef LLVMConstString2(const char *Str, unsigned Length,
                               LLVMBool DontNullTerminate);
 
-
 /**
  * Returns true if the specified constant is an array of i8.
  *
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index bb9d404790e77e..e15941745de365 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -1531,7 +1531,6 @@ LLVMValueRef LLVMConstStringInContext2(LLVMContextRef C, const char *Str,
                                            DontNullTerminate == 0));
 }
 
-
 LLVMValueRef LLVMConstString(const char *Str, unsigned Length,
                              LLVMBool DontNullTerminate) {
   return LLVMConstStringInContext(LLVMGetGlobalContext(), Str, Length,



More information about the llvm-commits mailing list