@@ -646,10 +646,6 @@ void CompilerInstance::createSema(TranslationUnitKind TUKind,
646
646
647
647
// Output Files
648
648
649
- void CompilerInstance::addOutputFile (OutputFile &&OutFile) {
650
- OutputFiles.push_back (std::move (OutFile));
651
- }
652
-
653
649
void CompilerInstance::clearOutputFiles (bool EraseFiles) {
654
650
for (OutputFile &OF : OutputFiles) {
655
651
if (!OF.TempFilename .empty ()) {
@@ -682,10 +678,25 @@ void CompilerInstance::clearOutputFiles(bool EraseFiles) {
682
678
683
679
std::unique_ptr<raw_pwrite_stream>
684
680
CompilerInstance::createDefaultOutputFile (bool Binary, StringRef InFile,
685
- StringRef Extension) {
686
- return createOutputFile (getFrontendOpts ().OutputFile , Binary,
687
- /* RemoveFileOnSignal=*/ true , InFile, Extension,
688
- getFrontendOpts ().UseTemporary );
681
+ StringRef Extension,
682
+ bool RemoveFileOnSignal,
683
+ bool CreateMissingDirectories) {
684
+ StringRef OutputPath = getFrontendOpts ().OutputFile ;
685
+ Optional<SmallString<128 >> PathStorage;
686
+ if (OutputPath.empty ()) {
687
+ if (InFile == " -" || Extension.empty ()) {
688
+ OutputPath = " -" ;
689
+ } else {
690
+ PathStorage.emplace (InFile);
691
+ llvm::sys::path::replace_extension (*PathStorage, Extension);
692
+ OutputPath = *PathStorage;
693
+ }
694
+ }
695
+
696
+ // Force a temporary file if RemoveFileOnSignal was disabled.
697
+ return createOutputFile (OutputPath, Binary, RemoveFileOnSignal,
698
+ getFrontendOpts ().UseTemporary || !RemoveFileOnSignal,
699
+ CreateMissingDirectories);
689
700
}
690
701
691
702
std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile () {
@@ -694,64 +705,40 @@ std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() {
694
705
695
706
std::unique_ptr<raw_pwrite_stream>
696
707
CompilerInstance::createOutputFile (StringRef OutputPath, bool Binary,
697
- bool RemoveFileOnSignal, StringRef InFile,
698
- StringRef Extension, bool UseTemporary,
708
+ bool RemoveFileOnSignal, bool UseTemporary,
699
709
bool CreateMissingDirectories) {
700
- std::string OutputPathName, TempPathName;
701
- std::error_code EC;
702
- std::unique_ptr<raw_pwrite_stream> OS = createOutputFile (
703
- OutputPath, EC, Binary, RemoveFileOnSignal, InFile, Extension,
704
- UseTemporary, CreateMissingDirectories, &OutputPathName, &TempPathName);
705
- if (!OS) {
706
- getDiagnostics ().Report (diag::err_fe_unable_to_open_output) << OutputPath
707
- << EC.message ();
708
- return nullptr ;
709
- }
710
-
711
- // Add the output file -- but don't try to remove "-", since this means we are
712
- // using stdin.
713
- addOutputFile (
714
- OutputFile ((OutputPathName != " -" ) ? OutputPathName : " " , TempPathName));
715
-
716
- return OS;
710
+ Expected<std::unique_ptr<raw_pwrite_stream>> OS =
711
+ createOutputFileImpl (OutputPath, Binary, RemoveFileOnSignal, UseTemporary,
712
+ CreateMissingDirectories);
713
+ if (OS)
714
+ return std::move (*OS);
715
+ getDiagnostics ().Report (diag::err_fe_unable_to_open_output)
716
+ << OutputPath << errorToErrorCode (OS.takeError ()).message ();
717
+ return nullptr ;
717
718
}
718
719
719
- std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile (
720
- StringRef OutputPath, std::error_code &Error , bool Binary,
721
- bool RemoveFileOnSignal, StringRef InFile, StringRef Extension ,
722
- bool UseTemporary, bool CreateMissingDirectories ,
723
- std::string *ResultPathName, std::string *TempPathName ) {
720
+ Expected< std::unique_ptr<llvm::raw_pwrite_stream>>
721
+ CompilerInstance::createOutputFileImpl (StringRef OutputPath , bool Binary,
722
+ bool RemoveFileOnSignal ,
723
+ bool UseTemporary ,
724
+ bool CreateMissingDirectories ) {
724
725
assert ((!CreateMissingDirectories || UseTemporary) &&
725
726
" CreateMissingDirectories is only allowed when using temporary files" );
726
727
727
- std::string OutFile, TempFile;
728
- if (!OutputPath.empty ()) {
729
- OutFile = std::string (OutputPath);
730
- } else if (InFile == " -" ) {
731
- OutFile = " -" ;
732
- } else if (!Extension.empty ()) {
733
- SmallString<128 > Path (InFile);
734
- llvm::sys::path::replace_extension (Path, Extension);
735
- OutFile = std::string (Path.str ());
736
- } else {
737
- OutFile = " -" ;
738
- }
739
-
740
728
std::unique_ptr<llvm::raw_fd_ostream> OS;
741
- std::string OSFile;
729
+ Optional<StringRef> OSFile;
742
730
743
731
if (UseTemporary) {
744
- if (OutFile == " -" )
732
+ if (OutputPath == " -" )
745
733
UseTemporary = false ;
746
734
else {
747
735
llvm::sys::fs::file_status Status;
748
736
llvm::sys::fs::status (OutputPath, Status);
749
737
if (llvm::sys::fs::exists (Status)) {
750
738
// Fail early if we can't write to the final destination.
751
- if (!llvm::sys::fs::can_write (OutputPath)) {
752
- Error = make_error_code (llvm::errc::operation_not_permitted);
753
- return nullptr ;
754
- }
739
+ if (!llvm::sys::fs::can_write (OutputPath))
740
+ return llvm::errorCodeToError (
741
+ make_error_code (llvm::errc::operation_not_permitted));
755
742
756
743
// Don't use a temporary if the output is a special file. This handles
757
744
// things like '-o /dev/null'
@@ -761,14 +748,15 @@ std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile(
761
748
}
762
749
}
763
750
751
+ std::string TempFile;
764
752
if (UseTemporary) {
765
753
// Create a temporary file.
766
754
// Insert -%%%%%%%% before the extension (if any), and because some tools
767
755
// (noticeable, clang's own GlobalModuleIndex.cpp) glob for build
768
756
// artifacts, also append .tmp.
769
- StringRef OutputExtension = llvm::sys::path::extension (OutFile );
757
+ StringRef OutputExtension = llvm::sys::path::extension (OutputPath );
770
758
SmallString<128 > TempPath =
771
- StringRef (OutFile ).drop_back (OutputExtension.size ());
759
+ StringRef (OutputPath ).drop_back (OutputExtension.size ());
772
760
TempPath += " -%%%%%%%%" ;
773
761
TempPath += OutputExtension;
774
762
TempPath += " .tmp" ;
@@ -795,22 +783,23 @@ std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile(
795
783
}
796
784
797
785
if (!OS) {
798
- OSFile = OutFile;
786
+ OSFile = OutputPath;
787
+ std::error_code EC;
799
788
OS.reset (new llvm::raw_fd_ostream (
800
- OSFile, Error ,
789
+ * OSFile, EC ,
801
790
(Binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_Text)));
802
- if (Error )
803
- return nullptr ;
791
+ if (EC )
792
+ return llvm::errorCodeToError (EC) ;
804
793
}
805
794
806
795
// Make sure the out stream file gets removed if we crash.
807
796
if (RemoveFileOnSignal)
808
- llvm::sys::RemoveFileOnSignal (OSFile);
797
+ llvm::sys::RemoveFileOnSignal (* OSFile);
809
798
810
- if (ResultPathName)
811
- *ResultPathName = OutFile;
812
- if (TempPathName)
813
- *TempPathName = TempFile;
799
+ // Add the output file -- but don't try to remove "-", since this means we are
800
+ // using stdin.
801
+ OutputFiles. emplace_back (((OutputPath != " - " ) ? OutputPath : " " ). str (),
802
+ std::move ( TempFile)) ;
814
803
815
804
if (!Binary || OS->supportsSeeking ())
816
805
return std::move (OS);
0 commit comments