Skip to content

Commit 21eec03

Browse files
committed
Add more tests, and a minor fix for a function returning gsl pointer.
1 parent 13b27f7 commit 21eec03

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
@@ -1120,8 +1120,8 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
11201120
// const std::string& Ref(const std::string& a [[clang::lifetimebound]]);
11211121
// string_view abc = Ref(std::string());
11221122
// The "Path" is [GSLPointerInit, LifetimeboundCall], where "L" is the
1123-
// temporary "std::string()" object. We need to check if the function with the
1124-
// lifetimebound attribute returns a "owner" type.
1123+
// temporary "std::string()" object. We need to check the return type of the
1124+
// function with the lifetimebound attribute.
11251125
if (Path.back().Kind == IndirectLocalPathEntry::LifetimeBoundCall) {
11261126
// The lifetimebound applies to the implicit object parameter of a method.
11271127
const FunctionDecl *FD =
@@ -1136,10 +1136,13 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
11361136
// We still respect this case even the type S is not an owner.
11371137
return Report;
11381138
}
1139-
// Check if the return type has an Owner attribute.
1140-
// e.g., const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1141-
if (FD && FD->getReturnType()->isReferenceType() &&
1142-
isRecordWithAttr<OwnerAttr>(FD->getReturnType()->getPointeeType()))
1139+
// Check the return type, e.g.
1140+
// const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1141+
// GSLPointer func(const Foo& foo [[clang::lifetimebound]])
1142+
if (FD &&
1143+
((FD->getReturnType()->isReferenceType() &&
1144+
isRecordWithAttr<OwnerAttr>(FD->getReturnType()->getPointeeType())) ||
1145+
isRecordWithAttr<PointerAttr>(FD->getReturnType())))
11431146
return Report;
11441147

11451148
return Abandon;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,28 @@ void test13() {
798798
namespace LifetimeboundInterleave {
799799

800800
const std::string& Ref(const std::string& abc [[clang::lifetimebound]]);
801+
802+
std::string_view TakeSv(std::string_view abc [[clang::lifetimebound]]);
803+
std::string_view TakeStrRef(const std::string& abc [[clang::lifetimebound]]);
804+
std::string_view TakeStr(std::string abc [[clang::lifetimebound]]);
805+
801806
std::string_view test1() {
802807
std::string_view t1 = Ref(std::string()); // expected-warning {{object backing}}
803808
t1 = Ref(std::string()); // expected-warning {{object backing}}
804809
return Ref(std::string()); // expected-warning {{returning address}}
810+
811+
std::string_view t2 = TakeSv(std::string()); // expected-warning {{object backing}}
812+
t2 = TakeSv(std::string()); // expected-warning {{object backing}}
813+
return TakeSv(std::string()); // expected-warning {{returning address}}
814+
815+
std::string_view t3 = TakeStrRef(std::string()); // expected-warning {{temporary}}
816+
t3 = TakeStrRef(std::string()); // expected-warning {{object backing}}
817+
return TakeStrRef(std::string()); // expected-warning {{returning address}}
818+
819+
820+
std::string_view t4 = TakeStr(std::string());
821+
t4 = TakeStr(std::string());
822+
return TakeStr(std::string());
805823
}
806824

807825
template <typename T>

0 commit comments

Comments
 (0)