[clang] [clang][ThreadSafety] Accept a pointee parameter of any redeclaration (PR #235813)
Jameson Nash via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 26 08:47:00 PDT 2026
https://github.com/vtjnash created https://github.com/llvm/llvm-project/pull/235813
An attribute on a function pointer may name a parameter of its pointee:
extern void (*cb)(int, Guarded *g);
void (*cb)(int, Guarded *g) REQUIRES(g->mu) = nullptr;
This also can come up for templates, where the "later" definition is the same definition, but re-instantiated later (templates also have other bugs which will be fixed separately). At a call through 'cb', the analysis substituted the call's arguments for the pointee's parameters, but it accepted only a parameter of the canonical declaration's prototype. Since the attribute above is on a redeclaration, it names that redeclaration's 'g', which hit the llvm_unreachable "ParmVarDecl does not belong to current declaration".
The `getCanonicalParamDecl` previously had two purposes: identifying the canonical identity of a parameter in the body, and this assert. There cannot be a body on a function pointer, so we don't need to be able to get its canonical identity. However, testing `isTranslationUnit` instead is exact here for our purposes of asserting on mistakes, based on how Sema (ActOnParamDeclarator) builds these attributes.
Assisted-by: Claude Opus 5.5
>From d51b2a18c6066245433da5b74ae86c4b6667a7a8 Mon Sep 17 00:00:00 2001
From: Jameson Nash <vtjnash at gmail.com>
Date: Thu, 24 Sep 2026 16:25:34 +0000
Subject: [PATCH] [clang][ThreadSafety] Accept a pointee parameter of any
redeclaration
An attribute on a function pointer may name a parameter of its pointee:
extern void (*cb)(int, Guarded *g);
void (*cb)(int, Guarded *g) REQUIRES(g->mu) = nullptr;
This also can come up for templates, where the "later" definition is the
same definition, but re-instantiated later (templates also have other
bugs which will be fixed separately). At a call through 'cb', the
analysis substituted the call's arguments for the pointee's parameters,
but it accepted only a parameter of the canonical declaration's
prototype. Since the attribute above is on a redeclaration, it names
that redeclaration's 'g', which hit the llvm_unreachable "ParmVarDecl
does not belong to current declaration".
The `getCanonicalParamDecl` previously had two purposes: identifying the
canonical identity of a parameter in the body, and this assert. There
cannot be a body on a function pointer, so we don't need to be able to
get its canonical identity.
Assisted-by: Claude Opus 5.5
---
clang/lib/Analysis/ThreadSafetyCommon.cpp | 21 +++++++------------
.../SemaCXX/warn-thread-safety-analysis.cpp | 12 +++++++++++
2 files changed, 19 insertions(+), 14 deletions(-)
diff --git a/clang/lib/Analysis/ThreadSafetyCommon.cpp b/clang/lib/Analysis/ThreadSafetyCommon.cpp
index d616e17c9aaedf..41427b7a5c8573 100644
--- a/clang/lib/Analysis/ThreadSafetyCommon.cpp
+++ b/clang/lib/Analysis/ThreadSafetyCommon.cpp
@@ -400,19 +400,6 @@ static const ParmVarDecl *getCanonicalParamDecl(const Decl *D, unsigned I) {
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;
}
@@ -453,7 +440,13 @@ til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE,
Match = (FD->getCanonicalDecl() == Canonical);
else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D))
Match = (MD->getCanonicalDecl() == Canonical);
- else if (getCanonicalParamDecl(Canonical, I) == PV->getCanonicalDecl())
+ else if (D->isTranslationUnit())
+ // Sema temporarily creates every parameter in the translation unit
+ // itself and moves it to its function once there is one. Thus, the
+ // parameters of a prototype nested in a declarator's type, such as a
+ // function pointer's, have no function and stay there. An attribute on
+ // the declarator can only name one of those, from its own type or a
+ // redeclaration's, so we know this must be a correct match.
Match = true;
else
llvm_unreachable("ParmVarDecl does not belong to current declaration");
diff --git a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
index d024f324e524dc..9ec82f60ded3fd 100644
--- a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
+++ b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
@@ -8307,6 +8307,18 @@ void test_attr_refers_to_param(Mutex *m) {
m->Unlock();
}
+struct Owner {
+ Mutex gmu;
+};
+
+// An attribute on a redeclaration names that redeclaration's parameters.
+extern void (*redecl_cb)(int, Owner *g);
+void (*redecl_cb)(int, Owner *g) EXCLUSIVE_LOCKS_REQUIRED(g->gmu) = nullptr;
+
+void callRedecl(int n, Owner *arg) {
+ redecl_cb(n, arg); // expected-warning {{calling function 'redecl_cb' requires holding mutex 'arg->gmu' exclusively}}
+}
+
// Function references name a function to call just like function pointers do.
void lock_impl(void) EXCLUSIVE_LOCK_FUNCTION(mu);
void unlock_impl(void) UNLOCK_FUNCTION(mu);
More information about the cfe-commits
mailing list