Skip to content

Commit 1b3f6d8

Browse files
committed
address comments
1 parent 46ca05c commit 1b3f6d8

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

clang/include/clang/Basic/AttrDocs.td

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3927,15 +3927,15 @@ that parameter may also be referred to by a capturing entity ``X``.
39273927

39283928
Below is a list of types of the parameters and what they're considered to refer to:
39293929

3930-
- A reference param is considered to refer to its referenced object.
3931-
- A pointer param is considered to refer to its pointee.
3932-
- A ``std::initializer_list<T>`` is considered to refer to its underlying array.
3933-
- Aggregates (arrays and simple ``struct``\s) are considered to refer to all
3934-
objects that their transitive subobjects refer to.
3930+
- A reference param (of non-view type) is considered to refer to its referenced object.
3931+
- A pointer param (of non-view type) is considered to refer to its pointee.
39353932
- View type param (type annotated with ``[[gsl::Pointer()]]``) is considered to refer
3936-
to its pointee. This holds true even if the view type appears as a reference
3933+
to its pointee (gsl owner). This holds true even if the view type appears as a reference
39373934
in the parameter. For example, both ``std::string_view`` and
39383935
``const std::string_view &`` are considered to refer to a ``std::string``.
3936+
- A ``std::initializer_list<T>`` is considered to refer to its underlying array.
3937+
- Aggregates (arrays and simple ``struct``\s) are considered to refer to all
3938+
objects that their transitive subobjects refer to.
39393939

39403940
Clang would diagnose when a temporary object is used as an argument to such an
39413941
annotated parameter.

clang/test/Sema/warn-lifetime-analysis-capture-by.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ const std::string* getPointerNoLB(const std::string &s);
3131

3232
void capturePointer(const std::string* sp [[clang::lifetime_capture_by(x)]], X &x);
3333

34+
// Vector
35+
void captureVector(const std::vector<int> &a [[clang::lifetime_capture_by(x)]], X &x);
36+
3437
struct ThisIsCaptured {
3538
void capture(X &x) [[clang::lifetime_capture_by(x)]];
3639
};
@@ -105,11 +108,17 @@ void use() {
105108
captureByGlobal(local_string);
106109
captureByGlobal(local_string_view);
107110

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);
113122
}
114123

115124
void captureDefaultArg(X &x, std::string_view s [[clang::lifetime_capture_by(x)]] = std::string());
@@ -136,6 +145,7 @@ void user_defined_containers() {
136145
set_of_int.insert(1); // expected-warning {{object whose reference is captured by 'set_of_int' will be destroyed}}
137146
MySet<std::string_view> set_of_sv;
138147
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());
139149
}
140150

141151
// Templated containers having **which distinguishes** between pointer-like and other element type.
@@ -223,7 +233,9 @@ void test1() {
223233
capture1(std::string_view(), x1);
224234

225235
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);
227239
capture2(std::string(), x2); // expected-warning {{captured by 'x2'}}
228240

229241
std::vector<std::string_view> x3;

0 commit comments

Comments
 (0)