@@ -17,11 +17,11 @@ void noCaptureInt(int i [[clang::lifetime_capture_by(x)]], X &x);
17
17
std::string_view substr (const std::string& s [[clang::lifetimebound]]);
18
18
std::string_view strcopy (const std::string& s);
19
19
20
- void captureSV (std::string_view s [[clang::lifetime_capture_by(x)]], X &x);
21
- void captureRValSV (std::string_view &&sv [[clang::lifetime_capture_by(x)]], X &x);
22
- void noCaptureSV (std::string_view sv, X &x);
23
- void captureS (const std::string &s [[clang::lifetime_capture_by(x)]], X &x);
24
- void captureRValS (std::string &&s [[clang::lifetime_capture_by(x)]], X &x);
20
+ void captureStringView (std::string_view s [[clang::lifetime_capture_by(x)]], X &x);
21
+ void captureRValStringView (std::string_view &&sv [[clang::lifetime_capture_by(x)]], X &x);
22
+ void noCaptureStringView (std::string_view sv, X &x);
23
+ void captureString (const std::string &s [[clang::lifetime_capture_by(x)]], X &x);
24
+ void captureRValString (std::string &&s [[clang::lifetime_capture_by(x)]], X &x);
25
25
26
26
const std::string& getLB (const std::string &s [[clang::lifetimebound]]);
27
27
const std::string& getLB (std::string_view sv [[clang::lifetimebound]]);
@@ -38,8 +38,8 @@ void captureByGlobal(std::string_view s [[clang::lifetime_capture_by(global)]]);
38
38
void captureByUnknown (std::string_view s [[clang::lifetime_capture_by(unknown)]]);
39
39
40
40
void use () {
41
- std::string_view local_sv ;
42
- std::string local_s ;
41
+ std::string_view local_string_view ;
42
+ std::string local_string ;
43
43
X x;
44
44
// Capture an 'int'.
45
45
int local;
@@ -51,34 +51,34 @@ void use() {
51
51
noCaptureInt (local, x);
52
52
53
53
// Capture using std::string_view.
54
- captureSV (local_sv , x);
55
- captureSV (std::string (), // expected-warning {{object whose reference is captured by 'x'}}
54
+ captureStringView (local_string_view , x);
55
+ captureStringView (std::string (), // expected-warning {{object whose reference is captured by 'x'}}
56
56
x);
57
- captureSV (substr (
57
+ captureStringView (substr (
58
58
std::string () // expected-warning {{object whose reference is captured by 'x'}}
59
59
), x);
60
- captureSV (substr (local_s ), x);
61
- captureSV (strcopy (std::string ()), x);
62
- captureRValSV (std::move (local_sv ), x);
63
- captureRValSV (std::string (), x); // expected-warning {{object whose reference is captured by 'x'}}
64
- captureRValSV (std::string_view{" abcd" }, x);
65
- captureRValSV (substr (local_s ), x);
66
- captureRValSV (substr (std::string ()), x); // expected-warning {{object whose reference is captured by 'x'}}
67
- captureRValSV (strcopy (std::string ()), x);
68
- noCaptureSV (local_sv , x);
69
- noCaptureSV (std::string (), x);
70
- noCaptureSV (substr (std::string ()), x);
60
+ captureStringView (substr (local_string ), x);
61
+ captureStringView (strcopy (std::string ()), x);
62
+ captureRValStringView (std::move (local_string_view ), x);
63
+ captureRValStringView (std::string (), x); // expected-warning {{object whose reference is captured by 'x'}}
64
+ captureRValStringView (std::string_view{" abcd" }, x);
65
+ captureRValStringView (substr (local_string ), x);
66
+ captureRValStringView (substr (std::string ()), x); // expected-warning {{object whose reference is captured by 'x'}}
67
+ captureRValStringView (strcopy (std::string ()), x);
68
+ noCaptureStringView (local_string_view , x);
69
+ noCaptureStringView (std::string (), x);
70
+ noCaptureStringView (substr (std::string ()), x);
71
71
72
72
// Capture using std::string.
73
- captureS (std::string (), x); // expected-warning {{object whose reference is captured by 'x'}}
74
- captureS (local_s , x);
75
- captureRValS (std::move (local_s ), x);
76
- captureRValS (std::string (), x); // expected-warning {{object whose reference is captured by 'x'}}
73
+ captureString (std::string (), x); // expected-warning {{object whose reference is captured by 'x'}}
74
+ captureString (local_string , x);
75
+ captureRValString (std::move (local_string ), x);
76
+ captureRValString (std::string (), x); // expected-warning {{object whose reference is captured by 'x'}}
77
77
78
78
// Capture with lifetimebound.
79
- captureSV (getLB (std::string ()), x); // expected-warning {{object whose reference is captured by 'x'}}
80
- captureSV (getLB (substr (std::string ())), x); // expected-warning {{object whose reference is captured by 'x'}}
81
- captureSV (getLB (getLB (
79
+ captureStringView (getLB (std::string ()), x); // expected-warning {{object whose reference is captured by 'x'}}
80
+ captureStringView (getLB (substr (std::string ())), x); // expected-warning {{object whose reference is captured by 'x'}}
81
+ captureStringView (getLB (getLB (
82
82
std::string () // expected-warning {{object whose reference is captured by 'x'}}
83
83
)), x);
84
84
capturePointer (getPointerLB (std::string ()), x); // expected-warning {{object whose reference is captured by 'x'}}
@@ -101,14 +101,23 @@ void use() {
101
101
// capture by global.
102
102
captureByGlobal (std::string ()); // expected-warning {{object whose reference is captured will be destroyed at the end of the full-expression}}
103
103
captureByGlobal (substr (std::string ())); // expected-warning {{captured}}
104
- captureByGlobal (local_s );
105
- captureByGlobal (local_sv );
104
+ captureByGlobal (local_string );
105
+ captureByGlobal (local_string_view );
106
106
107
107
// // capture by unknown.
108
108
captureByGlobal (std::string ()); // expected-warning {{object whose reference is captured will be destroyed at the end of the full-expression}}
109
109
captureByGlobal (substr (std::string ())); // expected-warning {{captured}}
110
- captureByGlobal (local_s);
111
- captureByGlobal (local_sv);
110
+ captureByGlobal (local_string);
111
+ captureByGlobal (local_string_view);
112
+ }
113
+
114
+ void captureDefaultArg (X &x, std::string_view s [[clang::lifetime_capture_by(x)]] = std::string());
115
+ void useCaptureDefaultArg () {
116
+ X x;
117
+ captureDefaultArg (x); // FIXME: Diagnose temporary default arg.
118
+ captureDefaultArg (x, std::string (" temp" )); // expected-warning {{captured}}
119
+ std::string local;
120
+ captureDefaultArg (x, local);
112
121
}
113
122
114
123
template <typename T> struct IsPointerLikeTypeImpl : std::false_type {};
@@ -189,13 +198,16 @@ void container_of_pointers() {
189
198
std::optional<std::string_view> optionalSV;
190
199
vsv.push_back (optionalSV.value ());
191
200
vsv.push_back (getOptionalS ().value ()); // expected-warning {{object whose reference is captured by 'vsv'}}
192
- vsv.push_back (getOptionalSV ().value ());
193
- vsv.push_back (getOptionalMySV ().value ());
201
+
202
+ // FIXME: Following 2 cases are false positives:
203
+ vsv.push_back (getOptionalSV ().value ()); // expected-warning {{object whose reference}}
204
+ vsv.push_back (getOptionalMySV ().value ()); // expected-warning {{object whose reference}}
194
205
195
206
// (maybe) FIXME: We may choose to diagnose the following case.
196
207
// This happens because 'MyStringViewNotPointer' is not marked as a [[gsl::Pointer]] but is derived from one.
197
208
vsv.push_back (getOptionalMySVNotP ().value ()); // expected-warning {{object whose reference is captured by 'vsv'}}
198
209
}
210
+
199
211
namespace temporary_views {
200
212
void capture1 (std::string_view s [[clang::lifetime_capture_by(x)]], std::vector<std::string_view>& x);
201
213
@@ -217,4 +229,4 @@ void test1() {
217
229
capture3 (std::string_view (), x3);
218
230
capture3 (std::string (), x3); // expected-warning {{captured by 'x3'}}
219
231
}
220
- }
232
+ }
0 commit comments