@@ -413,34 +413,36 @@ int main(int argc, char *argv[]) {
413413 RefactoringConfig.Range = Range;
414414 RefactoringConfig.PreferredName = options::NewName;
415415 std::string Error;
416- std::unique_ptr<SourceEditConsumer> pConsumer;
416+
417+ StringRef RewrittenOutputFile = options::RewrittenOutputFile;
418+ if (RewrittenOutputFile.empty ())
419+ RewrittenOutputFile = " -" ;
420+ std::error_code EC;
421+ llvm::raw_fd_ostream RewriteStream (RewrittenOutputFile, EC);
422+ if (RewriteStream.has_error ()) {
423+ llvm::errs () << " Could not open rewritten output file" ;
424+ return 1 ;
425+ }
426+
427+ SmallVector<std::unique_ptr<SourceEditConsumer>> Consumers;
428+ if (!options::RewrittenOutputFile.empty () ||
429+ options::DumpIn == options::DumpType::REWRITTEN) {
430+ Consumers.emplace_back (new SourceEditOutputConsumer (
431+ SF->getASTContext ().SourceMgr , BufferID, RewriteStream));
432+ }
417433 switch (options::DumpIn) {
418434 case options::DumpType::REWRITTEN:
419- pConsumer.reset (new SourceEditOutputConsumer (SF->getASTContext ().SourceMgr ,
420- BufferID, llvm::outs ()));
435+ // Already added
421436 break ;
422437 case options::DumpType::JSON:
423- pConsumer. reset (new SourceEditJsonConsumer (llvm::outs ()));
438+ Consumers. emplace_back (new SourceEditJsonConsumer (llvm::outs ()));
424439 break ;
425440 case options::DumpType::TEXT:
426- if (options::RewrittenOutputFile.empty ()) {
427- pConsumer.reset (new SourceEditTextConsumer (llvm::outs ()));
428- } else {
429- std::error_code EC;
430- static llvm::raw_fd_ostream FileStream (options::RewrittenOutputFile, EC,
431- llvm::sys::fs::OF_None);
432- if (FileStream.has_error () || EC) {
433- llvm::errs () << " Could not open rewritten output file" ;
434- return 1 ;
435- }
436- pConsumer.reset (new DuplicatingSourceEditConsumer (
437- new SourceEditTextConsumer (llvm::outs ()),
438- new SourceEditOutputConsumer (SF->getASTContext ().SourceMgr , BufferID,
439- FileStream)));
440- }
441+ Consumers.emplace_back (new SourceEditTextConsumer (llvm::outs ()));
441442 break ;
442443 }
443444
444- return refactorSwiftModule (CI.getMainModule (), RefactoringConfig, *pConsumer,
445- PrintDiags);
445+ BroadcastingSourceEditConsumer BroadcastConsumer (Consumers);
446+ return refactorSwiftModule (CI.getMainModule (), RefactoringConfig,
447+ BroadcastConsumer, PrintDiags);
446448}
0 commit comments