Skip to content

Commit 035c09e

Browse files
committed
add more tests per comments.
1 parent f6a9125 commit 035c09e

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ Improvements to Clang's diagnostics
292292

293293
- Clang now warns for u8 character literals used in C23 with ``-Wpre-c23-compat`` instead of ``-Wpre-c++17-compat``.
294294

295-
- Clang now diagnoses cases where a dangling `GSLOwner<GSLPointer>`` object is constructed, e.g. `std::vector<string_view> v = {std::string}` (#GH100526).
295+
- Clang now diagnoses cases where a dangling ``GSLOwner<GSLPointer>`` object is constructed, e.g. `std::vector<string_view> v = {std::string()};` (#GH100526).
296296

297297
Improvements to Clang's time-trace
298298
----------------------------------

clang/include/clang/Basic/AttrDocs.td

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6690,9 +6690,9 @@ When the Owner's lifetime ends, it will consider the Pointer to be dangling.
66906690
P.getInt(); // P is dangling
66916691
}
66926692

6693-
If a template class is annotated with [[gsl::Owner]], and the first instantiated
6694-
template argument is a pointer type (raw pointer, or [[gsl::Pointer]]), the
6695-
analysis will consider the instantiated class as a container of the pointer.
6693+
If a template class is annotated with ``[[gsl::Owner]]``, and the first
6694+
instantiated template argument is a pointer type (raw pointer, or ``[[gsl::Pointer]]``),
6695+
the analysis will consider the instantiated class as a container of the pointer.
66966696
When constructing such an object from a GSL owner object, the analysis will
66976697
assume that the container holds a pointer to the owner object. Consequently,
66986698
when the owner object is destroyed, the pointer will be considered dangling.

clang/test/Sema/warn-lifetime-analysis-nocfg.cpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,14 @@ void test() {
580580
namespace GH100526 {
581581
void test() {
582582
std::vector<std::string_view> v1({std::string()}); // expected-warning {{object backing the pointer will be destroyed at the end}}
583-
std::vector<std::string_view> v2({std::string(), std::string_view()}); // expected-warning {{object backing the pointer will be destroyed at the end}}
584-
std::vector<std::string_view> v3({std::string_view(), std::string()}); // expected-warning {{object backing the pointer will be destroyed at the end}}
583+
std::vector<std::string_view> v2({
584+
std::string(), // expected-warning {{object backing the pointer will be destroyed at the end}}
585+
std::string_view()
586+
});
587+
std::vector<std::string_view> v3({
588+
std::string_view(),
589+
std::string() // expected-warning {{object backing the pointer will be destroyed at the end}}
590+
});
585591

586592
std::optional<std::string_view> o1 = std::string(); // expected-warning {{object backing the pointer}}
587593

@@ -591,6 +597,8 @@ void test() {
591597
// 2. the temporary object owns the underlying string which is copied from s.
592598
// 3. the t3 object holds the view to the underlying string of the temporary object.
593599
std::optional<std::string_view> o2 = std::make_optional(s); // expected-warning {{object backing the pointer}}
600+
std::optional<std::string_view> o3 = std::optional<std::string>(s); // expected-warning {{object backing the pointer}}
601+
std::optional<std::string_view> o4 = std::optional<std::string_view>(s);
594602

595603
// FIXME: should work for assignment cases
596604
v1 = {std::string()};
@@ -599,10 +607,11 @@ void test() {
599607
// no warning on copying pointers.
600608
std::vector<std::string_view> n1 = {std::string_view()};
601609
std::optional<std::string_view> n2 = {std::string_view()};
602-
std::optional<std::string_view> n3 = std::make_optional(std::string_view());
610+
std::optional<std::string_view> n3 = std::string_view();
611+
std::optional<std::string_view> n4 = std::make_optional(std::string_view());
603612
const char* b = "";
604-
std::optional<std::string_view> n4 = std::make_optional(b);
605-
std::optional<std::string_view> n5 = std::make_optional("test");
613+
std::optional<std::string_view> n5 = std::make_optional(b);
614+
std::optional<std::string_view> n6 = std::make_optional("test");
606615
}
607616

608617
std::vector<std::string_view> test2(int i) {
@@ -612,4 +621,12 @@ std::vector<std::string_view> test2(int i) {
612621
return std::vector<std::string_view>(t.begin(), t.end());
613622
}
614623

624+
std::optional<std::string_view> test3(int i) {
625+
std::string s;
626+
std::string_view sv;
627+
if (i)
628+
return s; // expected-warning {{address of stack memory associated}}
629+
return sv; // fine
630+
}
631+
615632
} // namespace GH100526

0 commit comments

Comments
 (0)