Skip to content

Commit 9d54400

Browse files
Module::getOrInsertFunction is using C-style vararg instead of variadic templates.
From a user prospective, it forces the use of an annoying nullptr to mark the end of the vararg, and there's not type checking on the arguments. The variadic template is an obvious solution to both issues. Differential Revision: https://reviews.llvm.org/D31070 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@299949 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent afa9824 commit 9d54400

30 files changed

+171
-200
lines changed

examples/BrainF/BrainF.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@ void BrainF::header(LLVMContext& C) {
7474

7575
//declare i32 @getchar()
7676
getchar_func = cast<Function>(module->
77-
getOrInsertFunction("getchar", IntegerType::getInt32Ty(C), NULL));
77+
getOrInsertFunction("getchar", IntegerType::getInt32Ty(C)));
7878

7979
//declare i32 @putchar(i32)
8080
putchar_func = cast<Function>(module->
8181
getOrInsertFunction("putchar", IntegerType::getInt32Ty(C),
82-
IntegerType::getInt32Ty(C), NULL));
82+
IntegerType::getInt32Ty(C)));
8383

8484
//Function header
8585

8686
//define void @brainf()
8787
brainf_func = cast<Function>(module->
88-
getOrInsertFunction("brainf", Type::getVoidTy(C), NULL));
88+
getOrInsertFunction("brainf", Type::getVoidTy(C)));
8989

9090
builder = new IRBuilder<>(BasicBlock::Create(C, label, brainf_func));
9191

@@ -156,7 +156,7 @@ void BrainF::header(LLVMContext& C) {
156156
//declare i32 @puts(i8 *)
157157
Function *puts_func = cast<Function>(module->
158158
getOrInsertFunction("puts", IntegerType::getInt32Ty(C),
159-
PointerType::getUnqual(IntegerType::getInt8Ty(C)), NULL));
159+
PointerType::getUnqual(IntegerType::getInt8Ty(C))));
160160

161161
//brainf.aberror:
162162
aberrorbb = BasicBlock::Create(C, label, brainf_func);

examples/BrainF/BrainFDriver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void addMainFunction(Module *mod) {
7777
getOrInsertFunction("main", IntegerType::getInt32Ty(mod->getContext()),
7878
IntegerType::getInt32Ty(mod->getContext()),
7979
PointerType::getUnqual(PointerType::getUnqual(
80-
IntegerType::getInt8Ty(mod->getContext()))), NULL));
80+
IntegerType::getInt8Ty(mod->getContext())))));
8181
{
8282
Function::arg_iterator args = main_func->arg_begin();
8383
Value *arg_0 = &*args++;

examples/Fibonacci/fibonacci.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ static Function *CreateFibFunction(Module *M, LLVMContext &Context) {
5454
// to return an int and take an int parameter.
5555
Function *FibF =
5656
cast<Function>(M->getOrInsertFunction("fib", Type::getInt32Ty(Context),
57-
Type::getInt32Ty(Context),
58-
nullptr));
57+
Type::getInt32Ty(Context)));
5958

6059
// Add a basic block to the function.
6160
BasicBlock *BB = BasicBlock::Create(Context, "EntryBlock", FibF);

examples/HowToUseJIT/HowToUseJIT.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,9 @@ int main() {
6969

7070
// Create the add1 function entry and insert this entry into module M. The
7171
// function will have a return type of "int" and take an argument of "int".
72-
// The '0' terminates the list of argument types.
7372
Function *Add1F =
7473
cast<Function>(M->getOrInsertFunction("add1", Type::getInt32Ty(Context),
75-
Type::getInt32Ty(Context),
76-
nullptr));
74+
Type::getInt32Ty(Context)));
7775

7876
// Add a basic block to the function. As before, it automatically inserts
7977
// because of the last argument.
@@ -102,8 +100,7 @@ int main() {
102100
// Now we're going to create function `foo', which returns an int and takes no
103101
// arguments.
104102
Function *FooF =
105-
cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context),
106-
nullptr));
103+
cast<Function>(M->getOrInsertFunction("foo", Type::getInt32Ty(Context)));
107104

108105
// Add a basic block to the FooF function.
109106
BB = BasicBlock::Create(Context, "EntryBlock", FooF);

examples/ParallelJIT/ParallelJIT.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ static Function* createAdd1(Module *M) {
5454
Function *Add1F =
5555
cast<Function>(M->getOrInsertFunction("add1",
5656
Type::getInt32Ty(M->getContext()),
57-
Type::getInt32Ty(M->getContext()),
58-
nullptr));
57+
Type::getInt32Ty(M->getContext())));
5958

6059
// Add a basic block to the function. As before, it automatically inserts
6160
// because of the last argument.
@@ -85,8 +84,7 @@ static Function *CreateFibFunction(Module *M) {
8584
Function *FibF =
8685
cast<Function>(M->getOrInsertFunction("fib",
8786
Type::getInt32Ty(M->getContext()),
88-
Type::getInt32Ty(M->getContext()),
89-
nullptr));
87+
Type::getInt32Ty(M->getContext())));
9088

9189
// Add a basic block to the function.
9290
BasicBlock *BB = BasicBlock::Create(M->getContext(), "EntryBlock", FibF);

include/llvm/IR/Module.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,22 @@ class Module {
321321
/// or a ConstantExpr BitCast of that type if the named function has a
322322
/// different type. This version of the method takes a null terminated list of
323323
/// function arguments, which makes it easier for clients to use.
324-
Constant *getOrInsertFunction(StringRef Name, AttributeList AttributeList,
325-
Type *RetTy, ...) LLVM_END_WITH_NULL;
324+
template<typename... ArgsTy>
325+
Constant *getOrInsertFunction(StringRef Name,
326+
AttributeList AttributeList,
327+
Type *RetTy, ArgsTy... Args)
328+
{
329+
SmallVector<Type*, sizeof...(ArgsTy)> ArgTys{Args...};
330+
return getOrInsertFunction(Name,
331+
FunctionType::get(RetTy, ArgTys, false),
332+
AttributeList);
333+
}
326334

327335
/// Same as above, but without the attributes.
328-
Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ...)
329-
LLVM_END_WITH_NULL;
336+
template<typename... ArgsTy>
337+
Constant *getOrInsertFunction(StringRef Name, Type *RetTy, ArgsTy... Args) {
338+
return getOrInsertFunction(Name, AttributeList{}, RetTy, Args...);
339+
}
330340

331341
/// Look up the specified function in the module symbol table. If it does not
332342
/// exist, return null.

lib/CodeGen/CountingFunctionInserter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ namespace {
4141
Type *VoidTy = Type::getVoidTy(F.getContext());
4242
Constant *CountingFn =
4343
F.getParent()->getOrInsertFunction(CountingFunctionName,
44-
VoidTy, nullptr);
44+
VoidTy);
4545
CallInst::Create(CountingFn, "", &*F.begin()->getFirstInsertionPt());
4646
return true;
4747
}

lib/CodeGen/IntrinsicLowering.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,21 +115,21 @@ void IntrinsicLowering::AddPrototypes(Module &M) {
115115
Type::getInt8PtrTy(Context),
116116
Type::getInt8PtrTy(Context),
117117
Type::getInt8PtrTy(Context),
118-
DL.getIntPtrType(Context), nullptr);
118+
DL.getIntPtrType(Context));
119119
break;
120120
case Intrinsic::memmove:
121121
M.getOrInsertFunction("memmove",
122122
Type::getInt8PtrTy(Context),
123123
Type::getInt8PtrTy(Context),
124124
Type::getInt8PtrTy(Context),
125-
DL.getIntPtrType(Context), nullptr);
125+
DL.getIntPtrType(Context));
126126
break;
127127
case Intrinsic::memset:
128128
M.getOrInsertFunction("memset",
129129
Type::getInt8PtrTy(Context),
130130
Type::getInt8PtrTy(Context),
131131
Type::getInt32Ty(M.getContext()),
132-
DL.getIntPtrType(Context), nullptr);
132+
DL.getIntPtrType(Context));
133133
break;
134134
case Intrinsic::sqrt:
135135
EnsureFPIntrinsicsExist(M, F, "sqrtf", "sqrt", "sqrtl");

lib/CodeGen/MachineOutliner.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ MachineOutliner::createOutlinedFunction(Module &M, const OutlinedFunction &OF,
10981098
// Create the function using an IR-level function.
10991099
LLVMContext &C = M.getContext();
11001100
Function *F = dyn_cast<Function>(
1101-
M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C), nullptr));
1101+
M.getOrInsertFunction(NameStream.str(), Type::getVoidTy(C)));
11021102
assert(F && "Function was null!");
11031103

11041104
// NOTE: If this is linkonceodr, then we can take advantage of linker deduping

lib/CodeGen/SafeStack.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ void SafeStack::checkStackGuard(IRBuilder<> &IRB, Function &F, ReturnInst &RI,
451451
IRBuilder<> IRBFail(CheckTerm);
452452
// FIXME: respect -fsanitize-trap / -ftrap-function here?
453453
Constant *StackChkFail = F.getParent()->getOrInsertFunction(
454-
"__stack_chk_fail", IRB.getVoidTy(), nullptr);
454+
"__stack_chk_fail", IRB.getVoidTy());
455455
IRBFail.CreateCall(StackChkFail, {});
456456
}
457457

0 commit comments

Comments
 (0)