Skip to content

Conversation

@Pranjal095
Copy link

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.

Partially resolves #158116

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.
@github-actions
Copy link

github-actions bot commented Nov 7, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:analysis labels Nov 7, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2025

@llvm/pr-subscribers-clang

Author: Pranjal Prajapati (Pranjal095)

Changes

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.

Partially resolves #158116


Full diff: https://github.com/llvm/llvm-project/pull/167048.diff

4 Files Affected:

  • (modified) clang/include/clang/Basic/Attr.td (+1-5)
  • (modified) clang/lib/Analysis/ThreadSafetyCommon.cpp (+7)
  • (added) clang/test/SemaCXX/gh158116.cpp (+25)
  • (modified) clang/test/SemaCXX/warn-thread-safety-analysis.cpp (-4)
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'}}
   }
 };
 

@llvmbot
Copy link
Member

llvmbot commented Nov 7, 2025

@llvm/pr-subscribers-clang-analysis

Author: Pranjal Prajapati (Pranjal095)

Changes

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.

Partially resolves #158116


Full diff: https://github.com/llvm/llvm-project/pull/167048.diff

4 Files Affected:

  • (modified) clang/include/clang/Basic/Attr.td (+1-5)
  • (modified) clang/lib/Analysis/ThreadSafetyCommon.cpp (+7)
  • (added) clang/test/SemaCXX/gh158116.cpp (+25)
  • (modified) clang/test/SemaCXX/warn-thread-safety-analysis.cpp (-4)
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'}}
   }
 };
 

@zwuis zwuis requested a review from erichkeane November 9, 2025 05:15
Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The spelling addition is roughly acceptable, but this doesn't accomplish even 'partial' fix of the bug. The idea of the bug report is that you should analyze the existing attributes that were mentioned in the bug report, and spend some time analyzing them to figure out WHICH need different spellings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:analysis clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

guarded_by attributes not supported using c++11 style

4 participants