Skip to content

Commit 46db62b

Browse files
committed
[Macros] Pass the attached macro mangling to ASTGen to use as the discriminator
when creating the macro expansion context.
1 parent f4b2b60 commit 46db62b

File tree

4 files changed

+24
-15
lines changed

4 files changed

+24
-15
lines changed

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ private func findSyntaxNodeInSourceFile<Node: SyntaxProtocol>(
293293
func expandAttachedMacro(
294294
diagEnginePtr: UnsafeMutablePointer<UInt8>,
295295
macroPtr: UnsafeRawPointer,
296+
discriminatorText: UnsafePointer<UInt8>,
297+
discriminatorTextLength: Int,
296298
rawMacroRole: UInt8,
297299
customAttrSourceFilePtr: UnsafeRawPointer,
298300
customAttrSourceLocPointer: UnsafePointer<UInt8>?,
@@ -344,7 +346,13 @@ func expandAttachedMacro(
344346
sourceManager.insert(declarationSourceFilePtr)
345347

346348
// Create an expansion context
347-
let context = sourceManager.createMacroExpansionContext()
349+
let discriminatorBuffer = UnsafeBufferPointer(
350+
start: discriminatorText, count: discriminatorTextLength
351+
)
352+
let discriminator = String(decoding: discriminatorBuffer, as: UTF8.self)
353+
let context = sourceManager.createMacroExpansionContext(
354+
discriminator: discriminator
355+
)
348356

349357
let macroName = customAttrNode.attributeName.description
350358
var evaluatedSyntaxStr: String

lib/Sema/TypeCheckMacros.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ extern "C" ptrdiff_t swift_ASTGen_expandFreestandingMacro(
5858

5959
extern "C" ptrdiff_t swift_ASTGen_expandAttachedMacro(
6060
void *diagEngine, void *macro,
61+
const char *discriminator,
62+
ptrdiff_t discriminatorLength,
6163
uint32_t rawMacroRole,
6264
void *customAttrSourceFile,
6365
const void *customAttrSourceLocation,
@@ -867,6 +869,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
867869
// Evaluate the macro.
868870
NullTerminatedStringRef evaluatedSource;
869871

872+
std::string discriminator;
870873
auto macroDef = macro->getDefinition();
871874
switch (macroDef.kind) {
872875
case MacroDefinition::Kind::Undefined:
@@ -932,11 +935,18 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
932935
if (auto var = dyn_cast<VarDecl>(attachedTo))
933936
searchDecl = var->getParentPatternBinding();
934937

938+
{
939+
Mangle::ASTMangler mangler;
940+
discriminator =
941+
mangler.mangleAttachedMacroExpansion(attachedTo, attr, role);
942+
}
943+
935944
const char *evaluatedSourceAddress;
936945
ptrdiff_t evaluatedSourceLength;
937946
swift_ASTGen_expandAttachedMacro(
938947
&ctx.Diags,
939948
externalDef.opaqueHandle,
949+
discriminator.data(), discriminator.size(),
940950
static_cast<uint32_t>(role),
941951
astGenAttrSourceFile, attr->AtLoc.getOpaquePointerValue(),
942952
astGenDeclSourceFile, searchDecl->getStartLoc().getOpaquePointerValue(),
@@ -955,14 +965,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
955965
}
956966

957967
// Figure out a reasonable name for the macro expansion buffer.
958-
std::string discriminator;
959-
std::string bufferName;
960-
{
961-
Mangle::ASTMangler mangler;
962-
discriminator =
963-
mangler.mangleAttachedMacroExpansion(attachedTo, attr, role);
964-
bufferName = adjustMacroExpansionBufferName(discriminator);
965-
}
968+
std::string bufferName = adjustMacroExpansionBufferName(discriminator);
966969

967970
// Dump macro expansions to standard output, if requested.
968971
if (ctx.LangOpts.DumpMacroExpansions) {

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -780,8 +780,7 @@ public struct WrapInType: PeerMacro {
780780
funcDecl
781781
.with(
782782
\.identifier,
783-
// FIXME: 'createUniqueName' starts with an int
784-
"method_\(context.createUniqueName(funcDecl.identifier.text))"
783+
"\(context.createUniqueName(funcDecl.identifier.text))"
785784
)
786785
.with(
787786
\.signature,
@@ -800,10 +799,9 @@ public struct WrapInType: PeerMacro {
800799
)
801800
.with(\.attributes, newAttributeList)
802801

803-
// FIXME: 'createUniqueName' starts with an int
804802
let structType: DeclSyntax =
805803
"""
806-
struct Wrapper_\(context.createUniqueName(funcDecl.identifier.text)) {
804+
struct \(context.createUniqueName(funcDecl.identifier.text)) {
807805
\(method)
808806
}
809807
"""

test/Macros/macro_expand_peers.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ func global(a: Int, b: String) {
3434
}
3535

3636
// CHECK-DUMP: @__swiftmacro_18macro_expand_peers6global1a1bySi_SStF10wrapInTypefMp_.swift
37-
// CHECK-DUMP: struct Wrapper_6globalfMu0_ {
38-
// CHECK-DUMP: func method_6globalfMu_(a: Int, b: String) {
37+
// CHECK-DUMP: struct $s18macro_expand_peers6global1a1bySi_SStF10wrapInTypefMp_6globalfMu0_ {
38+
// CHECK-DUMP: func $s18macro_expand_peers6global1a1bySi_SStF10wrapInTypefMp_6globalfMu_(a: Int, b: String) {
3939
// CHECK-DUMP: global(a: a, b: b)
4040
// CHECK-DUMP: }
4141
// CHECK-DUMP: }

0 commit comments

Comments
 (0)