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.
This commit is contained in:
Tony Allevato
2025-01-30 13:50:16 -05:00
parent 9d3bcca73f
commit 237e79a467
2 changed files with 32 additions and 9 deletions

View File

@@ -28,6 +28,7 @@
#include "swift/AST/ParameterList.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/TypeVisitor.h"
#include "swift/AST/USRGeneration.h"
#include "swift/Basic/Assertions.h"
@@ -2731,7 +2732,11 @@ namespace {
void visitMacroExpansionDecl(MacroExpansionDecl *MED, Label label) {
printCommon(MED, "macro_expansion_decl", label);
printName(MED->getMacroName().getFullName(), Label::optional("name"));
if (MemberLoading == ASTDumpMemberLoading::TypeChecked) {
printDeclRefField(MED->getMacroRef(), Label::always("macro"));
} else {
printName(MED->getMacroName().getFullName(), Label::optional("name"));
}
printRec(MED->getArgs(), Label::optional("args"));
printFoot();
}
@@ -4931,10 +4936,21 @@ public:
}
void visitCustomAttr(CustomAttr *Attr, Label label) {
printCommon(Attr, "custom_attr", label);
printTypeField(Attr->getType(), Label::always("type"));
if (Attr->getType()) {
printTypeField(Attr->getType(), Label::always("type"));
} else if (MemberLoading == ASTDumpMemberLoading::TypeChecked) {
// If the type is null, it might be a macro reference. Try that if we're
// dumping the fully type-checked AST.
auto macroRef =
evaluateOrDefault(const_cast<ASTContext *>(Ctx)->evaluator,
ResolveMacroRequest{Attr, DC}, ConcreteDeclRef());
if (macroRef) {
printDeclRefField(macroRef, Label::always("macro"));
}
}
if (!Writer.isParsable()) {
// The type has the semantic information we want for parsable outputs, so
// omit the `TypeRepr` there.
// omit the `TypeRepr` there. This also works for macro references.
printRec(Attr->getTypeRepr(), Label::optional("type_repr"));
}
if (Attr->getArgs())

View File

@@ -6,8 +6,7 @@
// RUN: %empty-directory(%t)
// 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
// 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)
// 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
// 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
import json_ast_macro_library
@@ -15,7 +14,11 @@ import json_ast_macro_library
struct X {
var y: Int
}
// CHECK: "_kind":"pattern_binding_decl"
// CHECK: "_kind":"struct_decl",
// CHECK-SAME: "usr":"s:4main1XV",
// CHECK-SAME: "_kind":"custom_attr",
// CHECK-SAME: "macro":{"_kind":"decl_ref","base_name":"InjectMember","decl_usr":"s:22json_ast_macro_library12InjectMemberyycfm","type_usr":"$syycD"}
// CHECK-SAME: "_kind":"pattern_binding_decl"
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main1X12InjectMemberfMm_.swift"
// CHECK-SAME: "name":"_macroInjectedMember"
@@ -26,15 +29,18 @@ struct Z {
// decl before we see the MacroExpansionDecl.
// CHECK-SAME: "_kind":"struct_decl",
// CHECK-SAME: "usr":"s:4main1ZV20FixedNameFreestanderV",
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main0033astdumpjsonmacrosswift_GwAFheaeGafMX{{\d+}}_{{\d+}}_33_{{[0-9A-F]+}}Ll18injectFreestandingfMf_.swift"
// CHECK-SAME: "_kind": "macro_expansion_decl",
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main0033astdumpjsonmacrosswift_GwAFheaeGafMX{{[0-9]+}}_{{[0-9]+}}_33_{{[0-9A-F]+}}Ll18injectFreestandingfMf_.swift"
// CHECK-SAME: "_kind":"macro_expansion_decl",
// CHECK-SAME: "auxiliary_decl_usrs":["s:4main1ZV20FixedNameFreestanderV"]
// CHECK-SAME: "macro":{"_kind":"decl_ref","base_name":"injectFreestanding","decl_usr":"s:22json_ast_macro_library18injectFreestandingyycfm","type_usr":"$syycD"}
@InjectPeer
struct ThisWillBePeered {}
// CHECK-SAME: "_kind":"struct_decl",
// CHECK-SAME: "usr":"s:4main16ThisWillBePeeredV",
// CHECK-SAME: "auxiliary_decl_usrs":["s:4main13FixedNamePeerV"],
// CHECK-SAME: "_kind":"custom_attr",
// CHECK-SAME: "macro":{"_kind":"decl_ref","base_name":"InjectPeer","decl_usr":"s:22json_ast_macro_library10InjectPeeryycfm","type_usr":"$syycD"}
// CHECK-SAME: "_kind":"struct_decl",
// CHECK-SAME: "usr":"s:4main13FixedNamePeerV",
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main16ThisWillBePeered10InjectPeerfMp_.swift"
@@ -42,6 +48,7 @@ struct ThisWillBePeered {}
#injectFreestanding
// CHECK-SAME: "_kind":"macro_expansion_decl",
// CHECK-SAME: "auxiliary_decl_usrs":["s:4main20FixedNameFreestanderV"],
// CHECK-SAME: "macro":{"_kind":"decl_ref","base_name":"injectFreestanding","decl_usr":"s:22json_ast_macro_library18injectFreestandingyycfm","type_usr":"$syycD"}
// CHECK-SAME: "_kind":"struct_decl",
// CHECK-SAME: "usr":"s:4main20FixedNameFreestanderV",
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main0033astdumpjsonmacrosswift_GwAFheaeGafMX{{\d+}}_{{\d+}}_33_{{[0-9A-F]+}}Ll18injectFreestandingfMf_.swift"
// CHECK-SAME: "buffer_id":"@__swiftmacro_4main0033astdumpjsonmacrosswift_GwAFheaeGafMX{{[0-9]+}}_{{[0-9]+}}_33_{{[0-9A-F]+}}Ll18injectFreestandingfMf_.swift"