Skip to content

Commit

Permalink
Modernize raw_fd_ostream's constructor a bit.
Browse files Browse the repository at this point in the history
Take a StringRef instead of a "const char *".
Take a "std::error_code &" instead of a "std::string &" for error.

A create static method would be even better, but this patch is already a bit too
big.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@216393 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Aug 25, 2014
1 parent a4c8f31 commit 8c96862
Show file tree
Hide file tree
Showing 36 changed files with 181 additions and 192 deletions.
5 changes: 2 additions & 3 deletions examples/BrainF/BrainFDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ int main(int argc, char **argv) {
OutputFilename = base+".bc";
}
if (OutputFilename != "-") {
std::string ErrInfo;
out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
sys::fs::F_None);
std::error_code EC;
out = new raw_fd_ostream(OutputFilename, EC, sys::fs::F_None);
}
}

Expand Down
12 changes: 6 additions & 6 deletions include/llvm/Analysis/DOTGraphTraitsPass.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ class DOTGraphTraitsPrinter : public FunctionPass {
bool runOnFunction(Function &F) override {
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
std::string Filename = Name + "." + F.getName().str() + ".dot";
std::string ErrorInfo;
std::error_code EC;

errs() << "Writing '" << Filename << "'...";

raw_fd_ostream File(Filename.c_str(), ErrorInfo, sys::fs::F_Text);
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
std::string GraphName = DOTGraphTraits<GraphT>::getGraphName(Graph);
std::string Title = GraphName + " for '" + F.getName().str() + "' function";

if (ErrorInfo.empty())
if (!EC)
WriteGraph(File, Graph, IsSimple, Title);
else
errs() << " error opening file for writing!";
Expand Down Expand Up @@ -129,14 +129,14 @@ class DOTGraphTraitsModulePrinter : public ModulePass {
bool runOnModule(Module &M) override {
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
std::string Filename = Name + ".dot";
std::string ErrorInfo;
std::error_code EC;

errs() << "Writing '" << Filename << "'...";

raw_fd_ostream File(Filename.c_str(), ErrorInfo, sys::fs::F_Text);
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
std::string Title = DOTGraphTraits<GraphT>::getGraphName(Graph);

if (ErrorInfo.empty())
if (!EC)
WriteGraph(File, Graph, IsSimple, Title);
else
errs() << " error opening file for writing!";
Expand Down
14 changes: 7 additions & 7 deletions include/llvm/Support/ToolOutputFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ class tool_output_file {
/// destructed after the raw_fd_ostream is destructed. It installs
/// cleanups in its constructor and uninstalls them in its destructor.
class CleanupInstaller {
/// Filename - The name of the file.
/// The name of the file.
std::string Filename;
public:
/// Keep - The flag which indicates whether we should not delete the file.
/// The flag which indicates whether we should not delete the file.
bool Keep;

explicit CleanupInstaller(const char *filename);
explicit CleanupInstaller(StringRef ilename);
~CleanupInstaller();
} Installer;

Expand All @@ -44,12 +44,12 @@ class tool_output_file {
raw_fd_ostream OS;

public:
/// tool_output_file - This constructor's arguments are passed to
/// to raw_fd_ostream's constructor.
tool_output_file(const char *filename, std::string &ErrorInfo,
/// This constructor's arguments are passed to to raw_fd_ostream's
/// constructor.
tool_output_file(StringRef Filename, std::error_code &EC,
sys::fs::OpenFlags Flags);

tool_output_file(const char *Filename, int FD);
tool_output_file(StringRef Filename, int FD);

/// os - Return the contained raw_fd_ostream.
raw_fd_ostream &os() { return OS; }
Expand Down
11 changes: 6 additions & 5 deletions include/llvm/Support/raw_ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/DataTypes.h"
#include <system_error>

namespace llvm {
class format_object_base;
Expand Down Expand Up @@ -341,17 +342,17 @@ class raw_fd_ostream : public raw_ostream {
void error_detected() { Error = true; }

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.
/// This allows optional flags to control how the file will be opened.
/// Open the specified file for writing. If an error occurs, information
/// about the error is put into EC, and the stream should be immediately
/// destroyed;
/// \p Flags allows optional flags to control how the file will be opened.
///
/// As a special case, if Filename is "-", then the stream will use
/// STDOUT_FILENO instead of opening a file. Note that it will still consider
/// itself to own the file descriptor. In particular, it will close the
/// file descriptor when it is done (this is necessary to detect
/// output errors).
raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
raw_fd_ostream(StringRef Filename, std::error_code &EC,
sys::fs::OpenFlags Flags);

/// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If
Expand Down
16 changes: 8 additions & 8 deletions lib/Analysis/CFGPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ namespace {
bool runOnFunction(Function &F) override {
std::string Filename = "cfg." + F.getName().str() + ".dot";
errs() << "Writing '" << Filename << "'...";

std::string ErrorInfo;
raw_fd_ostream File(Filename.c_str(), ErrorInfo, sys::fs::F_Text);

if (ErrorInfo.empty())
std::error_code EC;
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);

if (!EC)
WriteGraph(File, (const Function*)&F);
else
errs() << " error opening file for writing!";
Expand Down Expand Up @@ -114,10 +114,10 @@ namespace {
std::string Filename = "cfg." + F.getName().str() + ".dot";
errs() << "Writing '" << Filename << "'...";

std::string ErrorInfo;
raw_fd_ostream File(Filename.c_str(), ErrorInfo, sys::fs::F_Text);
if (ErrorInfo.empty())
std::error_code EC;
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);

if (!EC)
WriteGraph(File, (const Function*)&F, true);
else
errs() << " error opening file for writing!";
Expand Down
6 changes: 3 additions & 3 deletions lib/Bitcode/Writer/BitWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ using namespace llvm;
/*===-- Operations on modules ---------------------------------------------===*/

int LLVMWriteBitcodeToFile(LLVMModuleRef M, const char *Path) {
std::string ErrorInfo;
raw_fd_ostream OS(Path, ErrorInfo, sys::fs::F_None);
std::error_code EC;
raw_fd_ostream OS(Path, EC, sys::fs::F_None);

if (!ErrorInfo.empty())
if (EC)
return -1;

WriteBitcodeToFile(unwrap(M), OS);
Expand Down
9 changes: 5 additions & 4 deletions lib/CodeGen/MachineVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,12 @@ void MachineFunction::verify(Pass *p, const char *Banner) const {
bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
raw_ostream *OutFile = nullptr;
if (OutFileName) {
std::string ErrorInfo;
OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
std::error_code EC;
OutFile = new raw_fd_ostream(OutFileName, EC,
sys::fs::F_Append | sys::fs::F_Text);
if (!ErrorInfo.empty()) {
errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
if (EC) {
errs() << "Error opening '" << OutFileName << "': " << EC.message()
<< '\n';
exit(1);
}

Expand Down
4 changes: 2 additions & 2 deletions lib/CodeGen/RegAllocPBQP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,8 @@ bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
std::ostringstream rs;
rs << round;
std::string graphFileName(fqn + "." + rs.str() + ".pbqpgraph");
std::string tmp;
raw_fd_ostream os(graphFileName.c_str(), tmp, sys::fs::F_Text);
std::error_code EC;
raw_fd_ostream os(graphFileName, EC, sys::fs::F_Text);
DEBUG(dbgs() << "Dumping graph for round " << round << " to \""
<< graphFileName << "\"\n");
problem->getGraph().dump(os);
Expand Down
16 changes: 9 additions & 7 deletions lib/IR/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,22 @@ void LLVMDumpModule(LLVMModuleRef M) {

LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
char **ErrorMessage) {
std::string error;
raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
if (!error.empty()) {
*ErrorMessage = strdup(error.c_str());
std::error_code EC;
raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
if (EC) {
*ErrorMessage = strdup(EC.message().c_str());
return true;
}

unwrap(M)->print(dest, nullptr);

if (!error.empty()) {
*ErrorMessage = strdup(error.c_str());
dest.close();

if (dest.has_error()) {
*ErrorMessage = strdup("Error printing to file");
return true;
}
dest.flush();

return false;
}

Expand Down
10 changes: 5 additions & 5 deletions lib/IR/GCOV.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,11 @@ FileInfo::openCoveragePath(StringRef CoveragePath) {
if (Options.NoOutput)
return llvm::make_unique<raw_null_ostream>();

std::string ErrorInfo;
auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str().c_str(),
ErrorInfo, sys::fs::F_Text);
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << "\n";
std::error_code EC;
auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str(), EC,
sys::fs::F_Text);
if (EC) {
errs() << EC.message() << "\n";
return llvm::make_unique<raw_null_ostream>();
}
return std::move(OS);
Expand Down
6 changes: 3 additions & 3 deletions lib/LTO/LTOCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ bool LTOCodeGenerator::writeMergedModules(const char *path,
applyScopeRestrictions();

// create output file
std::string ErrInfo;
tool_output_file Out(path, ErrInfo, sys::fs::F_None);
if (!ErrInfo.empty()) {
std::error_code EC;
tool_output_file Out(path, EC, sys::fs::F_None);
if (EC) {
errMsg = "could not open bitcode file for writing: ";
errMsg += path;
return false;
Expand Down
8 changes: 4 additions & 4 deletions lib/MC/MCParser/DarwinAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,13 +638,13 @@ bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
// Open the secure log file if we haven't already.
raw_ostream *OS = getContext().getSecureLog();
if (!OS) {
std::string Err;
OS = new raw_fd_ostream(SecureLogFile, Err,
std::error_code EC;
OS = new raw_fd_ostream(SecureLogFile, EC,
sys::fs::F_Append | sys::fs::F_Text);
if (!Err.empty()) {
if (EC) {
delete OS;
return Error(IDLoc, Twine("can't open secure log file: ") +
SecureLogFile + " (" + Err + ")");
SecureLogFile + " (" + EC.message() + ")");
}
getContext().setSecureLog(OS);
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Support/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ raw_ostream *llvm::CreateInfoOutputFile() {
// each time -stats or -time-passes wants to print output to it. To
// compensate for this, the test-suite Makefiles have code to delete the
// info output file before running commands which write to it.
std::string Error;
raw_ostream *Result = new raw_fd_ostream(
OutputFilename.c_str(), Error, sys::fs::F_Append | sys::fs::F_Text);
if (Error.empty())
std::error_code EC;
raw_ostream *Result = new raw_fd_ostream(OutputFilename, EC,
sys::fs::F_Append | sys::fs::F_Text);
if (!EC)
return Result;

errs() << "Error opening info-output-file '"
Expand Down
15 changes: 7 additions & 8 deletions lib/Support/ToolOutputFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
#include "llvm/Support/Signals.h"
using namespace llvm;

tool_output_file::CleanupInstaller::CleanupInstaller(const char *filename)
: Filename(filename), Keep(false) {
tool_output_file::CleanupInstaller::CleanupInstaller(StringRef Filename)
: Filename(Filename), Keep(false) {
// Arrange for the file to be deleted if the process is killed.
if (Filename != "-")
sys::RemoveFileOnSignal(Filename);
Expand All @@ -34,14 +34,13 @@ tool_output_file::CleanupInstaller::~CleanupInstaller() {
sys::DontRemoveFileOnSignal(Filename);
}

tool_output_file::tool_output_file(const char *filename, std::string &ErrorInfo,
tool_output_file::tool_output_file(StringRef Filename, std::error_code &EC,
sys::fs::OpenFlags Flags)
: Installer(filename), OS(filename, ErrorInfo, Flags) {
: Installer(Filename), OS(Filename, EC, Flags) {
// If open fails, no cleanup is needed.
if (!ErrorInfo.empty())
if (EC)
Installer.Keep = true;
}

tool_output_file::tool_output_file(const char *Filename, int FD)
: Installer(Filename), OS(FD, true) {
}
tool_output_file::tool_output_file(StringRef Filename, int FD)
: Installer(Filename), OS(FD, true) {}
16 changes: 4 additions & 12 deletions lib/Support/raw_ostream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,20 +426,14 @@ void format_object_base::home() {
// raw_fd_ostream
//===----------------------------------------------------------------------===//

/// 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.
raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
sys::fs::OpenFlags Flags)
: Error(false), UseAtomicWrites(false), pos(0) {
assert(Filename && "Filename is null");
ErrorInfo.clear();

EC = std::error_code();
// Handle "-" as stdout. Note that when we do this, we consider ourself
// the owner of stdout. This means that we can do things like close the
// file descriptor when we're done and set the "binary" flag globally.
if (Filename[0] == '-' && Filename[1] == 0) {
if (Filename == "-") {
FD = STDOUT_FILENO;
// If user requested binary then put stdout into binary mode if
// possible.
Expand All @@ -450,11 +444,9 @@ raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
return;
}

std::error_code EC = sys::fs::openFileForWrite(Filename, FD, Flags);
EC = sys::fs::openFileForWrite(Filename, FD, Flags);

if (EC) {
ErrorInfo = "Error opening output file '" + std::string(Filename) + "': " +
EC.message();
ShouldClose = false;
return;
}
Expand Down
20 changes: 10 additions & 10 deletions lib/TableGen/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ static int createDependencyFile(const TGParser &Parser, const char *argv0) {
errs() << argv0 << ": the option -d must be used together with -o\n";
return 1;
}
std::string Error;
tool_output_file DepOut(DependFilename.c_str(), Error, sys::fs::F_Text);
if (!Error.empty()) {
errs() << argv0 << ": error opening " << DependFilename
<< ":" << Error << "\n";
std::error_code EC;
tool_output_file DepOut(DependFilename, EC, sys::fs::F_Text);
if (EC) {
errs() << argv0 << ": error opening " << DependFilename << ":"
<< EC.message() << "\n";
return 1;
}
DepOut.os() << OutputFilename << ":";
Expand Down Expand Up @@ -101,11 +101,11 @@ int TableGenMain(char *argv0, TableGenMainFn *MainFn) {
if (Parser.ParseFile())
return 1;

std::string Error;
tool_output_file Out(OutputFilename.c_str(), Error, sys::fs::F_Text);
if (!Error.empty()) {
errs() << argv0 << ": error opening " << OutputFilename
<< ":" << Error << "\n";
std::error_code EC;
tool_output_file Out(OutputFilename, EC, sys::fs::F_Text);
if (EC) {
errs() << argv0 << ": error opening " << OutputFilename << ":"
<< EC.message() << "\n";
return 1;
}
if (!DependFilename.empty()) {
Expand Down
8 changes: 4 additions & 4 deletions lib/Target/TargetMachineC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,10 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,

LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
std::string error;
raw_fd_ostream dest(Filename, error, sys::fs::F_None);
if (!error.empty()) {
*ErrorMessage = strdup(error.c_str());
std::error_code EC;
raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
if (EC) {
*ErrorMessage = strdup(EC.message().c_str());
return true;
}
formatted_raw_ostream destf(dest);
Expand Down
Loading

0 comments on commit 8c96862

Please sign in to comment.