Skip to content

Commit 69edf7f

Browse files
committed
[webkit.UncountedLambdaCapturesChecker] Support [[clang::noescape]] on a constructor (llvm#126869)
Added the support for annotating a constructor's argument with [[clang::noescape]]. We explicitly ignore CXXConstructExpr which is visited as a part of CallExpr so that construction of closures like Function, CompletionHandler, etc... don't result in a warning.
1 parent d109d96 commit 69edf7f

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class UncountedLambdaCapturesChecker
4141
const UncountedLambdaCapturesChecker *Checker;
4242
llvm::DenseSet<const DeclRefExpr *> DeclRefExprsToIgnore;
4343
llvm::DenseSet<const LambdaExpr *> LambdasToIgnore;
44+
llvm::DenseSet<const CXXConstructExpr *> ConstructToIgnore;
4445
QualType ClsType;
4546

4647
using Base = RecursiveASTVisitor<LocalVisitor>;
@@ -109,6 +110,26 @@ class UncountedLambdaCapturesChecker
109110
return safeGetName(NsDecl) == "WTF" && safeGetName(Decl) == "switchOn";
110111
}
111112

113+
bool VisitCXXConstructExpr(CXXConstructExpr *CE) {
114+
if (ConstructToIgnore.contains(CE))
115+
return true;
116+
if (auto *Callee = CE->getConstructor()) {
117+
unsigned ArgIndex = 0;
118+
for (auto *Param : Callee->parameters()) {
119+
if (ArgIndex >= CE->getNumArgs())
120+
return true;
121+
auto *Arg = CE->getArg(ArgIndex)->IgnoreParenCasts();
122+
if (auto *L = findLambdaInArg(Arg)) {
123+
LambdasToIgnore.insert(L);
124+
if (!Param->hasAttr<NoEscapeAttr>())
125+
Checker->visitLambdaExpr(L, shouldCheckThis());
126+
}
127+
++ArgIndex;
128+
}
129+
}
130+
return true;
131+
}
132+
112133
bool VisitCallExpr(CallExpr *CE) {
113134
checkCalleeLambda(CE);
114135
if (auto *Callee = CE->getDirectCallee()) {
@@ -146,8 +167,10 @@ class UncountedLambdaCapturesChecker
146167
auto *CtorArg = CE->getArg(0)->IgnoreParenCasts();
147168
if (!CtorArg)
148169
return nullptr;
149-
if (auto *Lambda = dyn_cast<LambdaExpr>(CtorArg))
170+
if (auto *Lambda = dyn_cast<LambdaExpr>(CtorArg)) {
171+
ConstructToIgnore.insert(CE);
150172
return Lambda;
173+
}
151174
auto *DRE = dyn_cast<DeclRefExpr>(CtorArg);
152175
if (!DRE)
153176
return nullptr;
@@ -160,6 +183,7 @@ class UncountedLambdaCapturesChecker
160183
TempExpr = dyn_cast<CXXBindTemporaryExpr>(Init->IgnoreParenCasts());
161184
if (!TempExpr)
162185
return nullptr;
186+
ConstructToIgnore.insert(CE);
163187
return dyn_cast_or_null<LambdaExpr>(TempExpr->getSubExpr());
164188
}
165189

clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ struct RefCountableWithLambdaCapturingThis {
298298
callLambda([&]() -> RefPtr<RefCountable> {
299299
return obj->next();
300300
});
301+
WTF::HashMap<int, RefPtr<RefCountable>> anotherMap([&] {
302+
return obj->next();
303+
});
301304
}
305+
302306
};
303307

304308
struct NonRefCountableWithLambdaCapturingThis {

0 commit comments

Comments
 (0)