Skip to content

Commit 065431f

Browse files
committed
Add CallBase::getCalledFunctionName
There's a lot of uses of `getCalledFunction()->getName()`, without ever checking if `getCalledFunction()` returns a nullptr. Add a new helper that returns an `std::optional<StringRef>`, that can be used to - Avoid method chaining - Have a more safe shortcut to get the callee name.
1 parent 9d92bea commit 065431f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

llvm/include/llvm/IR/InstrTypes.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,16 @@ class CallBase : public Instruction {
13451345
return nullptr;
13461346
}
13471347

1348+
/// Shortcut to retrieving the name of the called function.
1349+
/// Returns std::nullopt, if the function cannot be found.
1350+
std::optional<StringRef> getCalledFunctionName() const {
1351+
Function *F = getCalledFunction();
1352+
if (F)
1353+
return F->getName();
1354+
1355+
return std::nullopt;
1356+
}
1357+
13481358
/// Return true if the callsite is an indirect call.
13491359
bool isIndirectCall() const;
13501360

0 commit comments

Comments
 (0)