[llvm] [llvm-c] Add non-cstring versions of LLVMGetNamedFunction and LLVMGetNamedGlobal (PR #103396)

via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 13 12:19:40 PDT 2024


https://github.com/wr7 created https://github.com/llvm/llvm-project/pull/103396

Add `LLVMGetNamedFunctionWithLength` and `LLVMGetNamedGlobalWithLength`

As far as i know, it isn't currently possible to use `LLVMGetNamedFunction` and `LLVMGetNamedGlobal` with non-null-terminated strings.

These functions may be more convenient for C programs that do not use null-terminated strings or for languages like Rust that primarily do not use null-terminated strings.

>From b0445e87ccb4363b94ebbdaa9b01f63ca8e492c2 Mon Sep 17 00:00:00 2001
From: wr7 <d-wr7 at outlook.com>
Date: Tue, 13 Aug 2024 13:49:16 -0500
Subject: [PATCH] [llvm-c] Add non-cstring versions of LLVMGetNamedFunction and
 LLVMGetNamedGlobal

---
 llvm/include/llvm-c/Core.h | 11 +++++++++++
 llvm/lib/IR/Core.cpp       |  8 ++++++++
 2 files changed, 19 insertions(+)

diff --git a/llvm/include/llvm-c/Core.h b/llvm/include/llvm-c/Core.h
index 4bde82d816e35..896a5e6193a4f 100644
--- a/llvm/include/llvm-c/Core.h
+++ b/llvm/include/llvm-c/Core.h
@@ -1179,6 +1179,16 @@ LLVMValueRef LLVMAddFunction(LLVMModuleRef M, const char *Name,
  */
 LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name);
 
+
+/**
+ * Obtain a Function value from a Module by its name.
+ *
+ * The returned value corresponds to a llvm::Function value.
+ *
+ * @see llvm::Module::getFunction()
+ */
+LLVMValueRef LLVMGetNamedFunctionWithLength(LLVMModuleRef M, const char *Name, size_t Length);
+
 /**
  * Obtain an iterator to the first Function in a Module.
  *
@@ -2633,6 +2643,7 @@ LLVMValueRef LLVMAddGlobalInAddressSpace(LLVMModuleRef M, LLVMTypeRef Ty,
                                          const char *Name,
                                          unsigned AddressSpace);
 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name);
+LLVMValueRef LLVMGetNamedGlobalWithLength(LLVMModuleRef M, const char *Name, size_t Length);
 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M);
 LLVMValueRef LLVMGetLastGlobal(LLVMModuleRef M);
 LLVMValueRef LLVMGetNextGlobal(LLVMValueRef GlobalVar);
diff --git a/llvm/lib/IR/Core.cpp b/llvm/lib/IR/Core.cpp
index 17c0bf72ef05d..52c58c273e868 100644
--- a/llvm/lib/IR/Core.cpp
+++ b/llvm/lib/IR/Core.cpp
@@ -2203,6 +2203,10 @@ LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
   return wrap(unwrap(M)->getNamedGlobal(Name));
 }
 
+LLVMValueRef LLVMGetNamedGlobalWithLength(LLVMModuleRef M, const char *Name, size_t Length) {
+  return wrap(unwrap(M)->getNamedGlobal(StringRef(Name, Length)));
+}
+
 LLVMValueRef LLVMGetFirstGlobal(LLVMModuleRef M) {
   Module *Mod = unwrap(M);
   Module::global_iterator I = Mod->global_begin();
@@ -2381,6 +2385,10 @@ LLVMValueRef LLVMGetNamedFunction(LLVMModuleRef M, const char *Name) {
   return wrap(unwrap(M)->getFunction(Name));
 }
 
+LLVMValueRef LLVMGetNamedFunctionWithLength(LLVMModuleRef M, const char *Name, size_t Length) {
+  return wrap(unwrap(M)->getFunction(StringRef(Name, Length)));
+}
+
 LLVMValueRef LLVMGetFirstFunction(LLVMModuleRef M) {
   Module *Mod = unwrap(M);
   Module::iterator I = Mod->begin();



More information about the llvm-commits mailing list