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. /// stream will use stdout instead.
/// \param Binary - The file should be opened in binary mode on /// \param Binary - The file should be opened in binary mode on
/// platforms that support this distinction. /// 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 /// 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. /// 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 /// occurs, information about the error is put into ErrorInfo, and the
/// stream should be immediately destroyed; the string will be empty /// stream should be immediately destroyed; the string will be empty
/// if no error occurred. /// 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) { std::string &ErrorInfo) : pos(0) {
ErrorInfo.clear(); ErrorInfo.clear();
@ -266,6 +266,8 @@ raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary,
if (Binary) if (Binary)
Flags |= O_BINARY; Flags |= O_BINARY;
#endif #endif
if (!Force)
Flags |= O_EXCL;
FD = open(Filename, Flags, 0664); FD = open(Filename, Flags, 0664);
if (FD < 0) { if (FD < 0) {
ErrorInfo = "Error opening output file '" + std::string(Filename) + "'"; ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";

View File

@ -20,7 +20,6 @@
#include "llvm/Support/FileUtilities.h" #include "llvm/Support/FileUtilities.h"
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <iostream>
using namespace llvm; using namespace llvm;
namespace { namespace {
@ -158,7 +157,7 @@ int LLI::ExecuteProgram(const std::string &Bitcode,
LLIArgs.push_back(Args[i].c_str()); LLIArgs.push_back(Args[i].c_str());
LLIArgs.push_back(0); LLIArgs.push_back(0);
std::cout << "<lli>" << std::flush; outs() << "<lli>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i) for (unsigned i=0, e = LLIArgs.size()-1; i != e; ++i)
errs() << " " << LLIArgs[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 (Bitcode.c_str()); // This is the input bitcode
LLCArgs.push_back (0); LLCArgs.push_back (0);
std::cout << "<llc>" << std::flush; outs() << "<llc>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
errs() << " " << LLCArgs[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(Args[i].c_str());
JITArgs.push_back(0); JITArgs.push_back(0);
std::cout << "<jit>" << std::flush; outs() << "<jit>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i) for (unsigned i=0, e = JITArgs.size()-1; i != e; ++i)
errs() << " " << JITArgs[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 (Bitcode.c_str()); // This is the input bitcode
LLCArgs.push_back (0); LLCArgs.push_back (0);
std::cout << "<cbe>" << std::flush; outs() << "<cbe>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i) for (unsigned i=0, e = LLCArgs.size()-1; i != e; ++i)
errs() << " " << LLCArgs[i]; errs() << " " << LLCArgs[i];
@ -621,7 +620,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
#endif #endif
GCCArgs.push_back(0); // NULL terminator GCCArgs.push_back(0); // NULL terminator
std::cout << "<gcc>" << std::flush; outs() << "<gcc>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i) for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
errs() << " " << GCCArgs[i]; errs() << " " << GCCArgs[i];
@ -665,7 +664,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
ProgramArgs.push_back(0); // NULL terminator ProgramArgs.push_back(0); // NULL terminator
// Now that we have a binary, run it! // Now that we have a binary, run it!
std::cout << "<program>" << std::flush; outs() << "<program>"; outs().flush();
DEBUG(errs() << "\nAbout to run:\t"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i) for (unsigned i=0, e = ProgramArgs.size()-1; i != e; ++i)
errs() << " " << ProgramArgs[i]; errs() << " " << ProgramArgs[i];
@ -680,7 +679,7 @@ int GCC::ExecuteProgram(const std::string &ProgramFile,
sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile), sys::Path(InputFile), sys::Path(OutputFile), sys::Path(OutputFile),
Timeout, MemoryLimit); Timeout, MemoryLimit);
} else { } else {
std::cout << "<run remotely>" << std::flush; outs() << "<run remotely>"; outs().flush();
int RemoteClientStatus = RunProgramWithTimeout(sys::Path(RemoteClientPath), int RemoteClientStatus = RunProgramWithTimeout(sys::Path(RemoteClientPath),
&ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile), &ProgramArgs[0], sys::Path(InputFile), sys::Path(OutputFile),
sys::Path(OutputFile), Timeout, MemoryLimit); 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"; DEBUG(errs() << "\nAbout to run:\t";
for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i) for (unsigned i=0, e = GCCArgs.size()-1; i != e; ++i)
errs() << " " << GCCArgs[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()); (*message)(LDPL_ERROR, "%s", ErrMsg.c_str());
return LDPS_ERR; 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); ErrMsg);
if (!ErrMsg.empty()) { if (!ErrMsg.empty()) {
delete objFile; delete objFile;

View File

@ -41,8 +41,6 @@
#include "llvm/Config/config.h" #include "llvm/Config/config.h"
#include "llvm/LinkAllVMCore.h" #include "llvm/LinkAllVMCore.h"
#include "llvm/Target/TargetSelect.h" #include "llvm/Target/TargetSelect.h"
#include <fstream>
#include <iostream>
#include <memory> #include <memory>
using namespace llvm; using namespace llvm;
@ -133,28 +131,22 @@ static formatted_raw_ostream *GetOutputStream(const char *ProgName) {
if (OutputFilename == "-") if (OutputFilename == "-")
return &fouts(); 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 // Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT // SIGINT
sys::RemoveFileOnSignal(sys::Path(OutputFilename)); sys::RemoveFileOnSignal(sys::Path(OutputFilename));
std::string error; std::string error;
raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
true, error); /*Binary=*/true, Force, error);
formatted_raw_ostream *Out =
new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
if (!error.empty()) { if (!error.empty()) {
errs() << error << '\n'; errs() << error << '\n';
delete Out; if (!Force)
errs() << "Use -f command line argument to force output\n";
delete FDOut;
return 0; return 0;
} }
formatted_raw_ostream *Out =
new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
return Out; return Out;
} }
@ -189,29 +181,24 @@ static formatted_raw_ostream *GetOutputStream(const char *ProgName) {
break; 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 // Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT // SIGINT
sys::RemoveFileOnSignal(sys::Path(OutputFilename)); sys::RemoveFileOnSignal(sys::Path(OutputFilename));
std::string error; std::string error;
raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Binary, error); Binary, Force, error);
formatted_raw_ostream *Out =
new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
if (!error.empty()) { if (!error.empty()) {
errs() << error << '\n'; errs() << error << '\n';
delete Out; if (!Force)
errs() << "Use -f command line argument to force output\n";
delete FDOut;
return 0; return 0;
} }
formatted_raw_ostream *Out =
new formatted_raw_ostream(*FDOut, formatted_raw_ostream::DELETE_STREAM);
return Out; return Out;
} }

View File

@ -28,8 +28,6 @@
#include "llvm/Support/SystemUtils.h" #include "llvm/Support/SystemUtils.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/System/Signals.h" #include "llvm/System/Signals.h"
#include <fstream>
#include <iostream>
#include <memory> #include <memory>
using namespace llvm; using namespace llvm;
@ -62,7 +60,7 @@ int main(int argc, char **argv) {
cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n"); cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
int exitCode = 0; int exitCode = 0;
std::ostream *Out = 0; raw_ostream *Out = 0;
try { try {
// Parse the file now... // Parse the file now...
SMDiagnostic Err; SMDiagnostic Err;
@ -86,23 +84,24 @@ int main(int argc, char **argv) {
if (OutputFilename != "") { // Specified an output filename? if (OutputFilename != "") { // Specified an output filename?
if (OutputFilename != "-") { // Not stdout? if (OutputFilename != "-") { // Not stdout?
if (!Force && std::ifstream(OutputFilename.c_str())) { std::string ErrorInfo;
// If force is not specified, make sure not to overwrite a file! Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
cerr << argv[0] << ": error opening '" << OutputFilename Force, ErrorInfo);
<< "': file exists!\n" if (!ErrorInfo.empty()) {
<< "Use -f command line argument to force output\n"; errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
delete Out;
return 1; return 1;
} }
Out = new std::ofstream(OutputFilename.c_str(), std::ios::out |
std::ios::trunc | std::ios::binary);
} else { // Specified stdout } else { // Specified stdout
// FIXME: cout is not binary! // FIXME: outs() is not binary!
Out = &std::cout; Out = &outs();
} }
} else { } else {
if (InputFilename == "-") { if (InputFilename == "-") {
OutputFilename = "-"; OutputFilename = "-";
Out = &std::cout; Out = &outs();
} else { } else {
std::string IFN = InputFilename; std::string IFN = InputFilename;
int Len = IFN.length(); int Len = IFN.length();
@ -114,27 +113,22 @@ int main(int argc, char **argv) {
} }
OutputFilename += ".bc"; OutputFilename += ".bc";
if (!Force && std::ifstream(OutputFilename.c_str())) { std::string ErrorInfo;
// If force is not specified, make sure not to overwrite a file! Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
cerr << argv[0] << ": error opening '" << OutputFilename Force, ErrorInfo);
<< "': file exists!\n" if (!ErrorInfo.empty()) {
<< "Use -f command line argument to force output\n"; errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
delete Out;
return 1; 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 // Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT // SIGINT
sys::RemoveFileOnSignal(sys::Path(OutputFilename)); sys::RemoveFileOnSignal(sys::Path(OutputFilename));
} }
} }
if (!Out->good()) {
cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
return 1;
}
if (!DisableOutput) if (!DisableOutput)
if (Force || !CheckBitcodeOutputToConsole(Out,true)) if (Force || !CheckBitcodeOutputToConsole(Out,true))
WriteBitcodeToFile(M.get(), *Out); WriteBitcodeToFile(M.get(), *Out);
@ -146,7 +140,7 @@ int main(int argc, char **argv) {
exitCode = 1; exitCode = 1;
} }
if (Out != &std::cout) delete Out; if (Out != &outs()) delete Out;
return exitCode; return exitCode;
} }

View File

@ -28,8 +28,6 @@
#include "llvm/Support/Streams.h" #include "llvm/Support/Streams.h"
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/System/Signals.h" #include "llvm/System/Signals.h"
#include <iostream>
#include <fstream>
#include <memory> #include <memory>
using namespace llvm; using namespace llvm;
@ -56,7 +54,7 @@ int main(int argc, char **argv) {
try { try {
cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n"); 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::string ErrorMessage;
std::auto_ptr<Module> M; 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. // Just use stdout. We won't actually print anything on it.
} else if (OutputFilename != "") { // Specified an output filename? } else if (OutputFilename != "") { // Specified an output filename?
if (OutputFilename != "-") { // Not stdout? if (OutputFilename != "-") { // Not stdout?
if (!Force && std::ifstream(OutputFilename.c_str())) { std::string ErrorInfo;
// If force is not specified, make sure not to overwrite a file! Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
cerr << argv[0] << ": error opening '" << OutputFilename Force, ErrorInfo);
<< "': file exists! Sending to standard output.\n"; if (!ErrorInfo.empty()) {
} else { errs() << ErrorInfo << '\n';
Out = new std::ofstream(OutputFilename.c_str()); if (!Force)
errs() << "Use -f command line argument to force output\n";
delete Out;
return 1;
} }
} }
} else { } else {
@ -101,38 +102,32 @@ int main(int argc, char **argv) {
OutputFilename = IFN+".ll"; OutputFilename = IFN+".ll";
} }
if (!Force && std::ifstream(OutputFilename.c_str())) { std::string ErrorInfo;
// If force is not specified, make sure not to overwrite a file! Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/false,
cerr << argv[0] << ": error opening '" << OutputFilename Force, ErrorInfo);
<< "': file exists! Sending to standard output.\n"; if (!ErrorInfo.empty()) {
} else { errs() << ErrorInfo << '\n';
Out = new std::ofstream(OutputFilename.c_str()); if (!Force)
errs() << "Use -f command line argument to force output\n";
delete Out;
return 1;
}
// Make sure that the Out file gets unlinked from the disk if we get a // Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT // SIGINT
sys::RemoveFileOnSignal(sys::Path(OutputFilename)); sys::RemoveFileOnSignal(sys::Path(OutputFilename));
} }
} }
}
if (!Out->good()) {
cerr << argv[0] << ": error opening " << OutputFilename
<< ": sending to stdout instead!\n";
Out = &std::cout;
}
// All that llvm-dis does is write the assembly to a file. // All that llvm-dis does is write the assembly to a file.
if (!DontPrint) { if (!DontPrint) {
PassManager Passes; PassManager Passes;
raw_os_ostream L(*Out); Passes.add(createPrintModulePass(Out));
Passes.add(createPrintModulePass(&L));
Passes.run(*M.get()); Passes.run(*M.get());
} }
if (Out != &std::cout) { if (Out != &outs())
((std::ofstream*)Out)->close();
delete Out; delete Out;
}
return 0; return 0;
} catch (const std::string& msg) { } catch (const std::string& msg) {
cerr << argv[0] << ": " << msg << "\n"; cerr << argv[0] << ": " << msg << "\n";

View File

@ -22,10 +22,9 @@
#include "llvm/Support/ManagedStatic.h" #include "llvm/Support/ManagedStatic.h"
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/PrettyStackTrace.h" #include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/System/Signals.h" #include "llvm/System/Signals.h"
#include <iostream>
#include <memory> #include <memory>
#include <fstream>
using namespace llvm; using namespace llvm;
// InputFilename - The filename to read from. // InputFilename - The filename to read from.
@ -111,28 +110,28 @@ int main(int argc, char **argv) {
Passes.add(createDeadTypeEliminationPass()); // Remove dead types... Passes.add(createDeadTypeEliminationPass()); // Remove dead types...
Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
std::ostream *Out = 0; raw_ostream *Out = 0;
if (OutputFilename != "-") { // Not stdout? if (OutputFilename != "-") { // Not stdout?
if (!Force && std::ifstream(OutputFilename.c_str())) { std::string ErrorInfo;
// If force is not specified, make sure not to overwrite a file! Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
cerr << argv[0] << ": error opening '" << OutputFilename Force, ErrorInfo);
<< "': file exists!\n" if (!ErrorInfo.empty()) {
<< "Use -f command line argument to force output\n"; errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
delete Out;
return 1; 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 } else { // Specified stdout
// FIXME: cout is not binary! // FIXME: errs() is not binary!
Out = &std::cout; Out = &errs();
} }
Passes.add(CreateBitcodeWriterPass(*Out)); Passes.add(createBitcodeWriterPass(*Out));
Passes.run(*M.get()); Passes.run(*M.get());
if (Out != &std::cout) if (Out != &errs())
delete Out; delete Out;
return 0; return 0;
} }

View File

@ -24,8 +24,6 @@
#include "llvm/Support/Streams.h" #include "llvm/Support/Streams.h"
#include "llvm/System/Signals.h" #include "llvm/System/Signals.h"
#include "llvm/System/Path.h" #include "llvm/System/Path.h"
#include <fstream>
#include <iostream>
#include <memory> #include <memory>
using namespace llvm; using namespace llvm;
@ -122,20 +120,16 @@ int main(int argc, char **argv) {
if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get(); if (DumpAsm) cerr << "Here's the assembly:\n" << *Composite.get();
// FIXME: cout is not binary! // 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 (OutputFilename != "-") {
if (!Force && std::ifstream(OutputFilename.c_str())) { std::string ErrorInfo;
// If force is not specified, make sure not to overwrite a file! Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
cerr << argv[0] << ": error opening '" << OutputFilename Force, ErrorInfo);
<< "': file exists!\n" if (!ErrorInfo.empty()) {
<< "Use -f command line argument to force output\n"; errs() << ErrorInfo << '\n';
return 1; if (!Force)
} errs() << "Use -f command line argument to force output\n";
std::ios::openmode io_mode = std::ios::out | std::ios::trunc | delete Out;
std::ios::binary;
Out = new std::ofstream(OutputFilename.c_str(), io_mode);
if (!Out->good()) {
cerr << argv[0] << ": error opening '" << OutputFilename << "'!\n";
return 1; return 1;
} }
@ -152,6 +146,6 @@ int main(int argc, char **argv) {
if (Verbose) cerr << "Writing bitcode...\n"; if (Verbose) cerr << "Writing bitcode...\n";
WriteBitcodeToFile(Composite.get(), *Out); WriteBitcodeToFile(Composite.get(), *Out);
if (Out != &std::cout) delete Out; if (Out != &outs()) delete Out;
return 0; return 0;
} }

View File

@ -186,7 +186,8 @@ const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg)
bool genResult = false; bool genResult = false;
{ {
raw_fd_ostream asmFD(raw_fd_ostream(uniqueAsmPath.c_str(), raw_fd_ostream asmFD(raw_fd_ostream(uniqueAsmPath.c_str(),
false, errMsg)); /*Binary=*/false, /*Force=*/true,
errMsg));
formatted_raw_ostream asmFile(asmFD); formatted_raw_ostream asmFile(asmFD);
if (!errMsg.empty()) if (!errMsg.empty())
return NULL; return NULL;

View File

@ -35,8 +35,6 @@
#include "llvm/Support/raw_ostream.h" #include "llvm/Support/raw_ostream.h"
#include "llvm/LinkAllPasses.h" #include "llvm/LinkAllPasses.h"
#include "llvm/LinkAllVMCore.h" #include "llvm/LinkAllVMCore.h"
#include <iostream>
#include <fstream>
#include <memory> #include <memory>
#include <algorithm> #include <algorithm>
using namespace llvm; using namespace llvm;
@ -342,21 +340,16 @@ int main(int argc, char **argv) {
// Figure out what stream we are supposed to write to... // Figure out what stream we are supposed to write to...
// FIXME: cout is not binary! // 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 (OutputFilename != "-") {
if (!Force && std::ifstream(OutputFilename.c_str())) { std::string ErrorInfo;
// If force is not specified, make sure not to overwrite a file! Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
cerr << argv[0] << ": error opening '" << OutputFilename Force, ErrorInfo);
<< "': file exists!\n" if (!ErrorInfo.empty()) {
<< "Use -f command line argument to force output\n"; errs() << ErrorInfo << '\n';
return 1; if (!Force)
} errs() << "Use -f command line argument to force output\n";
std::ios::openmode io_mode = std::ios::out | std::ios::trunc | delete Out;
std::ios::binary;
Out = new std::ofstream(OutputFilename.c_str(), io_mode);
if (!Out->good()) {
cerr << argv[0] << ": error opening " << OutputFilename << "!\n";
return 1; return 1;
} }
@ -479,13 +472,13 @@ int main(int argc, char **argv) {
// Write bitcode out to disk or cout as the last step... // Write bitcode out to disk or cout as the last step...
if (!NoOutput && !AnalyzeOnly) if (!NoOutput && !AnalyzeOnly)
Passes.add(CreateBitcodeWriterPass(*Out)); Passes.add(createBitcodeWriterPass(*Out));
// Now that we have all of the passes ready, run them. // Now that we have all of the passes ready, run them.
Passes.run(*M.get()); Passes.run(*M.get());
// Delete the ofstream. // Delete the ofstream.
if (Out != &std::cout) if (Out != &outs())
delete Out; delete Out;
return 0; return 0;

View File

@ -171,7 +171,8 @@ int main(int argc, char **argv) {
raw_ostream *Out = &outs(); raw_ostream *Out = &outs();
if (OutputFilename != "-") { if (OutputFilename != "-") {
std::string Error; 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()) { if (!Error.empty()) {
errs() << argv[0] << ": error opening " << OutputFilename errs() << argv[0] << ": error opening " << OutputFilename