Skip to content

Commit eb06129

Browse files
committed
[SourceKit] Add macro expansion support for conformance macros
1 parent d1bfe72 commit eb06129

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/Refactoring/Refactoring.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8549,6 +8549,14 @@ getMacroExpansionBuffers(MacroDecl *macro, const CustomAttr *attr, Decl *decl) {
85498549
allBufferIDs.append(bufferIDs.begin(), bufferIDs.end());
85508550
}
85518551

8552+
if (roles.contains(MacroRole::Conformance)) {
8553+
if (auto nominal = dyn_cast<NominalTypeDecl>(decl)) {
8554+
auto bufferIDs = evaluateOrDefault(
8555+
ctx.evaluator, ExpandConformanceMacros{nominal}, { });
8556+
allBufferIDs.append(bufferIDs.begin(), bufferIDs.end());
8557+
}
8558+
}
8559+
85528560
// Drop any buffers that come from other macros. We could eliminate this
85538561
// step by adding more fine-grained requests above, which only expand for a
85548562
// single custom attribute.

test/SourceKit/Macros/macro_basic.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ struct S3 {
4545
}
4646
}
4747

48+
@attached(conformance)
49+
macro Hashable() = #externalMacro(module: "MacroDefinition", type: "HashableMacro")
50+
51+
@Hashable
52+
struct S4 { }
53+
4854
// FIXME: Swift parser is not enabled on Linux CI yet.
4955
// REQUIRES: OS=macosx
5056

@@ -162,7 +168,7 @@ struct S3 {
162168
// ACCESSOR2_EXPAND-NEXT: source.edit.kind.active:
163169
// ACCESSOR2_EXPAND-NEXT: 33:3-33:20 ""
164170

165-
//##-- Refactoring expanding the second accessor macro
171+
//##-- Refactoring expanding the addCompletionHandler macro.
166172
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=42:5 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=PEER_EXPAND %s
167173
// PEER_EXPAND: source.edit.kind.active:
168174
// PEER_EXPAND-NEXT: 45:4-45:4 (@__swiftmacro_9MacroUser2S3V1f1a3for_SSSi_SSSdtYaF20addCompletionHandlerfMp_.swift) "
@@ -176,6 +182,16 @@ struct S3 {
176182
// PEER_EXPAND-NEXT: source.edit.kind.active:
177183
// PEER_EXPAND-NEXT: 42:3-42:24 ""
178184

185+
//##-- Refactoring expanding a conformance macro.
186+
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=51:5 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=CONFORMANCE_EXPAND %s
187+
// CONFORMANCE_EXPAND: source.edit.kind.active:
188+
// CONFORMANCE_EXPAND-NEXT: 52:14-52:14 (@__swiftmacro_9MacroUser2S4V8HashablefMc_.swift) "
189+
// CONFORMANCE_EXPAND-EMPTY:
190+
// CONFORMANCE_EXPAND-NEXT: extension S4 : Hashable {}
191+
// CONFORMANCE_EXPAND-NEXT: "
192+
// CONFORMANCE_EXPAND-NEXT: source.edit.kind.active:
193+
// CONFORMANCE_EXPAND-NEXT: 51:1-51:10 ""
194+
179195
//##-- Doc info, mostly just checking we don't crash because of the separate buffers
180196
// RUN: %sourcekitd-test -req=doc-info %s -- ${COMPILER_ARGS_WITHOUT_SOURCE[@]} | %FileCheck -check-prefix=DOCINFO %s
181197
// DOCINFO: key.name: "myTypeWrapper()"

0 commit comments

Comments
 (0)