Skip to content

Commit ea0ac83

Browse files
committed
ConstExtract macro added extensions
1 parent ae9e957 commit ea0ac83

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

lib/ConstExtract/ConstExtract.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,11 @@ gatherConstValuesForPrimary(const std::unordered_set<std::string> &Protocols,
560560
ConformanceDecls);
561561
for (auto D : SF->getTopLevelDecls())
562562
D->walk(ConformanceCollector);
563+
// Visit macro expanded extensions
564+
if (auto *synthesizedSF = SF->getSynthesizedFile())
565+
for (auto D : synthesizedSF->getTopLevelDecls())
566+
if (isa<ExtensionDecl>(D))
567+
D->walk(ConformanceCollector);
563568

564569
for (auto *CD : ConformanceDecls)
565570
Result.emplace_back(evaluateOrDefault(

test/ConstExtraction/ExtractFromMacroExpansion.swift

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ macro AddMacroAddedStruct() = #externalMacro(module: "MacroDefinition", type: "A
1515
@freestanding(declaration, names: named(macroAddedVar))
1616
macro AddMacroAddedVar() = #externalMacro(module: "MacroDefinition", type: "AddVarDeclMacro")
1717

18-
@attached(extension, conformances: MyProto, names: prefixed(_extension_))
18+
@attached(extension, conformances: MyProto, names: prefixed(_extension_), named(_Extension_MyProto))
1919
macro AddExtension() = #externalMacro(module: "MacroDefinition", type: "AddExtensionMacro")
2020

2121
@attached(peer, names: prefixed(_peer_))
@@ -43,6 +43,9 @@ struct MyStruct {
4343
#AddMacroAddedVar
4444

4545
@AddPeerVar
46+
@AddExtension
47+
@AddMemberVar
48+
@AddPeerStruct
4649
struct Inner { }
4750
}
4851

@@ -131,3 +134,83 @@ extension MyStruct {
131134
// CHECK: "type": "Swift.Int",
132135
// CHECK: "valueKind": "RawLiteral",
133136
// CHECK: "value": "3"
137+
138+
139+
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct.Inner",
140+
// CHECK: "properties": [
141+
// CHECK: "label": "_member_Inner",
142+
// CHECK: "type": "Swift.Int",
143+
// CHECK: "valueKind": "RawLiteral",
144+
// CHECK: "value": "5"
145+
146+
// CHECK: "label": "_extension_Inner",
147+
// CHECK: "type": "Swift.Int",
148+
// CHECK: "valueKind": "RawLiteral",
149+
// CHECK: "value": "3"
150+
151+
152+
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct._Peer_Inner",
153+
// CHECK: "properties": [
154+
// CHECK: "label": "peerMacroVar",
155+
// CHECK: "type": "Swift.Int",
156+
// CHECK: "valueKind": "RawLiteral",
157+
// CHECK: "value": "7"
158+
159+
// CHECK: "label": "macroAddedVar",
160+
// CHECK: "type": "Swift.Int",
161+
// CHECK: "valueKind": "RawLiteral",
162+
// CHECK: "value": "2"
163+
164+
// CHECK: "label": "_peer_peerMacroVar",
165+
// CHECK: "type": "Swift.Int",
166+
// CHECK: "valueKind": "RawLiteral",
167+
// CHECK: "value": "4"
168+
169+
// CHECK: "label": "_member__Peer_Inner",
170+
// CHECK: "type": "Swift.Int",
171+
// CHECK: "valueKind": "RawLiteral",
172+
// CHECK: "value": "5"
173+
174+
// CHECK: "label": "_extension__Peer_Inner",
175+
// CHECK: "type": "Swift.Int",
176+
// CHECK: "valueKind": "RawLiteral",
177+
// CHECK: "value": "3"
178+
179+
180+
// CHECK: "typeName": "ExtractFromMacroExpansion.MacroAddedStruct._Extension_MyProto",
181+
// CHECK: "properties": [
182+
// CHECK: "label": "nested",
183+
// CHECK: "type": "Swift.Int",
184+
// CHECK: "valueKind": "RawLiteral",
185+
// CHECK: "value": "8"
186+
187+
188+
// CHECK: "typeName": "ExtractFromMacroExpansion._Peer_MyStruct._Extension_MyProto",
189+
// CHECK: "properties": [
190+
// CHECK: "label": "nested",
191+
// CHECK: "type": "Swift.Int",
192+
// CHECK: "valueKind": "RawLiteral",
193+
// CHECK: "value": "8"
194+
195+
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct._Extension_MyProto",
196+
// CHECK: "properties": [
197+
// CHECK: "label": "nested",
198+
// CHECK: "type": "Swift.Int",
199+
// CHECK: "valueKind": "RawLiteral",
200+
// CHECK: "value": "8"
201+
202+
203+
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct._Peer_Inner._Extension_MyProto",
204+
// CHECK: "properties": [
205+
// CHECK: "label": "nested",
206+
// CHECK: "type": "Swift.Int",
207+
// CHECK: "valueKind": "RawLiteral",
208+
// CHECK: "value": "8"
209+
210+
211+
// CHECK: "typeName": "ExtractFromMacroExpansion.MyStruct.Inner._Extension_MyProto",
212+
// CHECK: "properties": [
213+
// CHECK: "label": "nested",
214+
// CHECK: "type": "Swift.Int",
215+
// CHECK: "valueKind": "RawLiteral",
216+
// CHECK: "value": "8"

test/ConstExtraction/Inputs/Macros.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,14 @@ public struct AddExtensionMacro: ExtensionMacro {
4343
) throws -> [ExtensionDeclSyntax] {
4444
let typeName = declaration.declGroupName
4545
return protocols.map {
46-
("extension \(typeName): \($0) { }" as DeclSyntax)
47-
.cast(ExtensionDeclSyntax.self)
46+
("""
47+
extension \(typeName): \($0) {
48+
struct _Extension_\($0): \($0) {
49+
var nested = 8
50+
}
51+
}
52+
""" as DeclSyntax)
53+
.cast(ExtensionDeclSyntax.self)
4854
} + [
4955
("""
5056
extension \(typeName) {

0 commit comments

Comments
 (0)