[clang] [LifetimeSafety] Warn when lifetimebound attribute is present on the definition but not on declaration (PR #197753)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Fri May 15 03:23:19 PDT 2026


================
@@ -415,6 +416,28 @@ class LifetimeChecker {
     }
   }
 
+  void reportMisplacedLifetimebound() {
+    const FunctionDecl *FDef = dyn_cast<FunctionDecl>(FD);
+    if (!FDef)
+      return;
+
+    const FunctionDecl *FDecl = FDef->getPreviousDecl();
----------------
Xazax-hun wrote:

We might have a long chain of redeclarations. Do we want to warn only for one redeclaration? 

Also, I wonder if we only care about redeclarations in the header files.

E.g.:
```
// foo.h
int* f(int *);

// foo.cpp
int* f(int *);
#include foo.h
int* f(int *);

// later

int *f(int* p [[clang::lifetimebound]]) {
  return p;
}
```

The most value here is asking the user to put the annotation on the declaration in `foo.h`. Less value in adding it to the other redeclarations. 

https://github.com/llvm/llvm-project/pull/197753


More information about the cfe-commits mailing list