[ModuleInterface] Factor out common AST-layer withOutputFile helper.

This commit is contained in:
Graydon Hoare
2018-10-18 00:29:31 -07:00
parent dc006e883a
commit fa95f7aebd
4 changed files with 59 additions and 52 deletions

View File

@@ -29,6 +29,7 @@
#include "swift/AST/ASTScope.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/FileSystem.h"
#include "swift/AST/GenericSignatureBuilder.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/AST/ASTMangler.h"
@@ -312,30 +313,6 @@ static bool writeSIL(SILModule &SM, const PrimarySpecificPaths &PSPs,
PSPs.OutputFilename, opts.EmitSortedSIL);
}
/// A wrapper around swift::atomicallyWritingToFile that handles diagnosing any
/// filesystem errors and ignores empty output paths.
///
/// \returns true if there were any errors, either from the filesystem
/// operations or from \p action returning true.
static bool atomicallyWritingToTextFile(
StringRef outputPath, DiagnosticEngine &diags,
llvm::function_ref<bool(llvm::raw_pwrite_stream &)> action) {
assert(!outputPath.empty());
bool actionFailed = false;
std::error_code EC =
swift::atomicallyWritingToFile(outputPath,
[&](llvm::raw_pwrite_stream &out) {
actionFailed = action(out);
});
if (EC) {
diags.diagnose(SourceLoc(), diag::error_opening_output,
outputPath, EC.message());
return true;
}
return actionFailed;
}
/// Prints the Objective-C "generated header" interface for \p M to \p
/// outputPath.
///
@@ -348,8 +325,8 @@ static bool printAsObjCIfNeeded(StringRef outputPath, ModuleDecl *M,
StringRef bridgingHeader, bool moduleIsPublic) {
if (outputPath.empty())
return false;
return atomicallyWritingToTextFile(outputPath, M->getDiags(),
[&](raw_ostream &out) -> bool {
return withOutputFile(M->getDiags(), outputPath,
[&](raw_ostream &out) -> bool {
auto requiredAccess = moduleIsPublic ? AccessLevel::Public
: AccessLevel::Internal;
return printAsObjC(out, M, bridgingHeader, requiredAccess);
@@ -368,8 +345,8 @@ static bool printParseableInterfaceIfNeeded(StringRef outputPath,
ModuleDecl *M) {
if (outputPath.empty())
return false;
return atomicallyWritingToTextFile(outputPath, M->getDiags(),
[M, Opts](raw_ostream &out) -> bool {
return withOutputFile(M->getDiags(), outputPath,
[M, Opts](raw_ostream &out) -> bool {
return swift::emitParseableInterface(out, Opts, M);
});
}