[clang] [clang][Sema][ThreadSafety] Accept attributes without arguments as meaning 'this' (PR #242937)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Nov 15 18:32:06 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Jameson Nash (vtjnash)
<details>
<summary>Changes</summary>
An attribute such as ACQUIRE or RELEASE without capability arguments is documented to refer to 'this': the analysis translates it to the self argument of the call (ThreadSafetyAnalyzer::getMutexIDs). But Sema accepts one only on a non-static method of a capability class, and elsewhere warns, even though it keeps the attribute.
On a function pointer field, the meaning of such an attribute has slowly changed without being explicitly proposed. d1e84bb9ad3b (#<!-- -->191187) allowed these attributes on function pointer fields. A call through a field had no self argument then, so such an attribute on a field translated to no capability and was dropped. dfeadc6b0836 (#<!-- -->231602) made the base of the member access the self argument of a call through a field, so that the attribute could name a sibling member. As a side effect, an attribute without arguments on a field came to name that object, as it does on a method. For
struct CAPABILITY("mutex") Node {
void (*lock)(void) ACQUIRE();
};
`n->lock()` acquires 'n', while Sema still warns that the attribute applies only to methods.
The two should agree. This change proposes keeping the analysis's meaning: on a field, an attribute without arguments refers to the object the field is called through. That is the documented rule for methods read with "the object called through" for 'this', so the documentation needs no change. It is also the only way to name that object from C, which has no 'this'. The analysis is unchanged: with the previous Sema check the new analysis tests pass, and only the
-Wthread-safety-attributes warnings differ.
Previously, it wasn't clear why this would be useful: https://github.com/llvm/llvm-project/issues/20777#issuecomment-1013343709 I've encountered a possible use: enforcing a constraint that isn't a real lock, so there's nothing to put in a member, but which would be useful to verify in this framework.
What is new to permit that now is just in Sema: on a field, it accepts an attribute without arguments, and checks that the record the field is a member of is a capability, as it checks the class of a method. For a field of an anonymous struct or union, that is the anonymous record, which is also what 'this' names inside one in C++. The two diagnostics change so that the first names function pointer members and the second does not say 'this' for a field.
The alternative is to restore the earlier behavior: add an exception to the analysis to ignore an attribute without arguments on a field, while keeping the Sema warning. The documentation would then need to say that an empty list means 'this' on a method but nothing on a function pointer field, and C code would still have no way to name the object.
Assisted-by: Claude (Fable 5.1, Opus 5.5)
---
Patch is 21.50 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/242937.diff
6 Files Affected:
- (modified) clang/include/clang/Basic/DiagnosticSemaKinds.td (+4-3)
- (modified) clang/lib/Sema/SemaDeclAttr.cpp (+12-5)
- (modified) clang/test/Sema/attr-capabilities.c (+27-9)
- (modified) clang/test/Sema/warn-thread-safety-analysis.c (+13)
- (modified) clang/test/SemaCXX/warn-thread-safety-analysis.cpp (+39)
- (modified) clang/test/SemaCXX/warn-thread-safety-parsing.cpp (+35-15)
``````````diff
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index dff45a081f2e8c..d69b01a070c2e5 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -4375,11 +4375,12 @@ def warn_thread_attribute_ignored : Warning<
InGroup<ThreadSafetyAttributes>, DefaultIgnore;
def warn_thread_attribute_not_on_non_static_member : Warning<
"%0 attribute without capability arguments can only be applied to non-static "
- "methods of a class">,
+ "methods and function pointer members of a class">,
InGroup<ThreadSafetyAttributes>, DefaultIgnore;
def warn_thread_attribute_not_on_capability_member : Warning<
- "%0 attribute without capability arguments refers to 'this', but %1 isn't "
- "annotated with 'capability' or 'scoped_lockable' attribute">,
+ "%0 attribute without capability arguments refers to "
+ "%select{'this'|the object it is called through}2, but %1 isn't annotated "
+ "with 'capability' or 'scoped_lockable' attribute">,
InGroup<ThreadSafetyAttributes>, DefaultIgnore;
def warn_thread_attribute_argument_not_lockable : Warning<
"%0 attribute requires arguments whose type is annotated "
diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp
index 0392c5067a1184..3aede092250dad 100644
--- a/clang/lib/Sema/SemaDeclAttr.cpp
+++ b/clang/lib/Sema/SemaDeclAttr.cpp
@@ -352,16 +352,23 @@ static void checkAttrArgsAreCapabilityObjs(Sema &S, Decl *D,
if (Sidx == AL.getNumArgs()) {
// If we don't have any capability arguments, the attribute implicitly
// refers to 'this'. So we need to make sure that 'this' exists, i.e. we're
- // a non-static method, and that the class is a (scoped) capability.
- const auto *MD = dyn_cast<const CXXMethodDecl>(D);
- if (MD && !MD->isStatic()) {
- const CXXRecordDecl *RD = MD->getParent();
+ // a non-static method, and that the class is a (scoped) capability. A
+ // function pointer field is likewise called through an object, which the
+ // attribute then refers to.
+ const RecordDecl *RD = nullptr;
+ if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) {
+ if (!MD->isStatic())
+ RD = MD->getParent();
+ } else if (const auto *FD = dyn_cast<FieldDecl>(D)) {
+ RD = FD->getParent();
+ }
+ if (RD) {
// FIXME -- need to check this again on template instantiation
if (!checkRecordDeclForAttr<CapabilityAttr>(RD) &&
!checkRecordDeclForAttr<ScopedLockableAttr>(RD))
S.Diag(AL.getLoc(),
diag::warn_thread_attribute_not_on_capability_member)
- << AL << MD->getParent();
+ << AL << RD << isa<FieldDecl>(D);
} else {
S.Diag(AL.getLoc(), diag::warn_thread_attribute_not_on_non_static_member)
<< AL;
diff --git a/clang/test/Sema/attr-capabilities.c b/clang/test/Sema/attr-capabilities.c
index 6d594bca075350..9967a8c053042e 100644
--- a/clang/test/Sema/attr-capabilities.c
+++ b/clang/test/Sema/attr-capabilities.c
@@ -37,25 +37,25 @@ void Func6(void) __attribute__((requires_shared_capability(BadCapability))) {}
void Func7(void) __attribute__((assert_capability(GUI))) {}
void Func8(void) __attribute__((assert_shared_capability(GUI))) {}
-void Func9(void) __attribute__((assert_capability())) {} // expected-warning {{'assert_capability' attribute without capability arguments can only be applied to non-static methods of a class}}
-void Func10(void) __attribute__((assert_shared_capability())) {} // expected-warning {{'assert_shared_capability' attribute without capability arguments can only be applied to non-static methods of a class}}
+void Func9(void) __attribute__((assert_capability())) {} // expected-warning {{'assert_capability' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
+void Func10(void) __attribute__((assert_shared_capability())) {} // expected-warning {{'assert_shared_capability' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
void Func11(void) __attribute__((acquire_capability(GUI))) {}
void Func12(void) __attribute__((acquire_shared_capability(GUI))) {}
-void Func13(void) __attribute__((acquire_capability())) {} // expected-warning {{'acquire_capability' attribute without capability arguments can only be applied to non-static methods of a class}}
-void Func14(void) __attribute__((acquire_shared_capability())) {} // expected-warning {{'acquire_shared_capability' attribute without capability arguments can only be applied to non-static methods of a class}}
+void Func13(void) __attribute__((acquire_capability())) {} // expected-warning {{'acquire_capability' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
+void Func14(void) __attribute__((acquire_shared_capability())) {} // expected-warning {{'acquire_shared_capability' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
void Func15(void) __attribute__((release_capability(GUI))) {}
void Func16(void) __attribute__((release_shared_capability(GUI))) {}
void Func17(void) __attribute__((release_generic_capability(GUI))) {}
-void Func18(void) __attribute__((release_capability())) {} // expected-warning {{'release_capability' attribute without capability arguments can only be applied to non-static methods of a class}}
-void Func19(void) __attribute__((release_shared_capability())) {} // expected-warning {{'release_shared_capability' attribute without capability arguments can only be applied to non-static methods of a class}}
-void Func20(void) __attribute__((release_generic_capability())) {} // expected-warning {{'release_generic_capability' attribute without capability arguments can only be applied to non-static methods of a class}}
+void Func18(void) __attribute__((release_capability())) {} // expected-warning {{'release_capability' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
+void Func19(void) __attribute__((release_shared_capability())) {} // expected-warning {{'release_shared_capability' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
+void Func20(void) __attribute__((release_generic_capability())) {} // expected-warning {{'release_generic_capability' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
-void Func21(void) __attribute__((try_acquire_capability(1))) {} // expected-warning {{'try_acquire_capability' attribute without capability arguments can only be applied to non-static methods of a class}}
-void Func22(void) __attribute__((try_acquire_shared_capability(1))) {} // expected-warning {{'try_acquire_shared_capability' attribute without capability arguments can only be applied to non-static methods of a class}}
+void Func21(void) __attribute__((try_acquire_capability(1))) {} // expected-warning {{'try_acquire_capability' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
+void Func22(void) __attribute__((try_acquire_shared_capability(1))) {} // expected-warning {{'try_acquire_shared_capability' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
void Func23(void) __attribute__((try_acquire_capability(1, GUI))) {}
void Func24(void) __attribute__((try_acquire_shared_capability(1, GUI))) {}
@@ -71,3 +71,21 @@ void Func30(void) __attribute__((requires_capability((Worker || Worker) && !GUI)
int AlsoNotACapability;
void Func31(void) __attribute__((requires_capability(GUI && AlsoNotACapability))); // expected-warning {{'requires_capability' attribute requires arguments whose type is annotated with 'capability' attribute; type here is 'int'}}
+
+// Without arguments, an attribute on a function pointer field refers to the
+// object the field is called through, which must be a capability.
+struct __attribute__((capability("mutex"))) FPCap {
+ void (*lock)(void) __attribute__((acquire_capability()));
+ void (*unlock)(void) __attribute__((release_capability()));
+};
+struct FPNotCap {
+ void (*lock)(void) __attribute__((acquire_capability())); // expected-warning {{'acquire_capability' attribute without capability arguments refers to the object it is called through, but 'FPNotCap' isn't annotated with 'capability' or 'scoped_lockable' attribute}}
+};
+
+// A field of an anonymous struct or union is called through the anonymous
+// object, which must be the capability, not the record it is in.
+struct __attribute__((capability("mutex"))) FPAnonInCap {
+ struct {
+ void (*lock)(void) __attribute__((acquire_capability())); // expected-warning {{'acquire_capability' attribute without capability arguments refers to the object it is called through, but 'struct (unnamed at}}
+ };
+};
diff --git a/clang/test/Sema/warn-thread-safety-analysis.c b/clang/test/Sema/warn-thread-safety-analysis.c
index 37dd29a659e977..7f482e1873d779 100644
--- a/clang/test/Sema/warn-thread-safety-analysis.c
+++ b/clang/test/Sema/warn-thread-safety-analysis.c
@@ -703,6 +703,19 @@ void late_param_other(void (*release)(struct Mutex *) UNLOCK_FUNCTION(mu),
} // expected-warning {{expecting mutex 'mu' to be held at the end of function}}
#endif
+// Without arguments, an attribute on a function pointer field refers to the
+// object the field is called through, as it refers to 'this' on a method.
+struct LOCKABLE Node {
+ void (*lock)(void) EXCLUSIVE_LOCK_FUNCTION();
+ void (*unlock)(void) UNLOCK_FUNCTION();
+};
+
+void test_node(struct Node *n, struct Node *other) {
+ n->lock();
+ other->unlock(); // expected-warning {{releasing mutex 'other' that was not held}}
+ n->unlock();
+}
+
// Test unusual trylock patterns
void do_some_work(void);
int work_data GUARDED_BY(mu1);
diff --git a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
index afde78942be560..e7208d5ef10442 100644
--- a/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
+++ b/clang/test/SemaCXX/warn-thread-safety-analysis.cpp
@@ -8518,6 +8518,45 @@ struct GuardedOpsSelf {
}
};
+// Without arguments, an attribute on a function pointer field refers to the
+// object the field is called through, the object 'this' names in it.
+struct LOCKABLE SelfOps {
+ void (*lock)() EXCLUSIVE_LOCK_FUNCTION();
+ void (*unlock)() UNLOCK_FUNCTION();
+ int data GUARDED_BY(this);
+ void self() { lock(); data = 1; unlock(); }
+ struct LOCKABLE {
+ void (*anonLock)() EXCLUSIVE_LOCK_FUNCTION();
+ void (*anonUnlock)() UNLOCK_FUNCTION();
+ int anonData GUARDED_BY(this); // the anonymous object, as for anonLock
+ };
+};
+
+void requiresSelfOps(SelfOps *s) EXCLUSIVE_LOCKS_REQUIRED(s);
+
+void testSelfOps(SelfOps *s, SelfOps *o) {
+ s->lock();
+ s->data = 1;
+ requiresSelfOps(s);
+ o->data = 1; // expected-warning {{writing variable 'data' requires holding mutex 'o' exclusively}}
+ s->unlock();
+ s->anonLock();
+ s->anonData = 1;
+ s->anonUnlock();
+}
+
+template <class F> struct LOCKABLE SelfOpsTemplate {
+ F lock EXCLUSIVE_LOCK_FUNCTION();
+ F unlock UNLOCK_FUNCTION();
+ int data GUARDED_BY(this);
+};
+
+void testSelfOpsTemplate(SelfOpsTemplate<void (*)()> *t) {
+ t->lock();
+ t->data = 1;
+ t->unlock();
+}
+
} // namespace FunctionPointers
namespace AnonymousMembers {
diff --git a/clang/test/SemaCXX/warn-thread-safety-parsing.cpp b/clang/test/SemaCXX/warn-thread-safety-parsing.cpp
index 70b3edf97e12f5..3d28a662814f81 100644
--- a/clang/test/SemaCXX/warn-thread-safety-parsing.cpp
+++ b/clang/test/SemaCXX/warn-thread-safety-parsing.cpp
@@ -588,11 +588,11 @@ UnlockableMu ab_var_arg_bad_5 ACQUIRED_BEFORE(mu_ab); // \
// takes zero or more arguments, all locks (vars/fields)
-void elf_function() EXCLUSIVE_LOCK_FUNCTION(); // expected-warning {{'exclusive_lock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+void elf_function() EXCLUSIVE_LOCK_FUNCTION(); // expected-warning {{'exclusive_lock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
void elf_function_args() EXCLUSIVE_LOCK_FUNCTION(mu1, mu2);
-int elf_testfn(int y) EXCLUSIVE_LOCK_FUNCTION(); // expected-warning {{'exclusive_lock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+int elf_testfn(int y) EXCLUSIVE_LOCK_FUNCTION(); // expected-warning {{'exclusive_lock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
int elf_testfn(int y) {
int x EXCLUSIVE_LOCK_FUNCTION() = y; // \
@@ -619,7 +619,7 @@ void elf_fun_params1(MutexLock& scope EXCLUSIVE_LOCK_FUNCTION(mu1));
void elf_fun_params2(int lvar EXCLUSIVE_LOCK_FUNCTION(mu1)); // \
// expected-warning{{'exclusive_lock_function' attribute applies to function parameters only if their type is a function pointer or a reference to a 'scoped_lockable'-annotated type}}
void elf_fun_params3(MutexLock& scope EXCLUSIVE_LOCK_FUNCTION()); // \
- // expected-warning{{'exclusive_lock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning{{'exclusive_lock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
// Check argument parsing.
@@ -682,11 +682,11 @@ template int elf_variadic_template<Mutex, UnlockableMu>(Mutex&, UnlockableMu&);
// takes zero or more arguments, all locks (vars/fields)
-void slf_function() SHARED_LOCK_FUNCTION(); // expected-warning {{'shared_lock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+void slf_function() SHARED_LOCK_FUNCTION(); // expected-warning {{'shared_lock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
void slf_function_args() SHARED_LOCK_FUNCTION(mu1, mu2);
-int slf_testfn(int y) SHARED_LOCK_FUNCTION(); // expected-warning {{'shared_lock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+int slf_testfn(int y) SHARED_LOCK_FUNCTION(); // expected-warning {{'shared_lock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
int slf_testfn(int y) {
int x SHARED_LOCK_FUNCTION() = y; // \
@@ -701,7 +701,7 @@ void slf_fun_params1(MutexLock& scope SHARED_LOCK_FUNCTION(mu1));
void slf_fun_params2(int lvar SHARED_LOCK_FUNCTION(mu1)); // \
// expected-warning {{'shared_lock_function' attribute applies to function parameters only if their type is a function pointer or a reference to a 'scoped_lockable'-annotated type}}
void slf_fun_params3(MutexLock& scope SHARED_LOCK_FUNCTION()); // \
- // expected-warning {{'shared_lock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning {{'shared_lock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
class SlfFoo {
private:
@@ -783,10 +783,10 @@ void etf_function() __attribute__((exclusive_trylock_function)); // \
void etf_function_args() EXCLUSIVE_TRYLOCK_FUNCTION(1, mu2);
void etf_function_arg() EXCLUSIVE_TRYLOCK_FUNCTION(1); // \
- // expected-warning {{'exclusive_trylock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning {{'exclusive_trylock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
int etf_testfn(int y) EXCLUSIVE_TRYLOCK_FUNCTION(1); // \
- // expected-warning {{'exclusive_trylock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning {{'exclusive_trylock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
int etf_testfn(int y) {
int x EXCLUSIVE_TRYLOCK_FUNCTION(1) = y; // \
@@ -824,7 +824,7 @@ int etf_function_6() EXCLUSIVE_TRYLOCK_FUNCTION(1, muRef);
int etf_function_7() EXCLUSIVE_TRYLOCK_FUNCTION(1, muDoubleWrapper.getWrapper()->getMu());
int etf_functetfn_8() EXCLUSIVE_TRYLOCK_FUNCTION(1, muPointer);
int etf_function_9() EXCLUSIVE_TRYLOCK_FUNCTION(true); // \
- // expected-warning {{'exclusive_trylock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning {{'exclusive_trylock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
// illegal attribute arguments
@@ -878,10 +878,10 @@ void stf_function() __attribute__((shared_trylock_function)); // \
void stf_function_args() SHARED_TRYLOCK_FUNCTION(1, mu2);
void stf_function_arg() SHARED_TRYLOCK_FUNCTION(1); // \
- // expected-warning {{'shared_trylock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning {{'shared_trylock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
int stf_testfn(int y) SHARED_TRYLOCK_FUNCTION(1); // \
- // expected-warning {{'shared_trylock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning {{'shared_trylock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
int stf_testfn(int y) {
int x SHARED_TRYLOCK_FUNCTION(1) = y; // \
@@ -920,7 +920,7 @@ int stf_function_6() SHARED_TRYLOCK_FUNCTION(1, muRef);
int stf_function_7() SHARED_TRYLOCK_FUNCTION(1, muDoubleWrapper.getWrapper()->getMu());
int stf_function_8() SHARED_TRYLOCK_FUNCTION(1, muPointer);
int stf_function_9() SHARED_TRYLOCK_FUNCTION(true); // \
- // expected-warning {{'shared_trylock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning {{'shared_trylock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
// illegal attribute arguments
@@ -968,13 +968,13 @@ template int stf_variadic_template<Mutex, UnlockableMu>(Mutex&, UnlockableMu&);
// takes zero or more arguments, all locks (vars/fields)
void uf_function() UNLOCK_FUNCTION(); // \
- // expected-warning {{'unlock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning {{'unlock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
void uf_function_args() UNLOCK_FUNCTION(mu1, mu2);
int uf_testfn(int y) UNLOCK_FUNCTION(); //\
- // expected-warning {{'unlock_function' attribute without capability arguments can only be applied to non-static methods of a class}}
+ // expected-warning {{'unlock_function' attribute without capability arguments can only be applied to non-static methods and function pointer members of a class}}
int uf_testfn(int y) {
int x UNLOCK_FUNCTION() = y; // \
@@ -1001,7 +1001,7 @@ void uf_fun_params1(MutexLock& scope UNLOCK_FUNCTION(mu1));
void uf_fun_params2(int lvar UNLOCK_FUNCTION(mu1)); // \
// expected-warning {{'unlock_function' attribute applies to function parameters only if their type is a function pointer or a reference to a 'scoped_lockable'-annotated type}}
void uf_fun_params3(MutexLock& scope UNLOCK_FUNCTION()); // \
- // expected-warning {{'unlock_function' attribute without capability arguments can only be applied to non-static methods of...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/242937
More information about the cfe-commits
mailing list