[clang] Thread Safety Analysis: Check managed capabilities of returned scoped capability (PR #131831)

Aaron Puchert via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 19 09:14:35 PDT 2025


================
@@ -3566,6 +3565,38 @@ void releaseMemberCall() {
   ReleasableMutexLock lock(&obj.mu);
   releaseMember(obj, lock);
 }
+#ifdef __cpp_guaranteed_copy_elision
+// expected-note at +2{{mutex acquired here}}
+// expected-note at +1{{see attribute on function here}}
+RelockableScope returnUnmatchTest() EXCLUSIVE_LOCK_FUNCTION(mu){
+  // expected-note at +1{{mutex acquired here}}
+  return RelockableScope(&mu2); // expected-warning{{mutex managed by '<temporary>' is 'mu2' instead of 'mu'}}
+} // expected-warning{{mutex 'mu2' is still held at the end of function}}
+  // expected-warning at -1{{expecting mutex 'mu' to be held at the end of function}}
+
+// expected-note at +2{{mutex acquired here}}
+// expected-note at +1{{see attribute on function here}}
+RelockableScope returnMoreTest() EXCLUSIVE_LOCK_FUNCTION(mu, mu2){
+  return RelockableScope(&mu); // expected-warning{{mutex 'mu2' not managed by '<temporary>'}}
+} // expected-warning{{expecting mutex 'mu2' to be held at the end of function}}
+
+// expected-note at +1{{see attribute on function here}}
+DoubleMutexLock returnFewerTest() EXCLUSIVE_LOCK_FUNCTION(mu){
+  // expected-note at +1{{mutex acquired here}}
+  return DoubleMutexLock(&mu,&mu2); // expected-warning{{did not expect mutex 'mu2' to be managed by '<temporary>'}}
+} // expected-warning{{mutex 'mu2' is still held at the end of function}}
+
+// expected-note at +1{{see attribute on function here}}
+RelockableMutexLock lockTest() EXCLUSIVE_LOCK_FUNCTION(mu) {
+  mu.Lock();
+  return RelockableMutexLock(&mu2, DeferTraits{}); // expected-warning{{mutex managed by '<temporary>' is 'mu2' instead of 'mu'}}
+}
+
+// expected-note at +1{{mutex acquired here}}
+RelockableMutexLock lockTest2() EXCLUSIVE_LOCK_FUNCTION(mu) {
+  return RelockableMutexLock(&mu, DeferTraits{});
+} // expected-warning{{expecting mutex 'mu' to be held at the end of function}}
+#endif
 
 } // end namespace PassingScope
----------------
aaronpuchert wrote:

Perhaps these tests should be in the namespace `ReturnScopedLockable`?

https://github.com/llvm/llvm-project/pull/131831


More information about the cfe-commits mailing list