PR43278: llvm-reduce: Use temporary file names (and ToolOutputFile) rather than unique ones - to ensure they're cleaned up

This modifies the tool somewhat to only create files when about to run
the "interestingness" test, and delete them immediately after - this
means some more files will be created sometimes (when "double checking"
work - which should probably be fixed/avoided anyway).

This now creates temporary files, rather than only unique ones, and also
uses ToolOutputFile (without ever calling "keep") to ensure the files
are deleted as soon as the interestingness test is run.

llvm-svn: 371696
diff --git a/llvm/tools/llvm-reduce/llvm-reduce.cpp b/llvm/tools/llvm-reduce/llvm-reduce.cpp
index 6836e1d..83dcf98 100644
--- a/llvm/tools/llvm-reduce/llvm-reduce.cpp
+++ b/llvm/tools/llvm-reduce/llvm-reduce.cpp
@@ -81,14 +81,13 @@
       parseInputFile(InputFilename, Context);
 
   // Initialize test environment
-  TestRunner Tester(TestFilename, TestArguments, InputFilename);
+  TestRunner Tester(TestFilename, TestArguments);
   Tester.setProgram(std::move(OriginalProgram));
 
   // Try to reduce code
   runDeltaPasses(Tester);
-  StringRef ReducedFilename = sys::path::filename(Tester.getReducedFilepath());
 
-  if (ReducedFilename == sys::path::filename(InputFilename)) {
+  if (!Tester.getProgram()) {
     errs() << "\nCouldnt reduce input :/\n";
   } else {
     // Print reduced file to STDOUT
@@ -100,7 +99,13 @@
       else if (OutputFilename.empty())
         OutputFilename = "reduced.ll";
 
-      sys::fs::copy_file(Tester.getReducedFilepath(), OutputFilename);
+      std::error_code EC;
+      raw_fd_ostream Out(OutputFilename, EC);
+      if (EC) {
+        errs() << "Error opening output file: " << EC.message() << "!\n";
+        exit(1);
+      }
+      Tester.getProgram()->print(Out, /*AnnotationWriter=*/nullptr);
       errs() << "\nDone reducing! Reduced testcase: " << OutputFilename << "\n";
     }
   }