Skip to content

Commit cfb99b5

Browse files
committed
Print the buffer ID for non-main buffers and test via macro expansions.
1 parent 464550e commit cfb99b5

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed

lib/AST/ASTDumper.cpp

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -695,12 +695,19 @@ namespace {
695695
/// single quotes when printing substitution maps in full.
696696
char Quote = '\"';
697697

698+
/// Tracks the source buffer ID of the main source file, which subclasses
699+
/// can use to distinguish ranges/locations in that file vs. ranges in other
700+
/// buffers, like macro expansions.
701+
unsigned MainBufferID;
702+
698703
public:
699704
virtual ~PrintWriterBase() {}
700705

701706
char quote() const { return Quote; }
702707
void setQuote(char quote) { Quote = quote; }
703708

709+
void setMainBufferID(unsigned bufferID) { MainBufferID = bufferID; }
710+
704711
/// Call `body` in a context where the printer is ready for a child to be
705712
/// printed.
706713
virtual void printRecArbitrary(std::function<void(Label)> body,
@@ -945,6 +952,12 @@ namespace {
945952
unsigned endOffset = srcMgr.getLocOffsetInBuffer(R.End, endBufferID);
946953
OS.attribute("end", endOffset);
947954

955+
// Only print the buffer ID when it doesn't match the main ID, so that we
956+
// distinguish macro expansions but don't bloat the output with the main
957+
// file name repeated over and over.
958+
if (startBufferID != MainBufferID)
959+
OS.attribute("buffer_id", srcMgr.getIdentifierForBuffer(startBufferID));
960+
948961
OS.objectEnd();
949962
OS.attributeEnd();
950963
}
@@ -2170,15 +2183,25 @@ namespace {
21702183

21712184
printAttributes(IDC->getDecl());
21722185

2173-
auto members = ParseIfNeeded ? IDC->getMembers()
2174-
: IDC->getCurrentMembersWithoutLoading();
2175-
printList(members, [&](Decl *D, Label label) {
2176-
printRec(D, label);
2177-
}, Label::optional("members"));
2186+
if (Writer.isParsable()) {
2187+
// Parsable outputs are meant to be used for semantic analysis, so we
2188+
// want the full list of members, including macro-generated ones.
2189+
printList(IDC->getABIMembers(), [&](Decl *D, Label label) {
2190+
printRec(D, label);
2191+
}, Label::optional("members"));
2192+
} else {
2193+
auto members = ParseIfNeeded ? IDC->getMembers()
2194+
: IDC->getCurrentMembersWithoutLoading();
2195+
printList(members, [&](Decl *D, Label label) {
2196+
printRec(D, label);
2197+
}, Label::optional("members"));
2198+
}
21782199
printFoot();
21792200
}
21802201

21812202
void visitSourceFile(const SourceFile &SF) {
2203+
Writer.setMainBufferID(SF.getBufferID());
2204+
21822205
printHead("source_file", ASTNodeColor, Label::optional(""));
21832206
printNameRaw([&](raw_ostream &OS) {
21842207
OS << SF.getFilename();
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Checks that macro-expanded members are dumped and that their source ranges
2+
// contain the appropriate buffer ID.
3+
4+
// REQUIRES: swift_swift_parser
5+
6+
// RUN: %target-swift-frontend -target %target-swift-5.9-abi-triple -disable-availability-checking -plugin-path %swift-plugin-dir -parse-as-library -dump-ast -dump-ast-format json %s -module-name main -o - | %FileCheck %s
7+
8+
@DebugDescription
9+
struct X {
10+
var y: Int
11+
12+
var debugDescription: String {
13+
"y is \(y)"
14+
}
15+
}
16+
17+
// CHECK: "buffer_id":"@__swiftmacro_

0 commit comments

Comments
 (0)