[clang] [llvm] clang: fix lifetime_capture_by indexing for member functions (PR #212264)

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 29 07:50:07 PDT 2026


================
@@ -4272,3 +4272,92 @@ void test_cyclic_cfg(int n) {
   }         // expected-note {{local variable 'a' is destroyed here}}
   v.use();  // expected-note {{later used here}}
 }
+
+namespace member_capture_by_param {
+struct S {
+  S(int *p [[clang::lifetime_capture_by(c)]], int *&c);
+  void cap(int *p [[clang::lifetime_capture_by(c)]], int *&c);
+  void cap_const(int *p [[clang::lifetime_capture_by(c)]], int *&c) const;
+  static void cap_static(int *p [[clang::lifetime_capture_by(c)]], int *&c);
+
+  template <typename T>
+  void cap_template(T *p [[clang::lifetime_capture_by(c)]], T *&c);
+
+  void cap_overload(int *p [[clang::lifetime_capture_by(c)]], int *&c);
+  void cap_overload(double *p [[clang::lifetime_capture_by(c)]], double *&c);
+
+  void cap_multi(int *p [[clang::lifetime_capture_by(c1, c2)]], int *&c1,
+                 int *&c2);
+  void cap_reordered(int *&c, int *p [[clang::lifetime_capture_by(c)]]);
+};
+
+namespace ns {
+void cap_ns(int *p [[clang::lifetime_capture_by(c)]], int *&c);
+} // namespace ns
+
+void use(int*);
+void test_member_capture_by() {
+  int *c1;
+  {
+    int i;
+    S s;
+    s.cap(&i, c1); // expected-warning {{local variable 'i' does not live long enough}}
+  } // expected-note {{local variable 'i' is destroyed here}}
+  use(c1); // expected-note {{later used here}}
----------------
usx95 wrote:

Could you please match the indents of `//` in expectations to match the style in rest of the file. Similar for all the tests below.

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


More information about the cfe-commits mailing list