@@ -1124,28 +1124,23 @@ static AnalysisResult analyzePathForGSLPointer(const IndirectLocalPath &Path,
1124
1124
// lifetimebound attribute returns a "owner" type.
1125
1125
if (Path.back ().Kind == IndirectLocalPathEntry::LifetimeBoundCall) {
1126
1126
// 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 );
1134
1128
// 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;
1148
1137
}
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
+
1149
1144
return Abandon;
1150
1145
}
1151
1146
@@ -1215,9 +1210,9 @@ static void checkExprLifetimeImpl(Sema &SemaRef,
1215
1210
bool IsGslPtrValueFromGslTempOwner = true ;
1216
1211
switch (analyzePathForGSLPointer (Path, L)) {
1217
1212
case Abandon:
1218
- return false ;
1213
+ return false ;
1219
1214
case Skip:
1220
- return true ;
1215
+ return true ;
1221
1216
case NotGSLPointer:
1222
1217
IsGslPtrValueFromGslTempOwner = false ;
1223
1218
LLVM_FALLTHROUGH;
0 commit comments