[clang] [webkit.UncountedLambdaCapturesChecker] Ignore trivial functions and [[clang::noescape]]. (PR #114897)

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 4 18:50:21 PST 2024


================
@@ -45,32 +52,119 @@ class UncountedLambdaCapturesChecker
       bool shouldVisitTemplateInstantiations() const { return true; }
       bool shouldVisitImplicitCode() const { return false; }
 
-      bool VisitLambdaExpr(LambdaExpr *L) {
-        Checker->visitLambdaExpr(L);
+      bool TraverseDecl(Decl *D) {
+        if (auto *CXXMD = dyn_cast<CXXMethodDecl>(D)) {
+          llvm::SaveAndRestore SavedDecl(ClsType);
+          ClsType = CXXMD->getThisType();
+          return Base::TraverseDecl(D);
+        }
+        return Base::TraverseDecl(D);
+      }
+
+      bool shouldCheckThis() {
+        auto result = !ClsType.isNull() ? isUnsafePtr(ClsType) : std::nullopt;
+        return result && *result;
+      }
+
+      bool VisitDeclRefExpr(DeclRefExpr *DRE) {
+        if (DeclRefExprsToIgnore.contains(DRE))
+          return true;
+        auto *VD = dyn_cast_or_null<VarDecl>(DRE->getDecl());
+        if (!VD)
+          return true;
+        auto *Init = VD->getInit();
+        if (!Init)
+          return true;
+        auto *L = dyn_cast_or_null<LambdaExpr>(Init->IgnoreParenCasts());
+        if (!L)
+          return true;
+        Checker->visitLambdaExpr(L, shouldCheckThis());
+        return true;
+      }
+
+      // WTF::switchOn(T, F... f) is a variadic template function and couldn't
+      // be annotated with NOESCAPE. We hard code it here to workaround that.
----------------
haoNoQ wrote:

Ooo interesting.

Technically there's standardized syntax for this sort of stuff: https://en.cppreference.com/w/cpp/language/parameter_pack
> Pack expansions are allowed in the lists of attributes if permitted by the attribute's specification. For example:
> ```
> template<int... args>
> [[vendor::attr(args)...]] void* f();
> ```
But, IIUC, this requires the attribute to be, like, a function attribute that receives the parameter identifier as an attribute argument(?)

So it'll probably not work out of the box but it's probably doable. If you run into many more functions of that nature, this could be a way out.

https://github.com/llvm/llvm-project/pull/114897


More information about the cfe-commits mailing list