[llvm] r271755 - [LockFileManager] Improve error output by using better error messages

Bruno Cardoso Lopes via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 3 17:34:01 PDT 2016


Author: bruno
Date: Fri Jun  3 19:34:00 2016
New Revision: 271755

URL: http://llvm.org/viewvc/llvm-project?rev=271755&view=rev
Log:
[LockFileManager] Improve error output by using better error messages

This is currently used by clang to lock access to modules; improve the
error message so that clang can use better output messages from locking
error issues.

rdar://problem/26529101

Differential Review: http://reviews.llvm.org/D20942

Modified:
    llvm/trunk/include/llvm/Support/LockFileManager.h
    llvm/trunk/lib/Support/LockFileManager.cpp

Modified: llvm/trunk/include/llvm/Support/LockFileManager.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/LockFileManager.h?rev=271755&r1=271754&r2=271755&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/LockFileManager.h (original)
+++ llvm/trunk/include/llvm/Support/LockFileManager.h Fri Jun  3 19:34:00 2016
@@ -57,6 +57,7 @@ private:
 
   Optional<std::pair<std::string, int> > Owner;
   Optional<std::error_code> Error;
+  std::string ErrorDiagMsg;
 
   LockFileManager(const LockFileManager &) = delete;
   LockFileManager &operator=(const LockFileManager &) = delete;
@@ -82,6 +83,15 @@ public:
   /// \brief Remove the lock file.  This may delete a different lock file than
   /// the one previously read if there is a race.
   std::error_code unsafeRemoveLockFile();
+
+  /// \brief Get error message, or "" if there is no error.
+  std::string getErrorMessage() const;
+
+  /// \brief Set error and error message
+  void setError(std::error_code &EC, StringRef ErrorMsg = "") {
+    Error = EC;
+    ErrorDiagMsg = ErrorMsg.str();
+  }
 };
 
 } // end namespace llvm

Modified: llvm/trunk/lib/Support/LockFileManager.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/LockFileManager.cpp?rev=271755&r1=271754&r2=271755&view=diff
==============================================================================
--- llvm/trunk/lib/Support/LockFileManager.cpp (original)
+++ llvm/trunk/lib/Support/LockFileManager.cpp Fri Jun  3 19:34:00 2016
@@ -144,7 +144,9 @@ LockFileManager::LockFileManager(StringR
 {
   this->FileName = FileName;
   if (std::error_code EC = sys::fs::make_absolute(this->FileName)) {
-    Error = EC;
+    std::string S("failed to obtain absolute path for ");
+    S.append(this->FileName.str());
+    setError(EC, S);
     return;
   }
   LockFileName = this->FileName;
@@ -161,7 +163,9 @@ LockFileManager::LockFileManager(StringR
   int UniqueLockFileID;
   if (std::error_code EC = sys::fs::createUniqueFile(
           UniqueLockFileName, UniqueLockFileID, UniqueLockFileName)) {
-    Error = EC;
+    std::string S("failed to create unique file ");
+    S.append(UniqueLockFileName.str());
+    setError(EC, S);
     return;
   }
 
@@ -169,7 +173,7 @@ LockFileManager::LockFileManager(StringR
   {
     SmallString<256> HostID;
     if (auto EC = getHostID(HostID)) {
-      Error = EC;
+      setError(EC, "failed to get host id");
       return;
     }
 
@@ -185,7 +189,10 @@ LockFileManager::LockFileManager(StringR
     if (Out.has_error()) {
       // We failed to write out PID, so make up an excuse, remove the
       // unique lock file, and fail.
-      Error = make_error_code(errc::no_space_on_device);
+      auto EC = make_error_code(errc::no_space_on_device);
+      std::string S("failed to write to ");
+      S.append(UniqueLockFileName.str());
+      setError(EC, S);
       sys::fs::remove(UniqueLockFileName);
       return;
     }
@@ -205,7 +212,10 @@ LockFileManager::LockFileManager(StringR
     }
 
     if (EC != errc::file_exists) {
-      Error = EC;
+      std::string S("failed to create link ");
+      raw_string_ostream OSS(S);
+      OSS << LockFileName.str() << " to " << UniqueLockFileName.str();
+      setError(EC, OSS.str());
       return;
     }
 
@@ -226,7 +236,9 @@ LockFileManager::LockFileManager(StringR
     // There is a lock file that nobody owns; try to clean it up and get
     // ownership.
     if ((EC = sys::fs::remove(LockFileName))) {
-      Error = EC;
+      std::string S("failed to remove lockfile ");
+      S.append(UniqueLockFileName.str());
+      setError(EC, S);
       return;
     }
   }
@@ -242,6 +254,19 @@ LockFileManager::LockFileState LockFileM
   return LFS_Owned;
 }
 
+std::string LockFileManager::getErrorMessage() const {
+  if (Error) {
+    std::string Str(ErrorDiagMsg);
+    std::string ErrCodeMsg = Error->message();
+    raw_string_ostream OSS(Str);
+    if (!ErrCodeMsg.empty())
+      OSS << ": " << Error->message();
+    OSS.flush();
+    return Str;
+  }
+  return "";
+}
+
 LockFileManager::~LockFileManager() {
   if (getState() != LFS_Owned)
     return;




More information about the llvm-commits mailing list