[llvm] Add StringMap::lookup() overload to take a default value and return a reference to the value to avoid a copy (PR #115469)
Jakub Kuderski via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 8 05:07:59 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 {
----------------
kuhar wrote:
This seems like quite a footgun to me: the lifetime of the default value won't be extended beyond the lookup expression, whereas the looked up object will be kept alive by the map.
Consider this code:
```c++
StringMap<SmallVector<int>> foo;
const auto &ints = foo.lookup("X", SmallVector<int>{});
```
I think `ints` will be a dangling reference.
https://github.com/llvm/llvm-project/pull/115469
More information about the llvm-commits
mailing list