@@ -1183,28 +1183,23 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
11831183 // lifetimebound attribute returns a "owner" type.
11841184 if (Path.back ().Kind == IndirectLocalPathEntry::LifetimeBoundCall) {
11851185 // The lifetimebound applies to the implicit object parameter of a method.
1186- if (const auto *Method = llvm::dyn_cast<CXXMethodDecl>(Path.back ().D )) {
1187- if (Method->getReturnType ()->isReferenceType () &&
1188- isRecordWithAttr<OwnerAttr>(
1189- Method->getReturnType ()->getPointeeType ()))
1190- return Report;
1191- return Abandon;
1192- }
1186+ const FunctionDecl* FD = llvm::dyn_cast_or_null<FunctionDecl>(Path.back ().D );
11931187 // The lifetimebound applies to a function parameter.
1194- const auto *PD = llvm::dyn_cast<ParmVarDecl>(Path.back ().D );
1195- if (const auto *FD = llvm::dyn_cast<FunctionDecl>(PD->getDeclContext ())) {
1196- if (isa<CXXConstructorDecl>(FD)) {
1197- // Constructor case: the parameter is annotated with lifetimebound
1198- // e.g., GSLPointer(const S& s [[clang::lifetimebound]])
1199- // We still respect this case even the type S is not an owner.
1200- return Report;
1201- }
1202- // For regular functions, check if the return type has an Owner attribute.
1203- // e.g., const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1204- if (FD->getReturnType ()->isReferenceType () &&
1205- isRecordWithAttr<OwnerAttr>(FD->getReturnType ()->getPointeeType ()))
1206- return Report;
1188+ if (const auto *PD = llvm::dyn_cast<ParmVarDecl>(Path.back ().D ))
1189+ FD = llvm::dyn_cast<FunctionDecl>(PD->getDeclContext ());
1190+
1191+ if (isa_and_present<CXXConstructorDecl>(FD)) {
1192+ // Constructor case: the parameter is annotated with lifetimebound
1193+ // e.g., GSLPointer(const S& s [[clang::lifetimebound]])
1194+ // We still respect this case even the type S is not an owner.
1195+ return Report;
12071196 }
1197+ // Check if the return type has an Owner attribute.
1198+ // e.g., const GSLOwner& func(const Foo& foo [[clang::lifetimebound]])
1199+ if (FD && FD->getReturnType ()->isReferenceType () &&
1200+ isRecordWithAttr<OwnerAttr>(FD->getReturnType ()->getPointeeType ()))
1201+ return Report;
1202+
12081203 return Abandon;
12091204 }
12101205
@@ -1273,9 +1268,9 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
12731268 bool IsGslPtrValueFromGslTempOwner = true ;
12741269 switch (analyzePathForGSLPointer (Path, L)) {
12751270 case Abandon:
1276- return false ;
1271+ return false ;
12771272 case Skip:
1278- return true ;
1273+ return true ;
12791274 case NotGSLPointer:
12801275 IsGslPtrValueFromGslTempOwner = false ;
12811276 LLVM_FALLTHROUGH;
0 commit comments