summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/CodeGen/MachineVerifier.cpp41
-rw-r--r--lib/Support/raw_ostream.cpp38
2 files changed, 43 insertions, 36 deletions
diff --git a/lib/CodeGen/MachineVerifier.cpp b/lib/CodeGen/MachineVerifier.cpp
index 238f90568f..ea2f8279d7 100644
--- a/lib/CodeGen/MachineVerifier.cpp
+++ b/lib/CodeGen/MachineVerifier.cpp
@@ -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"
diff --git a/lib/Support/raw_ostream.cpp b/lib/Support/raw_ostream.cpp
index 94507bd67b..2e151118d6 100644
--- a/lib/Support/raw_ostream.cpp
+++ b/lib/Support/raw_ostream.cpp
@@ -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;