================
@@ -164,50 +171,70 @@ class LifetimeChecker {
/// Returns the declaration of a function that is visible across translation
/// units, if such a declaration exists and is different from the definition.
- static const FunctionDecl *getCrossTUDecl(const ParmVarDecl &PVD,
+ static const FunctionDecl *getCrossTUDecl(const FunctionDecl &FD,
SourceManager &SM) {
- const auto *FD = dyn_cast<FunctionDecl>(PVD.getDeclContext());
- if (!FD)
- return nullptr;
- if (!FD->isExternallyVisible())
+ if (!FD.isExternallyVisible())
return nullptr;
- const FileID DefinitionFile = SM.getFileID(FD->getLocation());
- for (const FunctionDecl *Redecl : FD->redecls())
+ const FileID DefinitionFile = SM.getFileID(FD.getLocation());
+ for (const FunctionDecl *Redecl : FD.redecls())
if (SM.getFileID(Redecl->getLocation()) != DefinitionFile)
return Redecl;
return nullptr;
}
+ static const FunctionDecl *getCrossTUDecl(const ParmVarDecl &PVD,
+ SourceManager &SM) {
+ if (const auto *FD = dyn_cast<FunctionDecl>(PVD.getDeclContext()))
+ return getCrossTUDecl(*FD, SM);
+ return nullptr;
+ }
+
void suggestAnnotations() {
if (!Reporter)
return;
SourceManager &SM = AST.getSourceManager();
- for (const auto &[PVD, EscapeExpr] : AnnotationWarningsMap) {
- if (const FunctionDecl *CrossTUDecl = getCrossTUDecl(*PVD, SM))
- Reporter->suggestAnnotation(
- SuggestionScope::CrossTU,
- CrossTUDecl->getParamDecl(PVD->getFunctionScopeIndex()),
- EscapeExpr);
- else
- Reporter->suggestAnnotation(SuggestionScope::IntraTU, PVD, EscapeExpr);
+ for (const auto &[Target, EscapeExpr] : AnnotationWarningsMap) {
+ if (const auto *PVD = Target.dyn_cast<const ParmVarDecl *>()) {
+ if (const FunctionDecl *CrossTUDecl = getCrossTUDecl(*PVD, SM))
+ Reporter->suggestAnnotation(
+ SuggestionScope::CrossTU,
+ CrossTUDecl->getParamDecl(PVD->getFunctionScopeIndex()),
+ EscapeExpr);
+ else
+ Reporter->suggestAnnotation(SuggestionScope::IntraTU, PVD,
+ EscapeExpr);
+ } else if (const auto *MD = Target.dyn_cast<const CXXMethodDecl *>()) {
+ if (const FunctionDecl *CrossTUDecl = getCrossTUDecl(*MD, SM))
+ Reporter->suggestAnnotation(SuggestionScope::CrossTU,
+ cast<const CXXMethodDecl>(CrossTUDecl),
+ EscapeExpr);
+ else
+ Reporter->suggestAnnotation(SuggestionScope::IntraTU, MD,
EscapeExpr);
+ }
}
}
void inferAnnotations() {
- for (const auto &[ConstPVD, EscapeExpr] : AnnotationWarningsMap) {
- ParmVarDecl *PVD = const_cast<ParmVarDecl *>(ConstPVD);
- const auto *FD = dyn_cast<FunctionDecl>(PVD->getDeclContext());
- if (!FD)
- continue;
- // Propagates inferred attributes via the most recent declaration to
- // ensure visibility for callers in post-order analysis.
- FD = getDeclWithMergedLifetimeBoundAttrs(FD);
- ParmVarDecl *InferredPVD = const_cast<ParmVarDecl *>(
- FD->getParamDecl(PVD->getFunctionScopeIndex()));
- if (!InferredPVD->hasAttr<LifetimeBoundAttr>())
- InferredPVD->addAttr(
- LifetimeBoundAttr::CreateImplicit(AST, PVD->getLocation()));
+ for (const auto &[Target, EscapeExpr] : AnnotationWarningsMap) {
+ if (const auto *MD = Target.dyn_cast<const CXXMethodDecl *>()) {
+ CXXMethodDecl *MutableMD = const_cast<CXXMethodDecl *>(MD);
+ if (!MutableMD->hasAttr<LifetimeBoundAttr>())
----------------
Xazax-hun wrote:
Does this work? I'd expect a call to `implicitObjectParamIsLifetimeBound` here
instead. Also, adding the attribute here might not be this easy, we usually
need to update the `TypeSourceInfo`.
We have the usual question about which declaration to update in the declaration
chain. Maybe the most recent one?
https://github.com/llvm/llvm-project/pull/176703
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits