@@ -339,130 +339,82 @@ void AnalysisController::executeWholeProgram() {
339339}
340340
341341void 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