[llvm] Add CallBase::getCalledFunctionName (PR #127038)

Thomas Symalla via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 13 02:01:23 PST 2025


https://github.com/tsymalla created https://github.com/llvm/llvm-project/pull/127038

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.

>From 065431f845b7e1954a3cc53070223ad5fe50512e Mon Sep 17 00:00:00 2001
From: Thomas Symalla <github at thomassymalla.de>
Date: Thu, 13 Feb 2025 10:53:43 +0100
Subject: [PATCH] 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.
---
 llvm/include/llvm/IR/InstrTypes.h | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/llvm/include/llvm/IR/InstrTypes.h b/llvm/include/llvm/IR/InstrTypes.h
index 90fe864d4ae71..c55a5a589fc53 100644
--- a/llvm/include/llvm/IR/InstrTypes.h
+++ b/llvm/include/llvm/IR/InstrTypes.h
@@ -1345,6 +1345,16 @@ class CallBase : public Instruction {
     return nullptr;
   }
 
+  /// Shortcut to retrieving the name of the called function.
+  /// Returns std::nullopt, if the function cannot be found.
+  std::optional<StringRef> getCalledFunctionName() const {
+    Function *F = getCalledFunction();
+    if (F)
+      return F->getName();
+
+    return std::nullopt;
+  }
+
   /// Return true if the callsite is an indirect call.
   bool isIndirectCall() const;
 



More information about the llvm-commits mailing list