Skip to content

Commit 1666c4f

Browse files
committed
Add more tests, and a minor fix for a function returning gsl pointer.
1 parent 8cb609c commit 1666c4f

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

clang/lib/Sema/CheckExprLifetime.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,8 +1179,8 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
11791179
// const std::string& Ref(const std::string& a [[clang::lifetimebound]]);
11801180
// string_view abc = Ref(std::string());
11811181
// The "Path" is [GSLPointerInit, LifetimeboundCall], where "L" is the
1182-
// temporary "std::string()" object. We need to check if the function with the
1183-
// lifetimebound attribute returns a "owner" type.
1182+
// temporary "std::string()" object. We need to check the return type of the
1183+
// function with the lifetimebound attribute.
11841184
if (Path.back().Kind == IndirectLocalPathEntry::LifetimeBoundCall) {
11851185
// The lifetimebound applies to the implicit object parameter of a method.
11861186
const FunctionDecl *FD =
@@ -1195,10 +1195,13 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
11951195
// We still respect this case even the type S is not an owner.
11961196
return Report;
11971197
}
1198-
// Check if the return type has an Owner attribute.
1199-
// e.g., const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1200-
if (FD && FD->getReturnType()->isReferenceType() &&
1201-
isRecordWithAttr<OwnerAttr>(FD->getReturnType()->getPointeeType()))
1198+
// Check the return type, e.g.
1199+
// const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1200+
// GSLPointer func(const Foo& foo [[clang::lifetimebound]])
1201+
if (FD &&
1202+
((FD->getReturnType()->isReferenceType() &&
1203+
isRecordWithAttr<OwnerAttr>(FD->getReturnType()->getPointeeType())) ||
1204+
isRecordWithAttr<PointerAttr>(FD->getReturnType())))
12021205
return Report;
12031206

12041207
return Abandon;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,10 +698,28 @@ void test() {
698698
namespace LifetimeboundInterleave {
699699

700700
const std::string& Ref(const std::string& abc [[clang::lifetimebound]]);
701+
702+
std::string_view TakeSv(std::string_view abc [[clang::lifetimebound]]);
703+
std::string_view TakeStrRef(const std::string& abc [[clang::lifetimebound]]);
704+
std::string_view TakeStr(std::string abc [[clang::lifetimebound]]);
705+
701706
std::string_view test1() {
702707
std::string_view t1 = Ref(std::string()); // expected-warning {{object backing}}
703708
t1 = Ref(std::string()); // expected-warning {{object backing}}
704709
return Ref(std::string()); // expected-warning {{returning address}}
710+
711+
std::string_view t2 = TakeSv(std::string()); // expected-warning {{object backing}}
712+
t2 = TakeSv(std::string()); // expected-warning {{object backing}}
713+
return TakeSv(std::string()); // expected-warning {{returning address}}
714+
715+
std::string_view t3 = TakeStrRef(std::string()); // expected-warning {{temporary}}
716+
t3 = TakeStrRef(std::string()); // expected-warning {{object backing}}
717+
return TakeStrRef(std::string()); // expected-warning {{returning address}}
718+
719+
720+
std::string_view t4 = TakeStr(std::string());
721+
t4 = TakeStr(std::string());
722+
return TakeStr(std::string());
705723
}
706724

707725
template <typename T>

0 commit comments

Comments
 (0)