brintos

brintos / llvm-project-archived public Read only

0
0
Text · 16.8 KiB · 111bad6 Raw
365 lines · cpp
1// RUN: %clang_cc1 -std=c++23 -verify %s2 3namespace usage_invalid {4  void void_return(int &param [[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 &param [[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 33namespace usage_ok {34  struct IntRef { int *target; };35 36  const int &crefparam(const int &param); // Omitted in first decl37  const int &crefparam(const int &param); // Omitted in second decl38  const int &crefparam(const int &param [[clang::lifetimebound]]); // Add LB39  const int &crefparam(const int &param) { return param; } // Omit in impl40  int &refparam(int &param [[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 &param [[clang::lifetimebound]]) {45    return (long long)&param;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  struct FieldCheck {79    struct Set {80      int a;81    };82    struct Pair {83      const int& a;84      int b;85      Set c;86      int * d;87    };88    Pair p;89    FieldCheck(const int& a): p(a){}90    Pair& getR() [[clang::lifetimebound]] { return p; }91    Pair* getP() [[clang::lifetimebound]] { return &p; }92    Pair* getNoLB() { return &p; }93  };94  void test_field_access() {95    int x = 0;96    const int& a = FieldCheck{x}.getR().a;97    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}}98    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}}99    const int& d = FieldCheck{x}.getNoLB()->c.a;100    const int* e = FieldCheck{x}.getR().d;101  }102}103 104namespace std {105  using size_t = __SIZE_TYPE__;106  template<typename T>107  struct basic_string {108    basic_string();109    basic_string(const T*);110 111    char &operator[](size_t) const [[clang::lifetimebound]];112  };113  using string =  basic_string<char>;114  string operator""s(const char *, size_t); // expected-warning {{user-defined literal suffixes not starting with '_' are reserved}}115 116  template<typename T>117  struct basic_string_view {118    basic_string_view();119    basic_string_view(const T *p);120    basic_string_view(const string &s [[clang::lifetimebound]]);121  };122  using string_view = basic_string_view<char>;123  string_view operator""sv(const char *, size_t); // expected-warning {{user-defined literal suffixes not starting with '_' are reserved}}124 125  struct vector {126    int *data();127    size_t size();128  };129 130  template<typename K, typename V> struct map {};131}132 133using std::operator""s;134using std::operator""sv;135 136namespace default_args {137  using IntArray = int[];138  const int *defaultparam1(const int &def1 [[clang::lifetimebound]] = 0); // #def1139  const int &defaultparam_array([[clang::lifetimebound]] const int *p = IntArray{1, 2, 3}); // #def2140  struct A {141    A(const char *, const int &def3 [[clang::lifetimebound]] = 0); // #def3142  };143  const int &defaultparam2(const int &def4 [[clang::lifetimebound]] = 0); // #def4144  const int &defaultparam3(const int &def5 [[clang::lifetimebound]] = defaultparam2(), const int &def6 [[clang::lifetimebound]] = 0); // #def5 #def6145  std::string_view defaultparam4(std::string_view s [[clang::lifetimebound]] = std::string()); // #def7146 147  const int *test_default_args() {148    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}}149    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}}150    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}}151    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}}152    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}}153    int local;154    const int &v = defaultparam2(local); // no warning155    const int &w = defaultparam2(1); // expected-warning {{temporary bound to local reference 'w' will be destroyed at the end of the full-expression}}156    if (false) {157      return &defaultparam2();  // expected-warning {{returning address of local temporary object}}158    }159    if (false) {160      return &defaultparam2(0);  // expected-warning {{returning address of local temporary object}} expected-note@#def4 {{initializing parameter 'def4' with default argument}}161    }162    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}}163    return nullptr;164  }165} // namespace default_args166 167namespace p0936r0_examples {168  std::string_view s = "foo"s; // expected-warning {{temporary}}169 170  std::string operator+(std::string_view s1, std::string_view s2);171  void f() {172    std::string_view sv = "hi";173    std::string_view sv2 = sv + sv; // expected-warning {{temporary}}174    sv2 = sv + sv; // expected-warning {{object backing the pointer}}175  }176 177  struct X { int a, b; };178  const int &f(const X &x [[clang::lifetimebound]]) { return x.a; }179  const int &r = f(X()); // expected-warning {{temporary}}180 181  char &c = std::string("hello my pretty long strong")[0]; // expected-warning {{temporary}}182 183  struct reversed_range {184    int *begin();185    int *end();186    int *p;187    std::size_t n;188  };189  template <typename R> reversed_range reversed(R &&r [[clang::lifetimebound]]) {190    return reversed_range{r.data(), r.size()};191  }192 193  std::vector make_vector();194  void use_reversed_range() {195    // No warning here because C++23 extends the lifetime of the temporary196    // in a range-based for loop.197    for (auto x : reversed(make_vector())) {}198  }199 200  template <typename K, typename V>201  const V &findOrDefault(const std::map<K, V> &m [[clang::lifetimebound]],202                         const K &key,203                         const V &defvalue [[clang::lifetimebound]]);204 205  // FIXME: Maybe weaken the wording here: "local reference 'v' could bind to temporary that will be destroyed at end of full-expression"?206  std::map<std::string, std::string> m;207  const std::string &v = findOrDefault(m, "foo"s, "bar"s); // expected-warning {{temporary bound to local reference 'v'}}208}209 210// definitions for std::move, std::forward et al.211namespace std {212inline namespace foo {213 214template <class T> struct remove_reference {215    typedef T type;216};217template <class T> struct remove_reference<T &> {218    typedef T type;219};220template <class T> struct remove_reference<T &&> {221    typedef T type;222};223 224template <class T> constexpr typename remove_reference<T>::type &&move(T &&t) {225    return static_cast<typename remove_reference<T>::type>(t);226}227 228template <class T>229constexpr T &&forward(typename remove_reference<T>::type &t) {230    return static_cast<T &&>(t);231}232 233template <class T>234constexpr T &&forward(typename remove_reference<T>::type &&t) {235    return static_cast<T &&>(t);236}237 238template <class T> constexpr const T &as_const(T &x) { return x; }239 240template <class T, bool RValueRef> struct PickRef {241    using type = typename remove_reference<T>::type &;242};243template <class T> struct PickRef<T, true> {244    using type = typename remove_reference<T>::type &&;245};246 247template <class T> struct is_lvalue_reference {248    static constexpr bool value = false;249};250 251template <class T> struct is_lvalue_reference<T &> {252    static constexpr bool value = true;253};254 255template <class T> struct is_const {256    static constexpr bool value = false;257};258 259template <class T> struct is_const<const T> {260    static constexpr bool value = true;261};262 263template <bool B, class T, class F> struct conditional {264    using type = T;265};266 267template <class T, class F> struct conditional<false, T, F> {268    using type = F;269};270 271template <class U, class T>272using CopyConst = typename conditional<is_const<remove_reference<U>>::value,273                                       const T, T>::type;274 275template <class U, class T>276using OverrideRef =277    typename conditional<is_lvalue_reference<U &&>::value,278                         typename remove_reference<T>::type &,279                         typename remove_reference<T>::type &&>::type;280 281template <class U, class T>282using ForwardLikeRetType = OverrideRef<U &&, CopyConst<U, T>>;283 284template <class U>285constexpr auto forward_like(auto &&t) -> ForwardLikeRetType<U, decltype(t)> {286    return static_cast<ForwardLikeRetType<U, decltype(t)>>(t);287}288 289template <class T>290auto move_if_noexcept(T &t) ->291    typename PickRef<T, noexcept(T(static_cast<T &&>(t)))>::type {292    return static_cast<293        typename PickRef<T, noexcept(T(static_cast<T &&>(t)))>::type>(t);294}295 296template <class T> T *addressof(T &arg) {297    return reinterpret_cast<T *>(298        &const_cast<char &>(reinterpret_cast<const volatile char &>(arg)));299}300 301template <class T> struct span {302  template<size_t _ArrayExtent>303	span(const T (&__arr)[_ArrayExtent]) noexcept;304};305 306} // namespace foo307} // namespace std308 309namespace move_forward_et_al_examples {310  struct S {311    S &self() [[clang::lifetimebound]] { return *this; }312  };313 314  S &&Move = std::move(S{}); // expected-warning {{temporary bound to local reference 'Move' will be destroyed at the end of the full-expression}}315  S MoveOk = std::move(S{});316 317  S &&Forward = std::forward<S &&>(S{}); // expected-warning {{temporary bound to local reference 'Forward' will be destroyed at the end of the full-expression}}318  S ForwardOk = std::forward<S &&>(S{});319 320  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}}321  S ForwardLikeOk = std::forward_like<int&&>(S{});322 323  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}}324  const S ConstOk = std::as_const(S{}.self());325 326  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}}327  S MoveIfNoExceptOk = std::move_if_noexcept(S{}.self());328 329  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}}330  S X;331  S *AddressOfOk = std::addressof(X);332} // namespace move_forward_et_al_examples333 334namespace ctor_cases {335std::basic_string_view<char> test1() {336  char abc[10];337  return abc;  // expected-warning {{address of stack memory associated with local variable}}338}339 340std::span<int> test2() {341  int abc[10];342  return abc; // expected-warning {{address of stack memory associated with local variable}}343}344} // namespace ctor_cases345 346namespace GH106372 {347class [[gsl::Owner]] Foo {};348class [[gsl::Pointer]] FooView {};349 350class NonAnnotatedFoo {};351class NonAnnotatedFooView {};352 353template <typename T>354struct StatusOr {355  template <typename U = T>356  StatusOr& operator=(U&& v [[clang::lifetimebound]]);357};358 359void test(StatusOr<FooView> foo1, StatusOr<NonAnnotatedFooView> foo2) {360  foo1 = Foo(); // expected-warning {{object backing 'foo1' will be destroyed at the end}}361  // This warning is triggered by the lifetimebound annotation, regardless of whether the class type is annotated with GSL.362  foo2 = NonAnnotatedFoo(); // expected-warning {{object backing 'foo2' will be destroyed at the end}}363}364} // namespace GH106372365