From 4ecff03131d39090e8a2aac7021a715605ce9e84 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Thu, 16 Nov 2017 01:06:36 +0000 Subject: [PATCH] Convert a use of createUniqueFile to TempFile::create. llvm-svn: 318361 --- llvm/tools/bugpoint/OptimizerDriver.cpp | 37 +++++++++++++------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/llvm/tools/bugpoint/OptimizerDriver.cpp b/llvm/tools/bugpoint/OptimizerDriver.cpp index c112ae00ed60..cc726486dfef 100644 --- a/llvm/tools/bugpoint/OptimizerDriver.cpp +++ b/llvm/tools/bugpoint/OptimizerDriver.cpp @@ -118,6 +118,11 @@ static cl::list OptArgs("opt-args", cl::Positional, cl::desc("..."), cl::ZeroOrMore, cl::PositionalEatsArgs); +struct DiscardTemp { + sys::fs::TempFile &File; + ~DiscardTemp() { consumeError(File.discard()); } +}; + /// runPasses - Run the specified passes on Program, outputting a bitcode file /// and writing the filename into OutputFile if successful. If the /// optimizations fail for some reason (optimizer crashes), return true, @@ -144,23 +149,22 @@ bool BugDriver::runPasses(Module *Program, OutputFilename = UniqueFilename.str(); // set up the input file name - SmallString<128> InputFilename; - int InputFD; - EC = sys::fs::createUniqueFile(OutputPrefix + "-input-%%%%%%%.bc", InputFD, - InputFilename); - if (EC) { + Expected Temp = + sys::fs::TempFile::create(OutputPrefix + "-input-%%%%%%%.bc"); + if (!Temp) { errs() << getToolName() - << ": Error making unique filename: " << EC.message() << "\n"; + << ": Error making unique filename: " << toString(Temp.takeError()) + << "\n"; return 1; } + DiscardTemp Discard{*Temp}; + raw_fd_ostream OS(Temp->FD, /*shouldClose*/ false); - ToolOutputFile InFile(InputFilename, InputFD); - - WriteBitcodeToFile(Program, InFile.os(), PreserveBitcodeUseListOrder); - InFile.os().close(); - if (InFile.os().has_error()) { - errs() << "Error writing bitcode file: " << InputFilename << "\n"; - InFile.os().clear_error(); + WriteBitcodeToFile(Program, OS, PreserveBitcodeUseListOrder); + OS.flush(); + if (OS.has_error()) { + errs() << "Error writing bitcode file: " << Temp->TmpName << "\n"; + OS.clear_error(); return 1; } @@ -189,9 +193,6 @@ bool BugDriver::runPasses(Module *Program, return 1; } - // Ok, everything that could go wrong before running opt is done. - InFile.keep(); - // setup the child process' arguments SmallVector Args; if (UseValgrind) { @@ -220,7 +221,7 @@ bool BugDriver::runPasses(Module *Program, E = pass_args.end(); I != E; ++I) Args.push_back(I->c_str()); - Args.push_back(InputFilename.c_str()); + Args.push_back(Temp->TmpName.c_str()); for (unsigned i = 0; i < NumExtraArgs; ++i) Args.push_back(*ExtraArgs); Args.push_back(nullptr); @@ -247,7 +248,7 @@ bool BugDriver::runPasses(Module *Program, sys::fs::remove(OutputFilename); // Remove the temporary input file as well - sys::fs::remove(InputFilename.c_str()); + consumeError(Temp->discard()); if (!Quiet) { if (result == 0)