[llvm-commits] [llvm] r99719 - /llvm/trunk/tools/llvm-ld/llvm-ld.cpp

Dan Gohman gohman at apple.com
Sat Mar 27 09:49:51 PDT 2010


Author: djg
Date: Sat Mar 27 11:49:51 2010
New Revision: 99719

URL: http://llvm.org/viewvc/llvm-project?rev=99719&view=rev
Log:
Make llvm-ld remove its output files in the event of an error.

Modified:
    llvm/trunk/tools/llvm-ld/llvm-ld.cpp

Modified: llvm/trunk/tools/llvm-ld/llvm-ld.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-ld/llvm-ld.cpp?rev=99719&r1=99718&r2=99719&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-ld/llvm-ld.cpp (original)
+++ llvm/trunk/tools/llvm-ld/llvm-ld.cpp Sat Mar 27 11:49:51 2010
@@ -30,6 +30,7 @@
 #include "llvm/Target/TargetData.h"
 #include "llvm/Target/TargetMachine.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/Support/FileUtilities.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PrettyStackTrace.h"
@@ -124,6 +125,10 @@
 /// everywhere.
 static std::string progname;
 
+/// FileRemover objects to clean up output files in the event of an error.
+static FileRemover OutputRemover;
+static FileRemover BitcodeOutputRemover;
+
 /// PrintAndExit - Prints a message to standard error and exits with error code
 ///
 /// Inputs:
@@ -236,10 +241,6 @@
   if (!ErrorInfo.empty())
     PrintAndExit(ErrorInfo, M);
 
-  // Ensure that the bitcode file gets removed from the disk if we get a
-  // terminating signal.
-  sys::RemoveFileOnSignal(sys::Path(FileName));
-
   // Write it out
   WriteBitcodeToFile(M, Out);
 }
@@ -582,8 +583,17 @@
     if (!LinkAsLibrary) BitcodeOutputFilename += ".bc";
   }
 
+  // Arrange for the bitcode output file to be deleted on any errors.
+  BitcodeOutputRemover = FileRemover(sys::Path(BitcodeOutputFilename));
+  sys::RemoveFileOnSignal(sys::Path(BitcodeOutputFilename));
+
+  // Generate the bitcode output.
   GenerateBitcode(Composite.get(), BitcodeOutputFilename);
 
+  // Arrange for the output file to be deleted on any errors.
+  OutputRemover = FileRemover(sys::Path(OutputFilename));
+  sys::RemoveFileOnSignal(sys::Path(OutputFilename));
+
   // If we are not linking a library, generate either a native executable
   // or a JIT shell script, depending upon what the user wants.
   if (!LinkAsLibrary) {
@@ -636,7 +646,6 @@
 
       // Mark the output files for removal if we get an interrupt.
       sys::RemoveFileOnSignal(AssemblyFile);
-      sys::RemoveFileOnSignal(sys::Path(OutputFilename));
 
       // Determine the locations of the llc and gcc programs.
       sys::Path llc = FindExecutable("llc", argv[0],
@@ -666,7 +675,6 @@
 
       // Mark the output files for removal if we get an interrupt.
       sys::RemoveFileOnSignal(CFile);
-      sys::RemoveFileOnSignal(sys::Path(OutputFilename));
 
       // Determine the locations of the llc and gcc programs.
       sys::Path llc = FindExecutable("llc", argv[0],
@@ -707,6 +715,10 @@
       PrintAndExit(ErrMsg, Composite.get());
   }
 
+  // Operations which may fail are now complete.
+  OutputRemover.releaseFile();
+  BitcodeOutputRemover.releaseFile();
+
   // Graceful exit
   return 0;
 }





More information about the llvm-commits mailing list