================
@@ -164,50 +171,68 @@ 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 (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<CXXMethodDecl>(CrossTUDecl),
+ EscapeExpr);
+ else
+ Reporter->suggestAnnotation(SuggestionScope::IntraTU, MD,
EscapeExpr);
+ }
}
}
----------------
usx95 wrote:
Let's try to reduce the branching here. Some refined AI suggestion to refactor
into functions:
```cpp
static void SuggestWithScopeForParmVar(LifetimeSafetyReporter *Reporter,
const ParmVarDecl *PVD,
SourceManager &SM,
const Expr *EscapeExpr) {
const FunctionDecl *CrossTUDecl = getCrossTUDecl(*PVD, SM);
SuggestionScope Scope = CrossTUDecl ? SuggestionScope::CrossTU
: SuggestionScope::IntraTU;
const ParmVarDecl *TargetPVD = CrossTUDecl
? CrossTUDecl->getParamDecl(PVD->getFunctionScopeIndex())
: PVD;
Reporter->suggestLifetimeboundToParmVar(Scope, TargetPVD, EscapeExpr);
}
static void SuggestWithScopeForImplicitThis(LifetimeSafetyReporter *Reporter,
const CXXMethodDecl *MD,
SourceManager &SM,
const Expr *EscapeExpr) {
const FunctionDecl *CrossTUDecl = getCrossTUDecl(*MD, SM);
SuggestionScope Scope = CrossTUDecl ? SuggestionScope::CrossTU
: SuggestionScope::IntraTU;
const CXXMethodDecl *TargetMD = CrossTUDecl
? cast<CXXMethodDecl>(CrossTUDecl)
: MD;
Reporter->suggestLifetimeboundToImplicitThis(Scope, TargetMD, EscapeExpr);
}
// In suggestAnnotations():
for (auto [Target, EscapeExpr] : AnnotationWarningsMap) {
if (const auto *PVD = Target.dyn_cast<const ParmVarDecl *>())
SuggestWithScopeForParmVar(Reporter, PVD, SM, EscapeExpr);
else if (const auto *MD = Target.dyn_cast<const CXXMethodDecl *>())
SuggestWithScopeForImplicitThis(Reporter, MD, SM, EscapeExpr);
}
```
https://github.com/llvm/llvm-project/pull/176703
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits