@@ -1183,28 +1183,23 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
1183
1183
// lifetimebound attribute returns a "owner" type.
1184
1184
if (Path.back ().Kind == IndirectLocalPathEntry::LifetimeBoundCall) {
1185
1185
// 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 );
1193
1187
// 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;
1207
1196
}
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
+
1208
1203
return Abandon;
1209
1204
}
1210
1205
@@ -1273,9 +1268,9 @@ checkExprLifetimeImpl(Sema &SemaRef, const InitializedEntity *InitEntity,
1273
1268
bool IsGslPtrValueFromGslTempOwner = true ;
1274
1269
switch (analyzePathForGSLPointer (Path, L)) {
1275
1270
case Abandon:
1276
- return false ;
1271
+ return false ;
1277
1272
case Skip:
1278
- return true ;
1273
+ return true ;
1279
1274
case NotGSLPointer:
1280
1275
IsGslPtrValueFromGslTempOwner = false ;
1281
1276
LLVM_FALLTHROUGH;
0 commit comments