@@ -401,13 +401,17 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
401401 visitLocalsRetainedByInitializer (Path, Arg, Visit, true );
402402 Path.pop_back ();
403403 };
404- auto VisitGSLPointerArg = [&](const Decl *D , Expr *Arg, bool Value ) {
404+ auto VisitGSLPointerArg = [&](const FunctionDecl *Callee , Expr *Arg) {
405405 // We are not interested in the temporary base objects of gsl Pointers:
406406 // Temp().ptr; // Here ptr might not dangle.
407407 if (isa<MemberExpr>(Arg->IgnoreImpCasts ()))
408408 return ;
409- // Once we initialized a value with a reference, it can no longer dangle.
410- if (!Value) {
409+ auto ReturnType = Callee->getReturnType ();
410+
411+ // Once we initialized a value with a non gsl-owner reference, it can no
412+ // longer dangle.
413+ if (ReturnType->isReferenceType () &&
414+ !isRecordWithAttr<OwnerAttr>(ReturnType->getPointeeType ())) {
411415 for (const IndirectLocalPathEntry &PE : llvm::reverse (Path)) {
412416 if (PE.Kind == IndirectLocalPathEntry::GslReferenceInit ||
413417 PE.Kind == IndirectLocalPathEntry::LifetimeBoundCall)
@@ -418,9 +422,10 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
418422 break ;
419423 }
420424 }
421- Path.push_back ({Value ? IndirectLocalPathEntry::GslPointerInit
422- : IndirectLocalPathEntry::GslReferenceInit,
423- Arg, D});
425+ Path.push_back ({ReturnType->isReferenceType ()
426+ ? IndirectLocalPathEntry::GslReferenceInit
427+ : IndirectLocalPathEntry::GslPointerInit,
428+ Arg, Callee});
424429 if (Arg->isGLValue ())
425430 visitLocalsRetainedByReferenceBinding (Path, Arg, RK_ReferenceBinding,
426431 Visit);
@@ -451,8 +456,7 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
451456 else if (EnableGSLAnalysis) {
452457 if (auto *CME = dyn_cast<CXXMethodDecl>(Callee);
453458 CME && shouldTrackImplicitObjectArg (CME))
454- VisitGSLPointerArg (Callee, ObjectArg,
455- !Callee->getReturnType ()->isReferenceType ());
459+ VisitGSLPointerArg (Callee, ObjectArg);
456460 }
457461 }
458462
@@ -463,13 +467,11 @@ static void visitFunctionCallArguments(IndirectLocalPath &Path, Expr *Call,
463467 VisitLifetimeBoundArg (Callee->getParamDecl (I), Args[I]);
464468 else if (EnableGSLAnalysis && I == 0 ) {
465469 if (shouldTrackFirstArgument (Callee)) {
466- VisitGSLPointerArg (Callee, Args[0 ],
467- !Callee->getReturnType ()->isReferenceType ());
470+ VisitGSLPointerArg (Callee, Args[0 ]);
468471 } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Call);
469472 CCE &&
470473 CCE->getConstructor ()->getParent ()->hasAttr <PointerAttr>()) {
471- VisitGSLPointerArg (CCE->getConstructor ()->getParamDecl (0 ), Args[0 ],
472- true );
474+ VisitGSLPointerArg (CCE->getConstructor (), Args[0 ]);
473475 }
474476 }
475477 }
0 commit comments