Skip to content

Commit 3b6a9c7

Browse files
committed
[Source manager] Use macro buffer names when writing files to disk.
Use the uniquenes of mangled names to keep macro expansion buffers around.
1 parent 2e96587 commit 3b6a9c7

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

lib/Basic/SourceLoc.cpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -192,27 +192,29 @@ SourceManager::getIDForBufferIdentifier(StringRef BufIdentifier) const {
192192
static Optional<std::string>
193193
dumpBufferToFile(const llvm::MemoryBuffer *buffer) {
194194
// 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))
204199
return None;
205200

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());
213204

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();
216218
}
217219

218220
StringRef SourceManager::getIdentifierForBuffer(
@@ -225,6 +227,11 @@ StringRef SourceManager::getIdentifierForBuffer(
225227
// so external clients can see it, do so now.
226228
if (ForceGeneratedSourceToDisk) {
227229
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+
228235
if (generatedInfo->onDiskBufferCopyFileName.empty()) {
229236
if (auto newFileNameOpt = dumpBufferToFile(buffer)) {
230237
generatedInfo->onDiskBufferCopyFileName =

test/Macros/macro_expand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func testFileID(a: Int, b: Int) {
3131
// CHECK-SIL: sil_scope [[SRC_SCOPE:[0-9]+]] { loc "{{.*}}macro_expand.swift":[[@LINE-2]]
3232
// CHECK-SIL: sil_scope {{[0-9]+}} { loc "{{.*}}":1:1 parent [[MACRO_SCOPE]] inlined_at [[SRC_SCOPE]] }
3333
34-
34+
3535
// CHECK: Builtin result is MacroUser/macro_expand.swift
3636
// CHECK-AST: macro_expansion_expr type='String'{{.*}}name=line
3737
print("Builtin result is \(#fileID)")

0 commit comments

Comments
 (0)