================
@@ -163,8 +164,25 @@ class LifetimeChecker {
void suggestAnnotations() {
if (!Reporter)
return;
- for (const auto &[PVD, EscapeExpr] : AnnotationWarningsMap)
- Reporter->suggestAnnotation(PVD, EscapeExpr);
+ SourceManager &SM = AST.getSourceManager();
+ for (const auto &[PVD, EscapeExpr] : AnnotationWarningsMap) {
+ const auto *FD = dyn_cast<FunctionDecl>(PVD->getDeclContext());
+ if (!FD)
+ continue;
+ // For public functions (external linkage), find the header declaration
+ // to annotate; otherwise, treat as private and annotate the definition.
+ if (FD->isExternallyVisible()) {
+ const FunctionDecl *CanonicalFD = FD->getCanonicalDecl();
+ const ParmVarDecl *ParmToAnnotate = PVD;
+ if (CanonicalFD != FD && SM.getFileID(FD->getLocation()) !=
+ SM.getFileID(CanonicalFD->getLocation()))
+ if (unsigned Index = PVD->getFunctionScopeIndex();
+ Index < CanonicalFD->getNumParams())
+ ParmToAnnotate = CanonicalFD->getParamDecl(Index);
+ Reporter->suggestAnnotationsPublic(ParmToAnnotate, EscapeExpr);
+ } else
+ Reporter->suggestAnnotationsPrivate(PVD, EscapeExpr);
----------------
usx95 wrote:
Consider extracting a helper function `getCrossTUDecl(const FunctionDecl
*Definition)` that returns the declaration visible to other translation units
(if it exists), or `nullptr` if the function is intra-TU only.
https://github.com/llvm/llvm-project/pull/171972
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits