[clang] Thread Safety Analysis: Support attributes on function pointers (PR #191187)

Marco Elver via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 29 06:57:11 PDT 2026


================
@@ -374,6 +379,30 @@ til::SExpr *SExprBuilder::translate(const Stmt *S, CallingContext *Ctx) {
   return new (Arena) til::Undefined(S);
 }
 
+/// Helper to extract the canonical parameter declaration from a function or
+/// function pointer. This unwraps pointer and reference types to reach the
+/// underlying function prototype.
+static const ParmVarDecl *getCanonicalParamDecl(const Decl *D, unsigned I) {
+  if (const auto *FD = dyn_cast<FunctionDecl>(D))
+    return FD->getCanonicalDecl()->getParamDecl(I);
+  if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
+    return MD->getCanonicalDecl()->getParamDecl(I);
+  if (const auto *DD = dyn_cast<DeclaratorDecl>(D)) {
+    if (auto *TSI = DD->getTypeSourceInfo()) {
+      TypeLoc TL = TSI->getTypeLoc();
+      if (auto RTL = TL.getAsAdjusted<ReferenceTypeLoc>())
+        TL = RTL.getPointeeLoc();
+      // A function pointer can be multiple levels deep.
+      while (auto PTL = TL.getAsAdjusted<PointerTypeLoc>())
+        TL = PTL.getPointeeLoc();
+      if (auto FPTL = TL.getAsAdjusted<FunctionProtoTypeLoc>())
+        if (I < FPTL.getNumParams())
+          return FPTL.getParam(I);
+    }
+  }
+  return nullptr;
----------------
melver wrote:

Reachable, and now tested. The new branch in `translateDeclRefExpr` fires when an attribute on a function pointer var/field references one of the function pointer's own parameters. New test `test_attr_refers_to_param` should cover it.

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


More information about the cfe-commits mailing list