Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class UncountedLambdaCapturesChecker
const UncountedLambdaCapturesChecker *Checker;
llvm::DenseSet<const DeclRefExpr *> DeclRefExprsToIgnore;
llvm::DenseSet<const LambdaExpr *> LambdasToIgnore;
llvm::DenseSet<const CXXConstructExpr *> ConstructToIgnore;
QualType ClsType;

explicit LocalVisitor(const UncountedLambdaCapturesChecker *Checker)
Expand Down Expand Up @@ -106,6 +107,26 @@ class UncountedLambdaCapturesChecker
return safeGetName(NsDecl) == "WTF" && safeGetName(Decl) == "switchOn";
}

bool VisitCXXConstructExpr(CXXConstructExpr *CE) override {
if (ConstructToIgnore.contains(CE))
return true;
if (auto *Callee = CE->getConstructor()) {
unsigned ArgIndex = 0;
for (auto *Param : Callee->parameters()) {
if (ArgIndex >= CE->getNumArgs())
return true;
auto *Arg = CE->getArg(ArgIndex)->IgnoreParenCasts();
if (auto *L = findLambdaInArg(Arg)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my understanding: Is the LambdaExpr not covered in VisitLambdaExpr? What's special about the expression being an argument to a constructor?

Copy link
Contributor Author

@rniwa rniwa Feb 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, so we wanna ignore lambda expressions that are passed to [[clang::noescape]] argument. The code in VisitCallExpr and now VisitCXXConstructExpr is there to detect this specific case, and then ignore the argument.

LambdasToIgnore.insert(L);
if (!Param->hasAttr<NoEscapeAttr>())
Checker->visitLambdaExpr(L, shouldCheckThis());
}
++ArgIndex;
}
}
return true;
}

bool VisitCallExpr(CallExpr *CE) override {
checkCalleeLambda(CE);
if (auto *Callee = CE->getDirectCallee()) {
Expand Down Expand Up @@ -143,8 +164,10 @@ class UncountedLambdaCapturesChecker
auto *CtorArg = CE->getArg(0)->IgnoreParenCasts();
if (!CtorArg)
return nullptr;
if (auto *Lambda = dyn_cast<LambdaExpr>(CtorArg))
if (auto *Lambda = dyn_cast<LambdaExpr>(CtorArg)) {
ConstructToIgnore.insert(CE);
return Lambda;
}
auto *DRE = dyn_cast<DeclRefExpr>(CtorArg);
if (!DRE)
return nullptr;
Expand All @@ -157,6 +180,7 @@ class UncountedLambdaCapturesChecker
TempExpr = dyn_cast<CXXBindTemporaryExpr>(Init->IgnoreParenCasts());
if (!TempExpr)
return nullptr;
ConstructToIgnore.insert(CE);
return dyn_cast_or_null<LambdaExpr>(TempExpr->getSubExpr());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,11 @@ struct RefCountableWithLambdaCapturingThis {
callLambda([&]() -> RefPtr<RefCountable> {
return obj->next();
});
WTF::HashMap<int, RefPtr<RefCountable>> anotherMap([&] {
return obj->next();
});
}

};

struct NonRefCountableWithLambdaCapturingThis {
Expand Down