@@ -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.
217223static 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