[clang] [clang] Introduce [[clang::lifetime_capture_by(X)]] (PR #111499)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 11 02:57:04 PST 2024


================
@@ -3967,6 +3967,69 @@ Attribute ``trivial_abi`` has no effect in the following cases:
   }];
 }
 
+
+def LifetimeCaptureByDocs : Documentation {
+  let Category = DocCatFunction;
+  let Content = [{
+    The ``lifetime_capture_by(X)`` attribute on a function parameter or implicit object
+parameter indicates that references to arguments passed to such parameters may be
+captured by the capturing entity ``X``.
+
+The capturing entity ``X`` can be one of the following:
+- Another (named) function parameter. 
+  
+  .. code-block:: c++
+
+    void addToSet(std::string_view a [[clang::lifetime_capture_by(s)]], std::set<std::string_view>& s) {
+      s.insert(a);
+    }
+
+- ``this`` (in case of member functions).
+  
+  .. code-block:: c++
+
+    class S {
+      void addToSet(std::string_view a [[clang::lifetime_capture_by(this)]]) {
+        s.insert(a);
+      }
+      std::set<std::string_view> s;
+    };
+
+- 'global', 'unknown' (without quotes).
+  
+  .. code-block:: c++
+
+    std::set<std::string_view> s;
+    void addToSet(std::string_view a [[clang::lifetime_capture_by(global)]]) {
----------------
Xazax-hun wrote:

One question is, how would this behave when there is an identifies collision. E.g.:
```
void addToSet(std::string_view a [[clang::lifetime_capture_by(global)]], bool global)
```
?

https://github.com/llvm/llvm-project/pull/111499


More information about the cfe-commits mailing list