[llvm] r318458 - Convert another use of createUniqueFile to TempFile::create.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 16 13:40:10 PST 2017


Author: rafael
Date: Thu Nov 16 13:40:10 2017
New Revision: 318458

URL: http://llvm.org/viewvc/llvm-project?rev=318458&view=rev
Log:
Convert another use of createUniqueFile to TempFile::create.

This one requires a new small feature in TempFile: the ability to keep
the temporary file with the temporary name.

Modified:
    llvm/trunk/include/llvm/Support/FileSystem.h
    llvm/trunk/lib/Support/Path.cpp
    llvm/trunk/tools/bugpoint/BugDriver.cpp
    llvm/trunk/tools/bugpoint/BugDriver.h
    llvm/trunk/tools/bugpoint/ExecutionDriver.cpp
    llvm/trunk/tools/bugpoint/OptimizerDriver.cpp

Modified: llvm/trunk/include/llvm/Support/FileSystem.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/FileSystem.h?rev=318458&r1=318457&r2=318458&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/FileSystem.h (original)
+++ llvm/trunk/include/llvm/Support/FileSystem.h Thu Nov 16 13:40:10 2017
@@ -723,6 +723,9 @@ public:
   // Keep this with the given name.
   Error keep(const Twine &Name);
 
+  // Keep this with the temporary name.
+  Error keep();
+
   // Delete the file.
   Error discard();
 

Modified: llvm/trunk/lib/Support/Path.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/Path.cpp?rev=318458&r1=318457&r2=318458&view=diff
==============================================================================
--- llvm/trunk/lib/Support/Path.cpp (original)
+++ llvm/trunk/lib/Support/Path.cpp Thu Nov 16 13:40:10 2017
@@ -805,6 +805,22 @@ Error TempFile::keep(const Twine &Name)
   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;

Modified: llvm/trunk/tools/bugpoint/BugDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/BugDriver.cpp?rev=318458&r1=318457&r2=318458&view=diff
==============================================================================
--- llvm/trunk/tools/bugpoint/BugDriver.cpp (original)
+++ llvm/trunk/tools/bugpoint/BugDriver.cpp Thu Nov 16 13:40:10 2017
@@ -33,6 +33,11 @@ Triple TargetTriple;
 }
 
 DiscardTemp::~DiscardTemp() {
+  if (SaveTemps) {
+    if (Error E = File.keep())
+      errs() << "Failed to keep temp file " << toString(std::move(E)) << '\n';
+    return;
+  }
   if (Error E = File.discard())
     errs() << "Failed to delete temp file " << toString(std::move(E)) << '\n';
 }

Modified: llvm/trunk/tools/bugpoint/BugDriver.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/BugDriver.h?rev=318458&r1=318457&r2=318458&view=diff
==============================================================================
--- llvm/trunk/tools/bugpoint/BugDriver.h (original)
+++ llvm/trunk/tools/bugpoint/BugDriver.h Thu Nov 16 13:40:10 2017
@@ -271,6 +271,7 @@ public:
   bool writeProgramToFile(const std::string &Filename, const Module *M) const;
   bool writeProgramToFile(const std::string &Filename, int FD,
                           const Module *M) const;
+  bool writeProgramToFile(int FD, const Module *M) const;
 
 private:
   /// initializeExecutionEnvironment - This method is used to set up the

Modified: llvm/trunk/tools/bugpoint/ExecutionDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/ExecutionDriver.cpp?rev=318458&r1=318457&r2=318458&view=diff
==============================================================================
--- llvm/trunk/tools/bugpoint/ExecutionDriver.cpp (original)
+++ llvm/trunk/tools/bugpoint/ExecutionDriver.cpp Thu Nov 16 13:40:10 2017
@@ -271,26 +271,23 @@ Error BugDriver::initializeExecutionEnvi
 ///
 Error BugDriver::compileProgram(Module *M) const {
   // Emit the program to a bitcode file...
-  SmallString<128> BitcodeFile;
-  int BitcodeFD;
-  std::error_code EC = sys::fs::createUniqueFile(
-      OutputPrefix + "-test-program-%%%%%%%.bc", BitcodeFD, BitcodeFile);
-  if (EC) {
-    errs() << ToolName << ": Error making unique filename: " << EC.message()
+  auto Temp =
+      sys::fs::TempFile::create(OutputPrefix + "-test-program-%%%%%%%.bc");
+  if (!Temp) {
+    errs() << ToolName
+           << ": Error making unique filename: " << toString(Temp.takeError())
            << "\n";
     exit(1);
   }
-  if (writeProgramToFile(BitcodeFile.str(), BitcodeFD, M)) {
-    errs() << ToolName << ": Error emitting bitcode to file '" << BitcodeFile
+  DiscardTemp Discard{*Temp};
+  if (writeProgramToFile(Temp->FD, M)) {
+    errs() << ToolName << ": Error emitting bitcode to file '" << Temp->TmpName
            << "'!\n";
     exit(1);
   }
 
-  // Remove the temporary bitcode file when we are done.
-  FileRemover BitcodeFileRemover(BitcodeFile.str(), !SaveTemps);
-
   // Actually compile the program!
-  return Interpreter->compileProgram(BitcodeFile.str(), Timeout, MemoryLimit);
+  return Interpreter->compileProgram(Temp->TmpName, Timeout, MemoryLimit);
 }
 
 /// executeProgram - This method runs "Program", capturing the output of the

Modified: llvm/trunk/tools/bugpoint/OptimizerDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/bugpoint/OptimizerDriver.cpp?rev=318458&r1=318457&r2=318458&view=diff
==============================================================================
--- llvm/trunk/tools/bugpoint/OptimizerDriver.cpp (original)
+++ llvm/trunk/tools/bugpoint/OptimizerDriver.cpp Thu Nov 16 13:40:10 2017
@@ -74,6 +74,16 @@ bool BugDriver::writeProgramToFile(const
   return writeProgramToFileAux(Out, M);
 }
 
+bool BugDriver::writeProgramToFile(int FD, const Module *M) const {
+  raw_fd_ostream OS(FD, /*shouldClose*/ false);
+  WriteBitcodeToFile(M, OS, PreserveBitcodeUseListOrder);
+  OS.flush();
+  if (!OS.has_error())
+    return false;
+  OS.clear_error();
+  return true;
+}
+
 bool BugDriver::writeProgramToFile(const std::string &Filename,
                                    const Module *M) const {
   std::error_code EC;




More information about the llvm-commits mailing list