In a nutshell,
- Go over all instructions in the function.
- If the instruction is a
CallInst
, check if it's a call tosprintf
(you can just check its name). - Create a new
CallInst
(via CallInst::Create) that callsprintf
instead ofsprintf
. I think the easiest way to get theValue
of theprintf
declaration is to use one of theModule::getOrCreate
methods. The type ofprintf
should be the same as the one forsprintf
minus the first parameter. - Set the operands of the new call instructions to be the same as for
sprintf
, minus the first parameter. - Replace the old call with the new call -
replaceInstWithInst
(in BasicBlockUtils.h) is probably the most convenient way. - Finally, you might want to track the usages of the old first parameter, remove them all and then remove it - so you'd get rid of that
puts
call.