@@ -31,6 +31,9 @@ const std::string* getPointerNoLB(const std::string &s);
31
31
32
32
void capturePointer (const std::string* sp [[clang::lifetime_capture_by(x)]], X &x);
33
33
34
+ // Vector
35
+ void captureVector (const std::vector<int > &a [[clang::lifetime_capture_by(x)]], X &x);
36
+
34
37
struct ThisIsCaptured {
35
38
void capture (X &x) [[clang::lifetime_capture_by(x)]];
36
39
};
@@ -105,11 +108,17 @@ void use() {
105
108
captureByGlobal (local_string);
106
109
captureByGlobal (local_string_view);
107
110
108
- // // capture by unknown.
109
- captureByGlobal (std::string ()); // expected-warning {{object whose reference is captured will be destroyed at the end of the full-expression}}
110
- captureByGlobal (substr (std::string ())); // expected-warning {{captured}}
111
- captureByGlobal (local_string);
112
- captureByGlobal (local_string_view);
111
+ // capture by unknown.
112
+ captureByUnknown (std::string ()); // expected-warning {{object whose reference is captured will be destroyed at the end of the full-expression}}
113
+ captureByUnknown (substr (std::string ())); // expected-warning {{captured}}
114
+ captureByUnknown (local_string);
115
+ captureByUnknown (local_string_view);
116
+
117
+ // capture arrays.
118
+ captureVector ({1 , 2 , 3 }, x); // expected-warning {{capture}}
119
+ captureVector (std::vector<int >{}, x); // expected-warning {{capture}}
120
+ std::vector<int > local_vector;
121
+ captureVector (local_vector, x);
113
122
}
114
123
115
124
void captureDefaultArg (X &x, std::string_view s [[clang::lifetime_capture_by(x)]] = std::string());
@@ -136,6 +145,7 @@ void user_defined_containers() {
136
145
set_of_int.insert (1 ); // expected-warning {{object whose reference is captured by 'set_of_int' will be destroyed}}
137
146
MySet<std::string_view> set_of_sv;
138
147
set_of_sv.insert (std::string ()); // expected-warning {{object whose reference is captured by 'set_of_sv' will be destroyed}}
148
+ set_of_sv.insert (std::string_view ());
139
149
}
140
150
141
151
// Templated containers having **which distinguishes** between pointer-like and other element type.
@@ -223,7 +233,9 @@ void test1() {
223
233
capture1 (std::string_view (), x1);
224
234
225
235
std::vector<std::string_view*> x2;
226
- capture2 (std::string_view (), x2); // FIXME: Warn when the temporary view itself is captured.
236
+ // Clang considers 'const std::string_view&' to refer to the owner
237
+ // 'std::string' and not 'std::string_view'. Therefore no diagnostic here.
238
+ capture2 (std::string_view (), x2);
227
239
capture2 (std::string (), x2); // expected-warning {{captured by 'x2'}}
228
240
229
241
std::vector<std::string_view> x3;
0 commit comments