Skip to content

Commit fa95f7a

Browse files
committed
[ModuleInterface] Factor out common AST-layer withOutputFile helper.
1 parent dc006e8 commit fa95f7a

File tree

4 files changed

+59
-52
lines changed

4 files changed

+59
-52
lines changed

include/swift/AST/FileSystem.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===--- FileSystem.h - File helpers that interact with Diags ---*- C++ -*-===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef SWIFT_AST_FILESYSTEM_H
14+
#define SWIFT_AST_FILESYSTEM_H
15+
16+
#include "swift/Basic/FileSystem.h"
17+
#include "swift/AST/DiagnosticEngine.h"
18+
19+
namespace swift {
20+
21+
/// A wrapper around swift::atomicallyWritingToFile that handles diagnosing any
22+
/// filesystem errors and asserts the output path is nonempty.
23+
///
24+
/// \returns true if there were any errors, either from the filesystem
25+
/// operations or from \p action returning true.
26+
inline bool
27+
withOutputFile(DiagnosticEngine &diags, StringRef outputPath,
28+
llvm::function_ref<bool(llvm::raw_pwrite_stream &)> action) {
29+
assert(!outputPath.empty());
30+
31+
bool actionFailed = false;
32+
std::error_code EC = swift::atomicallyWritingToFile(
33+
outputPath,
34+
[&](llvm::raw_pwrite_stream &out) { actionFailed = action(out); });
35+
if (EC) {
36+
diags.diagnose(SourceLoc(), diag::error_opening_output, outputPath,
37+
EC.message());
38+
return true;
39+
}
40+
return actionFailed;
41+
}
42+
} // end namespace swift
43+
44+
#endif // SWIFT_AST_FILESYSTEM_H

lib/FrontendTool/FrontendTool.cpp

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "swift/AST/ASTScope.h"
3030
#include "swift/AST/DiagnosticsFrontend.h"
3131
#include "swift/AST/DiagnosticsSema.h"
32+
#include "swift/AST/FileSystem.h"
3233
#include "swift/AST/GenericSignatureBuilder.h"
3334
#include "swift/AST/IRGenOptions.h"
3435
#include "swift/AST/ASTMangler.h"
@@ -312,30 +313,6 @@ static bool writeSIL(SILModule &SM, const PrimarySpecificPaths &PSPs,
312313
PSPs.OutputFilename, opts.EmitSortedSIL);
313314
}
314315

315-
/// A wrapper around swift::atomicallyWritingToFile that handles diagnosing any
316-
/// filesystem errors and ignores empty output paths.
317-
///
318-
/// \returns true if there were any errors, either from the filesystem
319-
/// operations or from \p action returning true.
320-
static bool atomicallyWritingToTextFile(
321-
StringRef outputPath, DiagnosticEngine &diags,
322-
llvm::function_ref<bool(llvm::raw_pwrite_stream &)> action) {
323-
assert(!outputPath.empty());
324-
325-
bool actionFailed = false;
326-
std::error_code EC =
327-
swift::atomicallyWritingToFile(outputPath,
328-
[&](llvm::raw_pwrite_stream &out) {
329-
actionFailed = action(out);
330-
});
331-
if (EC) {
332-
diags.diagnose(SourceLoc(), diag::error_opening_output,
333-
outputPath, EC.message());
334-
return true;
335-
}
336-
return actionFailed;
337-
}
338-
339316
/// Prints the Objective-C "generated header" interface for \p M to \p
340317
/// outputPath.
341318
///
@@ -348,8 +325,8 @@ static bool printAsObjCIfNeeded(StringRef outputPath, ModuleDecl *M,
348325
StringRef bridgingHeader, bool moduleIsPublic) {
349326
if (outputPath.empty())
350327
return false;
351-
return atomicallyWritingToTextFile(outputPath, M->getDiags(),
352-
[&](raw_ostream &out) -> bool {
328+
return withOutputFile(M->getDiags(), outputPath,
329+
[&](raw_ostream &out) -> bool {
353330
auto requiredAccess = moduleIsPublic ? AccessLevel::Public
354331
: AccessLevel::Internal;
355332
return printAsObjC(out, M, bridgingHeader, requiredAccess);
@@ -368,8 +345,8 @@ static bool printParseableInterfaceIfNeeded(StringRef outputPath,
368345
ModuleDecl *M) {
369346
if (outputPath.empty())
370347
return false;
371-
return atomicallyWritingToTextFile(outputPath, M->getDiags(),
372-
[M, Opts](raw_ostream &out) -> bool {
348+
return withOutputFile(M->getDiags(), outputPath,
349+
[M, Opts](raw_ostream &out) -> bool {
373350
return swift::emitParseableInterface(out, Opts, M);
374351
});
375352
}

lib/FrontendTool/ReferenceDependencies.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/DiagnosticEngine.h"
1818
#include "swift/AST/DiagnosticsFrontend.h"
1919
#include "swift/AST/ExistentialLayout.h"
20+
#include "swift/AST/FileSystem.h"
2021
#include "swift/AST/Module.h"
2122
#include "swift/AST/ModuleLoader.h"
2223
#include "swift/AST/NameLookup.h"
@@ -193,17 +194,10 @@ bool ReferenceDependenciesEmitter::emit(DiagnosticEngine &diags,
193194
// that may have been there. No error handling -- this is just a nicety, it
194195
// doesn't matter if it fails.
195196
llvm::sys::fs::rename(outputPath, outputPath + "~");
196-
std::error_code EC =
197-
swift::atomicallyWritingToFile(outputPath,
198-
[&](llvm::raw_pwrite_stream &out) {
197+
return withOutputFile(diags, outputPath, [&](llvm::raw_pwrite_stream &out) {
199198
ReferenceDependenciesEmitter::emit(SF, depTracker, out);
199+
return false;
200200
});
201-
if (EC) {
202-
diags.diagnose(SourceLoc(), diag::error_opening_output, outputPath,
203-
EC.message());
204-
return true;
205-
}
206-
return false;
207201
}
208202

209203
void ReferenceDependenciesEmitter::emit(SourceFile *SF,

lib/Serialization/Serialization.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/ASTWalker.h"
1818
#include "swift/AST/DiagnosticsCommon.h"
1919
#include "swift/AST/Expr.h"
20+
#include "swift/AST/FileSystem.h"
2021
#include "swift/AST/ForeignErrorConvention.h"
2122
#include "swift/AST/GenericEnvironment.h"
2223
#include "swift/AST/Initializer.h"
@@ -5158,19 +5159,6 @@ void Serializer::writeDocToStream(raw_ostream &os, ModuleOrSourceFile DC,
51585159
S.writeToStream(os);
51595160
}
51605161

5161-
static inline bool
5162-
withOutputFile(ASTContext &ctx, StringRef outputPath,
5163-
llvm::function_ref<void(raw_ostream &)> action){
5164-
std::error_code EC = swift::atomicallyWritingToFile(outputPath,
5165-
action);
5166-
if (!EC)
5167-
return false;
5168-
5169-
ctx.Diags.diagnose(SourceLoc(), diag::error_opening_output,
5170-
outputPath, EC.message());
5171-
return true;
5172-
}
5173-
51745162
void swift::serialize(ModuleOrSourceFile DC,
51755163
const SerializationOptions &options,
51765164
const SILModule *M) {
@@ -5183,20 +5171,24 @@ void swift::serialize(ModuleOrSourceFile DC,
51835171
return;
51845172
}
51855173

5186-
bool hadError = withOutputFile(getContext(DC), options.OutputPath,
5174+
bool hadError = withOutputFile(getContext(DC).Diags,
5175+
options.OutputPath,
51875176
[&](raw_ostream &out) {
51885177
SharedTimer timer("Serialization, swiftmodule");
51895178
Serializer::writeToStream(out, DC, M, options);
5179+
return false;
51905180
});
51915181
if (hadError)
51925182
return;
51935183

51945184
if (options.DocOutputPath && options.DocOutputPath[0] != '\0') {
5195-
(void)withOutputFile(getContext(DC), options.DocOutputPath,
5185+
(void)withOutputFile(getContext(DC).Diags,
5186+
options.DocOutputPath,
51965187
[&](raw_ostream &out) {
51975188
SharedTimer timer("Serialization, swiftdoc");
51985189
Serializer::writeDocToStream(out, DC, options.GroupInfoPath,
51995190
getContext(DC));
5191+
return false;
52005192
});
52015193
}
52025194
}

0 commit comments

Comments
 (0)