[clang] Thread Safety Analysis: Support passing scoped locks between functions with appropriate annotations (PR #110523)

Aaron Puchert via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 29 15:04:20 PST 2024


================
@@ -336,6 +336,40 @@ Improvements to Clang's diagnostics
   local variables passed to function calls using the ``[[clang::musttail]]``
   attribute.
 
+- The Thread Safety Analysis (#GH110523) now supports passing scoped capabilities into functions:
+  an attribute on the scoped capability parameter indicates both the expected associated capabilities and,
+  like in the case of attributes on the function declaration itself, their state before and after the call.
+
+  .. code-block:: c++
+
+    #include "mutex.h"
+
+    class MutexUnlocker {
+      Mutex* mu;
+
+    public:
+      MutexUnlocker(Mutex* m) RELEASE(m) : mu(m)  { mu->Unlock(); }
+      ~MutexUnlocker() ACQUIRE(mu) { mu->Lock(); }
+    };
+
+    Mutex mu1, mu2;
+    int a GUARDED_BY(mu1);
+
+    void require(MutexLocker& scope REQUIRES(mu1)) {
+      scope.Unlock();
+      a = 0; // Warning!  Requires mu1.
+      scope.Lock();
+    }
+
+    void testParameter() {
+      MutexLocker scope(&mu1);
+      MutexLocker scope2(&mu2);
----------------
aaronpuchert wrote:

To condense it a bit:
```suggestion
      MutexLocker scope(&mu1), scope2(&mu2);
```

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


More information about the cfe-commits mailing list