Skip to content

Commit db0a85f

Browse files
committed
[Debug Info] Store the source code for macro expansions in the debug info
This patch stores the source code of macro in the source field of DIFile, which can be used together with the DW_LLVM_LNCT_source extension to emit the source code into DWARF debug info. rdar://110926109
1 parent e4dd71b commit db0a85f

File tree

2 files changed

+42
-14
lines changed

2 files changed

+42
-14
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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,

test/Macros/macro_expand.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
// RUN: %target-swift-frontend -swift-version 5 -emit-sil -load-plugin-library %t/%target-library-name(MacroDefinition) %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-SIL %s
2525

2626
// Debug info IR testing
27-
// RUN: %target-swift-frontend -swift-version 5 -emit-ir -load-plugin-library %t/%target-library-name(MacroDefinition) %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-IR %s
27+
// RUN: %target-swift-frontend -swift-version 5 -dwarf-version 5 -emit-ir -load-plugin-library %t/%target-library-name(MacroDefinition) %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-IR %s
2828

2929
// Execution testing
3030
// RUN: %target-build-swift -swift-version 5 -g -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser -Xfrontend -emit-dependencies-path -Xfrontend %t/main.d -Xfrontend -emit-reference-dependencies-path -Xfrontend %t/main.swiftdeps
@@ -163,10 +163,11 @@ func testFileID(a: Int, b: Int) {
163163
// CHECK-AST: macro_expansion_expr type='String'{{.*}}name=line
164164
print("Builtin result is \(#fileID)")
165165
print(
166-
/// CHECK-IR-DAG: ![[L1:[0-9]+]] = distinct !DILocation(line: [[@LINE+1]], column: 5
166+
// CHECK-IR-DAG: ![[L1:[0-9]+]] = distinct !DILocation(line: [[@LINE+1]], column: 5
167167
#addBlocker(
168-
/// CHECK-IR-DAG: ![[L2:[0-9]+]] = distinct !DILocation({{.*}}inlinedAt: ![[L1]])
169-
/// CHECK-IR-DAG: ![[L3:[0-9]+]] = !DILocation({{.*}}inlinedAt: ![[L2]])
168+
// CHECK-IR-DAG: ![[L2:[0-9]+]] = distinct !DILocation({{.*}}inlinedAt: ![[L1]])
169+
// CHECK-IR-DAG: !DIFile(filename: "@__swiftmacro_9MacroUser10testFileID{{.*}}", {{.*}}source: "\22MacroUser/macro_expand.swift\22")
170+
170171
#stringify(a - b)
171172
)
172173
)

0 commit comments

Comments
 (0)