| 1 |
// RUN: %clang_cc1 -std=c++2a -verify %s |
| 2 |
|
| 3 |
namespace usage_invalid { |
| 4 |
// FIXME: Should we diagnose a void return type? |
| 5 |
void voidreturn(int ¶m [[clang::lifetimebound]]); |
| 6 |
|
| 7 |
int *not_class_member() [[clang::lifetimebound]]; // expected-error {{non-member function has no implicit object parameter}} |
| 8 |
struct A { |
| 9 |
A() [[clang::lifetimebound]]; // expected-error {{cannot be applied to a constructor}} |
| 10 |
~A() [[clang::lifetimebound]]; // expected-error {{cannot be applied to a destructor}} |
| 11 |
static int *static_class_member() [[clang::lifetimebound]]; // expected-error {{static member function has no implicit object parameter}} |
| 12 |
int not_function [[clang::lifetimebound]]; // expected-error {{only applies to parameters and implicit object parameters}} |
| 13 |
int [[clang::lifetimebound]] also_not_function; // expected-error {{cannot be applied to types}} |
| 14 |
}; |
| 15 |
int *attr_with_param(int ¶m [[clang::lifetimebound(42)]]); // expected-error {{takes no arguments}} |
| 16 |
} |
| 17 |
|
| 18 |
namespace usage_ok { |
| 19 |
struct IntRef { int *target; }; |
| 20 |
|
| 21 |
int &refparam(int ¶m [[clang::lifetimebound]]); |
| 22 |
int &classparam(IntRef param [[clang::lifetimebound]]); |
| 23 |
|
| 24 |
// Do not diagnose non-void return types; they can still be lifetime-bound. |
| 25 |
long long ptrintcast(int ¶m [[clang::lifetimebound]]) { |
| 26 |
return (long long)¶m; |
| 27 |
} |
| 28 |
// Likewise. |
| 29 |
int &intptrcast(long long param [[clang::lifetimebound]]) { |
| 30 |
return *(int*)param; |
| 31 |
} |
| 32 |
|
| 33 |
struct A { |
| 34 |
A(); |
| 35 |
A(int); |
| 36 |
int *class_member() [[clang::lifetimebound]]; |
| 37 |
operator int*() [[clang::lifetimebound]]; |
| 38 |
}; |
| 39 |
|
| 40 |
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}} |
| 41 |
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}} |
| 42 |
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}} |
| 43 |
|
| 44 |
void test_assignment() { |
| 45 |
p = A().class_member(); // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}} |
| 46 |
p = {A().class_member()}; // expected-warning {{object backing the pointer p will be destroyed at the end of the full-expression}} |
| 47 |
q = A(); // expected-warning {{object backing the pointer q will be destroyed at the end of the full-expression}} |
| 48 |
r = A(1); // expected-warning {{object backing the pointer r will be destroyed at the end of the full-expression}} |
| 49 |
} |
| 50 |
|
| 51 |
struct FieldCheck { |
| 52 |
struct Set { |
| 53 |
int a; |
| 54 |
}; |
| 55 |
struct Pair { |
| 56 |
const int& a; |
| 57 |
int b; |
| 58 |
Set c; |
| 59 |
int * d; |
| 60 |
}; |
| 61 |
Pair p; |
| 62 |
FieldCheck(const int& a): p(a){} |
| 63 |
Pair& getR() [[clang::lifetimebound]] { return p; } |
| 64 |
Pair* getP() [[clang::lifetimebound]] { return &p; } |
| 65 |
Pair* getNoLB() { return &p; } |
| 66 |
}; |
| 67 |
void test_field_access() { |
| 68 |
int x = 0; |
| 69 |
const int& a = FieldCheck{x}.getR().a; |
| 70 |
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}} |
| 71 |
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}} |
| 72 |
const int& d = FieldCheck{x}.getNoLB()->c.a; |
| 73 |
const int* e = FieldCheck{x}.getR().d; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
# 1 "<std>" 1 3 |
| 78 |
namespace std { |
| 79 |
using size_t = __SIZE_TYPE__; |
| 80 |
struct string { |
| 81 |
string(); |
| 82 |
string(const char*); |
| 83 |
|
| 84 |
char &operator[](size_t) const [[clang::lifetimebound]]; |
| 85 |
}; |
| 86 |
string operator""s(const char *, size_t); |
| 87 |
|
| 88 |
struct string_view { |
| 89 |
string_view(); |
| 90 |
string_view(const char *p [[clang::lifetimebound]]); |
| 91 |
string_view(const string &s [[clang::lifetimebound]]); |
| 92 |
}; |
| 93 |
string_view operator""sv(const char *, size_t); |
| 94 |
|
| 95 |
struct vector { |
| 96 |
int *data(); |
| 97 |
size_t size(); |
| 98 |
}; |
| 99 |
|
| 100 |
template<typename K, typename V> struct map {}; |
| 101 |
} |
| 102 |
# 68 "attr-lifetimebound.cpp" 2 |
| 103 |
|
| 104 |
using std::operator""s; |
| 105 |
using std::operator""sv; |
| 106 |
|
| 107 |
namespace p0936r0_examples { |
| 108 |
std::string_view s = "foo"s; // expected-warning {{temporary}} |
| 109 |
|
| 110 |
std::string operator+(std::string_view s1, std::string_view s2); |
| 111 |
void f() { |
| 112 |
std::string_view sv = "hi"; |
| 113 |
std::string_view sv2 = sv + sv; // expected-warning {{temporary}} |
| 114 |
sv2 = sv + sv; // FIXME: can we infer that we should warn here too? |
| 115 |
} |
| 116 |
|
| 117 |
struct X { int a, b; }; |
| 118 |
const int &f(const X &x [[clang::lifetimebound]]) { return x.a; } |
| 119 |
const int &r = f(X()); // expected-warning {{temporary}} |
| 120 |
|
| 121 |
char &c = std::string("hello my pretty long strong")[0]; // expected-warning {{temporary}} |
| 122 |
|
| 123 |
struct reversed_range { |
| 124 |
int *begin(); |
| 125 |
int *end(); |
| 126 |
int *p; |
| 127 |
std::size_t n; |
| 128 |
}; |
| 129 |
template <typename R> reversed_range reversed(R &&r [[clang::lifetimebound]]) { |
| 130 |
return reversed_range{r.data(), r.size()}; |
| 131 |
} |
| 132 |
|
| 133 |
std::vector make_vector(); |
| 134 |
void use_reversed_range() { |
| 135 |
// FIXME: Don't expose the name of the internal range variable. |
| 136 |
for (auto x : reversed(make_vector())) {} // expected-warning {{temporary implicitly bound to local reference will be destroyed at the end of the full-expression}} |
| 137 |
} |
| 138 |
|
| 139 |
template <typename K, typename V> |
| 140 |
const V &findOrDefault(const std::map<K, V> &m [[clang::lifetimebound]], |
| 141 |
const K &key, |
| 142 |
const V &defvalue [[clang::lifetimebound]]); |
| 143 |
|
| 144 |
// FIXME: Maybe weaken the wording here: "local reference 'v' could bind to temporary that will be destroyed at end of full-expression"? |
| 145 |
std::map<std::string, std::string> m; |
| 146 |
const std::string &v = findOrDefault(m, "foo"s, "bar"s); // expected-warning {{temporary bound to local reference 'v'}} |
| 147 |
} |
| 148 |
|
| 149 |
// definitions for std::move, std::forward et al. |
| 150 |
namespace std { |
| 151 |
inline namespace foo { |
| 152 |
|
| 153 |
template <class T> struct remove_reference { |
| 154 |
typedef T type; |
| 155 |
}; |
| 156 |
template <class T> struct remove_reference<T &> { |
| 157 |
typedef T type; |
| 158 |
}; |
| 159 |
template <class T> struct remove_reference<T &&> { |
| 160 |
typedef T type; |
| 161 |
}; |
| 162 |
|
| 163 |
template <class T> constexpr typename remove_reference<T>::type &&move(T &&t) { |
| 164 |
return static_cast<typename remove_reference<T>::type>(t); |
| 165 |
} |
| 166 |
|
| 167 |
template <class T> |
| 168 |
constexpr T &&forward(typename remove_reference<T>::type &t) { |
| 169 |
return static_cast<T &&>(t); |
| 170 |
} |
| 171 |
|
| 172 |
template <class T> |
| 173 |
constexpr T &&forward(typename remove_reference<T>::type &&t) { |
| 174 |
return static_cast<T &&>(t); |
| 175 |
} |
| 176 |
|
| 177 |
template <class T> constexpr const T &as_const(T &x) { return x; } |
| 178 |
|
| 179 |
template <class T, bool RValueRef> struct PickRef { |
| 180 |
using type = typename remove_reference<T>::type &; |
| 181 |
}; |
| 182 |
template <class T> struct PickRef<T, true> { |
| 183 |
using type = typename remove_reference<T>::type &&; |
| 184 |
}; |
| 185 |
|
| 186 |
template <class T> struct is_lvalue_reference { |
| 187 |
static constexpr bool value = false; |
| 188 |
}; |
| 189 |
|
| 190 |
template <class T> struct is_lvalue_reference<T &> { |
| 191 |
static constexpr bool value = true; |
| 192 |
}; |
| 193 |
|
| 194 |
template <class T> struct is_const { |
| 195 |
static constexpr bool value = false; |
| 196 |
}; |
| 197 |
|
| 198 |
template <class T> struct is_const<const T> { |
| 199 |
static constexpr bool value = true; |
| 200 |
}; |
| 201 |
|
| 202 |
template <bool B, class T, class F> struct conditional { |
| 203 |
using type = T; |
| 204 |
}; |
| 205 |
|
| 206 |
template <class T, class F> struct conditional<false, T, F> { |
| 207 |
using type = F; |
| 208 |
}; |
| 209 |
|
| 210 |
template <class U, class T> |
| 211 |
using CopyConst = typename conditional<is_const<remove_reference<U>>::value, |
| 212 |
const T, T>::type; |
| 213 |
|
| 214 |
template <class U, class T> |
| 215 |
using OverrideRef = |
| 216 |
typename conditional<is_lvalue_reference<U &&>::value, |
| 217 |
typename remove_reference<T>::type &, |
| 218 |
typename remove_reference<T>::type &&>::type; |
| 219 |
|
| 220 |
template <class U, class T> |
| 221 |
using ForwardLikeRetType = OverrideRef<U &&, CopyConst<U, T>>; |
| 222 |
|
| 223 |
template <class U> |
| 224 |
constexpr auto forward_like(auto &&t) -> ForwardLikeRetType<U, decltype(t)> { |
| 225 |
return static_cast<ForwardLikeRetType<U, decltype(t)>>(t); |
| 226 |
} |
| 227 |
|
| 228 |
template <class T> |
| 229 |
auto move_if_noexcept(T &t) -> |
| 230 |
typename PickRef<T, noexcept(T(static_cast<T &&>(t)))>::type { |
| 231 |
return static_cast< |
| 232 |
typename PickRef<T, noexcept(T(static_cast<T &&>(t)))>::type>(t); |
| 233 |
} |
| 234 |
|
| 235 |
template <class T> T *addressof(T &arg) { |
| 236 |
return reinterpret_cast<T *>( |
| 237 |
&const_cast<char &>(reinterpret_cast<const volatile char &>(arg))); |
| 238 |
} |
| 239 |
|
| 240 |
} // namespace foo |
| 241 |
} // namespace std |
| 242 |
|
| 243 |
namespace move_forward_et_al_examples { |
| 244 |
struct S { |
| 245 |
S &self() [[clang::lifetimebound]] { return *this; } |
| 246 |
}; |
| 247 |
|
| 248 |
S &&Move = std::move(S{}); // expected-warning {{temporary bound to local reference 'Move' will be destroyed at the end of the full-expression}} |
| 249 |
S MoveOk = std::move(S{}); |
| 250 |
|
| 251 |
S &&Forward = std::forward<S &&>(S{}); // expected-warning {{temporary bound to local reference 'Forward' will be destroyed at the end of the full-expression}} |
| 252 |
S ForwardOk = std::forward<S &&>(S{}); |
| 253 |
|
| 254 |
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}} |
| 255 |
S ForwardLikeOk = std::forward_like<int&&>(S{}); |
| 256 |
|
| 257 |
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}} |
| 258 |
const S ConstOk = std::as_const(S{}.self()); |
| 259 |
|
| 260 |
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}} |
| 261 |
S MoveIfNoExceptOk = std::move_if_noexcept(S{}.self()); |
| 262 |
|
| 263 |
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}} |
| 264 |
S X; |
| 265 |
S *AddressOfOk = std::addressof(X); |
| 266 |
} // namespace move_forward_et_al_examples |
| 267 |
|