Skip to content

Commit 237e79a

Browse files
committed
Print the macro's concrete decl ref for any macro expansions.
This gives us the macro's USR, letting us match it up to its declaration elsewhere.
1 parent 9d3bcca commit 237e79a

File tree

2 files changed

+32
-9
lines changed

2 files changed

+32
-9
lines changed

lib/AST/ASTDumper.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "swift/AST/ParameterList.h"
2929
#include "swift/AST/ProtocolConformance.h"
3030
#include "swift/AST/SourceFile.h"
31+
#include "swift/AST/TypeCheckRequests.h"
3132
#include "swift/AST/TypeVisitor.h"
3233
#include "swift/AST/USRGeneration.h"
3334
#include "swift/Basic/Assertions.h"
@@ -2731,7 +2732,11 @@ namespace {
27312732

27322733
void visitMacroExpansionDecl(MacroExpansionDecl *MED, Label label) {
27332734
printCommon(MED, "macro_expansion_decl", label);
2734-
printName(MED->getMacroName().getFullName(), Label::optional("name"));
2735+
if (MemberLoading == ASTDumpMemberLoading::TypeChecked) {
2736+
printDeclRefField(MED->getMacroRef(), Label::always("macro"));
2737+
} else {
2738+
printName(MED->getMacroName().getFullName(), Label::optional("name"));
2739+
}
27352740
printRec(MED->getArgs(), Label::optional("args"));
27362741
printFoot();
27372742
}
@@ -4931,10 +4936,21 @@ class PrintAttribute : public AttributeVisitor<PrintAttribute, void, Label>,
49314936
}
49324937
void visitCustomAttr(CustomAttr *Attr, Label label) {
49334938
printCommon(Attr, "custom_attr", label);
4934-
printTypeField(Attr->getType(), Label::always("type"));
4939+
if (Attr->getType()) {
4940+
printTypeField(Attr->getType(), Label::always("type"));
4941+
} else if (MemberLoading == ASTDumpMemberLoading::TypeChecked) {
4942+
// If the type is null, it might be a macro reference. Try that if we're
4943+
// dumping the fully type-checked AST.
4944+
auto macroRef =
4945+
evaluateOrDefault(const_cast<ASTContext *>(Ctx)->evaluator,
4946+
ResolveMacroRequest{Attr, DC}, ConcreteDeclRef());
4947+
if (macroRef) {
4948+
printDeclRefField(macroRef, Label::always("macro"));
4949+
}
4950+
}
49354951
if (!Writer.isParsable()) {
49364952
// The type has the semantic information we want for parsable outputs, so
4937-
// omit the `TypeRepr` there.
4953+
// omit the `TypeRepr` there. This also works for macro references.
49384954
printRec(Attr->getTypeRepr(), Label::optional("type_repr"));
49394955
}
49404956
if (Attr->getArgs())

test/Frontend/ast-dump-json-macros.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
// RUN: %empty-directory(%t)
77
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/json_ast_macro_definitions.swift -g -no-toolchain-stdlib-rpath
88
// RUN: %target-swift-frontend -swift-version 5 -emit-module -o %t/json_ast_macro_library.swiftmodule %S/Inputs/json_ast_macro_library.swift -module-name json_ast_macro_library -load-plugin-library %t/%target-library-name(MacroDefinition)
9-
// RUN: %target-swift-frontend -target %target-swift-5.9-abi-triple -I %t -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library -dump-ast -dump-ast-format json %s -module-name main -o -
10-
// | %FileCheck %s
9+
// RUN: %target-swift-frontend -target %target-swift-5.9-abi-triple -I %t -load-plugin-library %t/%target-library-name(MacroDefinition) -parse-as-library -dump-ast -dump-ast-format json %s -module-name main -o - | %FileCheck %s
1110

1211
import json_ast_macro_library
1312

1413
@InjectMember
1514
struct X {
1615
var y: Int
1716
}
18-
// CHECK: "_kind":"pattern_binding_decl"
17+
// CHECK: "_kind":"struct_decl",
18+
// CHECK-SAME: "usr":"s:4main1XV",
19+
// CHECK-SAME: "_kind":"custom_attr",
20+
// CHECK-SAME: "macro":{"_kind":"decl_ref","base_name":"InjectMember","decl_usr":"s:22json_ast_macro_library12InjectMemberyycfm","type_usr":"$syycD"}
21+
// CHECK-SAME: "_kind":"pattern_binding_decl"
1922
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main1X12InjectMemberfMm_.swift"
2023
// CHECK-SAME: "name":"_macroInjectedMember"
2124

@@ -26,22 +29,26 @@ struct Z {
2629
// decl before we see the MacroExpansionDecl.
2730
// CHECK-SAME: "_kind":"struct_decl",
2831
// CHECK-SAME: "usr":"s:4main1ZV20FixedNameFreestanderV",
29-
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main0033astdumpjsonmacrosswift_GwAFheaeGafMX{{\d+}}_{{\d+}}_33_{{[0-9A-F]+}}Ll18injectFreestandingfMf_.swift"
30-
// CHECK-SAME: "_kind": "macro_expansion_decl",
32+
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main0033astdumpjsonmacrosswift_GwAFheaeGafMX{{[0-9]+}}_{{[0-9]+}}_33_{{[0-9A-F]+}}Ll18injectFreestandingfMf_.swift"
33+
// CHECK-SAME: "_kind":"macro_expansion_decl",
3134
// CHECK-SAME: "auxiliary_decl_usrs":["s:4main1ZV20FixedNameFreestanderV"]
35+
// CHECK-SAME: "macro":{"_kind":"decl_ref","base_name":"injectFreestanding","decl_usr":"s:22json_ast_macro_library18injectFreestandingyycfm","type_usr":"$syycD"}
3236

3337
@InjectPeer
3438
struct ThisWillBePeered {}
3539
// CHECK-SAME: "_kind":"struct_decl",
3640
// CHECK-SAME: "usr":"s:4main16ThisWillBePeeredV",
3741
// CHECK-SAME: "auxiliary_decl_usrs":["s:4main13FixedNamePeerV"],
42+
// CHECK-SAME: "_kind":"custom_attr",
43+
// CHECK-SAME: "macro":{"_kind":"decl_ref","base_name":"InjectPeer","decl_usr":"s:22json_ast_macro_library10InjectPeeryycfm","type_usr":"$syycD"}
3844
// CHECK-SAME: "_kind":"struct_decl",
3945
// CHECK-SAME: "usr":"s:4main13FixedNamePeerV",
4046
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main16ThisWillBePeered10InjectPeerfMp_.swift"
4147

4248
#injectFreestanding
4349
// CHECK-SAME: "_kind":"macro_expansion_decl",
4450
// CHECK-SAME: "auxiliary_decl_usrs":["s:4main20FixedNameFreestanderV"],
51+
// CHECK-SAME: "macro":{"_kind":"decl_ref","base_name":"injectFreestanding","decl_usr":"s:22json_ast_macro_library18injectFreestandingyycfm","type_usr":"$syycD"}
4552
// CHECK-SAME: "_kind":"struct_decl",
4653
// CHECK-SAME: "usr":"s:4main20FixedNameFreestanderV",
47-
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main0033astdumpjsonmacrosswift_GwAFheaeGafMX{{\d+}}_{{\d+}}_33_{{[0-9A-F]+}}Ll18injectFreestandingfMf_.swift"
54+
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main0033astdumpjsonmacrosswift_GwAFheaeGafMX{{[0-9]+}}_{{[0-9]+}}_33_{{[0-9A-F]+}}Ll18injectFreestandingfMf_.swift"

0 commit comments

Comments
 (0)