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
This commit is contained in:
Dan Gohman 2009-07-15 17:29:42 +00:00
parent 2286f8dc4c
commit a1bdcedc38
12 changed files with 116 additions and 146 deletions

View File

@ -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.

View File

@ -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) + "'";

View File

@ -20,7 +20,6 @@
#include "llvm/Support/FileUtilities.h"
#include <fstream>
#include <sstream>
#include <iostream>
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 << "<lli>" << std::flush;
outs() << "<lli>"; 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 << "<llc>" << std::flush;
outs() << "<llc>"; 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 << "<jit>" << std::flush;
outs() << "<jit>"; 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 << "<cbe>" << std::flush;
outs() << "<cbe>"; 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 << "<gcc>" << std::flush;
outs() << "<gcc>"; 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 << "<program>" << std::flush;
outs() << "<program>"; 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 << "<run remotely>" << std::flush;
outs() << "<run remotely>"; 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 << "<gcc>" << std::flush;
outs() << "<gcc>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
errs() << " " << GCCArgs[i];

View File

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

View File

@ -41,8 +41,6 @@
#include "llvm/Config/config.h"
#include "llvm/LinkAllVMCore.h"
#include "llvm/Target/TargetSelect.h"
#include <fstream>
#include <iostream>
#include <memory>
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;
}

View File

@ -28,8 +28,6 @@
#include "llvm/Support/SystemUtils.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Signals.h"
#include <fstream>
#include <iostream>
#include <memory>
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;
}

View File

@ -28,8 +28,6 @@
#include "llvm/Support/Streams.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Signals.h"
#include <iostream>
#include <fstream>
#include <memory>
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<Module> 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";

View File

@ -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 <iostream>
#include <memory>
#include <fstream>
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;
}

View File

@ -24,8 +24,6 @@
#include "llvm/Support/Streams.h"
#include "llvm/System/Signals.h"
#include "llvm/System/Path.h"
#include <fstream>
#include <iostream>
#include <memory>
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;
}

View File

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

View File

@ -35,8 +35,6 @@
#include "llvm/Support/raw_ostream.h"
#include "llvm/LinkAllPasses.h"
#include "llvm/LinkAllVMCore.h"
#include <iostream>
#include <fstream>
#include <memory>
#include <algorithm>
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;

View File

@ -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