| 1 |
// RUN: rm -rf %t |
| 2 |
// RUN: split-file %s %t |
| 3 |
// RUN: %clang_cc1 -fsyntax-only -fexperimental-lifetime-safety -fexperimental-lifetime-safety-inference -Wexperimental-lifetime-safety-suggestions -Wexperimental-lifetime-safety -Wno-dangling -I%t -verify %t/test_source.cpp |
| 4 |
|
| 5 |
View definition_before_header(View a); |
| 6 |
|
| 7 |
//--- test_header.h |
| 8 |
#ifndef TEST_HEADER_H |
| 9 |
#define TEST_HEADER_H |
| 10 |
|
| 11 |
struct View; |
| 12 |
|
| 13 |
struct [[gsl::Owner]] MyObj { |
| 14 |
int id; |
| 15 |
~MyObj() {} // Non-trivial destructor |
| 16 |
MyObj operator+(MyObj); |
| 17 |
|
| 18 |
View getView() const [[clang::lifetimebound]]; |
| 19 |
}; |
| 20 |
|
| 21 |
struct [[gsl::Pointer()]] View { |
| 22 |
View(const MyObj&); // Borrows from MyObj |
| 23 |
View(); |
| 24 |
void use() const; |
| 25 |
}; |
| 26 |
|
| 27 |
View definition_before_header(View a); // expected-warning {{parameter in cross-TU function should be marked [[clang::lifetimebound]]}} |
| 28 |
|
| 29 |
View return_view_directly(View a); // expected-warning {{parameter in cross-TU function should be marked [[clang::lifetimebound]]}} |
| 30 |
|
| 31 |
View conditional_return_view( |
| 32 |
View a, // expected-warning {{parameter in cross-TU function should be marked [[clang::lifetimebound]]}} |
| 33 |
View b, // expected-warning {{parameter in cross-TU function should be marked [[clang::lifetimebound]]}} |
| 34 |
bool c); |
| 35 |
|
| 36 |
int* return_pointer_directly(int* a); // expected-warning {{parameter in cross-TU function should be marked [[clang::lifetimebound]]}} |
| 37 |
|
| 38 |
MyObj* return_pointer_object(MyObj* a); // expected-warning {{parameter in cross-TU function should be marked [[clang::lifetimebound]]}} |
| 39 |
|
| 40 |
inline View inline_header_return_view(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 41 |
return a; // expected-note {{param returned here}} |
| 42 |
} |
| 43 |
|
| 44 |
View redeclared_in_header(View a); |
| 45 |
inline View redeclared_in_header(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 46 |
return a; // expected-note {{param returned here}} |
| 47 |
} |
| 48 |
|
| 49 |
#endif // TEST_HEADER_H |
| 50 |
|
| 51 |
//--- test_source.cpp |
| 52 |
|
| 53 |
#include "test_header.h" |
| 54 |
|
| 55 |
View definition_before_header(View a) { |
| 56 |
return a; // expected-note {{param returned here}} |
| 57 |
} |
| 58 |
|
| 59 |
View return_view_directly(View a) { |
| 60 |
return a; // expected-note {{param returned here}} |
| 61 |
} |
| 62 |
|
| 63 |
View conditional_return_view(View a, View b, bool c) { |
| 64 |
View res; |
| 65 |
if (c) |
| 66 |
res = a; |
| 67 |
else |
| 68 |
res = b; |
| 69 |
return res; // expected-note 2 {{param returned here}} |
| 70 |
} |
| 71 |
|
| 72 |
int* return_pointer_directly(int* a) { |
| 73 |
return a; // expected-note {{param returned here}} |
| 74 |
} |
| 75 |
|
| 76 |
MyObj* return_pointer_object(MyObj* a) { |
| 77 |
return a; // expected-note {{param returned here}} |
| 78 |
} |
| 79 |
|
| 80 |
MyObj& return_reference(MyObj& a, // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 81 |
MyObj& b, // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 82 |
bool c) { |
| 83 |
if(c) { |
| 84 |
return a; // expected-note {{param returned here}} |
| 85 |
} |
| 86 |
return b; // expected-note {{param returned here}} |
| 87 |
} |
| 88 |
|
| 89 |
const MyObj& return_reference_const(const MyObj& a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 90 |
return a; // expected-note {{param returned here}} |
| 91 |
} |
| 92 |
|
| 93 |
MyObj* return_ptr_to_ref(MyObj& a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 94 |
return &a; // expected-note {{param returned here}} |
| 95 |
} |
| 96 |
|
| 97 |
MyObj& return_ref_to_ptr(MyObj* a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 98 |
return *a; // expected-note {{param returned here}} |
| 99 |
} |
| 100 |
|
| 101 |
View return_ref_to_ptr_multiple(MyObj* a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 102 |
return *(&(*(&(*a)))); // expected-note {{param returned here}} |
| 103 |
} |
| 104 |
|
| 105 |
View return_view_from_reference(MyObj& p) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 106 |
return p; // expected-note {{param returned here}} |
| 107 |
} |
| 108 |
|
| 109 |
struct Container { |
| 110 |
MyObj data; |
| 111 |
const MyObj& getData() [[clang::lifetimebound]] { return data; } |
| 112 |
}; |
| 113 |
// FIXME: c.data does not forward loans |
| 114 |
View return_struct_field(const Container& c) { |
| 115 |
return c.data; |
| 116 |
} |
| 117 |
View return_struct_lifetimebound_getter(Container& c) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 118 |
return c.getData().getView(); // expected-note {{param returned here}} |
| 119 |
} |
| 120 |
|
| 121 |
View return_view_from_reference_lifetimebound_member(MyObj& p) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} |
| 122 |
return p.getView(); // expected-note {{param returned here}} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
View return_cross_tu_func_View(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 127 |
return return_view_directly(a); // expected-note {{param returned here}} |
| 128 |
} |
| 129 |
|
| 130 |
MyObj* return_cross_tu_func_obj(MyObj* a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 131 |
return return_pointer_object(a); // expected-note {{param returned here}} |
| 132 |
} |
| 133 |
|
| 134 |
int* return_cross_tu_func_pointer(int* a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 135 |
return return_pointer_directly(a); // expected-note {{param returned here}} |
| 136 |
} |
| 137 |
|
| 138 |
namespace { |
| 139 |
View only_one_paramter_annotated(View a [[clang::lifetimebound]], |
| 140 |
View b, // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 141 |
bool c) { |
| 142 |
if(c) |
| 143 |
return a; |
| 144 |
return b; // expected-note {{param returned here}} |
| 145 |
} |
| 146 |
|
| 147 |
View reassigned_to_another_parameter( |
| 148 |
View a, |
| 149 |
View b) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 150 |
a = b; |
| 151 |
return a; // expected-note {{param returned here}} |
| 152 |
} |
| 153 |
|
| 154 |
View intra_tu_func_redecl(View a); |
| 155 |
View intra_tu_func_redecl(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 156 |
return a; // expected-note {{param returned here}} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
static View return_view_static(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 161 |
return a; // expected-note {{param returned here}} |
| 162 |
} |
| 163 |
|
| 164 |
//===----------------------------------------------------------------------===// |
| 165 |
// FIXME Test Cases |
| 166 |
//===----------------------------------------------------------------------===// |
| 167 |
|
| 168 |
struct ReturnsSelf { |
| 169 |
const ReturnsSelf& get() const { |
| 170 |
return *this; |
| 171 |
} |
| 172 |
}; |
| 173 |
|
| 174 |
struct ViewProvider { |
| 175 |
MyObj data; |
| 176 |
View getView() const { |
| 177 |
return data; |
| 178 |
} |
| 179 |
}; |
| 180 |
|
| 181 |
// FIXME: Fails to generate lifetime suggestions for the implicit 'this' parameter, as this feature is not yet implemented. |
| 182 |
void test_get_on_temporary() { |
| 183 |
const ReturnsSelf& s_ref = ReturnsSelf().get(); |
| 184 |
(void)s_ref; |
| 185 |
} |
| 186 |
|
| 187 |
// FIXME: Fails to generate lifetime suggestions for the implicit 'this' parameter, as this feature is not yet implemented. |
| 188 |
void test_getView_on_temporary() { |
| 189 |
View sv = ViewProvider{1}.getView(); |
| 190 |
(void)sv; |
| 191 |
} |
| 192 |
|
| 193 |
//===----------------------------------------------------------------------===// |
| 194 |
// Annotation Inference Test Cases |
| 195 |
//===----------------------------------------------------------------------===// |
| 196 |
|
| 197 |
namespace correct_order_inference { |
| 198 |
View return_view_by_func(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 199 |
return return_view_directly(a); // expected-note {{param returned here}} |
| 200 |
} |
| 201 |
|
| 202 |
MyObj* return_pointer_by_func(MyObj* a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 203 |
return return_pointer_object(a); // expected-note {{param returned here}} |
| 204 |
} |
| 205 |
} // namespace correct_order_inference |
| 206 |
|
| 207 |
namespace incorrect_order_inference_view { |
| 208 |
View return_view_callee(View a); |
| 209 |
|
| 210 |
// FIXME: No lifetime annotation suggestion when functions are not present in the callee-before-caller pattern |
| 211 |
View return_view_caller(View a) { |
| 212 |
return return_view_callee(a); |
| 213 |
} |
| 214 |
|
| 215 |
View return_view_callee(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 216 |
return a; // expected-note {{param returned here}} |
| 217 |
} |
| 218 |
} // namespace incorrect_order_inference_view |
| 219 |
|
| 220 |
namespace incorrect_order_inference_object { |
| 221 |
MyObj* return_object_callee(MyObj* a); |
| 222 |
|
| 223 |
// FIXME: No lifetime annotation suggestion warning when functions are not present in the callee-before-caller pattern |
| 224 |
MyObj* return_object_caller(MyObj* a) { |
| 225 |
return return_object_callee(a); |
| 226 |
} |
| 227 |
|
| 228 |
MyObj* return_object_callee(MyObj* a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 229 |
return a; // expected-note {{param returned here}} |
| 230 |
} |
| 231 |
} // namespace incorrect_order_inference_object |
| 232 |
|
| 233 |
namespace simple_annotation_inference { |
| 234 |
View inference_callee_return_identity(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 235 |
return a; // expected-note {{param returned here}} |
| 236 |
} |
| 237 |
|
| 238 |
View inference_caller_forwards_callee(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 239 |
return inference_callee_return_identity(a); // expected-note {{param returned here}} |
| 240 |
} |
| 241 |
|
| 242 |
View inference_top_level_return_stack_view() { |
| 243 |
MyObj local_stack; |
| 244 |
return inference_caller_forwards_callee(local_stack); // expected-warning {{address of stack memory is returned later}} |
| 245 |
// expected-note@-1 {{returned here}} |
| 246 |
} |
| 247 |
} // namespace simple_annotation_inference |
| 248 |
|
| 249 |
namespace inference_in_order_with_redecls { |
| 250 |
View inference_callee_return_identity(View a); |
| 251 |
View inference_callee_return_identity(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 252 |
return a; // expected-note {{param returned here}} |
| 253 |
} |
| 254 |
|
| 255 |
View inference_caller_forwards_callee(View a); |
| 256 |
View inference_caller_forwards_callee(View a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 257 |
return inference_callee_return_identity(a); // expected-note {{param returned here}} |
| 258 |
} |
| 259 |
|
| 260 |
View inference_top_level_return_stack_view() { |
| 261 |
MyObj local_stack; |
| 262 |
return inference_caller_forwards_callee(local_stack); // expected-warning {{address of stack memory is returned later}} |
| 263 |
// expected-note@-1 {{returned here}} |
| 264 |
} |
| 265 |
} // namespace inference_in_order_with_redecls |
| 266 |
|
| 267 |
namespace inference_with_templates { |
| 268 |
template<typename T> |
| 269 |
T* template_identity(T* a) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}}. |
| 270 |
return a; // expected-note {{param returned here}} |
| 271 |
} |
| 272 |
|
| 273 |
template<typename T> |
| 274 |
T* template_caller(T* a) { |
| 275 |
return template_identity(a); // expected-note {{in instantiation of function template specialization 'inference_with_templates::template_identity<MyObj>' requested here}} |
| 276 |
} |
| 277 |
|
| 278 |
// FIXME: Fails to detect UAR as template instantiations are deferred to the end of the Translation Unit. |
| 279 |
MyObj* test_template_inference_with_stack() { |
| 280 |
MyObj local_stack; |
| 281 |
return template_caller(&local_stack); // expected-note {{in instantiation of function template specialization 'inference_with_templates::template_caller<MyObj>' requested here}} |
| 282 |
} |
| 283 |
} // namespace inference_with_templates |
| 284 |
|
| 285 |
//===----------------------------------------------------------------------===// |
| 286 |
// Negative Test Cases |
| 287 |
//===----------------------------------------------------------------------===// |
| 288 |
|
| 289 |
View already_annotated(View a [[clang::lifetimebound]]) { |
| 290 |
return a; |
| 291 |
} |
| 292 |
|
| 293 |
MyObj return_obj_by_value(MyObj& p) { |
| 294 |
return p; |
| 295 |
} |
| 296 |
|
| 297 |
MyObj GlobalMyObj; |
| 298 |
View Global = GlobalMyObj; |
| 299 |
View Reassigned(View a) { |
| 300 |
a = Global; |
| 301 |
return a; |
| 302 |
} |