[clang] [LifetimeSafety] Implicit lifetimebound for accessor methods of GSL owners only (PR #174741)
Utkarsh Saxena via cfe-commits
cfe-commits at lists.llvm.org
Thu Jan 8 07:25:54 PST 2026
https://github.com/usx95 updated https://github.com/llvm/llvm-project/pull/174741
>From 218b3a5e3983d74f25fe5979860d8f6b76f1222c Mon Sep 17 00:00:00 2001
From: Utkarsh Saxena <usx at google.com>
Date: Tue, 16 Dec 2025 12:13:42 +0000
Subject: [PATCH] only-for-owners
---
clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp | 3 ++-
clang/test/Sema/warn-lifetime-analysis-nocfg.cpp | 4 +++-
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp b/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp
index 2772fe20de19b..f6afc43843a82 100644
--- a/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp
+++ b/clang/lib/Analysis/LifetimeSafety/LifetimeAnnotations.cpp
@@ -99,7 +99,8 @@ bool shouldTrackImplicitObjectArg(const CXXMethodDecl *Callee) {
if (!isGslPointerType(Callee->getFunctionObjectParameterType()) &&
!isGslOwnerType(Callee->getFunctionObjectParameterType()))
return false;
- if (isPointerLikeType(Callee->getReturnType())) {
+ if (isPointerLikeType(Callee->getReturnType()) &&
+ isGslOwnerType(Callee->getFunctionObjectParameterType())) {
if (!Callee->getIdentifier())
return false;
return llvm::StringSwitch<bool>(Callee->getName())
diff --git a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
index 7634dbf2f6733..b04ed86cd8abd 100644
--- a/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
+++ b/clang/test/Sema/warn-lifetime-analysis-nocfg.cpp
@@ -249,8 +249,10 @@ int &doNotFollowReferencesForLocalOwner() {
return p; // ok
}
+// It is not possible to annotate accessors of view types
+// (like std::string_view::begin()) to catch bugs like the following:
const char *trackThroughMultiplePointer() {
- return std::basic_string_view<char>(std::basic_string<char>()).begin(); // expected-warning {{returning address of local temporary object}}
+ return std::basic_string_view<char>(std::basic_string<char>()).begin();
}
struct X {
More information about the cfe-commits
mailing list