Change raw_fd_ostream to take flags as an optional bitmask

instead of as two bools.  Use this to add a F_Append flag
which has the obvious behavior.

Other unrelated changes conflated into this patch:

1. REmove EH stuff from llvm-dis and llvm-as, the try blocks
   are dead.
2. Simplify the filename inference code in llvm-as/llvm-dis,
   because raw_fd_ostream does the right thing with '-'.
3. Switch machine verifier to use raw_ostream instead of ostream
   (Which is the thing that needed append in the first place).

llvm-svn: 79807
This commit is contained in:
Chris Lattner 2009-08-23 02:51:22 +00:00
parent 622ddae92b
commit 9e6f1f160a
16 changed files with 226 additions and 243 deletions

View File

@ -334,19 +334,32 @@ class raw_fd_ostream : public raw_ostream {
virtual size_t preferred_buffer_size();
public:
/// raw_fd_ostream - Open the specified file for writing. If an
/// error 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.
enum {
/// F_Force - When opening a file, this flag makes raw_fd_ostream overwrite
/// a file if it already exists instead of emitting an error. This may not
/// be specified with F_Append.
F_Force = 1,
/// F_Append - When opening a file, if it already exists append to the
/// existing file instead of returning an error. This may not be specified
/// with F_Force.
F_Append = 2,
/// F_Binary - The file should be opened in binary mode on platforms that
/// support this distinction.
F_Binary = 4
};
/// raw_fd_ostream - Open the specified file for writing. If an error 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.
/// This allows optional flags to control how the file will be opened.
///
/// \param Filename - The file to open. If this is "-" then the
/// stream will use stdout instead.
/// \param Binary - The file should be opened in binary mode on
/// platforms that support this distinction.
/// \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(const char *Filename, std::string &ErrorInfo,
unsigned Flags = 0);
/// 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

@ -39,8 +39,6 @@
#include "llvm/Support/Debug.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <fstream>
using namespace llvm;
namespace {
@ -65,7 +63,7 @@ namespace {
const bool allowPhysDoubleDefs;
const char *const OutFileName;
std::ostream *OS;
raw_ostream *OS;
const MachineFunction *MF;
const TargetMachine *TM;
const TargetRegisterInfo *TRI;
@ -173,21 +171,24 @@ static RegisterPass<MachineVerifier>
MachineVer("machineverifier", "Verify generated machine code");
static const PassInfo *const MachineVerifyID = &MachineVer;
FunctionPass *
llvm::createMachineVerifierPass(bool allowPhysDoubleDefs)
{
FunctionPass *llvm::createMachineVerifierPass(bool allowPhysDoubleDefs) {
return new MachineVerifier(allowPhysDoubleDefs);
}
bool
MachineVerifier::runOnMachineFunction(MachineFunction &MF)
{
std::ofstream OutFile;
bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
raw_ostream *OutFile = 0;
if (OutFileName) {
OutFile.open(OutFileName, std::ios::out | std::ios::app);
OS = &OutFile;
std::string ErrorInfo;
OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
raw_fd_ostream::F_Append);
if (!ErrorInfo.empty()) {
errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
exit(1);
}
OS = OutFile;
} else {
OS = cerr.stream();
OS = &errs();
}
foundErrors = 0;
@ -212,14 +213,10 @@ MachineVerifier::runOnMachineFunction(MachineFunction &MF)
}
visitMachineFunctionAfter();
if (OutFileName)
OutFile.close();
else if (foundErrors) {
std::string msg;
raw_string_ostream Msg(msg);
Msg << "Found " << foundErrors << " machine code errors.";
llvm_report_error(Msg.str());
}
if (OutFile)
delete OutFile;
else if (foundErrors)
llvm_report_error("Found "+Twine(foundErrors)+" machine code errors.");
// Clean up.
regsLive.clear();
@ -234,7 +231,7 @@ MachineVerifier::runOnMachineFunction(MachineFunction &MF)
void MachineVerifier::report(const char *msg, const MachineFunction *MF) {
assert(MF);
*OS << "\n";
*OS << '\n';
if (!foundErrors++)
MF->print(*OS);
*OS << "*** Bad machine code: " << msg << " ***\n"

View File

@ -322,8 +322,12 @@ 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, bool Force,
std::string &ErrorInfo) : pos(0) {
raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
unsigned Flags) : pos(0) {
// Verify that we don't have both "append" and "force".
assert((!(Flags & F_Force) || !(Flags & F_Append)) &&
"Cannot specify both 'force' and 'append' file creation flags!");
ErrorInfo.clear();
// Handle "-" as stdout.
@ -331,20 +335,26 @@ raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
FD = STDOUT_FILENO;
// If user requested binary then put stdout into binary mode if
// possible.
if (Binary)
if (Flags & F_Binary)
sys::Program::ChangeStdoutToBinary();
ShouldClose = false;
return;
}
int Flags = O_WRONLY|O_CREAT|O_TRUNC;
int OpenFlags = O_WRONLY|O_CREAT;
#ifdef O_BINARY
if (Binary)
Flags |= O_BINARY;
OpenFlags |= O_BINARY;
#endif
if (!Force)
Flags |= O_EXCL;
FD = open(Filename, Flags, 0664);
if (Flags & F_Force)
OpenFlags |= O_TRUNC;
else if (Flags & F_Append)
OpenFlags |= O_APPEND;
else
OpenFlags |= O_EXCL;
FD = open(Filename, OpenFlags, 0664);
if (FD < 0) {
ErrorInfo = "Error opening output file '" + std::string(Filename) + "'";
ShouldClose = false;
@ -354,14 +364,14 @@ raw_fd_ostream::raw_fd_ostream(const char *Filename, bool Binary, bool Force,
}
raw_fd_ostream::~raw_fd_ostream() {
if (FD >= 0) {
flush();
if (ShouldClose)
if (::close(FD) != 0)
error_detected();
}
if (FD < 0) return;
flush();
if (ShouldClose)
if (::close(FD) != 0)
error_detected();
}
void raw_fd_ostream::write_impl(const char *Ptr, size_t Size) {
assert (FD >= 0 && "File already closed.");
pos += Size;

View File

@ -336,9 +336,8 @@ Module *BugDriver::ExtractMappedBlocksFromModule(const
sys::RemoveFileOnSignal(uniqueFilename);
std::string ErrorInfo;
raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(),
/*Binary=*/false, /*Force=*/true,
ErrorInfo);
raw_fd_ostream BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo,
raw_fd_ostream::F_Force);
if (!ErrorInfo.empty()) {
outs() << "*** Basic Block extraction failed!\n";
errs() << "Error writing list of blocks to not extract: " << ErrorInfo

View File

@ -362,10 +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(),
/*Binary=*/true,
/*Force=*/true,
ErrMsg);
raw_fd_ostream *objFile =
new raw_fd_ostream(uniqueObjPath.c_str(), ErrMsg,
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
if (!ErrMsg.empty()) {
delete objFile;
(*message)(LDPL_ERROR, "%s", ErrMsg.c_str());

View File

@ -137,8 +137,10 @@ static formatted_raw_ostream *GetOutputStream(const char *TargetName,
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
std::string error;
raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
/*Binary=*/true, Force, error);
raw_fd_ostream *FDOut =
new raw_fd_ostream(OutputFilename.c_str(), error,
(Force ? raw_fd_ostream::F_Force : 0)|
raw_fd_ostream::F_Binary);
if (!error.empty()) {
errs() << error << '\n';
if (!Force)
@ -187,8 +189,11 @@ static formatted_raw_ostream *GetOutputStream(const char *TargetName,
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
std::string error;
raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(),
Binary, Force, error);
unsigned OpenFlags = 0;
if (Force) OpenFlags |= raw_fd_ostream::F_Force;
if (Binary) OpenFlags |= raw_fd_ostream::F_Binary;
raw_fd_ostream *FDOut = new raw_fd_ostream(OutputFilename.c_str(), error,
OpenFlags);
if (!error.empty()) {
errs() << error << '\n';
if (!Force)

View File

@ -58,88 +58,64 @@ int main(int argc, char **argv) {
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
cl::ParseCommandLineOptions(argc, argv, "llvm .ll -> .bc assembler\n");
int exitCode = 0;
raw_ostream *Out = 0;
try {
// Parse the file now...
SMDiagnostic Err;
std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
if (M.get() == 0) {
Err.Print(argv[0], errs());
return 1;
}
if (!DisableVerify) {
std::string Err;
if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
errs() << argv[0]
<< ": assembly parsed, but does not verify as correct!\n";
errs() << Err;
return 1;
}
}
if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
if (OutputFilename != "") { // Specified an output filename?
if (OutputFilename != "-") { // Not stdout?
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;
}
} else { // Specified stdout
// FIXME: outs() is not binary!
Out = &outs();
}
} else {
if (InputFilename == "-") {
OutputFilename = "-";
Out = &outs();
} else {
std::string IFN = InputFilename;
int Len = IFN.length();
if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
// Source ends in .ll
OutputFilename = std::string(IFN.begin(), IFN.end()-3);
} else {
OutputFilename = IFN; // Append a .bc to it
}
OutputFilename += ".bc";
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;
}
// Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
}
}
if (!DisableOutput)
if (Force || !CheckBitcodeOutputToConsole(Out,true))
WriteBitcodeToFile(M.get(), *Out);
} catch (const std::string& msg) {
errs() << argv[0] << ": " << msg << "\n";
exitCode = 1;
} catch (...) {
errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
exitCode = 1;
// Parse the file now...
SMDiagnostic Err;
std::auto_ptr<Module> M(ParseAssemblyFile(InputFilename, Err, Context));
if (M.get() == 0) {
Err.Print(argv[0], errs());
return 1;
}
if (Out != &outs()) delete Out;
return exitCode;
if (!DisableVerify) {
std::string Err;
if (verifyModule(*M.get(), ReturnStatusAction, &Err)) {
errs() << argv[0]
<< ": assembly parsed, but does not verify as correct!\n";
errs() << Err;
return 1;
}
}
if (DumpAsm) errs() << "Here's the assembly:\n" << *M.get();
// Infer the output filename if needed.
if (OutputFilename.empty()) {
if (InputFilename == "-") {
OutputFilename = "-";
} else {
std::string IFN = InputFilename;
int Len = IFN.length();
if (IFN[Len-3] == '.' && IFN[Len-2] == 'l' && IFN[Len-1] == 'l') {
// Source ends in .ll
OutputFilename = std::string(IFN.begin(), IFN.end()-3);
} else {
OutputFilename = IFN; // Append a .bc to it
}
OutputFilename += ".bc";
}
}
std::string ErrorInfo;
std::auto_ptr<raw_ostream> Out
(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
(Force?raw_fd_ostream::F_Force : 0) |
raw_fd_ostream::F_Binary));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
return 1;
}
// Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT.
if (OutputFilename != "-")
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
if (!DisableOutput)
if (Force || !CheckBitcodeOutputToConsole(Out.get(), true))
WriteBitcodeToFile(M.get(), *Out);
return 0;
}

View File

@ -50,90 +50,70 @@ int main(int argc, char **argv) {
LLVMContext &Context = getGlobalContext();
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
try {
cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
cl::ParseCommandLineOptions(argc, argv, "llvm .bc -> .ll disassembler\n");
raw_ostream *Out = &outs(); // Default to printing to stdout.
std::string ErrorMessage;
std::auto_ptr<Module> M;
if (MemoryBuffer *Buffer
= MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
delete Buffer;
}
if (M.get() == 0) {
errs() << argv[0] << ": ";
if (ErrorMessage.size())
errs() << ErrorMessage << "\n";
else
errs() << "bitcode didn't read correctly.\n";
return 1;
}
if (DontPrint) {
// Just use stdout. We won't actually print anything on it.
} else if (OutputFilename != "") { // Specified an output filename?
if (OutputFilename != "-") { // Not stdout?
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 {
if (InputFilename == "-") {
OutputFilename = "-";
} else {
std::string IFN = InputFilename;
int Len = IFN.length();
if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c') {
// Source ends in .bc
OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
} else {
OutputFilename = IFN+".ll";
}
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;
}
// 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;
Passes.add(createPrintModulePass(Out));
Passes.run(*M.get());
}
if (Out != &outs())
delete Out;
return 0;
} catch (const std::string& msg) {
errs() << argv[0] << ": " << msg << "\n";
} catch (...) {
errs() << argv[0] << ": Unexpected unknown exception occurred.\n";
std::string ErrorMessage;
std::auto_ptr<Module> M;
if (MemoryBuffer *Buffer
= MemoryBuffer::getFileOrSTDIN(InputFilename, &ErrorMessage)) {
M.reset(ParseBitcodeFile(Buffer, Context, &ErrorMessage));
delete Buffer;
}
return 1;
if (M.get() == 0) {
errs() << argv[0] << ": ";
if (ErrorMessage.size())
errs() << ErrorMessage << "\n";
else
errs() << "bitcode didn't read correctly.\n";
return 1;
}
// Just use stdout. We won't actually print anything on it.
if (DontPrint)
OutputFilename = "-";
if (OutputFilename.empty()) { // Unspecified output, infer it.
if (InputFilename == "-") {
OutputFilename = "-";
} else {
const std::string &IFN = InputFilename;
int Len = IFN.length();
// If the source ends in .bc, strip it off.
if (IFN[Len-3] == '.' && IFN[Len-2] == 'b' && IFN[Len-1] == 'c')
OutputFilename = std::string(IFN.begin(), IFN.end()-3)+".ll";
else
OutputFilename = IFN+".ll";
}
}
std::string ErrorInfo;
std::auto_ptr<raw_fd_ostream>
Out(new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
(Force?raw_fd_ostream::F_Force : 0) |
raw_fd_ostream::F_Binary));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)
errs() << "Use -f command line argument to force output\n";
return 1;
}
// Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT.
if (OutputFilename != "-")
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
// All that llvm-dis does is write the assembly to a file.
if (!DontPrint) {
PassManager Passes;
Passes.add(createPrintModulePass(Out.get()));
Passes.run(*M.get());
}
return 0;
}

View File

@ -114,8 +114,9 @@ int main(int argc, char **argv) {
if (OutputFilename != "-") { // Not stdout?
std::string ErrorInfo;
Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
Force, ErrorInfo);
Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
raw_fd_ostream::F_Binary |
(Force ? raw_fd_ostream::F_Force : 0));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)

View File

@ -229,8 +229,8 @@ void GenerateBitcode(Module* M, const std::string& FileName) {
// Create the output file.
std::string ErrorInfo;
raw_fd_ostream Out(FileName.c_str(), /*Binary=*/true, /*Force=*/true,
ErrorInfo);
raw_fd_ostream Out(FileName.c_str(), ErrorInfo,
raw_fd_ostream::F_Force | raw_fd_ostream::F_Binary);
if (!ErrorInfo.empty())
PrintAndExit(ErrorInfo);
@ -427,8 +427,8 @@ static void EmitShellScript(char **argv) {
// Output the script to start the program...
std::string ErrorInfo;
raw_fd_ostream Out2(OutputFilename.c_str(), /*Binary=*/false, /*Force=*/true,
ErrorInfo);
raw_fd_ostream Out2(OutputFilename.c_str(), ErrorInfo,
llvm::raw_fd_ostream::F_Force);
if (!ErrorInfo.empty())
PrintAndExit(ErrorInfo);

View File

@ -122,8 +122,9 @@ int main(int argc, char **argv) {
raw_ostream *Out = &outs(); // Default to printing to stdout...
if (OutputFilename != "-") {
std::string ErrorInfo;
Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
Force, ErrorInfo);
Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
raw_fd_ostream::F_Binary |
(Force ? raw_fd_ostream::F_Force : 0));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)

View File

@ -174,16 +174,18 @@ static const Target *GetTarget(const char *ProgName) {
}
static formatted_raw_ostream *GetOutputStream() {
if (OutputFilename == "" || OutputFilename == "-")
return &fouts();
if (OutputFilename == "")
OutputFilename = "-";
// Make sure that the Out file gets unlinked from the disk if we get a
// SIGINT
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
// SIGINT.
if (OutputFilename != "-")
sys::RemoveFileOnSignal(sys::Path(OutputFilename));
std::string Err;
raw_fd_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(),
/*Binary=*/false, Force, Err);
raw_fd_ostream *Out = new raw_fd_ostream(OutputFilename.c_str(), Err,
raw_fd_ostream::F_Binary |
(Force ? raw_fd_ostream::F_Force : 0));
if (!Err.empty()) {
errs() << Err << '\n';
if (!Force)

View File

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

View File

@ -376,8 +376,9 @@ int main(int argc, char **argv) {
raw_ostream *Out = &outs(); // Default to printing to stdout...
if (OutputFilename != "-") {
std::string ErrorInfo;
Out = new raw_fd_ostream(OutputFilename.c_str(), /*Binary=*/true,
Force, ErrorInfo);
Out = new raw_fd_ostream(OutputFilename.c_str(), ErrorInfo,
raw_fd_ostream::F_Binary |
(Force ? raw_fd_ostream::F_Force : 0));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
if (!Force)

View File

@ -65,8 +65,8 @@ int main(int argc, char **argv) {
if (!Quiet)
outs() << argv[0] << ": Updating '" << OutputFilename
<< "', contents changed.\n";
raw_fd_ostream OutStream(OutputFilename.c_str(), /*Binary=*/true,
/*Force=*/true, ErrorStr);
raw_fd_ostream OutStream(OutputFilename.c_str(), ErrorStr,
raw_fd_ostream::F_Force|raw_fd_ostream::F_Binary);
if (!ErrorStr.empty()) {
errs() << argv[0] << ": Unable to write output '"
<< OutputFilename << "': " << ErrorStr << '\n';

View File

@ -171,8 +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(), /*Binary=*/false,
/*Force=*/true, Error);
Out = new raw_fd_ostream(OutputFilename.c_str(), Error,
raw_fd_ostream::F_Force);
if (!Error.empty()) {
errs() << argv[0] << ": error opening " << OutputFilename