aboutsummaryrefslogtreecommitdiff
path: root/llvm/unittests/IR/FunctionTest.cpp
diff options
context:
space:
mode:
authorAMS21 <AMS21.github@gmail.com>2025-10-07 16:49:39 +0200
committerGitHub <noreply@github.com>2025-10-07 14:49:39 +0000
commit01f4510118b8a9a3ca1f7f1e4b19bd2e918c7dc0 (patch)
treea982683165768f995093664a2a72a8f859fc3f50 /llvm/unittests/IR/FunctionTest.cpp
parent6d44b9082e42b918a152098ec70ed409c4da8c79 (diff)
downloadllvm-01f4510118b8a9a3ca1f7f1e4b19bd2e918c7dc0.zip
llvm-01f4510118b8a9a3ca1f7f1e4b19bd2e918c7dc0.tar.gz
llvm-01f4510118b8a9a3ca1f7f1e4b19bd2e918c7dc0.tar.bz2
[LLVM-C] Upstream `LLVMGetOrInsertFunction` from rustc (#162235)
Add `LLVMGetOrInsertFunction` to the C API as a thin wrapper over `Module::getOrInsertFunction`, upstreamed from [rustc](https://github.com/rust-lang/rust/blob/d773bd07d63a74adcf25ea5f4aae986be94cac5e/compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp#L203). It provides a single-call way to get or create a function declaration, avoiding `LLVMGetNamedFunction` + `LLVMAddFunction` and is more idiomatic.
Diffstat (limited to 'llvm/unittests/IR/FunctionTest.cpp')
-rw-r--r--llvm/unittests/IR/FunctionTest.cpp19
1 files changed, 19 insertions, 0 deletions
diff --git a/llvm/unittests/IR/FunctionTest.cpp b/llvm/unittests/IR/FunctionTest.cpp
index 7ba7584..8ed7699 100644
--- a/llvm/unittests/IR/FunctionTest.cpp
+++ b/llvm/unittests/IR/FunctionTest.cpp
@@ -625,4 +625,23 @@ TEST(FunctionTest, Personality) {
EXPECT_FALSE(LLVMHasPersonalityFn(wrap(F)));
}
+TEST(FunctionTest, LLVMGetOrInsertFunction) {
+ LLVMContext Ctx;
+ Module M("test", Ctx);
+ Type *Int8Ty = Type::getInt8Ty(Ctx);
+ FunctionType *FTy = FunctionType::get(Int8Ty, false);
+
+ // Create the function using the C API
+ LLVMValueRef FuncRef = LLVMGetOrInsertFunction(wrap(&M), "F", 1, wrap(FTy));
+
+ // Verify that the returned value is a function and has the correct type
+ Function *Func = unwrap<Function>(FuncRef);
+ EXPECT_EQ(Func->getName(), "F");
+ EXPECT_EQ(Func->getFunctionType(), FTy);
+
+ // Call LLVMGetOrInsertFunction again to ensure it returns the same function
+ LLVMValueRef FuncRef2 = LLVMGetOrInsertFunction(wrap(&M), "F", 1, wrap(FTy));
+ EXPECT_EQ(FuncRef, FuncRef2);
+}
+
} // end namespace