| 1 |
// RUN: %clang_cc1 -std=c++23 -verify %s |
| 2 |
|
| 3 |
namespace usage_invalid { |
| 4 |
void void_return(int ¶m [[clang::lifetimebound]]); // expected-error {{'lifetimebound' attribute cannot be applied to a parameter of a function that returns void; did you mean 'lifetime_capture_by(X)'}} |
| 5 |
|
| 6 |
int *not_class_member() [[clang::lifetimebound]]; // expected-error {{non-member function has no implicit object parameter}} |
| 7 |
struct A { |
| 8 |
A() [[clang::lifetimebound]]; // expected-error {{cannot be applied to a constructor}} |
| 9 |
~A() [[clang::lifetimebound]]; // expected-error {{cannot be applied to a destructor}} |
| 10 |
static int *static_class_member() [[clang::lifetimebound]]; // expected-error {{static member function has no implicit object parameter}} |
| 11 |
int *explicit_object(this A&) [[clang::lifetimebound]]; // expected-error {{explicit object member function has no implicit object parameter}} |
| 12 |
int attr_on_var [[clang::lifetimebound]]; // expected-error {{only applies to parameters and implicit object parameters}} |
| 13 |
int [[clang::lifetimebound]] attr_on_int; // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 14 |
int * [[clang::lifetimebound]] attr_on_int_ptr; // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 15 |
int * [[clang::lifetimebound]] * attr_on_int_ptr_ptr; // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 16 |
int (* [[clang::lifetimebound]] attr_on_func_ptr)(); // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 17 |
void void_return_member() [[clang::lifetimebound]]; // expected-error {{'lifetimebound' attribute cannot be applied to an implicit object parameter of a function that returns void; did you mean 'lifetime_capture_by(X)'}} |
| 18 |
}; |
| 19 |
int *attr_with_param(int ¶m [[clang::lifetimebound(42)]]); // expected-error {{takes no arguments}} |
| 20 |
|
| 21 |
void attr_on_ptr_arg(int * [[clang::lifetimebound]] ptr); // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 22 |
static_assert((int [[clang::lifetimebound]]) 12); // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 23 |
int* attr_on_unnamed_arg(const int& [[clang::lifetimebound]]); // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 24 |
template <typename T> |
| 25 |
int* attr_on_template_ptr_arg(T * [[clang::lifetimebound]] ptr); // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 26 |
|
| 27 |
int (*func_ptr)(int) [[clang::lifetimebound]]; // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 28 |
int (*(*func_ptr_ptr)(int) [[clang::lifetimebound]])(int); // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 29 |
struct X {}; |
| 30 |
int (X::*member_func_ptr)(int) [[clang::lifetimebound]]; // expected-error {{'clang::lifetimebound' attribute only applies to parameters and implicit object parameters}} |
| 31 |
} |
| 32 |
|
| 33 |
namespace usage_ok { |
| 34 |
struct IntRef { int *target; }; |
| 35 |
|
| 36 |
const int &crefparam(const int ¶m); // Omitted in first decl |
| 37 |
const int &crefparam(const int ¶m); // Omitted in second decl |
| 38 |
const int &crefparam(const int ¶m [[clang::lifetimebound]]); // Add LB |
| 39 |
const int &crefparam(const int ¶m) { return param; } // Omit in impl |
| 40 |
int &refparam(int ¶m [[clang::lifetimebound]]); |
| 41 |
int &classparam(IntRef param [[clang::lifetimebound]]); |
| 42 |
|
| 43 |
// Do not diagnose non-void return types; they can still be lifetime-bound. |
| 44 |
long long ptrintcast(int ¶m [[clang::lifetimebound]]) { |
| 45 |
return (long long)¶m; |
| 46 |
} |
| 47 |
// Likewise. |
| 48 |
int &intptrcast(long long param [[clang::lifetimebound]]) { |
| 49 |
return *(int*)param; |
| 50 |
} |
| 51 |
|
| 52 |
template <class T, class R = void> R dependent_void(const T& t [[clang::lifetimebound]]); |
| 53 |
void dependent_void_instantiation() { |
| 54 |
dependent_void<int>(1); // OK: Returns void. |
| 55 |
int x = dependent_void<int, int>(1); // expected-warning {{temporary whose address is used as value of local variable 'x' will be destroyed at the end of the full-expression}} |
| 56 |
dependent_void<int, int>(1); // OK: Returns an unused value. |
| 57 |
} |
| 58 |
|
| 59 |
struct A { |
| 60 |
A(); |
| 61 |
A(int); |
| 62 |
int *class_member() [[clang::lifetimebound]]; |
| 63 |
operator int*() [[clang::lifetimebound]]; |
| 64 |
}; |
| 65 |
|
| 66 |
int *p = A().class_member(); // expected-warning {{temporary whose address is used as value of local variable 'p' will be destroyed at the end of the full-expression}} |
| 67 |
int *q = A(); // expected-warning {{temporary whose address is used as value of local variable 'q' will be destroyed at the end of the full-expression}} |
| 68 |
int *r = A(1); // expected-warning {{temporary whose address is used as value of local variable 'r' will be destroyed at the end of the full-expression}} |
| 69 |
const int& s = crefparam(2); // expected-warning {{temporary bound to local reference 's' will be destroyed at the end of the full-expression}} |
| 70 |
|
| 71 |
void test_assignment() { |
| 72 |
p = A().class_member(); // expected-warning {{object backing the pointer 'p' will be destroyed at the end of the full-expression}} |
| 73 |
p = {A().class_member()}; // expected-warning {{object backing the pointer 'p' will be destroyed at the end of the full-expression}} |
| 74 |
q = A(); // expected-warning {{object backing the pointer 'q' will be destroyed at the end of the full-expression}} |
| 75 |
r = A(1); // expected-warning {{object backing the pointer 'r' will be destroyed at the end of the full-expression}} |
| 76 |
} |
| 77 |
|
| 78 |
// Test that lifetimebound on implicit 'this' is propagated across redeclarations |
| 79 |
struct B { |
| 80 |
int *method() [[clang::lifetimebound]]; |
| 81 |
int i; |
| 82 |
}; |
| 83 |
int *B::method() { return &i; } |
| 84 |
|
| 85 |
// Test that lifetimebound on implicit 'this' is propagated across redeclarations |
| 86 |
struct C { |
| 87 |
int *method(); |
| 88 |
int i; |
| 89 |
}; |
| 90 |
int *C::method() [[clang::lifetimebound]] { return &i; } |
| 91 |
|
| 92 |
void test_lifetimebound_on_implicit_this() { |
| 93 |
int *t = B().method(); // expected-warning {{temporary whose address is used as value of local variable 't' will be destroyed at the end of the full-expression}} |
| 94 |
t = {B().method()}; // expected-warning {{object backing the pointer 't' will be destroyed at the end of the full-expression}} |
| 95 |
t = C().method(); // expected-warning {{object backing the pointer 't' will be destroyed at the end of the full-expression}} |
| 96 |
t = {C().method()}; // expected-warning {{object backing the pointer 't' will be destroyed at the end of the full-expression}} |
| 97 |
} |
| 98 |
|
| 99 |
struct FieldCheck { |
| 100 |
struct Set { |
| 101 |
int a; |
| 102 |
}; |
| 103 |
struct Pair { |
| 104 |
const int& a; |
| 105 |
int b; |
| 106 |
Set c; |
| 107 |
int * d; |
| 108 |
}; |
| 109 |
Pair p; |
| 110 |
FieldCheck(const int& a): p(a){} |
| 111 |
Pair& getR() [[clang::lifetimebound]] { return p; } |
| 112 |
Pair* getP() [[clang::lifetimebound]] { return &p; } |
| 113 |
Pair* getNoLB() { return &p; } |
| 114 |
}; |
| 115 |
void test_field_access() { |
| 116 |
int x = 0; |
| 117 |
const int& a = FieldCheck{x}.getR().a; |
| 118 |
const int& b = FieldCheck{x}.getP()->b; // expected-warning {{temporary bound to local reference 'b' will be destroyed at the end of the full-expression}} |
| 119 |
const int& c = FieldCheck{x}.getP()->c.a; // expected-warning {{temporary bound to local reference 'c' will be destroyed at the end of the full-expression}} |
| 120 |
const int& d = FieldCheck{x}.getNoLB()->c.a; |
| 121 |
const int* e = FieldCheck{x}.getR().d; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
namespace std { |
| 126 |
using size_t = __SIZE_TYPE__; |
| 127 |
template<typename T> |
| 128 |
struct basic_string { |
| 129 |
basic_string(); |
| 130 |
basic_string(const T*); |
| 131 |
|
| 132 |
char &operator[](size_t) const [[clang::lifetimebound]]; |
| 133 |
}; |
| 134 |
using string = basic_string<char>; |
| 135 |
string operator""s(const char *, size_t); // expected-warning {{user-defined literal suffixes not starting with '_' are reserved}} |
| 136 |
|
| 137 |
template<typename T> |
| 138 |
struct basic_string_view { |
| 139 |
basic_string_view(); |
| 140 |
basic_string_view(const T *p); |
| 141 |
basic_string_view(const string &s [[clang::lifetimebound]]); |
| 142 |
}; |
| 143 |
using string_view = basic_string_view<char>; |
| 144 |
string_view operator""sv(const char *, size_t); // expected-warning {{user-defined literal suffixes not starting with '_' are reserved}} |
| 145 |
|
| 146 |
struct vector { |
| 147 |
int *data(); |
| 148 |
size_t size(); |
| 149 |
}; |
| 150 |
|
| 151 |
template<typename K, typename V> struct map {}; |
| 152 |
} |
| 153 |
|
| 154 |
using std::operator""s; |
| 155 |
using std::operator""sv; |
| 156 |
|
| 157 |
namespace default_args { |
| 158 |
using IntArray = int[]; |
| 159 |
const int *defaultparam1(const int &def1 [[clang::lifetimebound]] = 0); // #def1 |
| 160 |
const int &defaultparam_array([[clang::lifetimebound]] const int *p = IntArray{1, 2, 3}); // #def2 |
| 161 |
struct A { |
| 162 |
A(const char *, const int &def3 [[clang::lifetimebound]] = 0); // #def3 |
| 163 |
}; |
| 164 |
const int &defaultparam2(const int &def4 [[clang::lifetimebound]] = 0); // #def4 |
| 165 |
const int &defaultparam3(const int &def5 [[clang::lifetimebound]] = defaultparam2(), const int &def6 [[clang::lifetimebound]] = 0); // #def5 #def6 |
| 166 |
std::string_view defaultparam4(std::string_view s [[clang::lifetimebound]] = std::string()); // #def7 |
| 167 |
|
| 168 |
const int *test_default_args() { |
| 169 |
const int *c = defaultparam1(); // expected-warning {{temporary whose address is used as value of local variable 'c' will be destroyed at the end of the full-expression}} expected-note@#def1 {{initializing parameter 'def1' with default argument}} |
| 170 |
A a = A(""); // expected-warning {{temporary whose address is used as value of local variable 'a' will be destroyed at the end of the full-expression}} expected-note@#def3 {{initializing parameter 'def3' with default argument}} |
| 171 |
const int &s = defaultparam2(); // expected-warning {{temporary bound to local reference 's' will be destroyed at the end of the full-expression}} expected-note@#def4 {{initializing parameter 'def4' with default argument}} |
| 172 |
const int &t = defaultparam3(); // expected-warning {{temporary bound to local reference 't' will be destroyed at the end of the full-expression}} expected-note@#def4 {{initializing parameter 'def4' with default argument}} expected-note@#def5 {{initializing parameter 'def5' with default argument}} expected-warning {{temporary bound to local reference 't' will be destroyed at the end of the full-expression}} expected-note@#def6 {{initializing parameter 'def6' with default argument}} |
| 173 |
const int &u = defaultparam_array(); // expected-warning {{temporary bound to local reference 'u' will be destroyed at the end of the full-expression}} expected-note@#def2 {{initializing parameter 'p' with default argument}} |
| 174 |
int local; |
| 175 |
const int &v = defaultparam2(local); // no warning |
| 176 |
const int &w = defaultparam2(1); // expected-warning {{temporary bound to local reference 'w' will be destroyed at the end of the full-expression}} |
| 177 |
if (false) { |
| 178 |
return &defaultparam2(); // expected-warning {{returning address of local temporary object}} |
| 179 |
} |
| 180 |
if (false) { |
| 181 |
return &defaultparam2(0); // expected-warning {{returning address of local temporary object}} expected-note@#def4 {{initializing parameter 'def4' with default argument}} |
| 182 |
} |
| 183 |
std::string_view sv = defaultparam4(); // expected-warning {{temporary whose address is used as value of local variable 'sv' will be destroyed at the end of the full-expression}} expected-note@#def7 {{initializing parameter 's' with default argument}} |
| 184 |
return nullptr; |
| 185 |
} |
| 186 |
} // namespace default_args |
| 187 |
|
| 188 |
namespace p0936r0_examples { |
| 189 |
std::string_view s = "foo"s; // expected-warning {{temporary}} |
| 190 |
|
| 191 |
std::string operator+(std::string_view s1, std::string_view s2); |
| 192 |
void f() { |
| 193 |
std::string_view sv = "hi"; |
| 194 |
std::string_view sv2 = sv + sv; // expected-warning {{temporary}} |
| 195 |
sv2 = sv + sv; // expected-warning {{object backing the pointer}} |
| 196 |
} |
| 197 |
|
| 198 |
struct X { int a, b; }; |
| 199 |
const int &f(const X &x [[clang::lifetimebound]]) { return x.a; } |
| 200 |
const int &r = f(X()); // expected-warning {{temporary}} |
| 201 |
|
| 202 |
char &c = std::string("hello my pretty long strong")[0]; // expected-warning {{temporary}} |
| 203 |
|
| 204 |
struct reversed_range { |
| 205 |
int *begin(); |
| 206 |
int *end(); |
| 207 |
int *p; |
| 208 |
std::size_t n; |
| 209 |
}; |
| 210 |
template <typename R> reversed_range reversed(R &&r [[clang::lifetimebound]]) { |
| 211 |
return reversed_range{r.data(), r.size()}; |
| 212 |
} |
| 213 |
|
| 214 |
std::vector make_vector(); |
| 215 |
void use_reversed_range() { |
| 216 |
// No warning here because C++23 extends the lifetime of the temporary |
| 217 |
// in a range-based for loop. |
| 218 |
for (auto x : reversed(make_vector())) {} |
| 219 |
} |
| 220 |
|
| 221 |
template <typename K, typename V> |
| 222 |
const V &findOrDefault(const std::map<K, V> &m [[clang::lifetimebound]], |
| 223 |
const K &key, |
| 224 |
const V &defvalue [[clang::lifetimebound]]); |
| 225 |
|
| 226 |
// FIXME: Maybe weaken the wording here: "local reference 'v' could bind to temporary that will be destroyed at end of full-expression"? |
| 227 |
std::map<std::string, std::string> m; |
| 228 |
const std::string &v = findOrDefault(m, "foo"s, "bar"s); // expected-warning {{temporary bound to local reference 'v'}} |
| 229 |
} |
| 230 |
|
| 231 |
// definitions for std::move, std::forward et al. |
| 232 |
namespace std { |
| 233 |
inline namespace foo { |
| 234 |
|
| 235 |
template <class T> struct remove_reference { |
| 236 |
typedef T type; |
| 237 |
}; |
| 238 |
template <class T> struct remove_reference<T &> { |
| 239 |
typedef T type; |
| 240 |
}; |
| 241 |
template <class T> struct remove_reference<T &&> { |
| 242 |
typedef T type; |
| 243 |
}; |
| 244 |
|
| 245 |
template <class T> constexpr typename remove_reference<T>::type &&move(T &&t) { |
| 246 |
return static_cast<typename remove_reference<T>::type>(t); |
| 247 |
} |
| 248 |
|
| 249 |
template <class T> |
| 250 |
constexpr T &&forward(typename remove_reference<T>::type &t) { |
| 251 |
return static_cast<T &&>(t); |
| 252 |
} |
| 253 |
|
| 254 |
template <class T> |
| 255 |
constexpr T &&forward(typename remove_reference<T>::type &&t) { |
| 256 |
return static_cast<T &&>(t); |
| 257 |
} |
| 258 |
|
| 259 |
template <class T> constexpr const T &as_const(T &x) { return x; } |
| 260 |
|
| 261 |
template <class T, bool RValueRef> struct PickRef { |
| 262 |
using type = typename remove_reference<T>::type &; |
| 263 |
}; |
| 264 |
template <class T> struct PickRef<T, true> { |
| 265 |
using type = typename remove_reference<T>::type &&; |
| 266 |
}; |
| 267 |
|
| 268 |
template <class T> struct is_lvalue_reference { |
| 269 |
static constexpr bool value = false; |
| 270 |
}; |
| 271 |
|
| 272 |
template <class T> struct is_lvalue_reference<T &> { |
| 273 |
static constexpr bool value = true; |
| 274 |
}; |
| 275 |
|
| 276 |
template <class T> struct is_const { |
| 277 |
static constexpr bool value = false; |
| 278 |
}; |
| 279 |
|
| 280 |
template <class T> struct is_const<const T> { |
| 281 |
static constexpr bool value = true; |
| 282 |
}; |
| 283 |
|
| 284 |
template <bool B, class T, class F> struct conditional { |
| 285 |
using type = T; |
| 286 |
}; |
| 287 |
|
| 288 |
template <class T, class F> struct conditional<false, T, F> { |
| 289 |
using type = F; |
| 290 |
}; |
| 291 |
|
| 292 |
template <class U, class T> |
| 293 |
using CopyConst = typename conditional<is_const<remove_reference<U>>::value, |
| 294 |
const T, T>::type; |
| 295 |
|
| 296 |
template <class U, class T> |
| 297 |
using OverrideRef = |
| 298 |
typename conditional<is_lvalue_reference<U &&>::value, |
| 299 |
typename remove_reference<T>::type &, |
| 300 |
typename remove_reference<T>::type &&>::type; |
| 301 |
|
| 302 |
template <class U, class T> |
| 303 |
using ForwardLikeRetType = OverrideRef<U &&, CopyConst<U, T>>; |
| 304 |
|
| 305 |
template <class U> |
| 306 |
constexpr auto forward_like(auto &&t) -> ForwardLikeRetType<U, decltype(t)> { |
| 307 |
return static_cast<ForwardLikeRetType<U, decltype(t)>>(t); |
| 308 |
} |
| 309 |
|
| 310 |
template <class T> |
| 311 |
auto move_if_noexcept(T &t) -> |
| 312 |
typename PickRef<T, noexcept(T(static_cast<T &&>(t)))>::type { |
| 313 |
return static_cast< |
| 314 |
typename PickRef<T, noexcept(T(static_cast<T &&>(t)))>::type>(t); |
| 315 |
} |
| 316 |
|
| 317 |
template <class T> T *addressof(T &arg) { |
| 318 |
return reinterpret_cast<T *>( |
| 319 |
&const_cast<char &>(reinterpret_cast<const volatile char &>(arg))); |
| 320 |
} |
| 321 |
|
| 322 |
template <class T> struct span { |
| 323 |
template<size_t _ArrayExtent> |
| 324 |
span(const T (&__arr)[_ArrayExtent]) noexcept; |
| 325 |
}; |
| 326 |
|
| 327 |
} // namespace foo |
| 328 |
} // namespace std |
| 329 |
|
| 330 |
namespace move_forward_et_al_examples { |
| 331 |
struct S { |
| 332 |
S &self() [[clang::lifetimebound]] { return *this; } |
| 333 |
}; |
| 334 |
|
| 335 |
S &&Move = std::move(S{}); // expected-warning {{temporary bound to local reference 'Move' will be destroyed at the end of the full-expression}} |
| 336 |
S MoveOk = std::move(S{}); |
| 337 |
|
| 338 |
S &&Forward = std::forward<S &&>(S{}); // expected-warning {{temporary bound to local reference 'Forward' will be destroyed at the end of the full-expression}} |
| 339 |
S ForwardOk = std::forward<S &&>(S{}); |
| 340 |
|
| 341 |
S &&ForwardLike = std::forward_like<int&&>(S{}); // expected-warning {{temporary bound to local reference 'ForwardLike' will be destroyed at the end of the full-expression}} |
| 342 |
S ForwardLikeOk = std::forward_like<int&&>(S{}); |
| 343 |
|
| 344 |
const S &Const = std::as_const(S{}.self()); // expected-warning {{temporary bound to local reference 'Const' will be destroyed at the end of the full-expression}} |
| 345 |
const S ConstOk = std::as_const(S{}.self()); |
| 346 |
|
| 347 |
S &&MoveIfNoExcept = std::move_if_noexcept(S{}.self()); // expected-warning {{temporary bound to local reference 'MoveIfNoExcept' will be destroyed at the end of the full-expression}} |
| 348 |
S MoveIfNoExceptOk = std::move_if_noexcept(S{}.self()); |
| 349 |
|
| 350 |
S *AddressOf = std::addressof(S{}.self()); // expected-warning {{temporary whose address is used as value of local variable 'AddressOf' will be destroyed at the end of the full-expression}} |
| 351 |
S X; |
| 352 |
S *AddressOfOk = std::addressof(X); |
| 353 |
} // namespace move_forward_et_al_examples |
| 354 |
|
| 355 |
namespace ctor_cases { |
| 356 |
std::basic_string_view<char> test1() { |
| 357 |
char abc[10]; |
| 358 |
return abc; // expected-warning {{address of stack memory associated with local variable}} |
| 359 |
} |
| 360 |
|
| 361 |
std::span<int> test2() { |
| 362 |
int abc[10]; |
| 363 |
return abc; // expected-warning {{address of stack memory associated with local variable}} |
| 364 |
} |
| 365 |
} // namespace ctor_cases |
| 366 |
|
| 367 |
namespace GH106372 { |
| 368 |
class [[gsl::Owner]] Foo {}; |
| 369 |
class [[gsl::Pointer]] FooView {}; |
| 370 |
|
| 371 |
class NonAnnotatedFoo {}; |
| 372 |
class NonAnnotatedFooView {}; |
| 373 |
|
| 374 |
template <typename T> |
| 375 |
struct StatusOr { |
| 376 |
template <typename U = T> |
| 377 |
StatusOr& operator=(U&& v [[clang::lifetimebound]]); |
| 378 |
}; |
| 379 |
|
| 380 |
void test(StatusOr<FooView> foo1, StatusOr<NonAnnotatedFooView> foo2) { |
| 381 |
foo1 = Foo(); // expected-warning {{object backing 'foo1' will be destroyed at the end}} |
| 382 |
// This warning is triggered by the lifetimebound annotation, regardless of whether the class type is annotated with GSL. |
| 383 |
foo2 = NonAnnotatedFoo(); // expected-warning {{object backing 'foo2' will be destroyed at the end}} |
| 384 |
} |
| 385 |
} // namespace GH106372 |