[llvm] r319134 - Move code. NFC.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 27 17:34:20 PST 2017


Author: rafael
Date: Mon Nov 27 17:34:20 2017
New Revision: 319134

URL: http://llvm.org/viewvc/llvm-project?rev=319134&view=rev
Log:
Move code. NFC.

This moves the TempFile implementation so that it can use system
specific code.

Modified:
    llvm/trunk/lib/Support/Path.cpp

Modified: llvm/trunk/lib/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=319134&r1=319133&r2=319134&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Path.cpp (original)
+++ llvm/trunk/lib/Support/Path.cpp Mon Nov 27 17:34:20 2017
@@ -762,89 +762,6 @@ std::error_code createUniqueFile(const T
   return createUniqueEntity(Model, Dummy, ResultPath, false, 0, FS_Name);
 }
 
-TempFile::TempFile(StringRef Name, int FD) : TmpName(Name), FD(FD) {}
-TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); }
-TempFile &TempFile::operator=(TempFile &&Other) {
-  TmpName = std::move(Other.TmpName);
-  FD = Other.FD;
-  Other.Done = true;
-  return *this;
-}
-
-TempFile::~TempFile() { assert(Done); }
-
-Error TempFile::discard() {
-  Done = true;
-  // Always try to close and remove.
-  std::error_code RemoveEC;
-  if (!TmpName.empty()) {
-    RemoveEC = fs::remove(TmpName);
-    sys::DontRemoveFileOnSignal(TmpName);
-  }
-
-  if (!RemoveEC)
-    TmpName = "";
-
-  if (FD != -1 && close(FD) == -1) {
-    std::error_code EC = std::error_code(errno, std::generic_category());
-    return errorCodeToError(EC);
-  }
-  FD = -1;
-
-  return errorCodeToError(RemoveEC);
-}
-
-Error TempFile::keep(const Twine &Name) {
-  assert(!Done);
-  Done = true;
-  // Always try to close and rename.
-  std::error_code RenameEC = fs::rename(TmpName, Name);
-  sys::DontRemoveFileOnSignal(TmpName);
-
-  if (!RenameEC)
-    TmpName = "";
-
-  if (close(FD) == -1) {
-    std::error_code EC(errno, std::generic_category());
-    return errorCodeToError(EC);
-  }
-  FD = -1;
-
-  return errorCodeToError(RenameEC);
-}
-
-Error TempFile::keep() {
-  assert(!Done);
-  Done = true;
-
-  sys::DontRemoveFileOnSignal(TmpName);
-  TmpName = "";
-
-  if (close(FD) == -1) {
-    std::error_code EC(errno, std::generic_category());
-    return errorCodeToError(EC);
-  }
-  FD = -1;
-
-  return Error::success();
-}
-
-Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode) {
-  int FD;
-  SmallString<128> ResultPath;
-  if (std::error_code EC = createUniqueFile(Model, FD, ResultPath, Mode))
-    return errorCodeToError(EC);
-
-  // Make sure we delete the file when RemoveFileOnSignal fails.
-  TempFile Ret(ResultPath, FD);
-  if (sys::RemoveFileOnSignal(ResultPath)) {
-    consumeError(Ret.discard());
-    std::error_code EC(errc::operation_not_permitted);
-    return errorCodeToError(EC);
-  }
-  return std::move(Ret);
-}
-
 static std::error_code
 createTemporaryFile(const Twine &Model, int &ResultFD,
                     llvm::SmallVectorImpl<char> &ResultPath, FSEntity Type,
@@ -1137,6 +1054,91 @@ ErrorOr<perms> getPermissions(const Twin
 
 namespace llvm {
 namespace sys {
+namespace fs {
+TempFile::TempFile(StringRef Name, int FD) : TmpName(Name), FD(FD) {}
+TempFile::TempFile(TempFile &&Other) { *this = std::move(Other); }
+TempFile &TempFile::operator=(TempFile &&Other) {
+  TmpName = std::move(Other.TmpName);
+  FD = Other.FD;
+  Other.Done = true;
+  return *this;
+}
+
+TempFile::~TempFile() { assert(Done); }
+
+Error TempFile::discard() {
+  Done = true;
+  // Always try to close and remove.
+  std::error_code RemoveEC;
+  if (!TmpName.empty()) {
+    RemoveEC = fs::remove(TmpName);
+    sys::DontRemoveFileOnSignal(TmpName);
+  }
+
+  if (!RemoveEC)
+    TmpName = "";
+
+  if (FD != -1 && close(FD) == -1) {
+    std::error_code EC = std::error_code(errno, std::generic_category());
+    return errorCodeToError(EC);
+  }
+  FD = -1;
+
+  return errorCodeToError(RemoveEC);
+}
+
+Error TempFile::keep(const Twine &Name) {
+  assert(!Done);
+  Done = true;
+  // Always try to close and rename.
+  std::error_code RenameEC = fs::rename(TmpName, Name);
+  sys::DontRemoveFileOnSignal(TmpName);
+
+  if (!RenameEC)
+    TmpName = "";
+
+  if (close(FD) == -1) {
+    std::error_code EC(errno, std::generic_category());
+    return errorCodeToError(EC);
+  }
+  FD = -1;
+
+  return errorCodeToError(RenameEC);
+}
+
+Error TempFile::keep() {
+  assert(!Done);
+  Done = true;
+
+  sys::DontRemoveFileOnSignal(TmpName);
+  TmpName = "";
+
+  if (close(FD) == -1) {
+    std::error_code EC(errno, std::generic_category());
+    return errorCodeToError(EC);
+  }
+  FD = -1;
+
+  return Error::success();
+}
+
+Expected<TempFile> TempFile::create(const Twine &Model, unsigned Mode) {
+  int FD;
+  SmallString<128> ResultPath;
+  if (std::error_code EC = createUniqueFile(Model, FD, ResultPath, Mode))
+    return errorCodeToError(EC);
+
+  // Make sure we delete the file when RemoveFileOnSignal fails.
+  TempFile Ret(ResultPath, FD);
+  if (sys::RemoveFileOnSignal(ResultPath)) {
+    consumeError(Ret.discard());
+    std::error_code EC(errc::operation_not_permitted);
+    return errorCodeToError(EC);
+  }
+  return std::move(Ret);
+}
+}
+
 namespace path {
 
 bool user_cache_directory(SmallVectorImpl<char> &Result, const Twine &Path1,




More information about the llvm-commits mailing list