Use TempFile in lto caching.

This requires a small change to TempFile: allowing a discard after a
failed keep.

With this the cache now handles signals and reuses a fd instead of
reopening the file.

llvm-svn: 318322
diff --git a/llvm/lib/Support/Path.cpp b/llvm/lib/Support/Path.cpp
index 63ea911..5f0adb3 100644
--- a/llvm/lib/Support/Path.cpp
+++ b/llvm/lib/Support/Path.cpp
@@ -772,13 +772,14 @@
 TempFile::~TempFile() { assert(Done); }
 
 Error TempFile::discard() {
-  if (Done)
-    return Error::success();
   Done = true;
   // Always try to close and remove.
-  std::error_code RemoveEC = fs::remove(TmpName);
-  sys::DontRemoveFileOnSignal(TmpName);
-  if (close(FD) == -1) {
+  std::error_code RemoveEC;
+  if (!TmpName.empty()) {
+    RemoveEC = fs::remove(TmpName);
+    sys::DontRemoveFileOnSignal(TmpName);
+  }
+  if (FD != -1 && close(FD) == -1) {
     std::error_code EC = std::error_code(errno, std::generic_category());
     return errorCodeToError(EC);
   }
@@ -791,10 +792,16 @@
   // 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);
 }