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

Utkarsh Saxena via cfe-commits cfe-commits at lists.llvm.org
Mon May 18 04:56:47 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();
----------------
usx95 wrote:

We add implicit attributes on redecl if we have attr in a previous decl. Therefore we could suggest attr on "first" decl in the TU (aka canonical decl). 

To prioritise "foo.h", we could instead suggest annotation on "first decl in a different file" but I would prefer to keep this policy simpler to understand by users. I would suggest to later have a separate warning enforcing that attrs on cross-tu redecls should match the canonical decl.

I also feel we should share this policy with annotation suggestions. Sharing this policy with `getCrossTUDecl` makes sense. To scope down this PR, we could change `getCrossTUDecl` in a separate PR to choose the canonical decl (instead of current: last decl in different file).

To summarise, we could:
1. (In this PR), introduce `getCanonicalDeclForAttr` to return the canonical decl and a warning scope (intra-tu vs xTU).
2. Next PR, replace `getCrossTUDecl` to use `getCanonicalDeclForAttr` for suggestions as well.
3. Next PR, identify unannotated cross-TU redeclarations not matching the canonical decl.

WDYT @Xazax-hun ?

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


More information about the cfe-commits mailing list