From f29140106f74d15ba357aa0a7f109adc939c3104 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Fri, 20 Aug 2010 16:59:15 +0000 Subject: [PATCH] Convert tools to use tool_output_file, and introduce error checking to places which previously lacked it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@111651 91177308-0d34-0410-b5e6-96231b3b80d8 --- tools/bugpoint/ExtractFunction.cpp | 10 +++++++++- tools/bugpoint/OptimizerDriver.cpp | 28 ++++++++++++++++++++-------- tools/gold/gold-plugin.cpp | 11 +++++++++-- tools/llvm-ld/llvm-ld.cpp | 12 ++++++++---- tools/lto/LTOCodeGenerator.cpp | 15 +++++++++++---- tools/opt/GraphPrinters.cpp | 16 +++++++++++----- 6 files changed, 68 insertions(+), 24 deletions(-) diff --git a/tools/bugpoint/ExtractFunction.cpp b/tools/bugpoint/ExtractFunction.cpp index 7d3040255ae..e9cae15ef55 100644 --- a/tools/bugpoint/ExtractFunction.cpp +++ b/tools/bugpoint/ExtractFunction.cpp @@ -325,7 +325,7 @@ Module *BugDriver::ExtractMappedBlocksFromModule(const sys::RemoveFileOnSignal(uniqueFilename); std::string ErrorInfo; - raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo); + tool_output_file BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo); if (!ErrorInfo.empty()) { outs() << "*** Basic Block extraction failed!\n"; errs() << "Error writing list of blocks to not extract: " << ErrorInfo @@ -343,6 +343,14 @@ Module *BugDriver::ExtractMappedBlocksFromModule(const << BB->getName() << "\n"; } BlocksToNotExtractFile.close(); + if (BlocksToNotExtractFile.has_error()) { + errs() << "Error writing list of blocks to not extract: " << ErrorInfo + << "\n"; + EmitProgressBitcode(M, "basicblockextractfail", true); + BlocksToNotExtractFile.clear_error(); + return 0; + } + BlocksToNotExtractFile.keep(); std::string uniqueFN = "--extract-blocks-file=" + uniqueFilename.str(); const char *ExtraArg = uniqueFN.c_str(); diff --git a/tools/bugpoint/OptimizerDriver.cpp b/tools/bugpoint/OptimizerDriver.cpp index ffd40997fc5..6d37761bcde 100644 --- a/tools/bugpoint/OptimizerDriver.cpp +++ b/tools/bugpoint/OptimizerDriver.cpp @@ -54,12 +54,18 @@ namespace { bool BugDriver::writeProgramToFile(const std::string &Filename, const Module *M) const { std::string ErrInfo; - raw_fd_ostream Out(Filename.c_str(), ErrInfo, - raw_fd_ostream::F_Binary); - if (!ErrInfo.empty()) return true; - - WriteBitcodeToFile(M, Out); - return false; + tool_output_file Out(Filename.c_str(), ErrInfo, + raw_fd_ostream::F_Binary); + if (ErrInfo.empty()) { + WriteBitcodeToFile(M, Out); + Out.close(); + if (!Out.has_error()) { + Out.keep(); + return false; + } + } + Out.clear_error(); + return true; } @@ -125,8 +131,8 @@ bool BugDriver::runPasses(Module *Program, } std::string ErrInfo; - raw_fd_ostream InFile(inputFilename.c_str(), ErrInfo, - raw_fd_ostream::F_Binary); + tool_output_file InFile(inputFilename.c_str(), ErrInfo, + raw_fd_ostream::F_Binary); if (!ErrInfo.empty()) { @@ -135,6 +141,12 @@ bool BugDriver::runPasses(Module *Program, } WriteBitcodeToFile(Program, InFile); InFile.close(); + if (InFile.has_error()) { + errs() << "Error writing bitcode file: " << inputFilename.str() << "\n"; + InFile.clear_error(); + return 1; + } + InFile.keep(); // setup the child process' arguments SmallVector Args; diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index db0db3b5b2e..858af6f4324 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -453,8 +453,8 @@ static ld_plugin_status all_symbols_read_hook(void) { (*message)(LDPL_ERROR, "%s", ErrMsg.c_str()); return LDPS_ERR; } - raw_fd_ostream objFile(uniqueObjPath.c_str(), ErrMsg, - raw_fd_ostream::F_Binary); + tool_output_file objFile(uniqueObjPath.c_str(), ErrMsg, + raw_fd_ostream::F_Binary); if (!ErrMsg.empty()) { (*message)(LDPL_ERROR, "%s", ErrMsg.c_str()); return LDPS_ERR; @@ -462,6 +462,13 @@ static ld_plugin_status all_symbols_read_hook(void) { objFile.write(buffer, bufsize); objFile.close(); + if (objFile.has_error()) { + (*message)(LDPL_ERROR, "Error writing output file '%s'", + uniqueObjPath.c_str()); + objFile.clear_error(); + return LDPS_ERR; + } + objFile.keep(); lto_codegen_dispose(cg); diff --git a/tools/llvm-ld/llvm-ld.cpp b/tools/llvm-ld/llvm-ld.cpp index eeb784b9e02..e4b2ae5d673 100644 --- a/tools/llvm-ld/llvm-ld.cpp +++ b/tools/llvm-ld/llvm-ld.cpp @@ -236,13 +236,16 @@ void GenerateBitcode(Module* M, const std::string& FileName) { // Create the output file. std::string ErrorInfo; - raw_fd_ostream Out(FileName.c_str(), ErrorInfo, - raw_fd_ostream::F_Binary); - if (!ErrorInfo.empty()) + tool_output_file Out(FileName.c_str(), ErrorInfo, + raw_fd_ostream::F_Binary); + if (!ErrorInfo.empty()) { PrintAndExit(ErrorInfo, M); + return; + } // Write it out WriteBitcodeToFile(M, Out); + Out.keep(); } /// GenerateAssembly - generates a native assembly language source file from the @@ -425,7 +428,7 @@ static void EmitShellScript(char **argv, Module *M) { // Output the script to start the program... std::string ErrorInfo; - raw_fd_ostream Out2(OutputFilename.c_str(), ErrorInfo); + tool_output_file Out2(OutputFilename.c_str(), ErrorInfo); if (!ErrorInfo.empty()) PrintAndExit(ErrorInfo, M); @@ -466,6 +469,7 @@ static void EmitShellScript(char **argv, Module *M) { Out2 << " -load=" << FullLibraryPath.str() << " \\\n"; } Out2 << " " << BitcodeOutputFilename << " ${1+\"$@\"}\n"; + Out2.keep(); } // BuildLinkItems -- This function generates a LinkItemList for the LinkItems diff --git a/tools/lto/LTOCodeGenerator.cpp b/tools/lto/LTOCodeGenerator.cpp index b69bcc35a71..67b17657872 100644 --- a/tools/lto/LTOCodeGenerator.cpp +++ b/tools/lto/LTOCodeGenerator.cpp @@ -155,8 +155,8 @@ bool LTOCodeGenerator::writeMergedModules(const char *path, // create output file std::string ErrInfo; - raw_fd_ostream Out(path, ErrInfo, - raw_fd_ostream::F_Binary); + tool_output_file Out(path, ErrInfo, + raw_fd_ostream::F_Binary); if (!ErrInfo.empty()) { errMsg = "could not open bitcode file for writing: "; errMsg += path; @@ -174,6 +174,7 @@ bool LTOCodeGenerator::writeMergedModules(const char *path, return true; } + Out.keep(); return false; } @@ -189,11 +190,17 @@ const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) // generate assembly code bool genResult = false; { - raw_fd_ostream asmFD(uniqueAsmPath.c_str(), errMsg); - formatted_raw_ostream asmFile(asmFD); + tool_output_file asmFD(uniqueAsmPath.c_str(), errMsg); + formatted_tool_output_file asmFile(asmFD); if (!errMsg.empty()) return NULL; genResult = this->generateAssemblyCode(asmFile, errMsg); + asmFile.close(); + if (asmFile.has_error()) { + asmFile.clear_error(); + return NULL; + } + asmFile.keep(); } if ( genResult ) { uniqueAsmPath.eraseFromDisk(); diff --git a/tools/opt/GraphPrinters.cpp b/tools/opt/GraphPrinters.cpp index 6a9e96516db..d689a4a1dae 100644 --- a/tools/opt/GraphPrinters.cpp +++ b/tools/opt/GraphPrinters.cpp @@ -28,13 +28,19 @@ static void WriteGraphToFile(raw_ostream &O, const std::string &GraphName, std::string Filename = GraphName + ".dot"; O << "Writing '" << Filename << "'..."; std::string ErrInfo; - raw_fd_ostream F(Filename.c_str(), ErrInfo); + tool_output_file F(Filename.c_str(), ErrInfo); - if (ErrInfo.empty()) + if (ErrInfo.empty()) { WriteGraph(F, GT); - else - O << " error opening file for writing!"; - O << "\n"; + F.close(); + if (!F.has_error()) { + O << "\n"; + F.keep(); + return; + } + } + F.clear_error(); + O << " error opening file for writing!\n"; }