add lifetime and no escape macros #896
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Add lifetimebound and noescape attribute macros
Although simdutf currently has no use cases for the lifetimebound attribute, define a cross-compiler macro for it to enable potential future usage. This follows the evolving C++ lifetime profile proposals and provides compatibility with MSVC and Clang attributes where available.
Additionally, introduce a macro for Clang's noescape attribute. This attribute indicates that a pointer parameter does not escape the function scope, allowing optimizers to make stronger assumptions. Include detailed explanatory comments describing its purpose, valid and invalid usage examples, current limitations (Clang-only support, declarative only with no static enforcement), and restriction to pointer parameters.
These macros fall back to empty definitions on unsupported compilers to ensure portability.
Add lifetimebound and noescape attribute macros
Although simdutf currently has no use cases for the lifetimebound attribute, define a cross-compiler macro for it to enable potential future usage. This follows the evolving C++ lifetime profile proposals and provides compatibility with MSVC and Clang attributes where available.
The lifetimebound attribute indicates that a pointer (or reference) parameter or return value does not outlive the object it refers to. It helps compilers detect potential lifetime issues, such as dangling pointers or references, by enforcing that the lifetime of the pointed-to object extends at least as long as the function's result or any derived pointers. For example, it could be applied to a function returning a pointer into an input buffer:
An invalid use might return a pointer to a local variable, which the compiler could warn about if the attribute is present.
The noescape attribute signals that the function does not store the pointer (or anything derived from it) in a location accessible after the function returns, such as globals or heap allocations. This enables better optimizations, like assuming the pointer remains valid only within the call. A valid example is local access only:
An invalid example stores it globally:
These macros fall back to empty definitions on unsupported compilers to ensure portability.