Skip to content

Commit 3fd1e99

Browse files
committed
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. llvm-svn: 216393
1 parent b709222 commit 3fd1e99

File tree

36 files changed

+181
-192
lines changed

36 files changed

+181
-192
lines changed

llvm/examples/BrainF/BrainFDriver.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,8 @@ int main(int argc, char **argv) {
107107
OutputFilename = base+".bc";
108108
}
109109
if (OutputFilename != "-") {
110-
std::string ErrInfo;
111-
out = new raw_fd_ostream(OutputFilename.c_str(), ErrInfo,
112-
sys::fs::F_None);
110+
std::error_code EC;
111+
out = new raw_fd_ostream(OutputFilename, EC, sys::fs::F_None);
113112
}
114113
}
115114

llvm/include/llvm/Analysis/DOTGraphTraitsPass.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ class DOTGraphTraitsPrinter : public FunctionPass {
6666
bool runOnFunction(Function &F) override {
6767
GraphT Graph = AnalysisGraphTraitsT::getGraph(&getAnalysis<AnalysisT>());
6868
std::string Filename = Name + "." + F.getName().str() + ".dot";
69-
std::string ErrorInfo;
69+
std::error_code EC;
7070

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

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

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

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

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

139-
if (ErrorInfo.empty())
139+
if (!EC)
140140
WriteGraph(File, Graph, IsSimple, Title);
141141
else
142142
errs() << " error opening file for writing!";

llvm/include/llvm/Support/ToolOutputFile.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ class tool_output_file {
2929
/// destructed after the raw_fd_ostream is destructed. It installs
3030
/// cleanups in its constructor and uninstalls them in its destructor.
3131
class CleanupInstaller {
32-
/// Filename - The name of the file.
32+
/// The name of the file.
3333
std::string Filename;
3434
public:
35-
/// Keep - The flag which indicates whether we should not delete the file.
35+
/// The flag which indicates whether we should not delete the file.
3636
bool Keep;
3737

38-
explicit CleanupInstaller(const char *filename);
38+
explicit CleanupInstaller(StringRef ilename);
3939
~CleanupInstaller();
4040
} Installer;
4141

@@ -44,12 +44,12 @@ class tool_output_file {
4444
raw_fd_ostream OS;
4545

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

52-
tool_output_file(const char *Filename, int FD);
52+
tool_output_file(StringRef Filename, int FD);
5353

5454
/// os - Return the contained raw_fd_ostream.
5555
raw_fd_ostream &os() { return OS; }

llvm/include/llvm/Support/raw_ostream.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/ADT/StringRef.h"
1818
#include "llvm/Support/Compiler.h"
1919
#include "llvm/Support/DataTypes.h"
20+
#include <system_error>
2021

2122
namespace llvm {
2223
class format_object_base;
@@ -341,17 +342,17 @@ class raw_fd_ostream : public raw_ostream {
341342
void error_detected() { Error = true; }
342343

343344
public:
344-
/// raw_fd_ostream - Open the specified file for writing. If an error occurs,
345-
/// information about the error is put into ErrorInfo, and the stream should
346-
/// be immediately destroyed; the string will be empty if no error occurred.
347-
/// This allows optional flags to control how the file will be opened.
345+
/// Open the specified file for writing. If an error occurs, information
346+
/// about the error is put into EC, and the stream should be immediately
347+
/// destroyed;
348+
/// \p Flags allows optional flags to control how the file will be opened.
348349
///
349350
/// As a special case, if Filename is "-", then the stream will use
350351
/// STDOUT_FILENO instead of opening a file. Note that it will still consider
351352
/// itself to own the file descriptor. In particular, it will close the
352353
/// file descriptor when it is done (this is necessary to detect
353354
/// output errors).
354-
raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
355+
raw_fd_ostream(StringRef Filename, std::error_code &EC,
355356
sys::fs::OpenFlags Flags);
356357

357358
/// raw_fd_ostream ctor - FD is the file descriptor that this writes to. If

llvm/lib/Analysis/CFGPrinter.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,11 @@ namespace {
7979
bool runOnFunction(Function &F) override {
8080
std::string Filename = "cfg." + F.getName().str() + ".dot";
8181
errs() << "Writing '" << Filename << "'...";
82-
83-
std::string ErrorInfo;
84-
raw_fd_ostream File(Filename.c_str(), ErrorInfo, sys::fs::F_Text);
8582

86-
if (ErrorInfo.empty())
83+
std::error_code EC;
84+
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
85+
86+
if (!EC)
8787
WriteGraph(File, (const Function*)&F);
8888
else
8989
errs() << " error opening file for writing!";
@@ -114,10 +114,10 @@ namespace {
114114
std::string Filename = "cfg." + F.getName().str() + ".dot";
115115
errs() << "Writing '" << Filename << "'...";
116116

117-
std::string ErrorInfo;
118-
raw_fd_ostream File(Filename.c_str(), ErrorInfo, sys::fs::F_Text);
119-
120-
if (ErrorInfo.empty())
117+
std::error_code EC;
118+
raw_fd_ostream File(Filename, EC, sys::fs::F_Text);
119+
120+
if (!EC)
121121
WriteGraph(File, (const Function*)&F, true);
122122
else
123123
errs() << " error opening file for writing!";

llvm/lib/Bitcode/Writer/BitWriter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ using namespace llvm;
1818
/*===-- Operations on modules ---------------------------------------------===*/
1919

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

24-
if (!ErrorInfo.empty())
24+
if (EC)
2525
return -1;
2626

2727
WriteBitcodeToFile(unwrap(M), OS);

llvm/lib/CodeGen/MachineVerifier.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,12 @@ void MachineFunction::verify(Pass *p, const char *Banner) const {
276276
bool MachineVerifier::runOnMachineFunction(MachineFunction &MF) {
277277
raw_ostream *OutFile = nullptr;
278278
if (OutFileName) {
279-
std::string ErrorInfo;
280-
OutFile = new raw_fd_ostream(OutFileName, ErrorInfo,
279+
std::error_code EC;
280+
OutFile = new raw_fd_ostream(OutFileName, EC,
281281
sys::fs::F_Append | sys::fs::F_Text);
282-
if (!ErrorInfo.empty()) {
283-
errs() << "Error opening '" << OutFileName << "': " << ErrorInfo << '\n';
282+
if (EC) {
283+
errs() << "Error opening '" << OutFileName << "': " << EC.message()
284+
<< '\n';
284285
exit(1);
285286
}
286287

llvm/lib/CodeGen/RegAllocPBQP.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
587587
std::ostringstream rs;
588588
rs << round;
589589
std::string graphFileName(fqn + "." + rs.str() + ".pbqpgraph");
590-
std::string tmp;
591-
raw_fd_ostream os(graphFileName.c_str(), tmp, sys::fs::F_Text);
590+
std::error_code EC;
591+
raw_fd_ostream os(graphFileName, EC, sys::fs::F_Text);
592592
DEBUG(dbgs() << "Dumping graph for round " << round << " to \""
593593
<< graphFileName << "\"\n");
594594
problem->getGraph().dump(os);

llvm/lib/IR/Core.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,22 @@ void LLVMDumpModule(LLVMModuleRef M) {
183183

184184
LLVMBool LLVMPrintModuleToFile(LLVMModuleRef M, const char *Filename,
185185
char **ErrorMessage) {
186-
std::string error;
187-
raw_fd_ostream dest(Filename, error, sys::fs::F_Text);
188-
if (!error.empty()) {
189-
*ErrorMessage = strdup(error.c_str());
186+
std::error_code EC;
187+
raw_fd_ostream dest(Filename, EC, sys::fs::F_Text);
188+
if (EC) {
189+
*ErrorMessage = strdup(EC.message().c_str());
190190
return true;
191191
}
192192

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

195-
if (!error.empty()) {
196-
*ErrorMessage = strdup(error.c_str());
195+
dest.close();
196+
197+
if (dest.has_error()) {
198+
*ErrorMessage = strdup("Error printing to file");
197199
return true;
198200
}
199-
dest.flush();
201+
200202
return false;
201203
}
202204

llvm/lib/IR/GCOV.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,11 +517,11 @@ FileInfo::openCoveragePath(StringRef CoveragePath) {
517517
if (Options.NoOutput)
518518
return llvm::make_unique<raw_null_ostream>();
519519

520-
std::string ErrorInfo;
521-
auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str().c_str(),
522-
ErrorInfo, sys::fs::F_Text);
523-
if (!ErrorInfo.empty()) {
524-
errs() << ErrorInfo << "\n";
520+
std::error_code EC;
521+
auto OS = llvm::make_unique<raw_fd_ostream>(CoveragePath.str(), EC,
522+
sys::fs::F_Text);
523+
if (EC) {
524+
errs() << EC.message() << "\n";
525525
return llvm::make_unique<raw_null_ostream>();
526526
}
527527
return std::move(OS);

llvm/lib/LTO/LTOCodeGenerator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ bool LTOCodeGenerator::writeMergedModules(const char *path,
163163
applyScopeRestrictions();
164164

165165
// create output file
166-
std::string ErrInfo;
167-
tool_output_file Out(path, ErrInfo, sys::fs::F_None);
168-
if (!ErrInfo.empty()) {
166+
std::error_code EC;
167+
tool_output_file Out(path, EC, sys::fs::F_None);
168+
if (EC) {
169169
errMsg = "could not open bitcode file for writing: ";
170170
errMsg += path;
171171
return false;

llvm/lib/MC/MCParser/DarwinAsmParser.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,13 @@ bool DarwinAsmParser::parseDirectiveSecureLogUnique(StringRef, SMLoc IDLoc) {
638638
// Open the secure log file if we haven't already.
639639
raw_ostream *OS = getContext().getSecureLog();
640640
if (!OS) {
641-
std::string Err;
642-
OS = new raw_fd_ostream(SecureLogFile, Err,
641+
std::error_code EC;
642+
OS = new raw_fd_ostream(SecureLogFile, EC,
643643
sys::fs::F_Append | sys::fs::F_Text);
644-
if (!Err.empty()) {
644+
if (EC) {
645645
delete OS;
646646
return Error(IDLoc, Twine("can't open secure log file: ") +
647-
SecureLogFile + " (" + Err + ")");
647+
SecureLogFile + " (" + EC.message() + ")");
648648
}
649649
getContext().setSecureLog(OS);
650650
}

llvm/lib/Support/Timer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ raw_ostream *llvm::CreateInfoOutputFile() {
6666
// each time -stats or -time-passes wants to print output to it. To
6767
// compensate for this, the test-suite Makefiles have code to delete the
6868
// info output file before running commands which write to it.
69-
std::string Error;
70-
raw_ostream *Result = new raw_fd_ostream(
71-
OutputFilename.c_str(), Error, sys::fs::F_Append | sys::fs::F_Text);
72-
if (Error.empty())
69+
std::error_code EC;
70+
raw_ostream *Result = new raw_fd_ostream(OutputFilename, EC,
71+
sys::fs::F_Append | sys::fs::F_Text);
72+
if (!EC)
7373
return Result;
7474

7575
errs() << "Error opening info-output-file '"

llvm/lib/Support/ToolOutputFile.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
#include "llvm/Support/Signals.h"
1717
using namespace llvm;
1818

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

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

45-
tool_output_file::tool_output_file(const char *Filename, int FD)
46-
: Installer(Filename), OS(FD, true) {
47-
}
45+
tool_output_file::tool_output_file(StringRef Filename, int FD)
46+
: Installer(Filename), OS(FD, true) {}

llvm/lib/Support/raw_ostream.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -426,20 +426,14 @@ void format_object_base::home() {
426426
// raw_fd_ostream
427427
//===----------------------------------------------------------------------===//
428428

429-
/// raw_fd_ostream - Open the specified file for writing. If an error
430-
/// occurs, information about the error is put into ErrorInfo, and the
431-
/// stream should be immediately destroyed; the string will be empty
432-
/// if no error occurred.
433-
raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
429+
raw_fd_ostream::raw_fd_ostream(StringRef Filename, std::error_code &EC,
434430
sys::fs::OpenFlags Flags)
435431
: Error(false), UseAtomicWrites(false), pos(0) {
436-
assert(Filename && "Filename is null");
437-
ErrorInfo.clear();
438-
432+
EC = std::error_code();
439433
// Handle "-" as stdout. Note that when we do this, we consider ourself
440434
// the owner of stdout. This means that we can do things like close the
441435
// file descriptor when we're done and set the "binary" flag globally.
442-
if (Filename[0] == '-' && Filename[1] == 0) {
436+
if (Filename == "-") {
443437
FD = STDOUT_FILENO;
444438
// If user requested binary then put stdout into binary mode if
445439
// possible.
@@ -450,11 +444,9 @@ raw_fd_ostream::raw_fd_ostream(const char *Filename, std::string &ErrorInfo,
450444
return;
451445
}
452446

453-
std::error_code EC = sys::fs::openFileForWrite(Filename, FD, Flags);
447+
EC = sys::fs::openFileForWrite(Filename, FD, Flags);
454448

455449
if (EC) {
456-
ErrorInfo = "Error opening output file '" + std::string(Filename) + "': " +
457-
EC.message();
458450
ShouldClose = false;
459451
return;
460452
}

llvm/lib/TableGen/Main.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ static int createDependencyFile(const TGParser &Parser, const char *argv0) {
5656
errs() << argv0 << ": the option -d must be used together with -o\n";
5757
return 1;
5858
}
59-
std::string Error;
60-
tool_output_file DepOut(DependFilename.c_str(), Error, sys::fs::F_Text);
61-
if (!Error.empty()) {
62-
errs() << argv0 << ": error opening " << DependFilename
63-
<< ":" << Error << "\n";
59+
std::error_code EC;
60+
tool_output_file DepOut(DependFilename, EC, sys::fs::F_Text);
61+
if (EC) {
62+
errs() << argv0 << ": error opening " << DependFilename << ":"
63+
<< EC.message() << "\n";
6464
return 1;
6565
}
6666
DepOut.os() << OutputFilename << ":";
@@ -101,11 +101,11 @@ int TableGenMain(char *argv0, TableGenMainFn *MainFn) {
101101
if (Parser.ParseFile())
102102
return 1;
103103

104-
std::string Error;
105-
tool_output_file Out(OutputFilename.c_str(), Error, sys::fs::F_Text);
106-
if (!Error.empty()) {
107-
errs() << argv0 << ": error opening " << OutputFilename
108-
<< ":" << Error << "\n";
104+
std::error_code EC;
105+
tool_output_file Out(OutputFilename, EC, sys::fs::F_Text);
106+
if (EC) {
107+
errs() << argv0 << ": error opening " << OutputFilename << ":"
108+
<< EC.message() << "\n";
109109
return 1;
110110
}
111111
if (!DependFilename.empty()) {

llvm/lib/Target/TargetMachineC.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,10 +223,10 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
223223

224224
LLVMBool LLVMTargetMachineEmitToFile(LLVMTargetMachineRef T, LLVMModuleRef M,
225225
char* Filename, LLVMCodeGenFileType codegen, char** ErrorMessage) {
226-
std::string error;
227-
raw_fd_ostream dest(Filename, error, sys::fs::F_None);
228-
if (!error.empty()) {
229-
*ErrorMessage = strdup(error.c_str());
226+
std::error_code EC;
227+
raw_fd_ostream dest(Filename, EC, sys::fs::F_None);
228+
if (EC) {
229+
*ErrorMessage = strdup(EC.message().c_str());
230230
return true;
231231
}
232232
formatted_raw_ostream destf(dest);

0 commit comments

Comments
 (0)