@@ -1124,28 +1124,23 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
11241124 // lifetimebound attribute returns a "owner" type.
11251125 if (Path.back ().Kind == IndirectLocalPathEntry::LifetimeBoundCall) {
11261126 // The lifetimebound applies to the implicit object parameter of a method.
1127- if (const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Path.back ().D )) {
1128- if (Method->getReturnType ()->isReferenceType () &&
1129- isRecordWithAttr<OwnerAttr>(
1130- Method->getReturnType ()->getPointeeType ()))
1131- return Report;
1132- return Abandon;
1133- }
1127+ const FunctionDecl* FD = llvm::dyn_cast_or_null<FunctionDecl>(Path.back ().D );
11341128 // The lifetimebound applies to a function parameter.
1135- const auto *PD = llvm::dyn_cast<ParmVarDecl>(Path.back ().D );
1136- if (const auto *FD = llvm::dyn_cast<FunctionDecl>(PD->getDeclContext ())) {
1137- if (isa<CXXConstructorDecl>(FD)) {
1138- // Constructor case: the parameter is annotated with lifetimebound
1139- // e.g., GSLPointer(const S& s [[clang::lifetimebound]])
1140- // We still respect this case even the type S is not an owner.
1141- return Report;
1142- }
1143- // For regular functions, check if the return type has an Owner attribute.
1144- // e.g., const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1145- if (FD->getReturnType ()->isReferenceType () &&
1146- isRecordWithAttr<OwnerAttr>(FD->getReturnType ()->getPointeeType ()))
1147- return Report;
1129+ if (const auto *PD = llvm::dyn_cast<ParmVarDecl>(Path.back ().D ))
1130+ FD = llvm::dyn_cast<FunctionDecl>(PD->getDeclContext ());
1131+
1132+ if (isa_and_present<CXXConstructorDecl>(FD)) {
1133+ // Constructor case: the parameter is annotated with lifetimebound
1134+ // e.g., GSLPointer(const S& s [[clang::lifetimebound]])
1135+ // We still respect this case even the type S is not an owner.
1136+ return Report;
11481137 }
1138+ // Check if the return type has an Owner attribute.
1139+ // e.g., const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1140+ if (FD && FD->getReturnType ()->isReferenceType () &&
1141+ isRecordWithAttr<OwnerAttr>(FD->getReturnType ()->getPointeeType ()))
1142+ return Report;
1143+
11491144 return Abandon;
11501145 }
11511146
@@ -1215,9 +1210,9 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
12151210 bool IsGslPtrValueFromGslTempOwner = true ;
12161211 switch (analyzePathForGSLPointer (Path, L)) {
12171212 case Abandon:
1218- return false ;
1213+ return false ;
12191214 case Skip:
1220- return true ;
1215+ return true ;
12211216 case NotGSLPointer:
12221217 IsGslPtrValueFromGslTempOwner = false ;
12231218 LLVM_FALLTHROUGH;
0 commit comments