@@ -268,10 +268,36 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
268268 // / Decode (and cache) a SourceLoc.
269269 FileAndLocation decodeSourceLoc (SourceLoc SL) {
270270 auto &Cached = FileAndLocationCache[SL.getOpaquePointerValue ()];
271- if (!Cached.File ) {
272- Cached = getFileAndLocation (
273- SILLocation::decode (SL, SM, /* ForceGeneratedSourceToDisk=*/ true ));
271+ if (Cached.File )
272+ return Cached;
273+
274+ if (!SL.isValid ()) {
275+ Cached.File = CompilerGeneratedFile;
276+ return Cached;
277+ }
278+
279+ // If the source buffer is a macro, extract its full text.
280+ llvm::Optional<StringRef> Source;
281+ bool ForceGeneratedSourceToDisk = Opts.DWARFVersion < 5 ;
282+ if (!ForceGeneratedSourceToDisk) {
283+ auto BufferID = SM.findBufferContainingLoc (SL);
284+ if (auto generatedInfo = SM.getGeneratedSourceInfo (BufferID)) {
285+ // We only care about macros, so skip everything else.
286+ if (generatedInfo->kind != GeneratedSourceInfo::ReplacedFunctionBody &&
287+ generatedInfo->kind != GeneratedSourceInfo::PrettyPrinted)
288+ if (auto *MemBuf = SM.getLLVMSourceMgr ().getMemoryBuffer (BufferID))
289+ Source = MemBuf->getBuffer ();
290+ }
274291 }
292+ Cached.File = getOrCreateFile (
293+ SM.getDisplayNameForLoc (SL, !ForceGeneratedSourceToDisk), Source);
294+ std::tie (Cached.Line , Cached.Column ) =
295+ SM.getPresumedLineAndColumnForLoc (SL);
296+ // When WinDbg finds two locations with the same line but different
297+ // columns, the user must select an address when they break on that
298+ // line. Also, clang does not emit column locations in CodeView for C++.
299+ if (Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView)
300+ Cached.Column = 0 ;
275301 return Cached;
276302 }
277303
@@ -295,7 +321,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
295321 const DeclContext *DC = D->getDeclContext ()->getModuleScopeContext ();
296322 StringRef Filename = getFilenameFromDC (DC);
297323 if (!Filename.empty ())
298- L.File = getOrCreateFile (Filename);
324+ L.File = getOrCreateFile (Filename, {} );
299325 return L;
300326 }
301327
@@ -306,7 +332,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
306332 // line. Also, clang does not emit column locations in CodeView for C++.
307333 bool CodeView = Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView;
308334 return {FL.line , CodeView ? (uint16_t )0 : FL.column ,
309- getOrCreateFile (FL.filename )};
335+ getOrCreateFile (FL.filename , {} )};
310336 }
311337
312338 // / Use the Swift SM to figure out the actual line/column of a SourceLoc.
@@ -345,7 +371,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
345371 return L;
346372 L.Line = PresumedLoc.getLine ();
347373 L.Column = PresumedLoc.getColumn ();
348- L.File = getOrCreateFile (PresumedLoc.getFilename ());
374+ L.File = getOrCreateFile (PresumedLoc.getFilename (), {} );
349375 return L;
350376 }
351377 return getSwiftFileAndLocation (D, End);
@@ -457,7 +483,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
457483
458484#endif
459485
460- llvm::DIFile *getOrCreateFile (StringRef Filename) {
486+ llvm::DIFile *getOrCreateFile (StringRef Filename,
487+ llvm::Optional<StringRef> Source) {
461488 if (Filename.empty ())
462489 Filename = SILLocation::getCompilerGeneratedLoc ()->filename ;
463490
@@ -486,7 +513,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
486513 }
487514 }
488515
489- return createFile (Filename, llvm::None, llvm::None );
516+ return createFile (Filename, llvm::None, Source );
490517 }
491518
492519 // / This is effectively \p clang::CGDebugInfo::createFile().
@@ -2077,7 +2104,7 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
20772104 MainFile = (RelFile && RelDir)
20782105 ? createFile (SourcePath, {}, {})
20792106 : DBuilder.createFile (RemappedFile, RemappedDir);
2080- CompilerGeneratedFile = getOrCreateFile (" " );
2107+ CompilerGeneratedFile = getOrCreateFile (" " , {} );
20812108
20822109 StringRef Sysroot = IGM.Context .SearchPathOpts .getSDKPath ();
20832110 StringRef SDK;
@@ -2287,7 +2314,7 @@ void IRGenDebugInfoImpl::addFailureMessageToCurrentLoc(IRBuilder &Builder,
22872314 else {
22882315 std::string FuncName = " Swift runtime failure: " ;
22892316 FuncName += failureMsg;
2290- llvm::DIFile *File = getOrCreateFile ({});
2317+ llvm::DIFile *File = getOrCreateFile ({}, {} );
22912318 TrapSP = DBuilder.createFunction (
22922319 File, FuncName, StringRef (), File, 0 ,
22932320 DIFnTy, 0 , llvm::DINode::FlagArtificial,
0 commit comments