@@ -212,10 +212,18 @@ SourceManager::getIDForBufferIdentifier(StringRef BufIdentifier) const {
212
212
return It->second ;
213
213
}
214
214
215
+ SourceManager::~SourceManager () {
216
+ for (auto &generated : GeneratedSourceInfos) {
217
+ free ((void *)generated.second .onDiskBufferCopyFileName .data ());
218
+ }
219
+ }
220
+
215
221
// / Dump the contents of the given memory buffer to a file, returning the
216
222
// / name of that file (when successful) and \c None otherwise.
217
223
static Optional<std::string>
218
- dumpBufferToFile (const llvm::MemoryBuffer *buffer) {
224
+ dumpBufferToFile (const llvm::MemoryBuffer *buffer,
225
+ const SourceManager &sourceMgr,
226
+ SourceRange originalSourceRange) {
219
227
// Create file in the system temporary directory.
220
228
SmallString<128 > outputFileName;
221
229
llvm::sys::path::system_temp_directory (true , outputFileName);
@@ -232,9 +240,30 @@ dumpBufferToFile(const llvm::MemoryBuffer *buffer) {
232
240
auto contents = buffer->getBuffer ();
233
241
out << contents;
234
242
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
+ }
238
267
});
239
268
if (ec)
240
269
return None;
@@ -258,7 +287,8 @@ StringRef SourceManager::getIdentifierForBuffer(
258
287
return buffer->getBufferIdentifier ();
259
288
260
289
if (generatedInfo->onDiskBufferCopyFileName .empty ()) {
261
- if (auto newFileNameOpt = dumpBufferToFile (buffer)) {
290
+ if (auto newFileNameOpt = dumpBufferToFile (
291
+ buffer, *this , generatedInfo->originalSourceRange )) {
262
292
generatedInfo->onDiskBufferCopyFileName =
263
293
strdup (newFileNameOpt->c_str ());
264
294
}
0 commit comments