From 2e3e9bdad724e81ff10ac6b4b104369e68a5a231 Mon Sep 17 00:00:00 2001 From: Pranjal095 Date: Fri, 14 Nov 2025 13:02:09 +0530 Subject: [PATCH] [Clang][ThreadSafety] Contextualize FieldDecl references in thread safety attribute arguments This enables the thread safety handler to handle field references appropriately in attribute arguments by creating proper TIL representations for FieldDecl references in translateDeclRefExpr. Earlier, the analyzer treated the reference to the field name mu without the context required for it to match on this->mu, which led to a false positive in clang/test/SemaCXX/warn-thread-safety-analysis.cpp. That false positive is resolved with the changes made here. --- clang/lib/Analysis/ThreadSafetyCommon.cpp | 7 +++++++ clang/test/SemaCXX/warn-thread-safety-analysis.cpp | 4 ---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/clang/lib/Analysis/ThreadSafetyCommon.cpp b/clang/lib/Analysis/ThreadSafetyCommon.cpp index ef48ae439c5f3..89a3087a59247 100644 --- a/clang/lib/Analysis/ThreadSafetyCommon.cpp +++ b/clang/lib/Analysis/ThreadSafetyCommon.cpp @@ -394,6 +394,13 @@ til::SExpr *SExprBuilder::translateDeclRefExpr(const DeclRefExpr *DRE, if (const auto *VarD = dyn_cast(VD)) return translateVariable(VarD, Ctx); + if (const auto *FD = dyn_cast(VD)) { + if (Ctx && Ctx->SelfArg) { + til::SExpr *E = new (Arena) til::SApply(SelfVar); + return new (Arena) til::Project(E, FD); + } + } + // For non-local variables, treat it as a reference to a named object. return new (Arena) til::LiteralPtr(VD); } diff --git a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp index 0e91639a271c5..f68699f701851 100644 --- a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp +++ b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp @@ -3590,14 +3590,10 @@ void requireDecl(RelockableScope &scope) { struct foo { Mutex mu; - // expected-note@+1{{see attribute on parameter here}} void require(RelockableScope &scope EXCLUSIVE_LOCKS_REQUIRED(mu)); void callRequire(){ RelockableScope scope(&mu); - // TODO: False positive due to incorrect parsing of parameter attribute arguments. require(scope); - // expected-warning@-1{{calling function 'require' requires holding mutex 'mu' exclusively}} - // expected-warning@-2{{mutex managed by 'scope' is 'mu' instead of 'mu'}} } };