[clang] [WebKit checkers] Honor NOESCAPE on a parameter of a type with a trivial destructor (PR #242582)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 13 21:39:13 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-static-analyzer-1
Author: Ryosuke Niwa (rniwa)
<details>
<summary>Changes</summary>
webkit.UncountedLambdaCapturesChecker and related checkers reported a lambda passed to a NOESCAPE parameter of type WTF::ScopedLambda, as in:
bool doBytesMatchMetadataList(..., const FragmentedSharedBuffer* bytes, ...) {
return doBytesMatchMetadataList(tainting, [&](auto algorithm) {
return cryptographicDigestForSharedBuffer(algorithm, bytes);
}, metadataList);
}
findLambdaInArg looked for the lambda in an argument which constructs a temporary only when the construction was wrapped in a CXXBindTemporaryExpr. A temporary of a type with a trivial destructor, such as WTF::ScopedLambda, isn't wrapped in one, so the lambda wasn't found, and the checker instead saw it passed to ScopedLambda's converting constructor, whose parameter isn't NOESCAPE.
Look for the lambda in such a construction whether or not it's wrapped in a CXXBindTemporaryExpr.
Co-authored with Claude.
rdar://192641679
---
Full diff: https://github.com/llvm/llvm-project/pull/242582.diff
2 Files Affected:
- (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp (+9-8)
- (modified) clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp (+47)
``````````diff
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp
index 4e2c1323a3ca4e..2585c459ef77d7 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/RawPtrRefLambdaCapturesChecker.cpp
@@ -379,14 +379,15 @@ class RawPtrRefLambdaCapturesChecker
LambdaExpr *findLambdaInArg(Expr *E) {
if (auto *Lambda = dyn_cast_or_null<LambdaExpr>(E))
return Lambda;
- auto *TempExpr = dyn_cast_or_null<CXXBindTemporaryExpr>(E);
- if (!TempExpr)
- return nullptr;
- E = TempExpr->getSubExpr()->IgnoreParenCasts();
- if (!E)
- return nullptr;
- if (auto *Lambda = dyn_cast<LambdaExpr>(E))
- return Lambda;
+ // A temporary of a type with a trivial destructor, such as
+ // WTF::ScopedLambda, isn't wrapped in a CXXBindTemporaryExpr.
+ if (auto *TempExpr = dyn_cast_or_null<CXXBindTemporaryExpr>(E)) {
+ E = TempExpr->getSubExpr()->IgnoreParenCasts();
+ if (!E)
+ return nullptr;
+ if (auto *Lambda = dyn_cast<LambdaExpr>(E))
+ return Lambda;
+ }
auto *CE = dyn_cast_or_null<CXXConstructExpr>(E);
if (!CE || !CE->getNumArgs())
return nullptr;
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
index 302c1114f3a623..c638ba0f7a15a2 100644
--- a/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures.cpp
@@ -757,3 +757,50 @@ void lambda_escapes_after_void_cast(RefCountable* obj) {
(void)lambda;
callFunctionOpaque(lambda);
}
+
+namespace WTF {
+
+// ScopedLambda has a trivial destructor, so its temporary isn't wrapped in a
+// CXXBindTemporaryExpr.
+template <typename> class ScopedLambda;
+template <typename Result, typename... Arguments>
+class ScopedLambda<Result(Arguments...)> {
+public:
+ ScopedLambda(Result (*impl)(void*, Arguments...) = nullptr, void* arg = nullptr)
+ : m_impl(impl), m_arg(arg) { }
+ template <typename Functor>
+ ScopedLambda(Functor&& functor)
+ : m_impl([](void* arg, Arguments... arguments) -> Result {
+ return (*static_cast<typename std::remove_reference<Functor>::type*>(arg))(arguments...);
+ })
+ , m_arg(const_cast<void*>(static_cast<const void*>(&functor))) { }
+ ScopedLambda(const ScopedLambda&) = delete;
+ Result operator()(Arguments... arguments) const { return m_impl(m_arg, arguments...); }
+
+private:
+ Result (*m_impl)(void*, Arguments...);
+ void* m_arg;
+};
+
+} // namespace WTF
+
+int computeWithObj(int, RefCountable*);
+bool callScopedLambdaNoEscape(int, [[clang::noescape]] const WTF::ScopedLambda<int(int)>&);
+bool callScopedLambda(int, const WTF::ScopedLambda<int(int)>&);
+
+void scoped_lambda_noescape(RefCountable* obj) {
+ callScopedLambdaNoEscape(1, [&](auto value) {
+ return computeWithObj(value, obj);
+ });
+ callScopedLambdaNoEscape(1, [&](int value) {
+ return computeWithObj(value, obj);
+ });
+ auto lambda = [&](int value) {
+ return computeWithObj(value, obj);
+ };
+ callScopedLambdaNoEscape(1, lambda);
+ callScopedLambda(1, [&](int value) {
+ return computeWithObj(value, obj);
+ // expected-warning at -1{{Implicitly captured variable 'obj' is a raw pointer to RefPtr-capable type 'RefCountable' [webkit.UncountedLambdaCapturesChecker]}}
+ });
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/242582
More information about the cfe-commits
mailing list