[clang] [llvm] [Support] Return `LockFileManager` errors right away (PR #130627)

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 11 13:27:59 PDT 2025


================
@@ -158,41 +160,40 @@ class RemoveUniqueLockFileOnSignal {
 } // end anonymous namespace
 
 LockFileManager::LockFileManager(StringRef FileName)
-{
-  this->FileName = FileName;
-  if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
-    std::string S("failed to obtain absolute path for ");
-    S.append(std::string(this->FileName));
-    setError(EC, S);
-    return;
-  }
-  LockFileName = this->FileName;
+    : FileName(FileName), Owner(OwnerUnknown{}) {}
+
+Expected<bool> LockFileManager::tryLock() {
+  assert(std::holds_alternative<OwnerUnknown>(Owner) &&
+         "lock has already been attempted");
+
+  SmallString<128> AbsoluteFileName(FileName);
+  if (std::error_code EC = sys::fs::make_absolute(AbsoluteFileName))
+    return createStringError("failed to obtain absolute path for " +
+                             AbsoluteFileName);
+  LockFileName = AbsoluteFileName;
   LockFileName += ".lock";
 
   // If the lock file already exists, don't bother to try to create our own
   // lock file; it won't work anyway. Just figure out who owns this lock file.
-  if ((Owner = readLockFile(LockFileName)))
-    return;
+  if (auto LockFileOwner = readLockFile(LockFileName)) {
+    Owner = std::move(*LockFileOwner);
+    return false;
+  }
 
   // Create a lock file that is unique to this instance.
   UniqueLockFileName = LockFileName;
   UniqueLockFileName += "-%%%%%%%%";
   int UniqueLockFileID;
   if (std::error_code EC = sys::fs::createUniqueFile(
-          UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
-    std::string S("failed to create unique file ");
-    S.append(std::string(UniqueLockFileName));
-    setError(EC, S);
-    return;
-  }
+          UniqueLockFileName, UniqueLockFileID, UniqueLockFileName))
+    return createStringError("failed to create unique file " +
+                             UniqueLockFileName);
----------------
jansvoboda11 wrote:

Sounds good. Last commits does the same with the other `createStringError()` call that did not take `std::error_code` previously (line 171).

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


More information about the cfe-commits mailing list