Modernize raw_fd_ostream's constructor a bit.

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
This commit is contained in:
Rafael Espindola
2014-08-25 18:16:47 +00:00
parent a4c8f31dd0
commit 8c96862847
36 changed files with 181 additions and 192 deletions

View File

@@ -66,15 +66,15 @@ static bool writeProgramToFileAux(tool_output_file &Out, const Module *M) {
bool BugDriver::writeProgramToFile(const std::string &Filename, int FD,
const Module *M) const {
tool_output_file Out(Filename.c_str(), FD);
tool_output_file Out(Filename, FD);
return writeProgramToFileAux(Out, M);
}
bool BugDriver::writeProgramToFile(const std::string &Filename,
const Module *M) const {
std::string ErrInfo;
tool_output_file Out(Filename.c_str(), ErrInfo, sys::fs::F_None);
if (ErrInfo.empty())
std::error_code EC;
tool_output_file Out(Filename, EC, sys::fs::F_None);
if (!EC)
return writeProgramToFileAux(Out, M);
return true;
}
@@ -149,7 +149,7 @@ bool BugDriver::runPasses(Module *Program,
return 1;
}
tool_output_file InFile(InputFilename.c_str(), InputFD);
tool_output_file InFile(InputFilename, InputFD);
WriteBitcodeToFile(Program, InFile.os());
InFile.os().close();

View File

@@ -775,9 +775,9 @@ static ld_plugin_status allSymbolsReadHook(raw_fd_ostream *ApiFile) {
else
path = output_name + ".bc";
{
std::string Error;
raw_fd_ostream OS(path.c_str(), Error, sys::fs::OpenFlags::F_None);
if (!Error.empty())
std::error_code EC;
raw_fd_ostream OS(path, EC, sys::fs::OpenFlags::F_None);
if (EC)
message(LDPL_FATAL, "Failed to write the output file.");
WriteBitcodeToFile(L.getModule(), OS);
}
@@ -799,11 +799,11 @@ static ld_plugin_status all_symbols_read_hook(void) {
if (!options::generate_api_file) {
Ret = allSymbolsReadHook(nullptr);
} else {
std::string Error;
raw_fd_ostream ApiFile("apifile.txt", Error, sys::fs::F_None);
if (!Error.empty())
std::error_code EC;
raw_fd_ostream ApiFile("apifile.txt", EC, sys::fs::F_None);
if (EC)
message(LDPL_FATAL, "Unable to open apifile.txt for writing: %s",
Error.c_str());
EC.message().c_str());
Ret = allSymbolsReadHook(&ApiFile);
}

View File

@@ -159,14 +159,13 @@ static tool_output_file *GetOutputStream(const char *TargetName,
}
// Open the file.
std::string error;
std::error_code EC;
sys::fs::OpenFlags OpenFlags = sys::fs::F_None;
if (!Binary)
OpenFlags |= sys::fs::F_Text;
tool_output_file *FDOut = new tool_output_file(OutputFilename.c_str(), error,
OpenFlags);
if (!error.empty()) {
errs() << error << '\n';
tool_output_file *FDOut = new tool_output_file(OutputFilename, EC, OpenFlags);
if (EC) {
errs() << EC.message() << '\n';
delete FDOut;
return nullptr;
}

View File

@@ -268,13 +268,13 @@ public:
std::string CacheName;
if (!getCacheFilename(ModuleID, CacheName))
return;
std::string errStr;
if (!CacheDir.empty()) { // Create user-defined cache dir.
SmallString<128> dir(CacheName);
sys::path::remove_filename(dir);
sys::fs::create_directories(Twine(dir));
}
raw_fd_ostream outfile(CacheName.c_str(), errStr, sys::fs::F_None);
std::error_code EC;
raw_fd_ostream outfile(CacheName, EC, sys::fs::F_None);
outfile.write(Obj.getBufferStart(), Obj.getBufferSize());
outfile.close();
}

View File

@@ -69,11 +69,11 @@ static void WriteOutputFile(const Module *M) {
}
}
std::string ErrorInfo;
std::error_code EC;
std::unique_ptr<tool_output_file> Out(
new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
new tool_output_file(OutputFilename, EC, sys::fs::F_None));
if (EC) {
errs() << EC.message() << '\n';
exit(1);
}

View File

@@ -171,11 +171,11 @@ int main(int argc, char **argv) {
}
}
std::string ErrorInfo;
std::error_code EC;
std::unique_ptr<tool_output_file> Out(
new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
new tool_output_file(OutputFilename, EC, sys::fs::F_None));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}

View File

@@ -261,10 +261,10 @@ int main(int argc, char **argv) {
Passes.add(createStripDeadDebugInfoPass()); // Remove dead debug info
Passes.add(createStripDeadPrototypesPass()); // Remove dead func decls
std::string ErrorInfo;
tool_output_file Out(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None);
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
std::error_code EC;
tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
if (EC) {
errs() << EC.message() << '\n';
return 1;
}

View File

@@ -110,10 +110,10 @@ int main(int argc, char **argv) {
if (DumpAsm) errs() << "Here's the assembly:\n" << *Composite;
std::string ErrorInfo;
tool_output_file Out(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None);
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
std::error_code EC;
tool_output_file Out(OutputFilename, EC, sys::fs::F_None);
if (EC) {
errs() << EC.message() << '\n';
return 1;
}

View File

@@ -165,11 +165,11 @@ int main(int argc, char **argv) {
return 1;
}
raw_fd_ostream FileStream(OutputFilename.c_str(), ErrorInfo,
sys::fs::F_None);
if (!ErrorInfo.empty()) {
std::error_code EC;
raw_fd_ostream FileStream(OutputFilename, EC, sys::fs::F_None);
if (EC) {
errs() << argv[0] << ": error opening the file '" << OutputFilename
<< "': " << ErrorInfo << "\n";
<< "': " << EC.message() << "\n";
return 1;
}

View File

@@ -208,11 +208,11 @@ static tool_output_file *GetOutputStream() {
if (OutputFilename == "")
OutputFilename = "-";
std::string Err;
std::error_code EC;
tool_output_file *Out =
new tool_output_file(OutputFilename.c_str(), Err, sys::fs::F_None);
if (!Err.empty()) {
errs() << Err << '\n';
new tool_output_file(OutputFilename, EC, sys::fs::F_None);
if (EC) {
errs() << EC.message() << '\n';
delete Out;
return nullptr;
}

View File

@@ -202,10 +202,10 @@ static const Target *getTarget(const ObjectFile *Obj = nullptr) {
static void emitDOTFile(const char *FileName, const MCFunction &f,
MCInstPrinter *IP) {
// Start a new dot file.
std::string Error;
raw_fd_ostream Out(FileName, Error, sys::fs::F_Text);
if (!Error.empty()) {
errs() << "llvm-objdump: warning: " << Error << '\n';
std::error_code EC;
raw_fd_ostream Out(FileName, EC, sys::fs::F_Text);
if (EC) {
errs() << "llvm-objdump: warning: " << EC.message() << '\n';
return;
}
@@ -386,10 +386,10 @@ static void DisassembleObject(const ObjectFile *Obj, bool InlineRelocs) {
}
}
if (!YAMLCFG.empty()) {
std::string Error;
raw_fd_ostream YAMLOut(YAMLCFG.c_str(), Error, sys::fs::F_Text);
if (!Error.empty()) {
errs() << ToolName << ": warning: " << Error << '\n';
std::error_code EC;
raw_fd_ostream YAMLOut(YAMLCFG, EC, sys::fs::F_Text);
if (EC) {
errs() << ToolName << ": warning: " << EC.message() << '\n';
return;
}
mcmodule2yaml(YAMLOut, *Mod, *MII, *MRI);

View File

@@ -48,10 +48,10 @@ int merge_main(int argc, const char *argv[]) {
if (OutputFilename.compare("-") == 0)
exitWithError("Cannot write indexed profdata format to stdout.");
std::string ErrorInfo;
raw_fd_ostream Output(OutputFilename.data(), ErrorInfo, sys::fs::F_None);
if (!ErrorInfo.empty())
exitWithError(ErrorInfo, OutputFilename);
std::error_code EC;
raw_fd_ostream Output(OutputFilename.data(), EC, sys::fs::F_None);
if (EC)
exitWithError(EC.message(), OutputFilename);
InstrProfWriter Writer;
for (const auto &Filename : Inputs) {
@@ -97,10 +97,10 @@ int show_main(int argc, const char *argv[]) {
if (OutputFilename.empty())
OutputFilename = "-";
std::string ErrorInfo;
raw_fd_ostream OS(OutputFilename.data(), ErrorInfo, sys::fs::F_Text);
if (!ErrorInfo.empty())
exitWithError(ErrorInfo, OutputFilename);
std::error_code EC;
raw_fd_ostream OS(OutputFilename.data(), EC, sys::fs::F_Text);
if (EC)
exitWithError(EC.message(), OutputFilename);
if (ShowAllFunctions && !ShowFunction.empty())
errs() << "warning: -function argument ignored: showing all functions\n";

View File

@@ -704,11 +704,10 @@ int main(int argc, char **argv) {
if (OutputFilename.empty())
OutputFilename = "-";
std::string ErrorInfo;
Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
sys::fs::F_None));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
std::error_code EC;
Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}

View File

@@ -385,11 +385,10 @@ int main(int argc, char **argv) {
if (OutputFilename.empty())
OutputFilename = "-";
std::string ErrorInfo;
Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
sys::fs::F_None));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
std::error_code EC;
Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
}
@@ -470,11 +469,10 @@ int main(int argc, char **argv) {
if (OutputFilename.empty())
OutputFilename = "-";
std::string ErrorInfo;
Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
sys::fs::F_None));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
std::error_code EC;
Out.reset(new tool_output_file(OutputFilename, EC, sys::fs::F_None));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}
}

View File

@@ -123,10 +123,10 @@ bool TempFile::init(const std::string &Ext) {
bool TempFile::writeBitcode(const Module &M) const {
DEBUG(dbgs() << " - write bitcode\n");
std::string ErrorInfo;
raw_fd_ostream OS(Filename.c_str(), ErrorInfo, sys::fs::F_None);
if (!ErrorInfo.empty()) {
DEBUG(dbgs() << "error: " << ErrorInfo << "\n");
std::error_code EC;
raw_fd_ostream OS(Filename, EC, sys::fs::F_None);
if (EC) {
DEBUG(dbgs() << "error: " << EC.message() << "\n");
return true;
}
@@ -136,10 +136,10 @@ bool TempFile::writeBitcode(const Module &M) const {
bool TempFile::writeAssembly(const Module &M) const {
DEBUG(dbgs() << " - write assembly\n");
std::string ErrorInfo;
raw_fd_ostream OS(Filename.c_str(), ErrorInfo, sys::fs::F_Text);
if (!ErrorInfo.empty()) {
DEBUG(dbgs() << "error: " << ErrorInfo << "\n");
std::error_code EC;
raw_fd_ostream OS(Filename, EC, sys::fs::F_Text);
if (EC) {
DEBUG(dbgs() << "error: " << EC.message() << "\n");
return true;
}

View File

@@ -83,11 +83,11 @@ int main(int argc, char **argv) {
if (OutputFilename.empty())
OutputFilename = "-";
std::string ErrorInfo;
std::error_code EC;
std::unique_ptr<tool_output_file> Out(
new tool_output_file(OutputFilename.c_str(), ErrorInfo, sys::fs::F_None));
if (!ErrorInfo.empty()) {
errs() << ErrorInfo << '\n';
new tool_output_file(OutputFilename, EC, sys::fs::F_None));
if (EC) {
errs() << EC.message() << '\n';
return 1;
}