[clang] Thread Safety Analysis: Support reentrant capabilities (PR #137133)

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 24 05:37:10 PDT 2025


================
@@ -139,22 +141,41 @@ class FactEntry : public CapabilityExpr {
   bool declared() const { return Source == Declared; }
   bool managed() const { return Source == Managed; }
 
-  virtual void
-  handleRemovalFromIntersection(const FactSet &FSet, FactManager &FactMan,
-                                SourceLocation JoinLoc, LockErrorKind LEK,
-                                ThreadSafetyHandler &Handler) const = 0;
+  virtual void handleRemovalFromIntersection(FactSet &FSet,
+                                             FactManager &FactMan,
+                                             SourceLocation JoinLoc,
+                                             LockErrorKind LEK,
+                                             ThreadSafetyHandler &Handler) = 0;
   virtual void handleLock(FactSet &FSet, FactManager &FactMan,
                           const FactEntry &entry,
-                          ThreadSafetyHandler &Handler) const = 0;
+                          ThreadSafetyHandler &Handler) = 0;
   virtual void handleUnlock(FactSet &FSet, FactManager &FactMan,
                             const CapabilityExpr &Cp, SourceLocation UnlockLoc,
-                            bool FullyRemove,
-                            ThreadSafetyHandler &Handler) const = 0;
+                            bool FullyRemove, ThreadSafetyHandler &Handler) = 0;
 
   // Return true if LKind >= LK, where exclusive > shared
   bool isAtLeast(LockKind LK) const {
     return  (LKind == LK_Exclusive) || (LK == LK_Shared);
   }
+
+  // Return true if we can acquire a capability reentrant.
+  [[nodiscard]] bool tryReenter(LockKind ReenterKind) {
+    if (!reentrant())
+      return false;
+    if (kind() != ReenterKind)
+      return false;
+    if (++ReentrancyCount == 0)
+      llvm::report_fatal_error("Maximum reentrancy reached");
----------------
AaronBallman wrote:

This should use a real diagnostic rather than reporting a fatal error, though I expect no user will ever run into the diagnostic in practice.

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


More information about the cfe-commits mailing list