https://github.com/NeKon69 updated https://github.com/llvm/llvm-project/pull/203767
>From 27a99f55662c3545b707bc12f432742e9f0822b1 Mon Sep 17 00:00:00 2001 From: NeKon69 <[email protected]> Date: Sun, 14 Jun 2026 16:11:12 +0300 Subject: [PATCH 1/4] [LifetimeSafety] Warn on inapplicable [[clang::lifetimebound]] when return type can not carry a lifetime --- .../Analyses/LifetimeSafety/LifetimeSafety.h | 5 +++- .../clang/Basic/DiagnosticSemaKinds.td | 5 ++++ clang/lib/Analysis/LifetimeSafety/Checker.cpp | 23 +++++++++++++--- clang/lib/Sema/SemaLifetimeSafety.h | 17 +++++++++--- .../LifetimeSafety/annotation-suggestions.cpp | 6 ++--- .../inapplicable-lifetimebound.cpp | 27 ++++++++++++++++++- .../misplaced-lifetimebound-intra-tu.cpp | 6 ++--- clang/test/Sema/LifetimeSafety/safety.cpp | 8 +++--- 8 files changed, 78 insertions(+), 19 deletions(-) diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h index 6368c1f2a48f4d..17af11f015b184 100644 --- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h +++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h @@ -189,7 +189,10 @@ class LifetimeSafetySemaHelper { const ParmVarDecl *PVDDef, const ParmVarDecl *PVDDecl) {} - virtual void reportInapplicableLifetimebound(const ParmVarDecl *PVD) {} + virtual void reportInapplicableLifetimebound(const ParmVarDecl *PVD, + QualType Type, + bool IsReturnType) {} + virtual void reportInapplicableLifetimebound(const CXXMethodDecl *MD) {} // Suggests lifetime bound annotations for implicit this. virtual void suggestLifetimeboundToImplicitThis(WarningScope Scope, diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 2ae80dc3223501..4119938a186c1f 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11196,6 +11196,11 @@ def warn_lifetime_safety_inapplicable_lifetimebound InGroup<LifetimeSafetyInapplicableLifetimebound>, DefaultIgnore; +def warn_lifetime_safety_inapplicable_lifetimebound_return + : Warning<"'lifetimebound' attribute has no effect on this function because return type %0 cannot carry a lifetime. The attribute only applies when the return type is a reference type, pointer type, or non-owner record type">, + InGroup<LifetimeSafetyInapplicableLifetimebound>, + DefaultIgnore; + def note_lifetime_safety_used_here : Note<"later used here">; def note_lifetime_safety_used_by_destructor : Note<"later used by the destructor of %0 at scope exit">; diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp index 59e382a8aef234..3e5c0ef646a553 100644 --- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp +++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp @@ -678,11 +678,26 @@ class LifetimeChecker { FDef->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) return; + QualType ReturnType = FDef->getReturnType(); + if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(FDef)) + ReturnType = AST.getTypeDeclType(cast<TypeDecl>(Ctor->getParent())); + + bool ReturnTypeHasOrigins = + FactMgr.getOriginMgr().hasOrigins(ReturnType, + /*IntrinsicOnly=*/true); + if (getImplicitObjectParamLifetimeBoundAttr(FDef) && !ReturnTypeHasOrigins) + SemaHelper->reportInapplicableLifetimebound(cast<CXXMethodDecl>(FDef)); + for (const auto &PVD : FDef->parameters()) - if (PVD->hasAttr<LifetimeBoundAttr>() && - !FactMgr.getOriginMgr().hasOrigins(PVD->getType(), - /*IntrinsicOnly=*/true)) - SemaHelper->reportInapplicableLifetimebound(PVD); + if (PVD->hasAttr<LifetimeBoundAttr>()) { + if (!FactMgr.getOriginMgr().hasOrigins(PVD->getType(), + /*IntrinsicOnly=*/true)) + SemaHelper->reportInapplicableLifetimebound(PVD, PVD->getType(), + /*IsReturnType=*/false); + if (!ReturnTypeHasOrigins) + SemaHelper->reportInapplicableLifetimebound(PVD, ReturnType, + /*IsReturnType=*/true); + } } void inferAnnotations() { diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h index c0e41ad3162fc4..d33f31779db784 100644 --- a/clang/lib/Sema/SemaLifetimeSafety.h +++ b/clang/lib/Sema/SemaLifetimeSafety.h @@ -513,13 +513,24 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper { << Attr->getRange(); } - void reportInapplicableLifetimebound(const ParmVarDecl *PVD) override { + void reportInapplicableLifetimebound(const ParmVarDecl *PVD, QualType Type, + bool IsReturnType) override { assert(PVD->hasAttr<LifetimeBoundAttr>() && "Expected parameter to have lifetimebound attribute"); const auto *Attr = PVD->getAttr<LifetimeBoundAttr>(); + unsigned DiagID = + IsReturnType + ? diag::warn_lifetime_safety_inapplicable_lifetimebound_return + : diag::warn_lifetime_safety_inapplicable_lifetimebound; + S.Diag(Attr->getLocation(), DiagID) << Type << Attr->getRange(); + } + + void reportInapplicableLifetimebound(const CXXMethodDecl *MD) override { + const auto *Attr = getImplicitObjectParamLifetimeBoundAttr(MD); + assert(Attr && "Expected lifetimebound attribute"); S.Diag(Attr->getLocation(), - diag::warn_lifetime_safety_inapplicable_lifetimebound) - << PVD->getType() << Attr->getRange(); + diag::warn_lifetime_safety_inapplicable_lifetimebound_return) + << MD->getReturnType() << Attr->getRange(); } void suggestLifetimeboundToImplicitThis(WarningScope Scope, diff --git a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp index f0811d1c00026e..ccb32a42bc84f9 100644 --- a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp +++ b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp @@ -1,7 +1,7 @@ // RUN: rm -rf %t // RUN: split-file %s %t // RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wlifetime-safety -Wlifetime-safety-annotation-placement -Wlifetime-safety-experimental-field-origin-escape -Wno-dangling -I%t -I%S -verify %t/test_source.cpp -// RUN: %clang_cc1 -fsyntax-only -std=c++23 -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wlifetime-safety -Wlifetime-safety-experimental-field-origin-escape -Wno-dangling -I%t -I%S -verify %t/test_source.cpp +// RUN: %clang_cc1 -fsyntax-only -std=c++23 -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wlifetime-safety -Wlifetime-safety-annotation-placement -Wlifetime-safety-experimental-field-origin-escape -Wno-dangling -I%t -I%S -verify %t/test_source.cpp // RUN: %clang_cc1 -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wlifetime-safety -Wlifetime-safety-experimental-field-origin-escape -Wno-dangling -I%t -I%S -fixit %t/test_source.cpp // RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety-suggestions -Wno-dangling -I%t -I%S -Werror=lifetime-safety-suggestions %t/test_source.cpp @@ -645,7 +645,7 @@ namespace make_unique_suggestion { struct LifetimeBoundCtor { View v; // FIXME: This test fails to propagate the lifetimebound in ctor if this is inferred (instead of the current explicit annotation). - LifetimeBoundCtor(const MyObj& obj [[clang::lifetimebound]]): v(obj) {} + LifetimeBoundCtor(const MyObj& obj [[clang::lifetimebound]]): v(obj) {} // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'make_unique_suggestion::LifetimeBoundCtor' cannot carry a lifetime}} }; std::unique_ptr<LifetimeBoundCtor> create_target(const MyObj& obj) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} @@ -679,7 +679,7 @@ void test_new_allocation() { struct LifetimeBoundCtor { View v; LifetimeBoundCtor(); - LifetimeBoundCtor(const MyObj& obj [[clang::lifetimebound]]) : v(obj) {} + LifetimeBoundCtor(const MyObj& obj [[clang::lifetimebound]]) : v(obj) {} // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'new_allocation_suggestion::LifetimeBoundCtor' cannot carry a lifetime}} }; struct HasCtorField { diff --git a/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp b/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp index 8a91e264d39cfc..3122b2db71716f 100644 --- a/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp +++ b/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp @@ -9,7 +9,14 @@ struct [[gsl::Pointer()]] View { View(const Owner &o [[clang::lifetimebound]]); }; -struct Plain {}; +struct Plain { + Plain() {} + Plain(const Plain &other [[clang::lifetimebound]]) {} // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Plain' cannot carry a lifetime}} + Plain operator+(const Plain &other [[clang::lifetimebound]]) const { return {}; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Plain' cannot carry a lifetime}} + Plain &operator=(const Plain &other [[clang::lifetimebound]]) { return *this; } + operator int() const [[clang::lifetimebound]] { return 0; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} + Plain(const Owner &o [[clang::lifetimebound]]) {} // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Plain' cannot carry a lifetime}} +}; enum Enum { Enumerator }; @@ -90,3 +97,21 @@ int *context_sensitive_origin_type( lifetime_annotated_return(i); return v[0]; } + +int getInt(const Owner &o [[clang::lifetimebound]]) { return 0; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} +Owner getOwner(const Owner &o [[clang::lifetimebound]]) { return o; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Owner' cannot carry a lifetime}} +Plain getPlain(const Owner &o [[clang::lifetimebound]]) { return {}; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Plain' cannot carry a lifetime}} + +View getView(const Owner &o [[clang::lifetimebound]]) { return View(); } +Owner *getOwnerPtr(Owner &o [[clang::lifetimebound]]) { return &o; } + +auto getAuto(const Owner &o [[clang::lifetimebound]]) { return 0; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} + +int getIntMultiParam(const Owner &o [[clang::lifetimebound]], int i [[clang::lifetimebound]]) { return 0; } // expected-warning 2 {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} \ + // expected-warning {{'lifetimebound' attribute has no effect on parameter of type 'int'}} + +void test_lambda() { + auto lambda = [](const Owner &o [[clang::lifetimebound]]) -> int { return 0; }; // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} + (void)lambda; +} + diff --git a/clang/test/Sema/LifetimeSafety/misplaced-lifetimebound-intra-tu.cpp b/clang/test/Sema/LifetimeSafety/misplaced-lifetimebound-intra-tu.cpp index 28dd65211b2686..d7a49b9b8e5df5 100644 --- a/clang/test/Sema/LifetimeSafety/misplaced-lifetimebound-intra-tu.cpp +++ b/clang/test/Sema/LifetimeSafety/misplaced-lifetimebound-intra-tu.cpp @@ -121,9 +121,9 @@ IntraSuppressedObj &intra_suppressed( return obj; } -struct View { - friend View friend_redecl(MyObj & obj // expected-warning {{'lifetimebound' attribute on this definition is not visible to callers before the definition; add it to the declaration instead}} - // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:{{[0-9]+}}-[[@LINE-1]]:{{[0-9]+}}}:" {{\[\[clang::lifetimebound\]\]}}" +struct [[gsl::Pointer()]] View { + friend View friend_redecl(MyObj & // expected-warning {{'lifetimebound' attribute on this definition is not visible to callers before the definition; add it to the declaration instead}} + obj // CHECK: fix-it:"{{.*}}":{[[@LINE]]:{{[0-9]+}}-[[@LINE]]:{{[0-9]+}}}:" {{\[\[clang::lifetimebound\]\]}}" ); }; diff --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp index 077c5de9648cbf..af1e29a4baffaa 100644 --- a/clang/test/Sema/LifetimeSafety/safety.cpp +++ b/clang/test/Sema/LifetimeSafety/safety.cpp @@ -1,6 +1,6 @@ // RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wlifetime-safety-experimental-destructor-use -Wlifetime-safety-annotation-placement -Wno-dangling -verify=expected,function %s // RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety -Wlifetime-safety-experimental-destructor-use -Wno-dangling -verify=expected,tu %s -// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wlifetime-safety-experimental-destructor-use -Wno-dangling -fcxx-exceptions -verify=expected,function %s +// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wlifetime-safety-experimental-destructor-use -Wlifetime-safety-annotation-placement -Wno-dangling -fcxx-exceptions -verify=expected,function %s #include "Inputs/lifetime-analysis.h" @@ -3270,7 +3270,7 @@ S S::return_self_after_registration() const { struct SWithUserDefinedCopyLikeOps { SWithUserDefinedCopyLikeOps(); - SWithUserDefinedCopyLikeOps(const std::string &s [[clang::lifetimebound]]) : owned(s), data(s) {} + SWithUserDefinedCopyLikeOps(const std::string &s [[clang::lifetimebound]]) : owned(s), data(s) {} // function-warning {{'lifetimebound' attribute has no effect on this function because return type 'track_origins_for_lifetimebound_record_type::SWithUserDefinedCopyLikeOps' cannot carry a lifetime}} SWithUserDefinedCopyLikeOps(const SWithUserDefinedCopyLikeOps &other) : owned("copy"), data(owned) {} @@ -3333,7 +3333,7 @@ void from_typedef_return() { struct SWithOriginPropagatingCopy { SWithOriginPropagatingCopy(); - SWithOriginPropagatingCopy(const std::string &s [[clang::lifetimebound]]) : data(s) {} + SWithOriginPropagatingCopy(const std::string &s [[clang::lifetimebound]]) : data(s) {} // function-warning {{'lifetimebound' attribute has no effect on this function because return type 'track_origins_for_lifetimebound_record_type::SWithOriginPropagatingCopy' cannot carry a lifetime}} SWithOriginPropagatingCopy(const SWithOriginPropagatingCopy &other) : data(other.data) {} std::string_view data; }; @@ -3350,7 +3350,7 @@ SWithOriginPropagatingCopy user_defined_copy_with_origin_propagation() { struct DefaultedOuter { DefaultedOuter(); - DefaultedOuter(const std::string &s [[clang::lifetimebound]]) : inner(s) {} + DefaultedOuter(const std::string &s [[clang::lifetimebound]]) : inner(s) {} // function-warning {{'lifetimebound' attribute has no effect on this function because return type 'track_origins_for_lifetimebound_record_type::DefaultedOuter' cannot carry a lifetime}} SWithUserDefinedCopyLikeOps inner; }; >From 55702d40718d3e2b84a39353c49ec8b7fe93daaa Mon Sep 17 00:00:00 2001 From: NeKon69 <[email protected]> Date: Sun, 28 Jun 2026 12:38:28 +0300 Subject: [PATCH 2/4] fixup --- .../Analyses/LifetimeSafety/LifetimeSafety.h | 8 +++--- clang/include/clang/Basic/DiagnosticGroups.td | 10 +++++++- .../clang/Basic/DiagnosticSemaKinds.td | 2 +- clang/lib/Analysis/LifetimeSafety/Checker.cpp | 15 +++++------ clang/lib/Sema/SemaLifetimeSafety.h | 25 +++++++++++++------ .../LifetimeSafety/annotation-suggestions.cpp | 4 +-- .../inapplicable-lifetimebound.cpp | 4 +-- clang/test/Sema/LifetimeSafety/safety.cpp | 8 +++--- 8 files changed, 46 insertions(+), 30 deletions(-) diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h index 17af11f015b184..34774f23807804 100644 --- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h +++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h @@ -190,9 +190,11 @@ class LifetimeSafetySemaHelper { const ParmVarDecl *PVDDecl) {} virtual void reportInapplicableLifetimebound(const ParmVarDecl *PVD, - QualType Type, - bool IsReturnType) {} - virtual void reportInapplicableLifetimebound(const CXXMethodDecl *MD) {} + QualType Type) {} + virtual void reportInapplicableLifetimeboundReturnTy(const ParmVarDecl *PVD, + QualType Type) {} + virtual void + reportInapplicableLifetimeboundReturnTy(const CXXMethodDecl *MD) {} // Suggests lifetime bound annotations for implicit this. virtual void suggestLifetimeboundToImplicitThis(WarningScope Scope, diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index 9f43b8570ea035..d1a507450b65c1 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -648,8 +648,16 @@ This warning may produce false-positives diagnostics when it cannot fully model }]; } +def LifetimeSafetyInapplicableLifetimeboundReturn + : DiagGroup<"lifetime-safety-inapplicable-lifetimebound-return"> { + code Documentation = [{ +Detects uses of [[clang::lifetimebound]] that have no effect because the function return type cannot carry a lifetime. + }]; +} + def LifetimeSafetyInapplicableLifetimebound - : DiagGroup<"lifetime-safety-inapplicable-lifetimebound"> { + : DiagGroup<"lifetime-safety-inapplicable-lifetimebound", + [LifetimeSafetyInapplicableLifetimeboundReturn]> { code Documentation = [{ Detects uses of [[clang::lifetimebound]] that have no effect because the type of the annotated parameter cannot carry a lifetime. }]; diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 4119938a186c1f..128548a97e57d4 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11198,7 +11198,7 @@ def warn_lifetime_safety_inapplicable_lifetimebound def warn_lifetime_safety_inapplicable_lifetimebound_return : Warning<"'lifetimebound' attribute has no effect on this function because return type %0 cannot carry a lifetime. The attribute only applies when the return type is a reference type, pointer type, or non-owner record type">, - InGroup<LifetimeSafetyInapplicableLifetimebound>, + InGroup<LifetimeSafetyInapplicableLifetimeboundReturn>, DefaultIgnore; def note_lifetime_safety_used_here : Note<"later used here">; diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp index 3e5c0ef646a553..2fcea526013766 100644 --- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp +++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp @@ -682,21 +682,18 @@ class LifetimeChecker { if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(FDef)) ReturnType = AST.getTypeDeclType(cast<TypeDecl>(Ctor->getParent())); - bool ReturnTypeHasOrigins = - FactMgr.getOriginMgr().hasOrigins(ReturnType, - /*IntrinsicOnly=*/true); + bool ReturnTypeHasOrigins = FactMgr.getOriginMgr().hasOrigins(ReturnType); if (getImplicitObjectParamLifetimeBoundAttr(FDef) && !ReturnTypeHasOrigins) - SemaHelper->reportInapplicableLifetimebound(cast<CXXMethodDecl>(FDef)); + SemaHelper->reportInapplicableLifetimeboundReturnTy( + cast<CXXMethodDecl>(FDef)); for (const auto &PVD : FDef->parameters()) if (PVD->hasAttr<LifetimeBoundAttr>()) { if (!FactMgr.getOriginMgr().hasOrigins(PVD->getType(), /*IntrinsicOnly=*/true)) - SemaHelper->reportInapplicableLifetimebound(PVD, PVD->getType(), - /*IsReturnType=*/false); - if (!ReturnTypeHasOrigins) - SemaHelper->reportInapplicableLifetimebound(PVD, ReturnType, - /*IsReturnType=*/true); + SemaHelper->reportInapplicableLifetimebound(PVD, PVD->getType()); + if (!ReturnTypeHasOrigins && !isa<CXXConstructorDecl>(FDef)) + SemaHelper->reportInapplicableLifetimeboundReturnTy(PVD, ReturnType); } } diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h index d33f31779db784..85041636466e6c 100644 --- a/clang/lib/Sema/SemaLifetimeSafety.h +++ b/clang/lib/Sema/SemaLifetimeSafety.h @@ -513,19 +513,28 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper { << Attr->getRange(); } - void reportInapplicableLifetimebound(const ParmVarDecl *PVD, QualType Type, - bool IsReturnType) override { + void reportInapplicableLifetimebound(const ParmVarDecl *PVD, + QualType Type) override { assert(PVD->hasAttr<LifetimeBoundAttr>() && "Expected parameter to have lifetimebound attribute"); const auto *Attr = PVD->getAttr<LifetimeBoundAttr>(); - unsigned DiagID = - IsReturnType - ? diag::warn_lifetime_safety_inapplicable_lifetimebound_return - : diag::warn_lifetime_safety_inapplicable_lifetimebound; - S.Diag(Attr->getLocation(), DiagID) << Type << Attr->getRange(); + S.Diag(Attr->getLocation(), + diag::warn_lifetime_safety_inapplicable_lifetimebound) + << Type << Attr->getRange(); + } + + void reportInapplicableLifetimeboundReturnTy(const ParmVarDecl *PVD, + QualType Type) override { + assert(PVD->hasAttr<LifetimeBoundAttr>() && + "Expected parameter to have lifetimebound attribute"); + const auto *Attr = PVD->getAttr<LifetimeBoundAttr>(); + S.Diag(Attr->getLocation(), + diag::warn_lifetime_safety_inapplicable_lifetimebound_return) + << Type << Attr->getRange(); } - void reportInapplicableLifetimebound(const CXXMethodDecl *MD) override { + void + reportInapplicableLifetimeboundReturnTy(const CXXMethodDecl *MD) override { const auto *Attr = getImplicitObjectParamLifetimeBoundAttr(MD); assert(Attr && "Expected lifetimebound attribute"); S.Diag(Attr->getLocation(), diff --git a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp index ccb32a42bc84f9..51bd993121d4a2 100644 --- a/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp +++ b/clang/test/Sema/LifetimeSafety/annotation-suggestions.cpp @@ -645,7 +645,7 @@ namespace make_unique_suggestion { struct LifetimeBoundCtor { View v; // FIXME: This test fails to propagate the lifetimebound in ctor if this is inferred (instead of the current explicit annotation). - LifetimeBoundCtor(const MyObj& obj [[clang::lifetimebound]]): v(obj) {} // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'make_unique_suggestion::LifetimeBoundCtor' cannot carry a lifetime}} + LifetimeBoundCtor(const MyObj& obj [[clang::lifetimebound]]): v(obj) {} }; std::unique_ptr<LifetimeBoundCtor> create_target(const MyObj& obj) { // expected-warning {{parameter in intra-TU function should be marked [[clang::lifetimebound]]}} @@ -679,7 +679,7 @@ void test_new_allocation() { struct LifetimeBoundCtor { View v; LifetimeBoundCtor(); - LifetimeBoundCtor(const MyObj& obj [[clang::lifetimebound]]) : v(obj) {} // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'new_allocation_suggestion::LifetimeBoundCtor' cannot carry a lifetime}} + LifetimeBoundCtor(const MyObj& obj [[clang::lifetimebound]]) : v(obj) {} }; struct HasCtorField { diff --git a/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp b/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp index 3122b2db71716f..83877a92ed3f1b 100644 --- a/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp +++ b/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp @@ -11,11 +11,11 @@ struct [[gsl::Pointer()]] View { struct Plain { Plain() {} - Plain(const Plain &other [[clang::lifetimebound]]) {} // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Plain' cannot carry a lifetime}} + Plain(const Plain &other [[clang::lifetimebound]]) {} Plain operator+(const Plain &other [[clang::lifetimebound]]) const { return {}; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Plain' cannot carry a lifetime}} Plain &operator=(const Plain &other [[clang::lifetimebound]]) { return *this; } operator int() const [[clang::lifetimebound]] { return 0; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} - Plain(const Owner &o [[clang::lifetimebound]]) {} // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Plain' cannot carry a lifetime}} + Plain(const Owner &o [[clang::lifetimebound]]) {} }; enum Enum { Enumerator }; diff --git a/clang/test/Sema/LifetimeSafety/safety.cpp b/clang/test/Sema/LifetimeSafety/safety.cpp index af1e29a4baffaa..077c5de9648cbf 100644 --- a/clang/test/Sema/LifetimeSafety/safety.cpp +++ b/clang/test/Sema/LifetimeSafety/safety.cpp @@ -1,6 +1,6 @@ // RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wlifetime-safety-experimental-destructor-use -Wlifetime-safety-annotation-placement -Wno-dangling -verify=expected,function %s // RUN: %clang_cc1 -fsyntax-only -flifetime-safety-inference -fexperimental-lifetime-safety-tu-analysis -Wlifetime-safety -Wlifetime-safety-experimental-destructor-use -Wno-dangling -verify=expected,tu %s -// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wlifetime-safety-experimental-destructor-use -Wlifetime-safety-annotation-placement -Wno-dangling -fcxx-exceptions -verify=expected,function %s +// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety -Wlifetime-safety-experimental-destructor-use -Wno-dangling -fcxx-exceptions -verify=expected,function %s #include "Inputs/lifetime-analysis.h" @@ -3270,7 +3270,7 @@ S S::return_self_after_registration() const { struct SWithUserDefinedCopyLikeOps { SWithUserDefinedCopyLikeOps(); - SWithUserDefinedCopyLikeOps(const std::string &s [[clang::lifetimebound]]) : owned(s), data(s) {} // function-warning {{'lifetimebound' attribute has no effect on this function because return type 'track_origins_for_lifetimebound_record_type::SWithUserDefinedCopyLikeOps' cannot carry a lifetime}} + SWithUserDefinedCopyLikeOps(const std::string &s [[clang::lifetimebound]]) : owned(s), data(s) {} SWithUserDefinedCopyLikeOps(const SWithUserDefinedCopyLikeOps &other) : owned("copy"), data(owned) {} @@ -3333,7 +3333,7 @@ void from_typedef_return() { struct SWithOriginPropagatingCopy { SWithOriginPropagatingCopy(); - SWithOriginPropagatingCopy(const std::string &s [[clang::lifetimebound]]) : data(s) {} // function-warning {{'lifetimebound' attribute has no effect on this function because return type 'track_origins_for_lifetimebound_record_type::SWithOriginPropagatingCopy' cannot carry a lifetime}} + SWithOriginPropagatingCopy(const std::string &s [[clang::lifetimebound]]) : data(s) {} SWithOriginPropagatingCopy(const SWithOriginPropagatingCopy &other) : data(other.data) {} std::string_view data; }; @@ -3350,7 +3350,7 @@ SWithOriginPropagatingCopy user_defined_copy_with_origin_propagation() { struct DefaultedOuter { DefaultedOuter(); - DefaultedOuter(const std::string &s [[clang::lifetimebound]]) : inner(s) {} // function-warning {{'lifetimebound' attribute has no effect on this function because return type 'track_origins_for_lifetimebound_record_type::DefaultedOuter' cannot carry a lifetime}} + DefaultedOuter(const std::string &s [[clang::lifetimebound]]) : inner(s) {} SWithUserDefinedCopyLikeOps inner; }; >From 9d5a056b483ec383ee936104e2afa153b6f93e38 Mon Sep 17 00:00:00 2001 From: NeKon69 <[email protected]> Date: Fri, 16 Oct 2026 19:16:26 +0300 Subject: [PATCH 3/4] reword diags --- clang/include/clang/Basic/DiagnosticGroups.td | 2 +- .../clang/Basic/DiagnosticSemaKinds.td | 2 +- .../inapplicable-lifetimebound.cpp | 21 ++++++++++--------- .../misplaced-lifetimebound-intra-tu.cpp | 4 ++-- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index d1a507450b65c1..cab0cda38f6b32 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -651,7 +651,7 @@ This warning may produce false-positives diagnostics when it cannot fully model def LifetimeSafetyInapplicableLifetimeboundReturn : DiagGroup<"lifetime-safety-inapplicable-lifetimebound-return"> { code Documentation = [{ -Detects uses of [[clang::lifetimebound]] that have no effect because the function return type cannot carry a lifetime. +Detects uses of [[clang::lifetimebound]] that may be incorrect or indicate a missing lifetime annotation on the return type. }]; } diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 128548a97e57d4..568c4a6bade2e0 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11197,7 +11197,7 @@ def warn_lifetime_safety_inapplicable_lifetimebound DefaultIgnore; def warn_lifetime_safety_inapplicable_lifetimebound_return - : Warning<"'lifetimebound' attribute has no effect on this function because return type %0 cannot carry a lifetime. The attribute only applies when the return type is a reference type, pointer type, or non-owner record type">, + : Warning<"'lifetimebound' attribute may not apply to return type %0">, InGroup<LifetimeSafetyInapplicableLifetimeboundReturn>, DefaultIgnore; diff --git a/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp b/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp index 83877a92ed3f1b..ea2a95f4ef1ba5 100644 --- a/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp +++ b/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp @@ -1,4 +1,6 @@ -// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-annotation-placement -Wno-dangling -verify %s +// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-annotation-placement -Wno-dangling -verify=expected,return %s +// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-inapplicable-lifetimebound-return -Wno-dangling -verify=return %s +// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-annotation-placement -Wno-lifetime-safety-inapplicable-lifetimebound-return -Wno-dangling -verify=expected %s #include "Inputs/lifetime-analysis.h" @@ -12,9 +14,9 @@ struct [[gsl::Pointer()]] View { struct Plain { Plain() {} Plain(const Plain &other [[clang::lifetimebound]]) {} - Plain operator+(const Plain &other [[clang::lifetimebound]]) const { return {}; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Plain' cannot carry a lifetime}} + Plain operator+(const Plain &other [[clang::lifetimebound]]) const { return {}; } // return-warning {{'lifetimebound' attribute may not apply to return type 'Plain'}} Plain &operator=(const Plain &other [[clang::lifetimebound]]) { return *this; } - operator int() const [[clang::lifetimebound]] { return 0; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} + operator int() const [[clang::lifetimebound]] { return 0; } // return-warning {{'lifetimebound' attribute may not apply to return type 'int'}} Plain(const Owner &o [[clang::lifetimebound]]) {} }; @@ -98,20 +100,19 @@ int *context_sensitive_origin_type( return v[0]; } -int getInt(const Owner &o [[clang::lifetimebound]]) { return 0; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} -Owner getOwner(const Owner &o [[clang::lifetimebound]]) { return o; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Owner' cannot carry a lifetime}} -Plain getPlain(const Owner &o [[clang::lifetimebound]]) { return {}; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'Plain' cannot carry a lifetime}} +int getInt(const Owner &o [[clang::lifetimebound]]) { return 0; } // return-warning {{'lifetimebound' attribute may not apply to return type 'int'}} +Owner getOwner(const Owner &o [[clang::lifetimebound]]) { return o; } // return-warning {{'lifetimebound' attribute may not apply to return type 'Owner'}} +Plain getPlain(const Owner &o [[clang::lifetimebound]]) { return {}; } // return-warning {{'lifetimebound' attribute may not apply to return type 'Plain'}} View getView(const Owner &o [[clang::lifetimebound]]) { return View(); } Owner *getOwnerPtr(Owner &o [[clang::lifetimebound]]) { return &o; } -auto getAuto(const Owner &o [[clang::lifetimebound]]) { return 0; } // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} +auto getAuto(const Owner &o [[clang::lifetimebound]]) { return 0; } // return-warning {{'lifetimebound' attribute may not apply to return type 'int'}} -int getIntMultiParam(const Owner &o [[clang::lifetimebound]], int i [[clang::lifetimebound]]) { return 0; } // expected-warning 2 {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} \ +int getIntMultiParam(const Owner &o [[clang::lifetimebound]], int i [[clang::lifetimebound]]) { return 0; } // return-warning 2 {{'lifetimebound' attribute may not apply to return type 'int'}} \ // expected-warning {{'lifetimebound' attribute has no effect on parameter of type 'int'}} void test_lambda() { - auto lambda = [](const Owner &o [[clang::lifetimebound]]) -> int { return 0; }; // expected-warning {{'lifetimebound' attribute has no effect on this function because return type 'int' cannot carry a lifetime}} + auto lambda = [](const Owner &o [[clang::lifetimebound]]) -> int { return 0; }; // return-warning {{'lifetimebound' attribute may not apply to return type 'int'}} (void)lambda; } - diff --git a/clang/test/Sema/LifetimeSafety/misplaced-lifetimebound-intra-tu.cpp b/clang/test/Sema/LifetimeSafety/misplaced-lifetimebound-intra-tu.cpp index d7a49b9b8e5df5..9b47ee3e55d7e1 100644 --- a/clang/test/Sema/LifetimeSafety/misplaced-lifetimebound-intra-tu.cpp +++ b/clang/test/Sema/LifetimeSafety/misplaced-lifetimebound-intra-tu.cpp @@ -122,8 +122,8 @@ IntraSuppressedObj &intra_suppressed( } struct [[gsl::Pointer()]] View { - friend View friend_redecl(MyObj & // expected-warning {{'lifetimebound' attribute on this definition is not visible to callers before the definition; add it to the declaration instead}} - obj // CHECK: fix-it:"{{.*}}":{[[@LINE]]:{{[0-9]+}}-[[@LINE]]:{{[0-9]+}}}:" {{\[\[clang::lifetimebound\]\]}}" + friend View friend_redecl(MyObj & obj // expected-warning {{'lifetimebound' attribute on this definition is not visible to callers before the definition; add it to the declaration instead}} + // CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:{{[0-9]+}}-[[@LINE-1]]:{{[0-9]+}}}:" {{\[\[clang::lifetimebound\]\]}}" ); }; >From ab9e590b3faebbe2aea02f0a21a75fd62cdaa05d Mon Sep 17 00:00:00 2001 From: NeKon69 <[email protected]> Date: Fri, 6 Nov 2026 16:31:14 +0300 Subject: [PATCH 4/4] Add experimental warnings, improve tests --- clang/docs/LifetimeSafety.md | 5 ++ clang/docs/ReleaseNotes.md | 10 ++++ .../Analyses/LifetimeSafety/LifetimeSafety.h | 11 ++-- clang/include/clang/Basic/DiagnosticGroups.td | 24 +++++++- .../clang/Basic/DiagnosticSemaKinds.td | 12 +++- clang/lib/Analysis/LifetimeSafety/Checker.cpp | 33 ++++++----- clang/lib/Sema/SemaLifetimeSafety.h | 47 ++++++++-------- .../inapplicable-lifetimebound.cpp | 55 ++++++++++++++----- 8 files changed, 140 insertions(+), 57 deletions(-) diff --git a/clang/docs/LifetimeSafety.md b/clang/docs/LifetimeSafety.md index 1587e8df324107..fbbf67b42f67c6 100644 --- a/clang/docs/LifetimeSafety.md +++ b/clang/docs/LifetimeSafety.md @@ -517,6 +517,9 @@ enables only the high-confidence subset of these checks. - `-Wlifetime-safety-validations`: Enables checks that validate existing lifetime annotations. + - `-Wlifetime-safety-annotation-placement`: Checks that lifetime annotations are placed on appropriate types and declarations. + - `-Wlifetime-safety-inapplicable-lifetimebound`: Flags `[[clang::lifetimebound]]` on parameter types such as scalars and owning values. Includes the return-type check below. + - `-Wlifetime-safety-inapplicable-lifetimebound-return`: Flags `[[clang::lifetimebound]]` on parameters or `this` in function definitions with untracked return types. - `-Wlifetime-safety-noescape`: Warns when a parameter marked with `[[clang::noescape]]` escapes the function. - `-Wlifetime-safety-lifetimebound-violation`: Warns when the analysis cannot verify that the return value can be lifetime bound to a parameter marked with `[[clang::lifetimebound]]`. - `-Wlifetime-safety-capture-by-violation`: Warns when a parameter with `[[clang::lifetime_capture_by]]` attributes is captured by the enclosing object, by global storage, or by another reference parameter, but none of the attributes names that entity (`lifetime_capture_by_this`, `lifetime_capture_by_global`, or the capturing parameter) or `unknown`. Pointer parameters, stores into members of a reference parameter, and captures in calls to free functions are not verified. @@ -526,6 +529,8 @@ enables only the high-confidence subset of these checks. - `-Wlifetime-safety-experimental-invalidation`: Warns when a pointer, reference, iterator or view is used after an operation that may invalidate it, such as container mutation or explicit destruction (e.g., `std::unique_ptr::reset`, `std::destroy_at`). - `-Wlifetime-safety-experimental-field-origin-escape`: Warns when a member function returns a value derived from a pointer, reference, or view field rather than from `*this` (e.g., `Span::data()`). Owned pointer fields (e.g., `Vector::data()`) are also reported; mark such methods `[[clang::lifetimebound]]` to silence the warning. - `-Wlifetime-safety-experimental-destructor-use`: Treats a non-trivial destructor run at scope exit as a use of the borrows the object holds, so an object destroyed after the storage it borrows from is reported. Destructors that never read the borrow (e.g., `std::function`'s) are also reported. + - `-Wlifetime-safety-experimental-inapplicable-lifetimebound-ctor-return`: Flags `[[clang::lifetimebound]]` on constructor parameters when the constructed type falls outside the lifetime model for that constructor. + - `-Wlifetime-safety-experimental-strict-lifetimebound-return`: Flags `[[clang::lifetimebound]]` when calls or constructions in the definition cause the analysis to track the return type or constructed type. ## Limitations diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index 6bbc910d86b5aa..513f10fa4b1bff 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -733,6 +733,16 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and - `-Wlifetime-safety-experimental-destructor-use`, which warns when an object whose non-trivial destructor may read a borrow is destroyed after the storage it borrows from. + - `-Wlifetime-safety-experimental-inapplicable-lifetimebound-ctor-return`, + which flags `[[clang::lifetimebound]]` on constructor parameters when the + constructed type falls outside the lifetime model for that constructor. + - `-Wlifetime-safety-experimental-strict-lifetimebound-return`, which flags + `[[clang::lifetimebound]]` when calls or constructions in the definition + cause the analysis to track the result type. + +- Added `-Wlifetime-safety-inapplicable-lifetimebound-return` under + `-Wlifetime-safety-annotation-placement`. It flags `[[clang::lifetimebound]]` + in function definitions with untracked return types. - Lifetime safety analysis is now enabled for C by default. The `-fexperimental-lifetime-safety-c` flag is renamed to `-flifetime-safety-c`. Use `-fno-lifetime-safety-c` to disable it. diff --git a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h index 34774f23807804..d7601f6a102c0a 100644 --- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h +++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h @@ -34,6 +34,7 @@ #include <memory> namespace clang { +class LifetimeBoundAttr; class LifetimeCaptureByAttr; } // namespace clang @@ -189,12 +190,10 @@ class LifetimeSafetySemaHelper { const ParmVarDecl *PVDDef, const ParmVarDecl *PVDDecl) {} - virtual void reportInapplicableLifetimebound(const ParmVarDecl *PVD, - QualType Type) {} - virtual void reportInapplicableLifetimeboundReturnTy(const ParmVarDecl *PVD, - QualType Type) {} - virtual void - reportInapplicableLifetimeboundReturnTy(const CXXMethodDecl *MD) {} + virtual void reportInapplicableLifetimebound(const ParmVarDecl *PVD) {} + virtual void reportLifetimeboundReturn(const LifetimeBoundAttr &Attr, + QualType Type, bool isCtor, + bool IsStrict) {} // Suggests lifetime bound annotations for implicit this. virtual void suggestLifetimeboundToImplicitThis(WarningScope Scope, diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td index cab0cda38f6b32..f9ba3e28351ac5 100644 --- a/clang/include/clang/Basic/DiagnosticGroups.td +++ b/clang/include/clang/Basic/DiagnosticGroups.td @@ -651,7 +651,8 @@ This warning may produce false-positives diagnostics when it cannot fully model def LifetimeSafetyInapplicableLifetimeboundReturn : DiagGroup<"lifetime-safety-inapplicable-lifetimebound-return"> { code Documentation = [{ -Detects uses of [[clang::lifetimebound]] that may be incorrect or indicate a missing lifetime annotation on the return type. +Flags ``[[clang::lifetimebound]]`` in function definitions with untracked +return types. }]; } @@ -785,11 +786,30 @@ from. May report destructors that never read the borrow (e.g. ``std::function``) }]; } +def LifetimeSafetyInapplicableLifetimeboundCtorReturn + : DiagGroup<"lifetime-safety-experimental-inapplicable-lifetimebound-ctor-return"> { + code Documentation = [{ +Experimental. Flags ``[[clang::lifetimebound]]`` on constructor parameters +when the constructed type falls outside the lifetime model for that constructor. + }]; +} + +def LifetimeSafetyStrictLifetimeboundReturn + : DiagGroup<"lifetime-safety-experimental-strict-lifetimebound-return"> { + code Documentation = [{ +Experimental. Flags ``[[clang::lifetimebound]]`` when calls or constructions +in the definition cause the analysis to track the return type or constructed +type. + }]; +} + def LifetimeSafetyExperimental : DiagGroup<"lifetime-safety-experimental", [LifetimeSafetyInvalidation, LifetimeSafetyFieldOriginEscape, - LifetimeSafetyDestructorUse]> { + LifetimeSafetyDestructorUse, + LifetimeSafetyInapplicableLifetimeboundCtorReturn, + LifetimeSafetyStrictLifetimeboundReturn]> { code Documentation = [{ Experimental lifetime safety warnings. These may have known false positives and their behavior may change. Not enabled by -Wlifetime-safety-all. diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index 568c4a6bade2e0..bb0b73ab09515b 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -11197,10 +11197,20 @@ def warn_lifetime_safety_inapplicable_lifetimebound DefaultIgnore; def warn_lifetime_safety_inapplicable_lifetimebound_return - : Warning<"'lifetimebound' attribute may not apply to return type %0">, + : Warning<"result type %0 is not known to carry a lifetime">, InGroup<LifetimeSafetyInapplicableLifetimeboundReturn>, DefaultIgnore; +def warn_lifetime_safety_inapplicable_lifetimebound_ctor_return + : Warning<"'lifetimebound' attribute may not apply to constructed object type %0">, + InGroup<LifetimeSafetyInapplicableLifetimeboundCtorReturn>, + DefaultIgnore; + +def warn_lifetime_safety_strict_lifetimebound_return + : Warning<"%select{result type|constructed object type}0 %1 may not carry a lifetime">, + InGroup<LifetimeSafetyStrictLifetimeboundReturn>, + DefaultIgnore; + def note_lifetime_safety_used_here : Note<"later used here">; def note_lifetime_safety_used_by_destructor : Note<"later used by the destructor of %0 at scope exit">; diff --git a/clang/lib/Analysis/LifetimeSafety/Checker.cpp b/clang/lib/Analysis/LifetimeSafety/Checker.cpp index 2fcea526013766..bfb64d39160da3 100644 --- a/clang/lib/Analysis/LifetimeSafety/Checker.cpp +++ b/clang/lib/Analysis/LifetimeSafety/Checker.cpp @@ -678,22 +678,29 @@ class LifetimeChecker { FDef->getTemplateSpecializationKind() == TSK_ImplicitInstantiation) return; - QualType ReturnType = FDef->getReturnType(); - if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(FDef)) - ReturnType = AST.getTypeDeclType(cast<TypeDecl>(Ctor->getParent())); + const auto &OriginMgr = FactMgr.getOriginMgr(); + const auto *Ctor = dyn_cast<CXXConstructorDecl>(FDef); + const QualType ReturnTy = Ctor ? AST.getCanonicalTagType(Ctor->getParent()) + : FDef->getReturnType(); + const bool HasIntrinsic = + OriginMgr.hasOrigins(ReturnTy, /*IntrinsicOnly=*/true); + // Calls and constructions can register the result type for origin tracking. + const bool IsStrict = !HasIntrinsic && OriginMgr.hasOrigins(ReturnTy); + auto ReportReturn = [&](const LifetimeBoundAttr &Attr) { + if (!HasIntrinsic) + SemaHelper->reportLifetimeboundReturn(Attr, ReturnTy, Ctor != nullptr, + IsStrict); + }; - bool ReturnTypeHasOrigins = FactMgr.getOriginMgr().hasOrigins(ReturnType); - if (getImplicitObjectParamLifetimeBoundAttr(FDef) && !ReturnTypeHasOrigins) - SemaHelper->reportInapplicableLifetimeboundReturnTy( - cast<CXXMethodDecl>(FDef)); + if (const auto *MD = dyn_cast<CXXMethodDecl>(FDef)) + if (const auto *Attr = getImplicitObjectParamLifetimeBoundAttr(MD)) + ReportReturn(*Attr); for (const auto &PVD : FDef->parameters()) - if (PVD->hasAttr<LifetimeBoundAttr>()) { - if (!FactMgr.getOriginMgr().hasOrigins(PVD->getType(), - /*IntrinsicOnly=*/true)) - SemaHelper->reportInapplicableLifetimebound(PVD, PVD->getType()); - if (!ReturnTypeHasOrigins && !isa<CXXConstructorDecl>(FDef)) - SemaHelper->reportInapplicableLifetimeboundReturnTy(PVD, ReturnType); + if (const auto *Attr = PVD->getAttr<LifetimeBoundAttr>()) { + if (!OriginMgr.hasOrigins(PVD->getType(), /*IntrinsicOnly=*/true)) + SemaHelper->reportInapplicableLifetimebound(PVD); + ReportReturn(*Attr); } } diff --git a/clang/lib/Sema/SemaLifetimeSafety.h b/clang/lib/Sema/SemaLifetimeSafety.h index 85041636466e6c..63355bc66d4640 100644 --- a/clang/lib/Sema/SemaLifetimeSafety.h +++ b/clang/lib/Sema/SemaLifetimeSafety.h @@ -75,8 +75,16 @@ inline bool ShouldCheckMisplacedLifetimebound(Sema &S, const Decl *D) { } inline bool ShouldCheckInapplicableLifetimebound(Sema &S, const Decl *D) { - return !S.getDiagnostics().isIgnored( - diag::warn_lifetime_safety_inapplicable_lifetimebound, D->getBeginLoc()); + DiagnosticsEngine &Diags = S.getDiagnostics(); + constexpr unsigned DiagIDs[] = { + diag::warn_lifetime_safety_inapplicable_lifetimebound, + diag::warn_lifetime_safety_inapplicable_lifetimebound_return, + diag::warn_lifetime_safety_inapplicable_lifetimebound_ctor_return, + diag::warn_lifetime_safety_strict_lifetimebound_return}; + for (unsigned DiagID : DiagIDs) + if (!Diags.isIgnored(DiagID, D->getBeginLoc())) + return true; + return false; } inline bool ShouldCheckFieldOriginEscape(Sema &S, const Decl *D) { @@ -513,33 +521,28 @@ class LifetimeSafetySemaHelperImpl final : public LifetimeSafetySemaHelper { << Attr->getRange(); } - void reportInapplicableLifetimebound(const ParmVarDecl *PVD, - QualType Type) override { + void reportInapplicableLifetimebound(const ParmVarDecl *PVD) override { assert(PVD->hasAttr<LifetimeBoundAttr>() && "Expected parameter to have lifetimebound attribute"); const auto *Attr = PVD->getAttr<LifetimeBoundAttr>(); S.Diag(Attr->getLocation(), diag::warn_lifetime_safety_inapplicable_lifetimebound) - << Type << Attr->getRange(); + << PVD->getType() << Attr->getRange(); } - void reportInapplicableLifetimeboundReturnTy(const ParmVarDecl *PVD, - QualType Type) override { - assert(PVD->hasAttr<LifetimeBoundAttr>() && - "Expected parameter to have lifetimebound attribute"); - const auto *Attr = PVD->getAttr<LifetimeBoundAttr>(); - S.Diag(Attr->getLocation(), - diag::warn_lifetime_safety_inapplicable_lifetimebound_return) - << Type << Attr->getRange(); - } - - void - reportInapplicableLifetimeboundReturnTy(const CXXMethodDecl *MD) override { - const auto *Attr = getImplicitObjectParamLifetimeBoundAttr(MD); - assert(Attr && "Expected lifetimebound attribute"); - S.Diag(Attr->getLocation(), - diag::warn_lifetime_safety_inapplicable_lifetimebound_return) - << MD->getReturnType() << Attr->getRange(); + void reportLifetimeboundReturn(const LifetimeBoundAttr &Attr, QualType Type, + bool isCtor, bool IsStrict) override { + if (IsStrict) { + S.Diag(Attr.getLocation(), + diag::warn_lifetime_safety_strict_lifetimebound_return) + << isCtor << Type << Attr.getRange(); + return; + } + unsigned DiagID = + isCtor + ? diag::warn_lifetime_safety_inapplicable_lifetimebound_ctor_return + : diag::warn_lifetime_safety_inapplicable_lifetimebound_return; + S.Diag(Attr.getLocation(), DiagID) << Type << Attr.getRange(); } void suggestLifetimeboundToImplicitThis(WarningScope Scope, diff --git a/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp b/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp index ea2a95f4ef1ba5..ccc6082288ed41 100644 --- a/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp +++ b/clang/test/Sema/LifetimeSafety/inapplicable-lifetimebound.cpp @@ -1,23 +1,28 @@ // RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-annotation-placement -Wno-dangling -verify=expected,return %s // RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-inapplicable-lifetimebound-return -Wno-dangling -verify=return %s // RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-annotation-placement -Wno-lifetime-safety-inapplicable-lifetimebound-return -Wno-dangling -verify=expected %s +// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-experimental-inapplicable-lifetimebound-ctor-return -Wno-dangling -verify=ctor %s +// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-experimental-strict-lifetimebound-return -Wno-dangling -verify=context %s +// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-annotation-placement -Wlifetime-safety-experimental -Wno-dangling -verify=expected,return,ctor,context %s +// RUN: %clang_cc1 -fsyntax-only -Wlifetime-safety-annotation-placement -Wlifetime-safety-experimental -Wno-lifetime-safety-experimental-inapplicable-lifetimebound-ctor-return -Wno-lifetime-safety-experimental-strict-lifetimebound-return -Wno-dangling -verify=expected,return %s #include "Inputs/lifetime-analysis.h" struct [[gsl::Owner]] Owner {}; struct [[gsl::Pointer()]] View { - View(); - View(const Owner &o [[clang::lifetimebound]]); + View() = default; + View(const Owner &o [[clang::lifetimebound]]) : ptr(&o) {} + const Owner *ptr = nullptr; }; struct Plain { Plain() {} - Plain(const Plain &other [[clang::lifetimebound]]) {} - Plain operator+(const Plain &other [[clang::lifetimebound]]) const { return {}; } // return-warning {{'lifetimebound' attribute may not apply to return type 'Plain'}} + Plain(const Plain &other [[clang::lifetimebound]]) {} // ctor-warning {{'lifetimebound' attribute may not apply to constructed object type 'Plain'}} + Plain operator+(const Plain &other [[clang::lifetimebound]]) const { return {}; } // return-warning {{result type 'Plain' is not known to carry a lifetime}} Plain &operator=(const Plain &other [[clang::lifetimebound]]) { return *this; } - operator int() const [[clang::lifetimebound]] { return 0; } // return-warning {{'lifetimebound' attribute may not apply to return type 'int'}} - Plain(const Owner &o [[clang::lifetimebound]]) {} + operator int() const [[clang::lifetimebound]] { return 0; } // return-warning {{result type 'int' is not known to carry a lifetime}} + Plain(const Owner &o [[clang::lifetimebound]]) {} // ctor-warning {{'lifetimebound' attribute may not apply to constructed object type 'Plain'}} }; enum Enum { Enumerator }; @@ -100,19 +105,43 @@ int *context_sensitive_origin_type( return v[0]; } -int getInt(const Owner &o [[clang::lifetimebound]]) { return 0; } // return-warning {{'lifetimebound' attribute may not apply to return type 'int'}} -Owner getOwner(const Owner &o [[clang::lifetimebound]]) { return o; } // return-warning {{'lifetimebound' attribute may not apply to return type 'Owner'}} -Plain getPlain(const Owner &o [[clang::lifetimebound]]) { return {}; } // return-warning {{'lifetimebound' attribute may not apply to return type 'Plain'}} +Owner getOwner(const Owner &o [[clang::lifetimebound]]) { return o; } // return-warning {{result type 'Owner' is not known to carry a lifetime}} +Plain getPlain(const Owner &o [[clang::lifetimebound]]) { return {}; } // return-warning {{result type 'Plain' is not known to carry a lifetime}} View getView(const Owner &o [[clang::lifetimebound]]) { return View(); } -Owner *getOwnerPtr(Owner &o [[clang::lifetimebound]]) { return &o; } -auto getAuto(const Owner &o [[clang::lifetimebound]]) { return 0; } // return-warning {{'lifetimebound' attribute may not apply to return type 'int'}} +Plain getPlainWithLifetimebound(const Owner &o [[clang::lifetimebound]]); +Plain forwardPlain(const Owner &o [[clang::lifetimebound]]) // context-warning {{result type 'Plain' may not carry a lifetime}} +{ + return getPlainWithLifetimebound(o); +} + +// The discarded factory result registers Plain for tracking across the body. +Plain discardPlainResult(const Owner &o [[clang::lifetimebound]]) { // context-warning {{result type 'Plain' may not carry a lifetime}} + getPlainWithLifetimebound(o); + return {}; +} + +struct PlainHolder { + Owner owner; + Plain get() const [[clang::lifetimebound]] { // context-warning {{result type 'Plain' may not carry a lifetime}} + return getPlainWithLifetimebound(owner); + } +}; + +struct RelaxedCtor { + RelaxedCtor(const RelaxedCtor &other [[clang::lifetimebound]]) {} // ctor-warning {{'lifetimebound' attribute may not apply to constructed object type 'RelaxedCtor'}} + RelaxedCtor(const Owner &owner [[clang::lifetimebound]]) { // context-warning {{constructed object type 'RelaxedCtor' may not carry a lifetime}} + RelaxedCtor copy(*this); + } +}; + +auto getAuto(const Owner &o [[clang::lifetimebound]]) { return 0; } // return-warning {{result type 'int' is not known to carry a lifetime}} -int getIntMultiParam(const Owner &o [[clang::lifetimebound]], int i [[clang::lifetimebound]]) { return 0; } // return-warning 2 {{'lifetimebound' attribute may not apply to return type 'int'}} \ +int getIntMultiParam(const Owner &o [[clang::lifetimebound]], int i [[clang::lifetimebound]]) { return 0; } // return-warning 2 {{result type 'int' is not known to carry a lifetime}} \ // expected-warning {{'lifetimebound' attribute has no effect on parameter of type 'int'}} void test_lambda() { - auto lambda = [](const Owner &o [[clang::lifetimebound]]) -> int { return 0; }; // return-warning {{'lifetimebound' attribute may not apply to return type 'int'}} + auto lambda = [](const Owner &o [[clang::lifetimebound]]) -> int { return 0; }; // return-warning {{result type 'int' is not known to carry a lifetime}} (void)lambda; } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
