Author: NeKon69
Date: 2026-05-11T16:41:17+05:30
New Revision: 6f31d41ad4ccfda7853bfd9c5cffd9177f08cda8

URL: 
https://github.com/llvm/llvm-project/commit/6f31d41ad4ccfda7853bfd9c5cffd9177f08cda8
DIFF: 
https://github.com/llvm/llvm-project/commit/6f31d41ad4ccfda7853bfd9c5cffd9177f08cda8.diff

LOG: [LifetimeSafety] Impove `[[clang::lifetimbound]]` violation diagnostics 
(#196824)

Reports lifetimebound verification diagnostics at the attribute
location, so declarations with the attribute now point at the
declaration rather than only at the function definition.

Added: 
    

Modified: 
    clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
    clang/lib/Sema/SemaLifetimeSafety.h
    clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp

Removed: 
    


################################################################################
diff  --git 
a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h 
b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
index 7ccf30ba14987..7b0799d923f40 100644
--- a/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
+++ b/clang/include/clang/Analysis/Analyses/LifetimeSafety/LifetimeSafety.h
@@ -110,7 +110,8 @@ class LifetimeSafetySemaHelper {
 
   // Reports misuse of [[clang::lifetimebound]] when parameter doesn't escape
   // through return.
-  virtual void reportLifetimeboundViolation(const ParmVarDecl *VD) {}
+  virtual void
+  reportLifetimeboundViolation(const ParmVarDecl *ParmWithLifetimebound) {}
 
   // Suggests lifetime bound annotations for implicit this.
   virtual void suggestLifetimeboundToImplicitThis(SuggestionScope Scope,

diff  --git a/clang/lib/Sema/SemaLifetimeSafety.h 
b/clang/lib/Sema/SemaLifetimeSafety.h
index 5b1cf41445399..1ef28d8ba2cee 100644
--- a/clang/lib/Sema/SemaLifetimeSafety.h
+++ b/clang/lib/Sema/SemaLifetimeSafety.h
@@ -180,11 +180,12 @@ class LifetimeSafetySemaHelperImpl : public 
LifetimeSafetySemaHelper {
 
   void reportLifetimeboundViolation(
       const ParmVarDecl *ParmWithLifetimebound) override {
+    const auto *Attr = ParmWithLifetimebound->getAttr<LifetimeBoundAttr>();
     StringRef ParamName = ParmWithLifetimebound->getName();
     bool HasName = ParamName.size() > 0;
-    S.Diag(ParmWithLifetimebound->getLocation(),
+    S.Diag(Attr->getLocation(),
            diag::warn_lifetime_safety_param_lifetimebound_violation)
-        << HasName << ParamName << ParmWithLifetimebound->getSourceRange();
+        << HasName << ParamName << Attr->getRange();
   }
 
   void suggestLifetimeboundToImplicitThis(SuggestionScope Scope,

diff  --git a/clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp 
b/clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp
index 941a3bb8ce1e3..5764647ca62e0 100644
--- a/clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp
+++ b/clang/test/Sema/warn-lifetime-safety-lifetimebound.cpp
@@ -81,9 +81,8 @@ View unnamed_lifetimebound_param(
   return View();
 }
 
-// FIXME: Should warn on declaration, not definiton
-View annotated_decl_but_not_def_not_returned(const MyObj &obj 
[[clang::lifetimebound]]);
+View annotated_decl_but_not_def_not_returned(const MyObj &obj 
[[clang::lifetimebound]]); // expected-warning {{could not verify that the 
return value can be lifetime bound to 'obj'}}
 
-View annotated_decl_but_not_def_not_returned(const MyObj &obj) { // 
expected-warning {{could not verify that the return value can be lifetime bound 
to 'obj'}}
+View annotated_decl_but_not_def_not_returned(const MyObj &obj) {
   return not_lb(obj);
 }


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to