From: Dan Gohman Date: Wed, 15 Jul 2009 17:29:42 +0000 (+0000) Subject: Add a Force option to raw_fd_ostream to specify whether opening X-Git-Tag: android-x86-6.0-r1~1003^2~18595 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=a1bdcedc3879510a874d24c450e07feb170d9cd6;p=android-x86%2Fexternal-llvm.git Add a Force option to raw_fd_ostream to specify whether opening an existing file is considered an error. Convert several tools to use raw_fd_ostream instead of std::ostream, and to use this new option instead of doing a manual check. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75801 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/raw_ostream.h b/include/llvm/Support/raw_ostream.h index 261a9225c43..2b7b62bce4f 100644 --- a/include/llvm/Support/raw_ostream.h +++ b/include/llvm/Support/raw_ostream.h @@ -251,7 +251,10 @@ public: /// stream will use stdout instead. /// \param Binary - The file should be opened in binary mode on /// platforms that support this distinction. - raw_fd_ostream(const char *Filename, bool Binary, std::string &ErrorInfo); + /// \param Force - Don't consider the case where the file already + /// exists to be an error. + raw_fd_ostream(const char *Filename, bool Binary, bool Force, + std::string &ErrorInfo); /// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If /// ShouldClose is true, this closes the file when the stream is destroyed. diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp index 0595fe47e12..b13d922b217 100644 --- a/lib/Support/raw_ostream.cpp +++ b/lib/Support/raw_ostream.cpp @@ -246,7 +246,7 @@ void format_object_base::home() { /// occurs, information about the error is put into ErrorInfo, and the /// stream should be immediately destroyed; the string will be empty /// if no error occurred. -raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, +raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force, std::string &ErrorInfo) : pos(0) { ErrorInfo.clear(); @@ -266,6 +266,8 @@ raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, if (Binary) Flags |= O_BINARY; #endif + if (!Force) + Flags |= O_EXCL; FD = open(Filename, Flags, 0664); if (FD < 0) { ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; diff --git a/tools/bugpoint/ToolRunner.cpp b/tools/bugpoint/ToolRunner.cpp index d610676d4de..a5e1e8b8a42 100644 --- a/tools/bugpoint/ToolRunner.cpp +++ b/tools/bugpoint/ToolRunner.cpp @@ -20,7 +20,6 @@ #include "llvm/Support/FileUtilities.h" #include #include -#include using namespace llvm; namespace { @@ -158,7 +157,7 @@ int LLI::ExecuteProgram(const std::string &Bitcode, LLIArgs.push_back(Args[i].c_str()); LLIArgs.push_back(0); - std::cout << "" << std::flush; + outs() << ""; outs().flush(); DEBUG(errs() << "\nAbout to run:\t"; for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i) errs() << " " << LLIArgs[i]; @@ -312,7 +311,7 @@ GCC::FileType LLC::OutputCode(const std::string &Bitcode, LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode LLCArgs.push_back (0); - std::cout << "" << std::flush; + outs() << ""; outs().flush(); DEBUG(errs() << "\nAbout to run:\t"; for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) errs() << " " << LLCArgs[i]; @@ -429,7 +428,7 @@ int JIT::ExecuteProgram(const std::string &Bitcode, JITArgs.push_back(Args[i].c_str()); JITArgs.push_back(0); - std::cout << "" << std::flush; + outs() << ""; outs().flush(); DEBUG(errs() << "\nAbout to run:\t"; for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i) errs() << " " << JITArgs[i]; @@ -478,7 +477,7 @@ GCC::FileType CBE::OutputCode(const std::string &Bitcode, LLCArgs.push_back (Bitcode.c_str()); // This is the input bitcode LLCArgs.push_back (0); - std::cout << "" << std::flush; + outs() << ""; outs().flush(); DEBUG(errs() << "\nAbout to run:\t"; for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) errs() << " " << LLCArgs[i]; @@ -621,7 +620,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile, #endif GCCArgs.push_back(0); // NULL terminator - std::cout << "" << std::flush; + outs() << ""; outs().flush(); DEBUG(errs() << "\nAbout to run:\t"; for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i) errs() << " " << GCCArgs[i]; @@ -665,7 +664,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile, ProgramArgs.push_back(0); // NULL terminator // Now that we have a binary, run it! - std::cout << "" << std::flush; + outs() << ""; outs().flush(); DEBUG(errs() << "\nAbout to run:\t"; for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i) errs() << " " << ProgramArgs[i]; @@ -680,7 +679,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile, sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), Timeout, MemoryLimit); } else { - std::cout << "" << std::flush; + outs() << ""; outs().flush(); int RemoteClientStatus = RunProgramWithTimeout(sys::Path(RemoteClientPath), &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), Timeout, MemoryLimit); @@ -756,7 +755,7 @@ int GCC::MakeSharedObject(const std::string &InputFile, FileType fileType, - std::cout << "" << std::flush; + outs() << ""; outs().flush(); DEBUG(errs() << "\nAbout to run:\t"; for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i) errs() << " " << GCCArgs[i]; diff --git a/tools/gold/gold-plugin.cpp b/tools/gold/gold-plugin.cpp index 146c53fbb71..9554b8adc31 100644 --- a/tools/gold/gold-plugin.cpp +++ b/tools/gold/gold-plugin.cpp @@ -362,7 +362,9 @@ ld_plugin_status all_symbols_read_hook(void) { (*message)(LDPL_ERROR, "%s", ErrMsg.c_str()); return LDPS_ERR; } - raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(), true, + raw_fd_ostream *objFile = new raw_fd_ostream(uniqueObjPath.c_str(), + /*Binary=*/true, + /*Force=*/true, ErrMsg); if (!ErrMsg.empty()) { delete objFile; diff --git a/tools/llc/llc.cpp b/tools/llc/llc.cpp index 9acfcd29000..c4bb41e1f06 100644 --- a/tools/llc/llc.cpp +++ b/tools/llc/llc.cpp @@ -41,8 +41,6 @@ #include "llvm/Config/config.h" #include "llvm/LinkAllVMCore.h" #include "llvm/Target/TargetSelect.h" -#include -#include #include using namespace llvm; @@ -133,28 +131,22 @@ static formatted_raw_ostream *GetOutputStream(const char *ProgName) { if (OutputFilename == "-") return &fouts(); - // Specified an output filename? - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - errs() << ProgName << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; - return 0; - } // Make sure that the Out file gets unlinked from the disk if we get a // SIGINT sys::RemoveFileOnSignal(sys::Path(OutputFilename)); std::string error; raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), - true, error); - formatted_raw_ostream *Out = - new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM); + /*Binary=*/true, Force, error); if (!error.empty()) { errs() << error << '\n'; - delete Out; + if (!Force) + errs() << "Use -f command line argument to force output\n"; + delete FDOut; return 0; } + formatted_raw_ostream *Out = + new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM); return Out; } @@ -189,29 +181,24 @@ static formatted_raw_ostream *GetOutputStream(const char *ProgName) { break; } - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - errs() << ProgName << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; - return 0; - } - // Make sure that the Out file gets unlinked from the disk if we get a // SIGINT sys::RemoveFileOnSignal(sys::Path(OutputFilename)); std::string error; raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), - Binary, error); - formatted_raw_ostream *Out = - new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM); + Binary, Force, error); if (!error.empty()) { errs() << error << '\n'; - delete Out; + if (!Force) + errs() << "Use -f command line argument to force output\n"; + delete FDOut; return 0; } + formatted_raw_ostream *Out = + new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM); + return Out; } diff --git a/tools/llvm-as/llvm-as.cpp b/tools/llvm-as/llvm-as.cpp index eccabd5d14a..1d572f15784 100644 --- a/tools/llvm-as/llvm-as.cpp +++ b/tools/llvm-as/llvm-as.cpp @@ -28,8 +28,6 @@ #include "llvm/Support/SystemUtils.h" #include "llvm/Support/raw_ostream.h" #include "llvm/System/Signals.h" -#include -#include #include using namespace llvm; @@ -62,7 +60,7 @@ int main(int argc, char **argv) { cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); int exitCode = 0; - std::ostream *Out = 0; + raw_ostream *Out = 0; try { // Parse the file now... SMDiagnostic Err; @@ -86,23 +84,24 @@ int main(int argc, char **argv) { if (OutputFilename != "") { // Specified an output filename? if (OutputFilename != "-") { // Not stdout? - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; + std::string ErrorInfo; + Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true, + Force, ErrorInfo); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + if (!Force) + errs() << "Use -f command line argument to force output\n"; + delete Out; return 1; } - Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | - std::ios::trunc | std::ios::binary); } else { // Specified stdout - // FIXME: cout is not binary! - Out = &std::cout; + // FIXME: outs() is not binary! + Out = &outs(); } } else { if (InputFilename == "-") { OutputFilename = "-"; - Out = &std::cout; + Out = &outs(); } else { std::string IFN = InputFilename; int Len = IFN.length(); @@ -114,27 +113,22 @@ int main(int argc, char **argv) { } OutputFilename += ".bc"; - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; + std::string ErrorInfo; + Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true, + Force, ErrorInfo); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + if (!Force) + errs() << "Use -f command line argument to force output\n"; + delete Out; return 1; } - - Out = new std::ofstream(OutputFilename.c_str(), std::ios::out | - std::ios::trunc | std::ios::binary); // Make sure that the Out file gets unlinked from the disk if we get a // SIGINT sys::RemoveFileOnSignal(sys::Path(OutputFilename)); } } - if (!Out->good()) { - cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; - return 1; - } - if (!DisableOutput) if (Force || !CheckBitcodeOutputToConsole(Out,true)) WriteBitcodeToFile(M.get(), *Out); @@ -146,7 +140,7 @@ int main(int argc, char **argv) { exitCode = 1; } - if (Out != &std::cout) delete Out; + if (Out != &outs()) delete Out; return exitCode; } diff --git a/tools/llvm-dis/llvm-dis.cpp b/tools/llvm-dis/llvm-dis.cpp index 901c8e9d3a9..8fa00d5b032 100644 --- a/tools/llvm-dis/llvm-dis.cpp +++ b/tools/llvm-dis/llvm-dis.cpp @@ -28,8 +28,6 @@ #include "llvm/Support/Streams.h" #include "llvm/Support/raw_ostream.h" #include "llvm/System/Signals.h" -#include -#include #include using namespace llvm; @@ -56,7 +54,7 @@ int main(int argc, char **argv) { try { cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n"); - std::ostream *Out = &std::cout; // Default to printing to stdout. + raw_ostream *Out = &outs(); // Default to printing to stdout. std::string ErrorMessage; std::auto_ptr M; @@ -80,12 +78,15 @@ int main(int argc, char **argv) { // Just use stdout. We won't actually print anything on it. } else if (OutputFilename != "") { // Specified an output filename? if (OutputFilename != "-") { // Not stdout? - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists! Sending to standard output.\n"; - } else { - Out = new std::ofstream(OutputFilename.c_str()); + std::string ErrorInfo; + Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false, + Force, ErrorInfo); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + if (!Force) + errs() << "Use -f command line argument to force output\n"; + delete Out; + return 1; } } } else { @@ -101,38 +102,32 @@ int main(int argc, char **argv) { OutputFilename = IFN+".ll"; } - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists! Sending to standard output.\n"; - } else { - Out = new std::ofstream(OutputFilename.c_str()); - - // Make sure that the Out file gets unlinked from the disk if we get a - // SIGINT - sys::RemoveFileOnSignal(sys::Path(OutputFilename)); + std::string ErrorInfo; + Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false, + Force, ErrorInfo); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + if (!Force) + errs() << "Use -f command line argument to force output\n"; + delete Out; + return 1; } - } - } - if (!Out->good()) { - cerr << argv[0] << ": error opening " << OutputFilename - << ": sending to stdout instead!\n"; - Out = &std::cout; + // Make sure that the Out file gets unlinked from the disk if we get a + // SIGINT + sys::RemoveFileOnSignal(sys::Path(OutputFilename)); + } } // All that llvm-dis does is write the assembly to a file. if (!DontPrint) { PassManager Passes; - raw_os_ostream L(*Out); - Passes.add(createPrintModulePass(&L)); + Passes.add(createPrintModulePass(Out)); Passes.run(*M.get()); } - if (Out != &std::cout) { - ((std::ofstream*)Out)->close(); + if (Out != &outs()) delete Out; - } return 0; } catch (const std::string& msg) { cerr << argv[0] << ": " << msg << "\n"; diff --git a/tools/llvm-extract/llvm-extract.cpp b/tools/llvm-extract/llvm-extract.cpp index af0cf0705bf..4809f9fae24 100644 --- a/tools/llvm-extract/llvm-extract.cpp +++ b/tools/llvm-extract/llvm-extract.cpp @@ -22,10 +22,9 @@ #include "llvm/Support/ManagedStatic.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/PrettyStackTrace.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/System/Signals.h" -#include #include -#include using namespace llvm; // InputFilename - The filename to read from. @@ -111,28 +110,28 @@ int main(int argc, char **argv) { Passes.add(createDeadTypeEliminationPass()); // Remove dead types... Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls - std::ostream *Out = 0; + raw_ostream *Out = 0; if (OutputFilename != "-") { // Not stdout? - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; + std::string ErrorInfo; + Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true, + Force, ErrorInfo); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + if (!Force) + errs() << "Use -f command line argument to force output\n"; + delete Out; return 1; } - std::ios::openmode io_mode = std::ios::out | std::ios::trunc | - std::ios::binary; - Out = new std::ofstream(OutputFilename.c_str(), io_mode); } else { // Specified stdout - // FIXME: cout is not binary! - Out = &std::cout; + // FIXME: errs() is not binary! + Out = &errs(); } - Passes.add(CreateBitcodeWriterPass(*Out)); + Passes.add(createBitcodeWriterPass(*Out)); Passes.run(*M.get()); - if (Out != &std::cout) + if (Out != &errs()) delete Out; return 0; } diff --git a/tools/llvm-link/llvm-link.cpp b/tools/llvm-link/llvm-link.cpp index f65e602f27f..e6349fae5bf 100644 --- a/tools/llvm-link/llvm-link.cpp +++ b/tools/llvm-link/llvm-link.cpp @@ -24,8 +24,6 @@ #include "llvm/Support/Streams.h" #include "llvm/System/Signals.h" #include "llvm/System/Path.h" -#include -#include #include using namespace llvm; @@ -122,20 +120,16 @@ int main(int argc, char **argv) { if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get(); // FIXME: cout is not binary! - std::ostream *Out = &std::cout; // Default to printing to stdout... + raw_ostream *Out = &outs(); // Default to printing to stdout... if (OutputFilename != "-") { - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; - return 1; - } - std::ios::openmode io_mode = std::ios::out | std::ios::trunc | - std::ios::binary; - Out = new std::ofstream(OutputFilename.c_str(), io_mode); - if (!Out->good()) { - cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n"; + std::string ErrorInfo; + Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true, + Force, ErrorInfo); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + if (!Force) + errs() << "Use -f command line argument to force output\n"; + delete Out; return 1; } @@ -152,6 +146,6 @@ int main(int argc, char **argv) { if (Verbose) cerr << "Writing bitcode...\n"; WriteBitcodeToFile(Composite.get(), *Out); - if (Out != &std::cout) delete Out; + if (Out != &outs()) delete Out; return 0; } diff --git a/tools/lto/LTOCodeGenerator.cpp b/tools/lto/LTOCodeGenerator.cpp index b4c4e7767b9..9596057733e 100644 --- a/tools/lto/LTOCodeGenerator.cpp +++ b/tools/lto/LTOCodeGenerator.cpp @@ -186,7 +186,8 @@ const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) bool genResult = false; { raw_fd_ostream asmFD(raw_fd_ostream(uniqueAsmPath.c_str(), - false, errMsg)); + /*Binary=*/false, /*Force=*/true, + errMsg)); formatted_raw_ostream asmFile(asmFD); if (!errMsg.empty()) return NULL; diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp index 68916194024..c92fad8f6f8 100644 --- a/tools/opt/opt.cpp +++ b/tools/opt/opt.cpp @@ -35,8 +35,6 @@ #include "llvm/Support/raw_ostream.h" #include "llvm/LinkAllPasses.h" #include "llvm/LinkAllVMCore.h" -#include -#include #include #include using namespace llvm; @@ -342,21 +340,16 @@ int main(int argc, char **argv) { // Figure out what stream we are supposed to write to... // FIXME: cout is not binary! - std::ostream *Out = &std::cout; // Default to printing to stdout... + raw_ostream *Out = &outs(); // Default to printing to stdout... if (OutputFilename != "-") { - if (!Force && std::ifstream(OutputFilename.c_str())) { - // If force is not specified, make sure not to overwrite a file! - cerr << argv[0] << ": error opening '" << OutputFilename - << "': file exists!\n" - << "Use -f command line argument to force output\n"; - return 1; - } - std::ios::openmode io_mode = std::ios::out | std::ios::trunc | - std::ios::binary; - Out = new std::ofstream(OutputFilename.c_str(), io_mode); - - if (!Out->good()) { - cerr << argv[0] << ": error opening " << OutputFilename << "!\n"; + std::string ErrorInfo; + Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true, + Force, ErrorInfo); + if (!ErrorInfo.empty()) { + errs() << ErrorInfo << '\n'; + if (!Force) + errs() << "Use -f command line argument to force output\n"; + delete Out; return 1; } @@ -479,13 +472,13 @@ int main(int argc, char **argv) { // Write bitcode out to disk or cout as the last step... if (!NoOutput && !AnalyzeOnly) - Passes.add(CreateBitcodeWriterPass(*Out)); + Passes.add(createBitcodeWriterPass(*Out)); // Now that we have all of the passes ready, run them. Passes.run(*M.get()); // Delete the ofstream. - if (Out != &std::cout) + if (Out != &outs()) delete Out; return 0; diff --git a/utils/TableGen/TableGen.cpp b/utils/TableGen/TableGen.cpp index 376bec1eb9d..315bb657a07 100644 --- a/utils/TableGen/TableGen.cpp +++ b/utils/TableGen/TableGen.cpp @@ -171,7 +171,8 @@ int main(int argc, char **argv) { raw_ostream *Out = &outs(); if (OutputFilename != "-") { std::string Error; - Out = new raw_fd_ostream(OutputFilename.c_str(), false, Error); + Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false, + /*Force=*/true, Error); if (!Error.empty()) { errs() << argv[0] << ": error opening " << OutputFilename