https://github.com/Pranjal095 updated https://github.com/llvm/llvm-project/pull/167048
>From aa901d58c5d5d294d342ee079d2389b2c67ec8ba Mon Sep 17 00:00:00 2001 From: Pranjal095 <[email protected]> Date: Sat, 8 Nov 2025 04:37:26 +0530 Subject: [PATCH 1/2] [Clang] Add C++11 spelling support for guarded_by attribute This enables [[clang::guarded_by(x)]], the C++11 attribute spelling for guarded_by. Note that this required the thread safety handler to handle field references appropriately in attribute arguments, which was brought about by creating proper TIL representations for FieldDecl references in translateDeclRefExpr. This in turn happily combats a known false positive present in clang/test/SemaCXX/warn-thread-safety-analysis.cpp which was (but no longer) marked as a TODO. --- clang/include/clang/Basic/Attr.td | 6 +---- clang/lib/Analysis/ThreadSafetyCommon.cpp | 7 ++++++ clang/test/SemaCXX/gh158116.cpp | 25 +++++++++++++++++++ .../SemaCXX/warn-thread-safety-analysis.cpp | 4 --- 4 files changed, 33 insertions(+), 9 deletions(-) create mode 100644 clang/test/SemaCXX/gh158116.cpp diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index 1013bfc575747..d3477ccd40ff5 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -4129,7 +4129,7 @@ def NoThreadSafetyAnalysis : InheritableAttr { } def GuardedBy : InheritableAttr { - let Spellings = [GNU<"guarded_by">]; + let Spellings = [Clang<"guarded_by", 0>]; let Args = [ExprArgument<"Arg">]; let LateParsed = LateAttrParseExperimentalExt; let TemplateDependent = 1; @@ -5017,10 +5017,6 @@ def HLSLUnparsedSemantic : HLSLAnnotationAttr { let Documentation = [InternalOnly]; } -def HLSLUserSemantic : HLSLSemanticAttr</* Indexable= */ 1> { - let Documentation = [InternalOnly]; -} - def HLSLSV_Position : HLSLSemanticAttr</* Indexable= */ 1> { let Documentation = [HLSLSV_PositionDocs]; } 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<VarDecl>(VD)) return translateVariable(VarD, Ctx); + if (const auto *FD = dyn_cast<FieldDecl>(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/gh158116.cpp b/clang/test/SemaCXX/gh158116.cpp new file mode 100644 index 0000000000000..1b28f37cc3166 --- /dev/null +++ b/clang/test/SemaCXX/gh158116.cpp @@ -0,0 +1,25 @@ +// RUN: %clang_cc1 -fsyntax-only -verify -Wthread-safety -std=c++11 %s + +class __attribute__((lockable)) Mutex { +public: + void lock() __attribute__((exclusive_lock_function)); + void unlock() __attribute__((unlock_function)); +}; + +class Foo { + Mutex mu; + int y __attribute__((guarded_by(mu))); + int z [[clang::guarded_by(mu)]]; + + void func1() { + y = 0; // expected-warning{{writing variable 'y' requires holding}} + z = 1; // expected-warning{{writing variable 'z' requires holding}} + } + + void func2() { + mu.lock(); + y = 2; + z = 3; + mu.unlock(); + } +}; \ No newline at end of file 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'}} } }; >From b52b217aa26389c40b4392504668135abd39ac74 Mon Sep 17 00:00:00 2001 From: Pranjal095 <[email protected]> Date: Sun, 9 Nov 2025 11:11:27 +0530 Subject: [PATCH 2/2] Fix: Add missing upstream code from previous commit --- clang/include/clang/Basic/Attr.td | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/Basic/Attr.td b/clang/include/clang/Basic/Attr.td index d3477ccd40ff5..1013bfc575747 100644 --- a/clang/include/clang/Basic/Attr.td +++ b/clang/include/clang/Basic/Attr.td @@ -4129,7 +4129,7 @@ def NoThreadSafetyAnalysis : InheritableAttr { } def GuardedBy : InheritableAttr { - let Spellings = [Clang<"guarded_by", 0>]; + let Spellings = [GNU<"guarded_by">]; let Args = [ExprArgument<"Arg">]; let LateParsed = LateAttrParseExperimentalExt; let TemplateDependent = 1; @@ -5017,6 +5017,10 @@ def HLSLUnparsedSemantic : HLSLAnnotationAttr { let Documentation = [InternalOnly]; } +def HLSLUserSemantic : HLSLSemanticAttr</* Indexable= */ 1> { + let Documentation = [InternalOnly]; +} + def HLSLSV_Position : HLSLSemanticAttr</* Indexable= */ 1> { let Documentation = [HLSLSV_PositionDocs]; } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
