[clang] [LifetimeSafety] Extend suggestions for `lifetimebound` to also warn on canonical declarations (PR #198784)

Gábor Horváth via cfe-commits cfe-commits at lists.llvm.org
Fri May 22 02:42:09 PDT 2026


================
@@ -316,103 +316,87 @@ class LifetimeChecker {
     }
   }
 
-  std::pair<const FunctionDecl *, WarningScope>
-  getCanonicalFunctionDeclForAttr(const FunctionDecl *FDef) {
+  // Returns declarations that should be annotated with lifetime attributes
+  // in order to annotate FDef: the canonical declaration and the earliest
+  // redeclarations in each other file. This defines the placement policy for
+  // lifetime annotations. Each target is paired with its corresponding warning
+  // scope.
+  llvm::SmallVector<std::pair<const FunctionDecl *, WarningScope>, 2>
+  getTargetDeclsForAttr(const FunctionDecl *FDef) {
     if (!FDef)
-      return {nullptr, WarningScope::IntraTU};
+      return {};
 
     assert(FDef->isThisDeclarationADefinition() &&
            "Expected FunctionDecl to be a definition");
 
     const auto &SM = FDef->getASTContext().getSourceManager();
-    const FileID DefFile =
-        SM.getFileID(SM.getExpansionLoc(FDef->getLocation()));
-    const FunctionDecl *CanonicalDecl = FDef->getCanonicalDecl();
-    WarningScope Scope = WarningScope::IntraTU;
 
-    Scope = SM.getFileID(SM.getExpansionLoc(CanonicalDecl->getLocation())) !=
-                    DefFile
-                ? WarningScope::CrossTU
-                : WarningScope::IntraTU;
+    auto GetLoc = [&SM](const FunctionDecl *FD) {
+      return SM.getExpansionLoc(FD->getLocation());
----------------
Xazax-hun wrote:

Hmm. I think this is technically correct, expansion loc determines where is this declaration actually visible. But it is also a bit hard for the users to understand what is going on. Not for this PR, but whenever we use an expansion loc, it would be nice to have both the expansion and the spelling loc in the diagnostic via a note (when they differ). But this is absolutely a small thing for the future, should not block anything here. 

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


More information about the cfe-commits mailing list