Convert tools to use tool_output_file, and introduce error
authorDan Gohman <gohman@apple.com>
Fri, 20 Aug 2010 16:59:15 +0000 (16:59 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 20 Aug 2010 16:59:15 +0000 (16:59 +0000)
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
tools/bugpoint/OptimizerDriver.cpp
tools/gold/gold-plugin.cpp
tools/llvm-ld/llvm-ld.cpp
tools/lto/LTOCodeGenerator.cpp
tools/opt/GraphPrinters.cpp

index 7d3040255ae144afe5023d0e7db6c3f590500927..e9cae15ef550696c4f238f2dde0985791a33d3d9 100644 (file)
@@ -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();
index ffd40997fc54e21a9428f054cd0577abafa2d45a..6d37761bcdeae47cd6165d7ee584fa0808ba40b1 100644 (file)
@@ -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<const char*, 8> Args;
index db0db3b5b2ec1bc81d4227945f72ccd3a421edb4..858af6f4324cfdcc2c0e269dcc10ec34e5cf74a6 100644 (file)
@@ -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);
 
index eeb784b9e028382d25165b0abb41b0d21f48e16f..e4b2ae5d673c0dcd1ff50c4bdbdf9357741b5b3f 100644 (file)
@@ -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
index b69bcc35a71b1948637f6d75895ead196f2891e5..67b17657872c757c354ed1d94fc767526252d224 100644 (file)
@@ -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();
index 6a9e96516db5c8adfdadbe8a61c05a88f69a0e8f..d689a4a1dae2e41eec13eb312b4eda8a252630ce 100644 (file)
@@ -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";
 }