Extract decl in macro added extensions

This commit is contained in:
Apollo Zhu
2024-03-18 22:58:17 -07:00
parent 87bde4048a
commit 708fb13c5b
3 changed files with 53 additions and 3 deletions

View File

@@ -560,6 +560,11 @@ gatherConstValuesForPrimary(const std::unordered_set<std::string> &Protocols,
ConformanceDecls);
for (auto D : SF->getTopLevelDecls())
D->walk(ConformanceCollector);
// Visit macro expanded extensions
if (auto *synthesizedSF = SF->getSynthesizedFile())
for (auto D : synthesizedSF->getTopLevelDecls())
if (isa<ExtensionDecl>(D))
D->walk(ConformanceCollector);
for (auto *CD : ConformanceDecls)
Result.emplace_back(evaluateOrDefault(

View File

@@ -15,7 +15,7 @@ macro AddMacroAddedStruct() = #externalMacro(module: "MacroDefinition", type: "A
@freestanding(declaration, names: named(macroAddedVar))
macro AddMacroAddedVar() = #externalMacro(module: "MacroDefinition", type: "AddVarDeclMacro")
@attached(extension, conformances: MyProto, names: prefixed(_extension_))
@attached(extension, conformances: MyProto, names: prefixed(_extension_), named(_Extension_MyProto))
macro AddExtension() = #externalMacro(module: "MacroDefinition", type: "AddExtensionMacro")
@attached(peer, names: prefixed(_peer_))
@@ -175,3 +175,42 @@ extension MyStruct {
// CHECK: "type": "Swift.Int",
// CHECK: "valueKind": "RawLiteral",
// CHECK: "value": "3"
// CHECK: "typeName": "ExtractFromMacroExpansion.MacroAddedStruct._Extension_MyProto",
// CHECK: "properties": [
// CHECK: "label": "nested",
// CHECK: "type": "Swift.Int",
// CHECK: "valueKind": "RawLiteral",
// CHECK: "value": "8"
// CHECK: "typeName": "ExtractFromMacroExpansion._Peer_MyStruct._Extension_MyProto",
// CHECK: "properties": [
// CHECK: "label": "nested",
// CHECK: "type": "Swift.Int",
// CHECK: "valueKind": "RawLiteral",
// CHECK: "value": "8"
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct._Extension_MyProto",
// CHECK: "properties": [
// CHECK: "label": "nested",
// CHECK: "type": "Swift.Int",
// CHECK: "valueKind": "RawLiteral",
// CHECK: "value": "8"
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct._Peer_Inner._Extension_MyProto",
// CHECK: "properties": [
// CHECK: "label": "nested",
// CHECK: "type": "Swift.Int",
// CHECK: "valueKind": "RawLiteral",
// CHECK: "value": "8"
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct.Inner._Extension_MyProto",
// CHECK: "properties": [
// CHECK: "label": "nested",
// CHECK: "type": "Swift.Int",
// CHECK: "valueKind": "RawLiteral",
// CHECK: "value": "8"

View File

@@ -43,8 +43,14 @@ public struct AddExtensionMacro: ExtensionMacro {
) throws -> [ExtensionDeclSyntax] {
let typeName = declaration.declGroupName
return protocols.map {
("extension \(typeName): \($0) { }" as DeclSyntax)
.cast(ExtensionDeclSyntax.self)
("""
extension \(typeName): \($0) {
struct _Extension_\($0): \($0) {
var nested = 8
}
}
""" as DeclSyntax)
.cast(ExtensionDeclSyntax.self)
} + [
("""
extension \(typeName) {