llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Add [[clang::lifetimebound]] to numerous functions in libc++ include headers

Open higher-performance opened this issue 4 months ago • 15 comments

This is a fix for #62390 and supersedes #112614.

Adds [[clang::lifetimebound]] to numerous methods such as back(), begin(), data(), emplace_back(), find(), insert(), operator[], etc., since all of their outputs require the container/owner to be alive for validity. It allows analyzing many more cases with lifetimebound, such as:

std::vector<T> get_elements();
const T &item = get_elements()[1];  // dangling reference

The changes were generated via the following Perl command:

git checkout "$(git merge-base HEAD upstream/HEAD)" -- libcxx/include && find libcxx/include -xdev -type f -not -name "*span*" -not -name "*view*" -not -name "*initializer_list*" -exec perl -0777 -pi -e 's/((?<!static )_LIBCPP_HIDE_FROM_ABI\b(?:(?!\bstatic\b)[^{()};])*(?:(?:(?:iterator|reference|[*&])(?:(?:,\s+bool)?>)?\s+(?:c?(?:after_|before_)?(?:begin|end)(?=[(][)])|at|front|back|find|lower_bound|upper_bound|equal_range|(?:emplace|erase|insert|operator\[\](?![(](?:diff|ptrdiff))|value)(?:_\w*)?)|pointer (?:data|get(?:_if)?)|operator\s+__self_view)[(][^{()};]*[)](?:noexcept(?:\s*[(](?:[^()]*|[^()]*[(][^()]*[)][^()]*)[)])?|(?!(?:_LIBCPP_LIFETIMEBOUND|requires)\b)\w+|\s+\b)*))(\s*)([{;]| requires\b)/$1 _LIBCPP_LIFETIMEBOUND$2$3/g' '{}' \+

higher-performance avatar Oct 17 '24 17:10 higher-performance