[llvm] Add StringMap::lookup() overload to take a default value and return a reference to the value to avoid a copy (PR #115469)
Boaz Brickner via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 8 07:53:57 PST 2024
================
@@ -257,6 +257,15 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
return ValueTy();
}
+ /// lookup - Return the entry for the specified key, or a default
+ /// provided value if no such entry exists.
+ const ValueTy &lookup(StringRef Key, const ValueTy& Value) const {
----------------
bricknerb wrote:
I agree that you could use this in a wrong way, but I'm not sure it's not true for many other use cases we're already familiar with.
In the future, we could probably make this more safe if we use the `lifetimebound` attribute.
For examples of APIs that rely on a parameter outliving the returned value, a quick in ADT search shows there are examples (partial list, and I only searched for const reference cases):
* https://github.com/llvm/llvm-project/blob/7844257fc2afe490ae4b923a770d20dabed5c3c6/llvm/include/llvm/ADT/APInt.h#L2217
* https://github.com/llvm/llvm-project/blob/7844257fc2afe490ae4b923a770d20dabed5c3c6/llvm/include/llvm/ADT/ConcurrentHashtable.h#L89
https://github.com/llvm/llvm-project/pull/115469
More information about the llvm-commits
mailing list