Skip to content

Commit

Permalink
[llvm-c] Add non-cstring versions of LLVMGetNamedFunction and LLVMGet…
Browse files Browse the repository at this point in the history
…NamedGlobal (llvm#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 new functions are more convenient for C programs that use
non-null-terminated strings or for languages like Rust that primarily
use non-null-terminated strings.
  • Loading branch information
wr7 authored Aug 16, 2024
1 parent 7e23a23 commit e8e8887
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions llvm/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ Changes to the C API
* ``LLVMX86MMXTypeInContext``
* ``LLVMX86MMXType``

* The following functions are added to further support non-null-terminated strings:

* ``LLVMGetNamedFunctionWithLength``
* ``LLVMGetNamedGlobalWithLength``

Changes to the CodeGen infrastructure
-------------------------------------

Expand Down
12 changes: 12 additions & 0 deletions llvm/include/llvm-c/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -2633,6 +2643,8 @@ 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);
Expand Down
10 changes: 10 additions & 0 deletions llvm/lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2203,6 +2203,11 @@ 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();
Expand Down Expand Up @@ -2381,6 +2386,11 @@ 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();
Expand Down

0 comments on commit e8e8887

Please sign in to comment.