Skip to content

Commit b7f75c1

Browse files
DougGregoradrian-prantl
authored andcommitted
[Debug info] Dump macro expansion buffers to disk for debug info to reference
When emitting debug info that references into a macro expansion, dump the macro expansion buffer into a file on disk (in the temporary directory) so that one can see the macro expansion buffers in the source files themselves. The test here validates that the generated LLVM IR looks correct, but I'm having a problem getting LLDB to actually locate the source code correctly.
1 parent 5f129af commit b7f75c1

File tree

4 files changed

+34
-12
lines changed

4 files changed

+34
-12
lines changed

include/swift/SIL/SILLocation.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,12 @@ class SILLocation {
410410
}
411411

412412
/// Extract the line, column, and filename from \p Loc.
413-
static FilenameAndLocation decode(SourceLoc Loc, const SourceManager &SM);
413+
///
414+
/// \p ForceGeneratedSourceToDisk can be set to true to create a temporary
415+
/// file on-disk for buffers containing generated source code, returning the
416+
/// name of that temporary file.
417+
static FilenameAndLocation decode(SourceLoc Loc, const SourceManager &SM,
418+
bool ForceGeneratedSourceToDisk = false);
414419

415420
/// Return the decoded FilenameAndLocation.
416421
/// In case the location has a separate AST node for debugging, this node is

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2951,8 +2951,10 @@ void IRGenDebugInfoImpl::emitTypeMetadata(IRGenFunction &IGF,
29512951
SILLocation::FilenameAndLocation
29522952
IRGenDebugInfoImpl::decodeSourceLoc(SourceLoc SL) {
29532953
auto &Cached = FilenameAndLocationCache[SL.getOpaquePointerValue()];
2954-
if (Cached.filename.empty())
2955-
Cached = sanitizeCodeViewFilenameAndLocation(SILLocation::decode(SL, SM));
2954+
if (Cached.filename.empty()) {
2955+
Cached = sanitizeCodeViewFilenameAndLocation(
2956+
SILLocation::decode(SL, SM, /*ForceGeneratedSourceToDisk=*/true));
2957+
}
29562958
return Cached;
29572959
}
29582960

lib/SIL/IR/SILLocation.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,11 @@ DeclContext *SILLocation::getAsDeclContext() const {
137137
}
138138

139139
SILLocation::FilenameAndLocation SILLocation::decode(SourceLoc Loc,
140-
const SourceManager &SM) {
140+
const SourceManager &SM,
141+
bool ForceGeneratedSourceToDisk) {
141142
FilenameAndLocation DL;
142143
if (Loc.isValid()) {
143-
DL.filename = SM.getDisplayNameForLoc(Loc);
144+
DL.filename = SM.getDisplayNameForLoc(Loc, ForceGeneratedSourceToDisk);
144145
std::tie(DL.line, DL.column) = SM.getPresumedLineAndColumnForLoc(Loc);
145146
}
146147
return DL;

test/Macros/macro_expand.swift

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
// RUN: not %target-swift-frontend -typecheck -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -module-name MacroUser -DTEST_DIAGNOSTICS -serialize-diagnostics-path %t/macro_expand.dia %s
99
// RUN: c-index-test -read-diagnostics %t/macro_expand.dia 2>&1 | %FileCheck -check-prefix CHECK-DIAGS %s
1010

11+
// Debug IR testing
12+
// RUN: %target-swift-frontend -emit-ir -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir %s -module-name MacroUser -o - -g | %FileCheck --check-prefix CHECK-IR %s
13+
14+
1115
// Execution testing
12-
// RUN: %target-build-swift -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser
16+
// RUN: %target-build-swift -g -enable-experimental-feature Macros -enable-experimental-feature Macros -load-plugin-library %t/%target-library-name(MacroDefinition) -I %swift-host-lib-dir -L %swift-host-lib-dir %s -o %t/main -module-name MacroUser
1317
// RUN: %target-run %t/main | %FileCheck %s
1418
// REQUIRES: executable_test
1519

@@ -53,14 +57,24 @@ func testStringify(a: Int, b: Int) {
5357
_ = (b, b2, s2, s3)
5458
}
5559

56-
struct Outer {
60+
// CHECK-IR-LABEL: define swiftcc void @"$s9MacroUser5OuterV4testyyF"
61+
// CHECK-IR: call {{.*}}@"$sSS21_builtinStringLiteral17utf8CodeUnitCount7isASCIISSBp_BwBi1_tcfC"{{.*}}!dbg ![[STRING_LITERAL_DI:[0-9]+]]
62+
// CHECK-IR: ![[STRING_LITERAL_DI]] = !DILocation(line: 1, column: 13, scope: ![[EXPANSION_FILE_SCOPE_DI:[0-9]+]])
63+
// CHECK-IR: ![[EXPANSION_FILE_SCOPE_DI]] = !DILexicalBlockFile(scope: ![[EXPANSION_FILE_SCOPE_SCOPE_DI:[0-9]+]], file: ![[EXPANSION_FILE_DI:[0-9]+]], discriminator: 0)
64+
// CHECK-IR: ![[EXPANSION_FILE_DI]] = !DIFile(filename: "{{.*}}swift-generated-sources{{.*}}.swift", directory: "")
65+
public struct Outer {
5766
var value: Int = 0
58-
func test() {
59-
_ = #stringify(1 + value)
67+
public func test() {
68+
let (a, b) = #stringify(1 + value)
6069

61-
_ = #stringify({ x in
70+
let (c, d) = #stringify({ x in
6271
x + 1
6372
})
73+
74+
_ = a
75+
_ = b
76+
_ = c
77+
_ = d
6478
}
6579
}
6680

@@ -82,9 +96,9 @@ func testAddBlocker(a: Int, b: Int, c: Int, oa: OnlyAdds) {
8296
// expected-note@-1{{in expansion of macro 'addBlocker' here}}
8397
// expected-note@-2{{use '-'}}{{22-23=-}}
8498

85-
// CHECK-DIAGS: macro_expand.swift:81:7-81:27:1:4: error: binary operator '-' cannot be applied to two 'OnlyAdds' operands [] []
99+
// CHECK-DIAGS: macro_expand.swift:[[@LINE-4]]:7-[[@LINE-4]]:27:1:4: error: binary operator '-' cannot be applied to two 'OnlyAdds' operands [] []
86100
// CHECK-DIAGS: CONTENTS OF FILE{{.*}}addBlocker
87-
// CHECK-DIAGS-NEXT: Original source range: {{.*}}macro_expand.swift:81:7 - {{.*}}macro_expand.swift:81:27
101+
// CHECK-DIAGS-NEXT: Original source range: {{.*}}macro_expand.swift:[[@LINE-6]]:7 - {{.*}}macro_expand.swift:[[@LINE-6]]:27
88102
// CHECK-DIAGS-NEXT: oa - oa
89103
// CHECK-DIAGS-NEXT: END CONTENTS OF FILE
90104
// Check recursion.

0 commit comments

Comments
 (0)