Skip to content

Commit 87cabb0

Browse files
committed
Print the buffer ID for non-main buffers and test via macro expansions.
1 parent a4c64b0 commit 87cabb0

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
@@ -703,12 +703,19 @@ namespace {
703703
/// single quotes when printing substitution maps in full.
704704
char Quote = '\"';
705705

706+
/// Tracks the source buffer ID of the main source file, which subclasses
707+
/// can use to distinguish ranges/locations in that file vs. ranges in other
708+
/// buffers, like macro expansions.
709+
unsigned MainBufferID;
710+
706711
public:
707712
virtual ~PrintWriterBase() {}
708713

709714
char quote() const { return Quote; }
710715
void setQuote(char quote) { Quote = quote; }
711716

717+
void setMainBufferID(unsigned bufferID) { MainBufferID = bufferID; }
718+
712719
/// Call `body` in a context where the printer is ready for a child to be
713720
/// printed.
714721
virtual void printRecArbitrary(std::function<void(Label)> body,
@@ -953,6 +960,12 @@ namespace {
953960
unsigned endOffset = srcMgr.getLocOffsetInBuffer(R.End, endBufferID);
954961
OS.attribute("end", endOffset);
955962

963+
// Only print the buffer ID when it doesn't match the main ID, so that we
964+
// distinguish macro expansions but don't bloat the output with the main
965+
// file name repeated over and over.
966+
if (startBufferID != MainBufferID)
967+
OS.attribute("buffer_id", srcMgr.getIdentifierForBuffer(startBufferID));
968+
956969
OS.objectEnd();
957970
OS.attributeEnd();
958971
}
@@ -2178,15 +2191,25 @@ namespace {
21782191

21792192
printAttributes(IDC->getDecl());
21802193

2181-
auto members = ParseIfNeeded ? IDC->getMembers()
2182-
: IDC->getCurrentMembersWithoutLoading();
2183-
printList(members, [&](Decl *D, Label label) {
2184-
printRec(D, label);
2185-
}, Label::optional("members"));
2194+
if (Writer.isParsable()) {
2195+
// Parsable outputs are meant to be used for semantic analysis, so we
2196+
// want the full list of members, including macro-generated ones.
2197+
printList(IDC->getABIMembers(), [&](Decl *D, Label label) {
2198+
printRec(D, label);
2199+
}, Label::optional("members"));
2200+
} else {
2201+
auto members = ParseIfNeeded ? IDC->getMembers()
2202+
: IDC->getCurrentMembersWithoutLoading();
2203+
printList(members, [&](Decl *D, Label label) {
2204+
printRec(D, label);
2205+
}, Label::optional("members"));
2206+
}
21862207
printFoot();
21872208
}
21882209

21892210
void visitSourceFile(const SourceFile &SF) {
2211+
Writer.setMainBufferID(SF.getBufferID());
2212+
21902213
printHead("source_file", ASTNodeColor, Label::optional(""));
21912214
printNameRaw([&](raw_ostream &OS) {
21922215
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)