================
@@ -3284,3 +3284,125 @@ void assign_non_capturing_to_function_ref(function_ref
&r) {
}
} // namespace GH126600
+
+//===----------------------------------------------------------------------===//
+// lifetime-capture-by
+//===----------------------------------------------------------------------===//
+
+void setCaptureBy(View& res, View in [[clang::lifetime_capture_by(res)]]);
+
+void use_after_free_capture_by() {
+ View res;
+ {
+ MyObj a;
+ setCaptureBy(res, a); // expected-warning {{object whose reference is
captured does not live long enough}}
+ } // expected-note {{destroyed here}}
+ (void)res; // expected-note {{later used here}}
+}
+
+View use_after_return_capture_by() {
+ MyObj a;
+ View res;
+ setCaptureBy(res, a); // expected-warning {{address of stack memory is
returned later}}
+ return res; // expected-note {{returned here}}
+
+}
+
+void transitive_capture() {
+ View v1, v2;
+ {
+ MyObj local;
+ setCaptureBy(v1, local); // expected-warning {{object whose reference is
captured does not live long enough}}
+ setCaptureBy(v2, v1);
+ } // expected-note {{destroyed here}}
+ (void)v2; // expected-note {{later used here}}
+}
+
+void set1(View& res, const MyObj& in [[clang::lifetime_capture_by(res)]]);
+
+void test_reference_to_view() {
+ View v;
+ {
+ MyObj local;
+ set1(v, local); // expected-warning {{object whose reference is captured
does not live long enough}}
+ } // expected-note {{destroyed here}}
+ (void)v; // expected-note {{later used here}}
+}
+
+// FIXME: Add special handling for multi-level pointers and lvalue expressions
which are not DeclRefExpr.
+void set2(MyObj** res, const MyObj& in [[clang::lifetime_capture_by(res)]]);
+
+void test_pointer_to_pointer() {
+ MyObj *ptr = nullptr;
+ {
+ MyObj local;
+ set2(&ptr, local);
+ }
+ (void)ptr;
+}
----------------
usx95 wrote:
Can you also try
```cpp
void test_pointer_to_pointer_2(MyObj **ptr) {
MyObj *ptr = nullptr;
{
MyObj local;
set2(ptr, local);
}
(void)ptr;
}
```
Should work likely.
https://github.com/llvm/llvm-project/pull/196884
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits