Skip to content

Commit c085f5a

Browse files
committed
Record original source range of generated source buffers into dumped files
This helps us establish where the code from a generated source buffer would go, for external tools that don't understand serialized diagnostics.
1 parent 956e81c commit c085f5a

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

include/swift/Basic/SourceManager.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class SourceManager {
129129
SourceManager(llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS =
130130
llvm::vfs::getRealFileSystem())
131131
: FileSystem(FS) {}
132+
~SourceManager();
132133

133134
llvm::SourceMgr &getLLVMSourceMgr() {
134135
return LLVMSourceMgr;

lib/Basic/SourceLoc.cpp

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,18 @@ SourceManager::getIDForBufferIdentifier(StringRef BufIdentifier) const {
212212
return It->second;
213213
}
214214

215+
SourceManager::~SourceManager() {
216+
for (auto &generated : GeneratedSourceInfos) {
217+
free((void*)generated.second.onDiskBufferCopyFileName.data());
218+
}
219+
}
220+
215221
/// Dump the contents of the given memory buffer to a file, returning the
216222
/// name of that file (when successful) and \c None otherwise.
217223
static Optional<std::string>
218-
dumpBufferToFile(const llvm::MemoryBuffer *buffer) {
224+
dumpBufferToFile(const llvm::MemoryBuffer *buffer,
225+
const SourceManager &sourceMgr,
226+
SourceRange originalSourceRange) {
219227
// Create file in the system temporary directory.
220228
SmallString<128> outputFileName;
221229
llvm::sys::path::system_temp_directory(true, outputFileName);
@@ -232,9 +240,30 @@ dumpBufferToFile(const llvm::MemoryBuffer *buffer) {
232240
auto contents = buffer->getBuffer();
233241
out << contents;
234242

235-
// Make sure we have a trailing newline.
236-
if (contents.empty() || contents.back() != '\n')
237-
out << "\n";
243+
// Make sure we have a trailing newline.
244+
if (contents.empty() || contents.back() != '\n')
245+
out << "\n";
246+
247+
// If we know the source range this comes from, append it later in
248+
// the file so one can trace.
249+
if (originalSourceRange.isValid()) {
250+
out << "\n";
251+
252+
auto originalFilename =
253+
sourceMgr.getDisplayNameForLoc(originalSourceRange.Start, true);
254+
unsigned startLine, startColumn, endLine, endColumn;
255+
std::tie(startLine, startColumn) =
256+
sourceMgr.getPresumedLineAndColumnForLoc(
257+
originalSourceRange.Start);
258+
std::tie(endLine, endColumn) =
259+
sourceMgr.getPresumedLineAndColumnForLoc(
260+
originalSourceRange.End);
261+
out << "// original-source-range: "
262+
<< originalFilename
263+
<< ":" << startLine << ":" << startColumn
264+
<< "-" << endLine << ":" << endColumn
265+
<< "\n";
266+
}
238267
});
239268
if (ec)
240269
return None;
@@ -258,7 +287,8 @@ StringRef SourceManager::getIdentifierForBuffer(
258287
return buffer->getBufferIdentifier();
259288

260289
if (generatedInfo->onDiskBufferCopyFileName.empty()) {
261-
if (auto newFileNameOpt = dumpBufferToFile(buffer)) {
290+
if (auto newFileNameOpt = dumpBufferToFile(
291+
buffer, *this, generatedInfo->originalSourceRange)) {
262292
generatedInfo->onDiskBufferCopyFileName =
263293
strdup(newFileNameOpt->c_str());
264294
}

0 commit comments

Comments
 (0)