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
This commit is contained in:
Dan Gohman 2010-08-20 16:59:15 +00:00
parent e7b67d0e94
commit f29140106f
6 changed files with 68 additions and 24 deletions

View 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();

View 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;

View 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);

View 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

View 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();

View 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";
}