[clang] Thread Safety Analysis: Use replaceLock instead of removeLock+addLock (PR #141500)

Marco Elver via cfe-commits cfe-commits at lists.llvm.org
Mon May 26 08:09:19 PDT 2025


https://github.com/melver created https://github.com/llvm/llvm-project/pull/141500

In ScopedLockableFactEntry::unlock(), we can avoid a second search, pop_back(), and push_back() if we use the already obtained iterator into the FactSet to replace the old FactEntry and take its position in the vector.

>From b77f2a1ebf98bde647ee535dbd1ab0c24dbdfdb3 Mon Sep 17 00:00:00 2001
From: Marco Elver <elver at google.com>
Date: Fri, 25 Apr 2025 18:51:16 +0200
Subject: [PATCH] Thread Safety Analysis: Use replaceLock instead of
 removeLock+addLock

In ScopedLockableFactEntry::unlock(), we can avoid a second search,
pop_back(), and push_back() if we use the already obtained iterator into
the FactSet to replace the old FactEntry and take its position in the
vector.
---
 clang/lib/Analysis/ThreadSafety.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Analysis/ThreadSafety.cpp b/clang/lib/Analysis/ThreadSafety.cpp
index 198e324414be9..9dffb8fcd2204 100644
--- a/clang/lib/Analysis/ThreadSafety.cpp
+++ b/clang/lib/Analysis/ThreadSafety.cpp
@@ -1071,9 +1071,9 @@ class ScopedLockableFactEntry : public FactEntry {
         return;
       }
 
-      FSet.removeLock(FactMan, Cp);
-      FSet.addLock(FactMan,
-                   std::make_unique<LockableFactEntry>(!Cp, LK_Exclusive, loc));
+      FSet.replaceLock(
+          FactMan, It,
+          std::make_unique<LockableFactEntry>(!Cp, LK_Exclusive, loc));
     } else if (Handler) {
       SourceLocation PrevLoc;
       if (const FactEntry *Neg = FSet.findLock(FactMan, !Cp))



More information about the cfe-commits mailing list