@@ -268,10 +268,36 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
268
268
// / Decode (and cache) a SourceLoc.
269
269
FileAndLocation decodeSourceLoc (SourceLoc SL) {
270
270
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
+ }
274
291
}
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 ;
275
301
return Cached;
276
302
}
277
303
@@ -295,7 +321,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
295
321
const DeclContext *DC = D->getDeclContext ()->getModuleScopeContext ();
296
322
StringRef Filename = getFilenameFromDC (DC);
297
323
if (!Filename.empty ())
298
- L.File = getOrCreateFile (Filename);
324
+ L.File = getOrCreateFile (Filename, {} );
299
325
return L;
300
326
}
301
327
@@ -306,7 +332,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
306
332
// line. Also, clang does not emit column locations in CodeView for C++.
307
333
bool CodeView = Opts.DebugInfoFormat == IRGenDebugInfoFormat::CodeView;
308
334
return {FL.line , CodeView ? (uint16_t )0 : FL.column ,
309
- getOrCreateFile (FL.filename )};
335
+ getOrCreateFile (FL.filename , {} )};
310
336
}
311
337
312
338
// / Use the Swift SM to figure out the actual line/column of a SourceLoc.
@@ -345,7 +371,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
345
371
return L;
346
372
L.Line = PresumedLoc.getLine ();
347
373
L.Column = PresumedLoc.getColumn ();
348
- L.File = getOrCreateFile (PresumedLoc.getFilename ());
374
+ L.File = getOrCreateFile (PresumedLoc.getFilename (), {} );
349
375
return L;
350
376
}
351
377
return getSwiftFileAndLocation (D, End);
@@ -457,7 +483,8 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
457
483
458
484
#endif
459
485
460
- llvm::DIFile *getOrCreateFile (StringRef Filename) {
486
+ llvm::DIFile *getOrCreateFile (StringRef Filename,
487
+ llvm::Optional<StringRef> Source) {
461
488
if (Filename.empty ())
462
489
Filename = SILLocation::getCompilerGeneratedLoc ()->filename ;
463
490
@@ -486,7 +513,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
486
513
}
487
514
}
488
515
489
- return createFile (Filename, llvm::None, llvm::None );
516
+ return createFile (Filename, llvm::None, Source );
490
517
}
491
518
492
519
// / This is effectively \p clang::CGDebugInfo::createFile().
@@ -2077,7 +2104,7 @@ IRGenDebugInfoImpl::IRGenDebugInfoImpl(const IRGenOptions &Opts,
2077
2104
MainFile = (RelFile && RelDir)
2078
2105
? createFile (SourcePath, {}, {})
2079
2106
: DBuilder.createFile (RemappedFile, RemappedDir);
2080
- CompilerGeneratedFile = getOrCreateFile (" " );
2107
+ CompilerGeneratedFile = getOrCreateFile (" " , {} );
2081
2108
2082
2109
StringRef Sysroot = IGM.Context .SearchPathOpts .getSDKPath ();
2083
2110
StringRef SDK;
@@ -2287,7 +2314,7 @@ void IRGenDebugInfoImpl::addFailureMessageToCurrentLoc(IRBuilder &Builder,
2287
2314
else {
2288
2315
std::string FuncName = " Swift runtime failure: " ;
2289
2316
FuncName += failureMsg;
2290
- llvm::DIFile *File = getOrCreateFile ({});
2317
+ llvm::DIFile *File = getOrCreateFile ({}, {} );
2291
2318
TrapSP = DBuilder.createFunction (
2292
2319
File, FuncName, StringRef (), File, 0 ,
2293
2320
DIFnTy, 0 , llvm::DINode::FlagArtificial,
0 commit comments