@@ -192,27 +192,29 @@ SourceManager::getIDForBufferIdentifier(StringRef BufIdentifier) const {
192
192
static Optional<std::string>
193
193
dumpBufferToFile (const llvm::MemoryBuffer *buffer) {
194
194
// Create file in the system temporary directory.
195
- SmallString<128 > tempFileModel;
196
- llvm::sys::path::system_temp_directory (true , tempFileModel);
197
- llvm::sys::path::append (
198
- tempFileModel, " swift-generated-sources-%%%%%%.swift" );
199
-
200
- // Open up a unique file.
201
- int tempFD = 0 ;
202
- SmallString<128 > tempFileName;
203
- if (llvm::sys::fs::createUniqueFile (tempFileModel, tempFD, tempFileName))
195
+ SmallString<128 > outputFileName;
196
+ llvm::sys::path::system_temp_directory (true , outputFileName);
197
+ llvm::sys::path::append (outputFileName, " swift-generated-sources" );
198
+ if (llvm::sys::fs::create_directory (outputFileName))
204
199
return None;
205
200
206
- // Dump the contents there.
207
- auto contents = buffer->getBuffer ();
208
- llvm::raw_fd_ostream out (tempFD, true );
209
- out << contents;
210
- if (contents.empty () || contents.back () != ' \n ' )
211
- out << " \n " ;
212
- out.flush ();
201
+ // Finalize the name of the resulting file. This is unique based on name
202
+ // mangling.
203
+ llvm::sys::path::append (outputFileName, buffer->getBufferIdentifier ());
213
204
214
- llvm::sys::DontRemoveFileOnSignal (tempFileName);
215
- return tempFileName.str ().str ();
205
+ std::error_code ec = atomicallyWritingToFile (outputFileName,
206
+ [&](llvm::raw_pwrite_stream &out) {
207
+ auto contents = buffer->getBuffer ();
208
+ out << contents;
209
+
210
+ // Make sure we have a trailing newline.
211
+ if (contents.empty () || contents.back () != ' \n ' )
212
+ out << " \n " ;
213
+ });
214
+ if (ec)
215
+ return None;
216
+
217
+ return outputFileName.str ().str ();
216
218
}
217
219
218
220
StringRef SourceManager::getIdentifierForBuffer (
@@ -225,6 +227,11 @@ StringRef SourceManager::getIdentifierForBuffer(
225
227
// so external clients can see it, do so now.
226
228
if (ForceGeneratedSourceToDisk) {
227
229
if (auto generatedInfo = getGeneratedSourceInfo (bufferID)) {
230
+ // We only care about macros, so skip everything else.
231
+ if (generatedInfo->kind == GeneratedSourceInfo::ReplacedFunctionBody ||
232
+ generatedInfo->kind == GeneratedSourceInfo::PrettyPrinted)
233
+ return buffer->getBufferIdentifier ();
234
+
228
235
if (generatedInfo->onDiskBufferCopyFileName .empty ()) {
229
236
if (auto newFileNameOpt = dumpBufferToFile (buffer)) {
230
237
generatedInfo->onDiskBufferCopyFileName =
0 commit comments