| 1 |
// RUN: %clang_cc1 -std=c++23 -verify %s |
| 2 |
|
| 3 |
struct S { |
| 4 |
const int *x; |
| 5 |
void captureInt(const int&x [[clang::lifetime_capture_by(this)]]) { this->x = &x; } |
| 6 |
}; |
| 7 |
|
| 8 |
/////////////////////////// |
| 9 |
// Test for valid usages. |
| 10 |
/////////////////////////// |
| 11 |
[[clang::lifetime_capture_by(unknown)]] // expected-error {{'clang::lifetime_capture_by' attribute only applies to parameters and implicit object parameters}} |
| 12 |
void nonMember( |
| 13 |
const int &x1 [[clang::lifetime_capture_by(s, t)]], |
| 14 |
S &s, |
| 15 |
S &t, |
| 16 |
const int &x2 [[clang::lifetime_capture_by(12345 + 12)]], // expected-error {{'lifetime_capture_by' attribute argument '12345 + 12' is not a known function parameter; must be a function parameter, 'this', 'global' or 'unknown'}} |
| 17 |
const int &x3 [[clang::lifetime_capture_by(abcdefgh)]], // expected-error {{'lifetime_capture_by' attribute argument 'abcdefgh' is not a known function parameter; must be a function parameter, 'this', 'global' or 'unknown'}} |
| 18 |
const int &x4 [[clang::lifetime_capture_by("abcdefgh")]], // expected-error {{'lifetime_capture_by' attribute argument '"abcdefgh"' is not a known function parameter; must be a function parameter, 'this', 'global' or 'unknown'}} |
| 19 |
const int &x5 [[clang::lifetime_capture_by(this)]], // expected-error {{'lifetime_capture_by' argument references unavailable implicit 'this'}} |
| 20 |
const int &x6 [[clang::lifetime_capture_by()]], // expected-error {{'lifetime_capture_by' attribute specifies no capturing entity}} |
| 21 |
const int& x7 [[clang::lifetime_capture_by(u, |
| 22 |
x7)]], // expected-error {{'lifetime_capture_by' argument references itself}} |
| 23 |
const int &x8 [[clang::lifetime_capture_by(global)]], |
| 24 |
const int &x9 [[clang::lifetime_capture_by(unknown)]], |
| 25 |
const int &test_memory_leak[[clang::lifetime_capture_by(x1,x2, x3, x4, x5, x6, x7, x8, x9)]], |
| 26 |
const S& u |
| 27 |
) |
| 28 |
{ |
| 29 |
s.captureInt(x1); |
| 30 |
} |
| 31 |
|
| 32 |
void unknown_param_name(const int& unknown, // expected-error {{parameter cannot be named 'unknown' while using 'lifetime_capture_by(unknown)'}} |
| 33 |
const int& s [[clang::lifetime_capture_by(unknown)]]); |
| 34 |
void global_param_name(const int& global, // expected-error {{parameter cannot be named 'global' while using 'lifetime_capture_by(global)'}} |
| 35 |
const int& s [[clang::lifetime_capture_by(global)]]); |
| 36 |
struct T { |
| 37 |
void member( |
| 38 |
const int &x [[clang::lifetime_capture_by(s)]], |
| 39 |
S &s, |
| 40 |
S &t, |
| 41 |
const int &y [[clang::lifetime_capture_by(s)]], |
| 42 |
const int &z [[clang::lifetime_capture_by(this, x, y)]], |
| 43 |
const int &u [[clang::lifetime_capture_by(global, unknown, x, s)]]) |
| 44 |
{ |
| 45 |
s.captureInt(x); |
| 46 |
} |
| 47 |
|
| 48 |
void explicit_this1(this T& self, const int &x [[clang::lifetime_capture_by(self)]]); |
| 49 |
void explicit_this2(this T& self, const int &x [[clang::lifetime_capture_by(this)]]); // expected-error {{argument references unavailable implicit 'this'}} |
| 50 |
}; |