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

via llvm-commits llvm-commits at lists.llvm.org
Thu Feb 13 02:02:13 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-ir

Author: Thomas Symalla (tsymalla)

<details>
<summary>Changes</summary>

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.

---
Full diff: https://github.com/llvm/llvm-project/pull/127038.diff


1 Files Affected:

- (modified) llvm/include/llvm/IR/InstrTypes.h (+10) 


``````````diff
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;
 

``````````

</details>


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


More information about the llvm-commits mailing list