|
22 | 22 | #include "swift/AST/NameLookup.h"
|
23 | 23 | #include "swift/AST/ReferencedNameTracker.h"
|
24 | 24 | #include "swift/AST/Types.h"
|
| 25 | +#include "swift/Basic/FileSystem.h" |
25 | 26 | #include "swift/Basic/LLVM.h"
|
26 | 27 | #include "swift/Basic/ReferenceDependencyKeys.h"
|
27 | 28 | #include "swift/Frontend/FrontendOptions.h"
|
@@ -65,11 +66,6 @@ class ReferenceDependenciesEmitter {
|
65 | 66 | llvm::raw_ostream &out);
|
66 | 67 |
|
67 | 68 | private:
|
68 |
| - /// Opens file for reference dependencies. Emits diagnostic if needed. |
69 |
| - /// |
70 |
| - /// \return nullptr on error |
71 |
| - static std::unique_ptr<llvm::raw_fd_ostream> openFile(DiagnosticEngine &diags, |
72 |
| - StringRef OutputPath); |
73 | 69 | /// Emits all the dependency information.
|
74 | 70 | void emit() const;
|
75 | 71 |
|
@@ -189,35 +185,24 @@ static std::string escape(DeclBaseName name) {
|
189 | 185 | return llvm::yaml::escape(name.userFacingName());
|
190 | 186 | }
|
191 | 187 |
|
192 |
| -std::unique_ptr<llvm::raw_fd_ostream> |
193 |
| -ReferenceDependenciesEmitter::openFile(DiagnosticEngine &diags, |
194 |
| - StringRef outputPath) { |
| 188 | +bool ReferenceDependenciesEmitter::emit(DiagnosticEngine &diags, |
| 189 | + SourceFile *const SF, |
| 190 | + const DependencyTracker &depTracker, |
| 191 | + StringRef outputPath) { |
195 | 192 | // Before writing to the dependencies file path, preserve any previous file
|
196 | 193 | // that may have been there. No error handling -- this is just a nicety, it
|
197 | 194 | // doesn't matter if it fails.
|
198 | 195 | llvm::sys::fs::rename(outputPath, outputPath + "~");
|
199 |
| - |
200 |
| - std::error_code EC; |
201 |
| - auto out = llvm::make_unique<llvm::raw_fd_ostream>(outputPath, EC, |
202 |
| - llvm::sys::fs::F_None); |
203 |
| - |
204 |
| - if (out->has_error() || EC) { |
| 196 | + std::error_code EC = |
| 197 | + swift::atomicallyWritingToFile(outputPath, |
| 198 | + [&](llvm::raw_pwrite_stream &out) { |
| 199 | + ReferenceDependenciesEmitter::emit(SF, depTracker, out); |
| 200 | + }); |
| 201 | + if (EC) { |
205 | 202 | diags.diagnose(SourceLoc(), diag::error_opening_output, outputPath,
|
206 | 203 | EC.message());
|
207 |
| - out->clear_error(); |
208 |
| - return nullptr; |
209 |
| - } |
210 |
| - return out; |
211 |
| -} |
212 |
| - |
213 |
| -bool ReferenceDependenciesEmitter::emit(DiagnosticEngine &diags, |
214 |
| - SourceFile *const SF, |
215 |
| - const DependencyTracker &depTracker, |
216 |
| - StringRef outputPath) { |
217 |
| - const std::unique_ptr<llvm::raw_ostream> out = openFile(diags, outputPath); |
218 |
| - if (!out.get()) |
219 | 204 | return true;
|
220 |
| - ReferenceDependenciesEmitter::emit(SF, depTracker, *out); |
| 205 | + } |
221 | 206 | return false;
|
222 | 207 | }
|
223 | 208 |
|
|
0 commit comments