diff options
author | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2020-11-20 18:04:32 -0800 |
---|---|---|
committer | Duncan P. N. Exon Smith <dexonsmith@apple.com> | 2021-01-26 15:56:19 -0800 |
commit | ad7aaa475e5e632242b07380ec47d2f35d077209 (patch) | |
tree | 7633c37dced0ea3e786f9fa47ee048e0a04dee63 /clang/lib/Frontend/CompilerInstance.cpp | |
parent | 4d28f0a6a4036388694bb83aebc0db9334b82f6e (diff) | |
download | llvm-ad7aaa475e5e632242b07380ec47d2f35d077209.zip llvm-ad7aaa475e5e632242b07380ec47d2f35d077209.tar.gz llvm-ad7aaa475e5e632242b07380ec47d2f35d077209.tar.bz2 |
Frontend: Fix layering between create{,Default}OutputFile, NFC
Fix layering between `CompilerInstance::createDefaultOutputFile` and the
two versions of `createOutputFile`.
- Add missing configuration flags to `createDefaultOutputFile` so that
GeneratePCHAction and GenerateModuleFromModuleMapAction can use it.
They previously promised that temporary files were turned on; now
`createDefaultOutputFile` handles that logic.
- Lift the logic handling `InFile` and `Extension` to
`createDefaultOutputFile`, since it's only the callers of that
function that are using it.
- Rename the deeper of the two `createOutputFile`s to
`createOutputFileImpl` and make it private to `CompilerInstance` (to
prove that no one else is using it).
- Sink the logic for adding to `CompilerInstance::OutputFiles` down to
`createOutputFileImpl`, allowing two "optional" (but always used)
`std::string*` out parameters to be removed.
- Instead of passing a `std::error_code` out parameter into
`createOutputFileImpl`, have it return `Expected<>`.
- As a drive-by, inline `CompilerInstance::addOutputFile` into its only
caller, `createOutputFileImpl`.
Clean layering makes it easier for a future commit to extract
`createOutputFileImpl` out of `CompilerInstance`.
Differential Revision: https://reviews.llvm.org/D93248
Diffstat (limited to 'clang/lib/Frontend/CompilerInstance.cpp')
-rw-r--r-- | clang/lib/Frontend/CompilerInstance.cpp | 113 |
1 files changed, 51 insertions, 62 deletions
diff --git a/clang/lib/Frontend/CompilerInstance.cpp b/clang/lib/Frontend/CompilerInstance.cpp index d60a0e8..8afd9b3 100644 --- a/clang/lib/Frontend/CompilerInstance.cpp +++ b/clang/lib/Frontend/CompilerInstance.cpp @@ -646,10 +646,6 @@ void CompilerInstance::createSema(TranslationUnitKind TUKind, // Output Files -void CompilerInstance::addOutputFile(OutputFile &&OutFile) { - OutputFiles.push_back(std::move(OutFile)); -} - void CompilerInstance::clearOutputFiles(bool EraseFiles) { for (OutputFile &OF : OutputFiles) { if (!OF.TempFilename.empty()) { @@ -682,10 +678,25 @@ void CompilerInstance::clearOutputFiles(bool EraseFiles) { std::unique_ptr<raw_pwrite_stream> CompilerInstance::createDefaultOutputFile(bool Binary, StringRef InFile, - StringRef Extension) { - return createOutputFile(getFrontendOpts().OutputFile, Binary, - /*RemoveFileOnSignal=*/true, InFile, Extension, - getFrontendOpts().UseTemporary); + StringRef Extension, + bool RemoveFileOnSignal, + bool CreateMissingDirectories) { + StringRef OutputPath = getFrontendOpts().OutputFile; + Optional<SmallString<128>> PathStorage; + if (OutputPath.empty()) { + if (InFile == "-" || Extension.empty()) { + OutputPath = "-"; + } else { + PathStorage.emplace(InFile); + llvm::sys::path::replace_extension(*PathStorage, Extension); + OutputPath = *PathStorage; + } + } + + // Force a temporary file if RemoveFileOnSignal was disabled. + return createOutputFile(OutputPath, Binary, RemoveFileOnSignal, + getFrontendOpts().UseTemporary || !RemoveFileOnSignal, + CreateMissingDirectories); } std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() { @@ -694,64 +705,40 @@ std::unique_ptr<raw_pwrite_stream> CompilerInstance::createNullOutputFile() { std::unique_ptr<raw_pwrite_stream> CompilerInstance::createOutputFile(StringRef OutputPath, bool Binary, - bool RemoveFileOnSignal, StringRef InFile, - StringRef Extension, bool UseTemporary, + bool RemoveFileOnSignal, bool UseTemporary, bool CreateMissingDirectories) { - std::string OutputPathName, TempPathName; - std::error_code EC; - std::unique_ptr<raw_pwrite_stream> OS = createOutputFile( - OutputPath, EC, Binary, RemoveFileOnSignal, InFile, Extension, - UseTemporary, CreateMissingDirectories, &OutputPathName, &TempPathName); - if (!OS) { - getDiagnostics().Report(diag::err_fe_unable_to_open_output) << OutputPath - << EC.message(); - return nullptr; - } - - // Add the output file -- but don't try to remove "-", since this means we are - // using stdin. - addOutputFile( - OutputFile((OutputPathName != "-") ? OutputPathName : "", TempPathName)); - - return OS; + Expected<std::unique_ptr<raw_pwrite_stream>> OS = + createOutputFileImpl(OutputPath, Binary, RemoveFileOnSignal, UseTemporary, + CreateMissingDirectories); + if (OS) + return std::move(*OS); + getDiagnostics().Report(diag::err_fe_unable_to_open_output) + << OutputPath << errorToErrorCode(OS.takeError()).message(); + return nullptr; } -std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile( - StringRef OutputPath, std::error_code &Error, bool Binary, - bool RemoveFileOnSignal, StringRef InFile, StringRef Extension, - bool UseTemporary, bool CreateMissingDirectories, - std::string *ResultPathName, std::string *TempPathName) { +Expected<std::unique_ptr<llvm::raw_pwrite_stream>> +CompilerInstance::createOutputFileImpl(StringRef OutputPath, bool Binary, + bool RemoveFileOnSignal, + bool UseTemporary, + bool CreateMissingDirectories) { assert((!CreateMissingDirectories || UseTemporary) && "CreateMissingDirectories is only allowed when using temporary files"); - std::string OutFile, TempFile; - if (!OutputPath.empty()) { - OutFile = std::string(OutputPath); - } else if (InFile == "-") { - OutFile = "-"; - } else if (!Extension.empty()) { - SmallString<128> Path(InFile); - llvm::sys::path::replace_extension(Path, Extension); - OutFile = std::string(Path.str()); - } else { - OutFile = "-"; - } - std::unique_ptr<llvm::raw_fd_ostream> OS; - std::string OSFile; + Optional<StringRef> OSFile; if (UseTemporary) { - if (OutFile == "-") + if (OutputPath == "-") UseTemporary = false; else { llvm::sys::fs::file_status Status; llvm::sys::fs::status(OutputPath, Status); if (llvm::sys::fs::exists(Status)) { // Fail early if we can't write to the final destination. - if (!llvm::sys::fs::can_write(OutputPath)) { - Error = make_error_code(llvm::errc::operation_not_permitted); - return nullptr; - } + if (!llvm::sys::fs::can_write(OutputPath)) + return llvm::errorCodeToError( + make_error_code(llvm::errc::operation_not_permitted)); // Don't use a temporary if the output is a special file. This handles // things like '-o /dev/null' @@ -761,14 +748,15 @@ std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile( } } + std::string TempFile; if (UseTemporary) { // Create a temporary file. // Insert -%%%%%%%% before the extension (if any), and because some tools // (noticeable, clang's own GlobalModuleIndex.cpp) glob for build // artifacts, also append .tmp. - StringRef OutputExtension = llvm::sys::path::extension(OutFile); + StringRef OutputExtension = llvm::sys::path::extension(OutputPath); SmallString<128> TempPath = - StringRef(OutFile).drop_back(OutputExtension.size()); + StringRef(OutputPath).drop_back(OutputExtension.size()); TempPath += "-%%%%%%%%"; TempPath += OutputExtension; TempPath += ".tmp"; @@ -795,22 +783,23 @@ std::unique_ptr<llvm::raw_pwrite_stream> CompilerInstance::createOutputFile( } if (!OS) { - OSFile = OutFile; + OSFile = OutputPath; + std::error_code EC; OS.reset(new llvm::raw_fd_ostream( - OSFile, Error, + *OSFile, EC, (Binary ? llvm::sys::fs::OF_None : llvm::sys::fs::OF_Text))); - if (Error) - return nullptr; + if (EC) + return llvm::errorCodeToError(EC); } // Make sure the out stream file gets removed if we crash. if (RemoveFileOnSignal) - llvm::sys::RemoveFileOnSignal(OSFile); + llvm::sys::RemoveFileOnSignal(*OSFile); - if (ResultPathName) - *ResultPathName = OutFile; - if (TempPathName) - *TempPathName = TempFile; + // Add the output file -- but don't try to remove "-", since this means we are + // using stdin. + OutputFiles.emplace_back(((OutputPath != "-") ? OutputPath : "").str(), + std::move(TempFile)); if (!Binary || OS->supportsSeeking()) return std::move(OS); |