Author: Jameson Nash Date: 2026-11-15T15:34:28-05:00 New Revision: d865ff992f525caaddb883a0b51098b0bb985e79
URL: https://github.com/llvm/llvm-project/commit/d865ff992f525caaddb883a0b51098b0bb985e79 DIFF: https://github.com/llvm/llvm-project/commit/d865ff992f525caaddb883a0b51098b0bb985e79.diff LOG: [clang][Sema][ThreadSafety] Accept a pointee parameter of any redeclaration (#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; Importantly, 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. A pointee's parameter belongs to no function, but instantiating one gave it the current context, so in a lambda in a function template it was taken for a parameter of the call operator at the same position, which asserted in getParamDecl. Keep it in the translation unit, as the one it is instantiated from is. When the declarator is itself a parameter, as for LOCK_RETURNED on a callback parameter, its attribute may also name a sibling parameter. That substituted the call's argument at the sibling's position, past the end of the arguments; match only a parameter nested deeper than the declarator. Assisted-by: Claude Opus 5.5 Added: Modified: clang/lib/Analysis/ThreadSafetyCommon.cpp clang/lib/Sema/SemaTemplateInstantiate.cpp clang/test/SemaCXX/warn-thread-safety-analysis.cpp Removed: ################################################################################ diff --git a/clang/lib/Analysis/ThreadSafetyCommon.cpp b/clang/lib/Analysis/ThreadSafetyCommon.cpp index c48ddd48c6f912..34502702ab14ff 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; } @@ -475,14 +462,25 @@ til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE, if (Ctx && Ctx->FunArgs) { const Decl *Canonical = Ctx->AttrDecl->getCanonicalDecl(); bool Match = false; - if (const auto *FD = dyn_cast<FunctionDecl>(D)) + if (const auto *FD = dyn_cast<FunctionDecl>(D)) { Match = (FD->getCanonicalDecl() == Canonical); - else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) + } else if (const auto *MD = dyn_cast<ObjCMethodDecl>(D)) { Match = (MD->getCanonicalDecl() == Canonical); - else if (getCanonicalParamDecl(Canonical, I) == PV->getCanonicalDecl()) - Match = true; - else + } 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. When the declarator is itself a parameter, its + // attribute may also name a sibling parameter, which is no deeper + // than it is; only one nested deeper is of its own prototype. + const auto *AttrPV = dyn_cast<ParmVarDecl>(Ctx->AttrDecl); + Match = !AttrPV || + PV->getFunctionScopeDepth() > AttrPV->getFunctionScopeDepth(); + } else { llvm_unreachable("ParmVarDecl does not belong to current declaration"); + } if (Match) { // Substitute call arguments for references to function parameters diff --git a/clang/lib/Sema/SemaTemplateInstantiate.cpp b/clang/lib/Sema/SemaTemplateInstantiate.cpp index 17842f55031200..4820aa6418f28d 100644 --- a/clang/lib/Sema/SemaTemplateInstantiate.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -3339,9 +3339,13 @@ ParmVarDecl *Sema::SubstParmVarDecl( CurrentInstantiationScope->InstantiatedLocal(OldParm, NewParm); } - // FIXME: OldParm may come from a FunctionProtoType, in which case CurContext - // can be anything, is this right ? - NewParm->setDeclContext(CurContext); + // A parameter that is not a function's own, such as one of a function + // pointer's pointee, belongs to no function (see ActOnParamDeclarator); keep + // it in the translation unit, where CheckParameter created it. + // FIXME: For other parameters from a FunctionProtoType, CurContext can be + // anything; is this right? + if (!OldParm->getDeclContext()->isTranslationUnit()) + NewParm->setDeclContext(CurContext); NewParm->setScopeInfo(OldParm->getFunctionScopeDepth(), OldParm->getFunctionScopeIndex() + indexAdjustment); diff --git a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp index 34c68679c3837b..afde78942be560 100644 --- a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp +++ b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp @@ -8307,6 +8307,42 @@ 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}} +} + +// An instantiated pointee parameter belongs to no function either, not to the +// lambda's call operator, which has no parameter at that position. +template <typename T> +void callInTemplateLambda(T *arg) { + auto lam = [&]() { + void (*cb)(T *g) EXCLUSIVE_LOCKS_REQUIRED(g->gmu) = nullptr; + cb(arg); // expected-warning {{calling function 'cb' requires holding mutex 'arg->gmu' exclusively}} + }; + lam(); +} + +void testCallInTemplateLambda(Owner *arg) { + callInTemplateLambda(arg); // expected-note {{in instantiation of function template specialization}} +} + +// The attribute on 'mu_of_g' names its sibling 'g', not a parameter of its own +// prototype, so a call to 'mu_of_g' has no argument for it. +void (*with_lock)(int n, Owner *g, Mutex *(*mu_of_g)() LOCK_RETURNED(&g->gmu)) + EXCLUSIVE_LOCKS_REQUIRED(mu_of_g()); + +void callWithLock(int n, Owner *arg, Mutex *(*fn)()) { + with_lock(n, arg, fn); // expected-warning {{calling function 'with_lock' requires holding mutex 'g->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); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
