Skip to content

Commit 4f1d3ac

Browse files
committed
refactor error handling for file stream to function openFileStream()
1 parent 03e719d commit 4f1d3ac

2 files changed

Lines changed: 43 additions & 99 deletions

File tree

include/phasar/Controller/AnalysisController.h

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,18 +81,14 @@ class AnalysisController {
8181

8282
void emitRequestedHelperAnalysisResults();
8383

84+
std::unique_ptr<llvm::raw_fd_ostream>
85+
openFileStream(llvm::StringRef Filename);
86+
8487
template <typename T> void emitRequestedDataFlowResults(T &WPA) {
85-
std::error_code EC;
8688
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitTextReport) {
8789
if (!ResultDirectory.empty()) {
88-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-report.txt",
89-
EC);
90-
if (EC) {
91-
llvm::errs() << "Failed to open file: "
92-
<< ResultDirectory.string() + "/psr-report.txt" << '\n';
93-
llvm::errs() << EC.message() << '\n';
94-
} else {
95-
WPA.emitTextReport(OFS);
90+
if (auto OFS = openFileStream("/psr-report.txt")) {
91+
WPA.emitTextReport(*OFS);
9692
}
9793
} else {
9894
WPA.emitTextReport(llvm::outs());
@@ -101,30 +97,17 @@ class AnalysisController {
10197
if (EmitterOptions &
10298
AnalysisControllerEmitterOptions::EmitGraphicalReport) {
10399
if (!ResultDirectory.empty()) {
104-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-report.html",
105-
EC);
106-
if (EC) {
107-
llvm::errs() << "Failed to open file: "
108-
<< ResultDirectory.string() + "/psr-report.txt" << '\n';
109-
llvm::errs() << EC.message() << '\n';
110-
} else {
111-
WPA.emitGraphicalReport(OFS);
100+
if (auto OFS = openFileStream("/psr-report.html")) {
101+
WPA.emitGraphicalReport(*OFS);
112102
}
113103
} else {
114104
WPA.emitGraphicalReport(llvm::outs());
115105
}
116106
}
117107
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitRawResults) {
118108
if (!ResultDirectory.empty()) {
119-
llvm::raw_fd_ostream OFS(
120-
ResultDirectory.string() + "/psr-raw-results.txt", EC);
121-
if (EC) {
122-
llvm::errs() << "Failed to open file: "
123-
<< ResultDirectory.string() + "/psr-raw-results.txt"
124-
<< '\n';
125-
llvm::errs() << EC.message() << '\n';
126-
} else {
127-
WPA.dumpResults(OFS);
109+
if (auto OFS = openFileStream("/psr-raw-results.txt")) {
110+
WPA.dumpResults(*OFS);
128111
}
129112
} else {
130113
WPA.dumpResults(llvm::outs());

lib/Controller/AnalysisController.cpp

Lines changed: 34 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -339,130 +339,82 @@ void AnalysisController::executeWholeProgram() {
339339
}
340340

341341
void AnalysisController::emitRequestedHelperAnalysisResults() {
342-
std::error_code EC;
343342
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitIR) {
344343
if (!ResultDirectory.empty()) {
345-
llvm::raw_fd_ostream OFS(
346-
ResultDirectory.string() + "/psr-preprocess-ir.ll", EC);
347-
if (EC) {
348-
llvm::errs() << "Failed to open file: "
349-
<< ResultDirectory.string() + "/psr-preprocess-ir.ll"
350-
<< '\n';
351-
llvm::errs() << EC.message() << '\n';
352-
} else {
353-
IRDB.emitPreprocessedIR(OFS);
344+
if (auto OFS = openFileStream("/psr-preprocess-ir.ll")) {
345+
IRDB.emitPreprocessedIR(*OFS);
354346
}
355347
} else {
356348
IRDB.emitPreprocessedIR();
357349
}
358350
}
359351
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitTHAsText) {
360352
if (!ResultDirectory.empty()) {
361-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-th.txt", EC);
362-
if (EC) {
363-
llvm::errs() << "Failed to open file: "
364-
<< ResultDirectory.string() + "/psr-th.txt" << '\n';
365-
llvm::errs() << EC.message() << '\n';
366-
} else {
367-
TH.print(OFS);
353+
if (auto OFS = openFileStream("/psr-th.txt")) {
354+
TH.print(*OFS);
368355
}
369356
} else {
370357
TH.print();
371358
}
372359
}
373360
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitTHAsDot) {
374361
if (!ResultDirectory.empty()) {
375-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-th.dot", EC);
376-
if (EC) {
377-
llvm::errs() << "Failed to open file: "
378-
<< ResultDirectory.string() + "/psr-th.dot" << '\n';
379-
llvm::errs() << EC.message() << '\n';
380-
} else {
381-
TH.printAsDot(OFS);
362+
if (auto OFS = openFileStream("/psr-th.dot")) {
363+
TH.printAsDot(*OFS);
382364
}
383365
} else {
384366
TH.printAsDot();
385367
}
386368
}
387369
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitTHAsJson) {
388370
if (!ResultDirectory.empty()) {
389-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-th.json", EC);
390-
if (EC) {
391-
llvm::errs() << "Failed to open file: "
392-
<< ResultDirectory.string() + "/psr-th.json" << '\n';
393-
llvm::errs() << EC.message() << '\n';
394-
} else {
395-
TH.printAsJson(OFS);
371+
if (auto OFS = openFileStream("/psr-th.json")) {
372+
TH.printAsJson(*OFS);
396373
}
397374
} else {
398375
TH.printAsJson();
399376
}
400377
}
401378
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitPTAAsText) {
402379
if (!ResultDirectory.empty()) {
403-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-pta.txt", EC);
404-
if (EC) {
405-
llvm::errs() << "Failed to open file: "
406-
<< ResultDirectory.string() + "/psr-pta.txt" << '\n';
407-
llvm::errs() << EC.message() << '\n';
408-
} else {
409-
PT.print(OFS);
380+
if (auto OFS = openFileStream("/psr-pta.txt")) {
381+
PT.print(*OFS);
410382
}
411383
} else {
412384
PT.print();
413385
}
414386
}
415387
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitPTAAsDot) {
416388
if (!ResultDirectory.empty()) {
417-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-pta.dot", EC);
418-
if (EC) {
419-
llvm::errs() << "Failed to open file: "
420-
<< ResultDirectory.string() + "/psr-pta.dot" << '\n';
421-
llvm::errs() << EC.message() << '\n';
422-
} else {
423-
PT.print(OFS);
389+
if (auto OFS = openFileStream("/psr-pta.dot")) {
390+
PT.print(*OFS);
424391
}
425392
} else {
426393
PT.print();
427394
}
428395
}
429396
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitPTAAsJson) {
430397
if (!ResultDirectory.empty()) {
431-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-pta.json", EC);
432-
if (EC) {
433-
llvm::errs() << "Failed to open file: "
434-
<< ResultDirectory.string() + "/psr-pta.json" << '\n';
435-
llvm::errs() << EC.message() << '\n';
436-
} else {
437-
PT.printAsJson(OFS);
398+
if (auto OFS = openFileStream("/psr-pta.json")) {
399+
PT.printAsJson(*OFS);
438400
}
439401
} else {
440402
PT.printAsJson();
441403
}
442404
}
443405
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitCGAsText) {
444406
if (!ResultDirectory.empty()) {
445-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-cg.txt", EC);
446-
if (EC) {
447-
llvm::errs() << "Failed to open file: "
448-
<< ResultDirectory.string() + "/psr-cg.txt" << '\n';
449-
llvm::errs() << EC.message() << '\n';
450-
} else {
451-
ICF.print(OFS);
407+
if (auto OFS = openFileStream("/psr-cg.txt")) {
408+
ICF.print(*OFS);
452409
}
453410
} else {
454411
ICF.print();
455412
}
456413
}
457414
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitCGAsDot) {
458415
if (!ResultDirectory.empty()) {
459-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-cg.dot", EC);
460-
if (EC) {
461-
llvm::errs() << "Failed to open file: "
462-
<< ResultDirectory.string() + "/psr-cg.dot" << '\n';
463-
llvm::errs() << EC.message() << '\n';
464-
} else {
465-
ICF.printAsDot(OFS);
416+
if (auto OFS = openFileStream("/psr-cg.dot")) {
417+
ICF.printAsDot(*OFS);
466418
}
467419
} else {
468420
ICF.printAsDot();
@@ -471,18 +423,27 @@ void AnalysisController::emitRequestedHelperAnalysisResults() {
471423

472424
if (EmitterOptions & AnalysisControllerEmitterOptions::EmitCGAsJson) {
473425
if (!ResultDirectory.empty()) {
474-
llvm::raw_fd_ostream OFS(ResultDirectory.string() + "/psr-cg.json", EC);
475-
if (EC) {
476-
llvm::errs() << "Failed to open file: "
477-
<< ResultDirectory.string() + "/psr-cg.json" << '\n';
478-
llvm::errs() << EC.message() << '\n';
479-
} else {
480-
ICF.printAsJson(OFS);
426+
if (auto OFS = openFileStream("/psr-cg.json")) {
427+
ICF.printAsJson(*OFS);
481428
}
482429
} else {
483430
ICF.printAsJson();
484431
}
485432
}
486433
}
487434

435+
std::unique_ptr<llvm::raw_fd_ostream>
436+
AnalysisController::openFileStream(llvm::StringRef FilePathSuffix) {
437+
std::error_code EC;
438+
auto OFS = std::make_unique<llvm::raw_fd_ostream>(
439+
ResultDirectory.string() + FilePathSuffix.str(), EC);
440+
if (EC) {
441+
OFS = nullptr;
442+
llvm::errs() << "Failed to open file: "
443+
<< ResultDirectory.string() + FilePathSuffix << '\n';
444+
llvm::errs() << EC.message() << '\n';
445+
}
446+
return OFS;
447+
}
448+
488449
} // namespace psr

0 commit comments

Comments
 (0)