llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Zhijie Wang (aeft) <details> <summary>Changes</summary> --- Full diff: https://github.com/llvm/llvm-project/pull/189508.diff 1 Files Affected: - (modified) clang/docs/LifetimeSafety.rst (+25-2) ``````````diff diff --git a/clang/docs/LifetimeSafety.rst b/clang/docs/LifetimeSafety.rst index ac76231dede67..efebb61699ee7 100644 --- a/clang/docs/LifetimeSafety.rst +++ b/clang/docs/LifetimeSafety.rst @@ -145,8 +145,8 @@ LifetimeBound The ``[[clang::lifetimebound]]`` attribute can be applied to function parameters or to the implicit ``this`` parameter of a method (by placing it after the -method declarator). It indicates that the returned pointer or reference becomes -invalid when the attributed parameter or ``this`` object is destroyed. +method declarator). It indicates that the returned value becomes invalid when +the attributed parameter or ``this`` object is destroyed. This is crucial for functions that return views or references to their arguments. @@ -172,6 +172,29 @@ Without ``[[clang::lifetimebound]]`` on ``getView()``, the analysis would not know that the value returned by ``getView()`` depends on the temporary ``MyOwner`` object, and it would not be able to diagnose the dangling ``sv``. +The analysis also tracks record types returned from functions and constructors +with ``[[clang::lifetimebound]]`` parameters: + +.. code-block:: c++ + + #include <string> + + struct S { + S(const std::string &s [[clang::lifetimebound]]); + }; + + S getS(const std::string &s [[clang::lifetimebound]]); + + void test() { + S a(std::string("temp")); // warning: object whose reference is captured does not live long enough + // note: destroyed here + (void)a; // note: later used here + + S b = getS(std::string("temp")); // warning: object whose reference is captured does not live long enough + // note: destroyed here + (void)b; // note: later used here + } + For more details, see `lifetimebound <https://clang.llvm.org/docs/AttributeReference.html#lifetimebound>`_. NoEscape `````````` </details> https://github.com/llvm/llvm-project/pull/189508 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
