@@ -407,6 +407,29 @@ func checkMacroDefinition(
407
407
}
408
408
}
409
409
410
+ // Make an expansion result for '@_cdecl' function caller.
411
+ func makeExpansionOutputResult(
412
+ expandedSource: String ? ,
413
+ outputPointer: UnsafeMutablePointer < UnsafePointer < UInt8 > ? > ,
414
+ outputLength: UnsafeMutablePointer < Int >
415
+ ) -> Int {
416
+ guard var expandedSource = expandedSource else {
417
+ return - 1
418
+ }
419
+
420
+ // Form the result buffer for our caller.
421
+ expandedSource. withUTF8 { utf8 in
422
+ let evaluatedResultPtr = UnsafeMutablePointer< UInt8> . allocate( capacity: utf8. count + 1 )
423
+ if let baseAddress = utf8. baseAddress {
424
+ evaluatedResultPtr. initialize ( from: baseAddress, count: utf8. count)
425
+ }
426
+ evaluatedResultPtr [ utf8. count] = 0
427
+
428
+ outputPointer. pointee = UnsafePointer ( evaluatedResultPtr)
429
+ outputLength. pointee = utf8. count
430
+ }
431
+ return 0
432
+ }
410
433
411
434
@_cdecl ( " swift_ASTGen_expandFreestandingMacro " )
412
435
@usableFromInline
@@ -470,23 +493,11 @@ func expandFreestandingMacro(
470
493
discriminator: discriminator)
471
494
}
472
495
473
- guard var expandedSource = expandedSource else {
474
- return - 1
475
- }
476
-
477
- // Form the result buffer for our caller.
478
- expandedSource. withUTF8 { utf8 in
479
- let evaluatedResultPtr = UnsafeMutablePointer< UInt8> . allocate( capacity: utf8. count + 1 )
480
- if let baseAddress = utf8. baseAddress {
481
- evaluatedResultPtr. initialize ( from: baseAddress, count: utf8. count)
482
- }
483
- evaluatedResultPtr [ utf8. count] = 0
484
-
485
- expandedSourcePointer. pointee = UnsafePointer ( evaluatedResultPtr)
486
- expandedSourceLength. pointee = utf8. count
487
- }
488
-
489
- return 0
496
+ return makeExpansionOutputResult (
497
+ expandedSource: expandedSource,
498
+ outputPointer: expandedSourcePointer,
499
+ outputLength: expandedSourceLength
500
+ )
490
501
}
491
502
492
503
func expandFreestandingMacroIPC(
@@ -516,7 +527,7 @@ func expandFreestandingMacroIPC(
516
527
preconditionFailure ( " unhandled macro role for freestanding macro " )
517
528
518
529
case . expression: pluginMacroRole = . expression
519
- case . declaration: pluginMacroRole = . freeStandingDeclaration
530
+ case . declaration: pluginMacroRole = . declaration
520
531
case . codeItem: pluginMacroRole = . codeItem
521
532
}
522
533
@@ -528,9 +539,15 @@ func expandFreestandingMacroIPC(
528
539
syntax: PluginMessage . Syntax ( syntax: Syntax ( expansionSyntax) , in: sourceFilePtr) !)
529
540
do {
530
541
let result = try macro. plugin. sendMessageAndWait ( message)
531
- guard
532
- case . expandFreestandingMacroResult( let expandedSource, let diagnostics) = result
533
- else {
542
+ let expandedSource : String ?
543
+ let diagnostics : [ PluginMessage . Diagnostic ]
544
+ switch result {
545
+ case
546
+ . expandMacroResult( let _expandedSource, let _diagnostics) ,
547
+ . expandFreestandingMacroResult( let _expandedSource, let _diagnostics) :
548
+ expandedSource = _expandedSource
549
+ diagnostics = _diagnostics
550
+ default :
534
551
throw PluginError . invalidReponseKind
535
552
}
536
553
@@ -707,10 +724,10 @@ func expandAttachedMacro(
707
724
)
708
725
let discriminator = String ( decoding: discriminatorBuffer, as: UTF8 . self)
709
726
710
- let expandedSources : [ String ] ?
727
+ let expandedSource : String ?
711
728
switch MacroPluginKind ( rawValue: macroKind) ! {
712
729
case . Executable:
713
- expandedSources = expandAttachedMacroIPC (
730
+ expandedSource = expandAttachedMacroIPC (
714
731
diagEnginePtr: diagEnginePtr,
715
732
macroPtr: macroPtr,
716
733
rawMacroRole: rawMacroRole,
@@ -722,7 +739,7 @@ func expandAttachedMacro(
722
739
parentDeclSourceFilePtr: parentDeclSourceFilePtr,
723
740
parentDeclNode: parentDeclNode)
724
741
case . InProcess:
725
- expandedSources = expandAttachedMacroInProcess (
742
+ expandedSource = expandAttachedMacroInProcess (
726
743
diagEnginePtr: diagEnginePtr,
727
744
macroPtr: macroPtr,
728
745
rawMacroRole: rawMacroRole,
@@ -735,26 +752,11 @@ func expandAttachedMacro(
735
752
parentDeclNode: parentDeclNode)
736
753
}
737
754
738
- guard let expandedSources = expandedSources else {
739
- return - 1
740
- }
741
-
742
- // Fixup the source.
743
- var expandedSource : String = collapse ( expansions: expandedSources, for: MacroRole ( rawMacroRole: rawMacroRole) , attachedTo: declarationNode)
744
-
745
- // Form the result buffer for our caller.
746
- expandedSource. withUTF8 { utf8 in
747
- let evaluatedResultPtr = UnsafeMutablePointer< UInt8> . allocate( capacity: utf8. count + 1 )
748
- if let baseAddress = utf8. baseAddress {
749
- evaluatedResultPtr. initialize ( from: baseAddress, count: utf8. count)
750
- }
751
- evaluatedResultPtr [ utf8. count] = 0
752
-
753
- expandedSourcePointer. pointee = UnsafePointer ( evaluatedResultPtr)
754
- expandedSourceLength. pointee = utf8. count
755
- }
756
-
757
- return 0
755
+ return makeExpansionOutputResult (
756
+ expandedSource: expandedSource,
757
+ outputPointer: expandedSourcePointer,
758
+ outputLength: expandedSourceLength
759
+ )
758
760
}
759
761
760
762
func expandAttachedMacroIPC(
@@ -768,7 +770,7 @@ func expandAttachedMacroIPC(
768
770
attachedTo declarationNode: DeclSyntax ,
769
771
parentDeclSourceFilePtr: UnsafePointer < ExportedSourceFile > ? ,
770
772
parentDeclNode: DeclSyntax ?
771
- ) -> [ String ] ? {
773
+ ) -> String ? {
772
774
let macroName : String = customAttrNode. attributeName. description
773
775
let macro = macroPtr. assumingMemoryBound ( to: ExportedExecutableMacro . self) . pointee
774
776
@@ -810,10 +812,27 @@ func expandAttachedMacroIPC(
810
812
declSyntax: declSyntax,
811
813
parentDeclSyntax: parentDeclSyntax)
812
814
do {
813
- let result = try macro. plugin. sendMessageAndWait ( message)
814
- guard
815
- case . expandAttachedMacroResult( let expandedSources, let diagnostics) = result
816
- else {
815
+ let expandedSource : String ?
816
+ let diagnostics : [ PluginMessage . Diagnostic ]
817
+ switch try macro. plugin. sendMessageAndWait ( message) {
818
+ case . expandMacroResult( let _expandedSource, let _diagnostics) :
819
+ expandedSource = _expandedSource
820
+ diagnostics = _diagnostics
821
+
822
+ // Handle legacy result message.
823
+ case . expandAttachedMacroResult( let _expandedSources, let _diagnostics) :
824
+ if let _expandedSources = _expandedSources {
825
+ expandedSource = SwiftSyntaxMacroExpansion . collapse (
826
+ expansions: _expandedSources,
827
+ for: MacroRole ( rawMacroRole: rawMacroRole) ,
828
+ attachedTo: declarationNode
829
+ )
830
+ } else {
831
+ expandedSource = nil
832
+ }
833
+ diagnostics = _diagnostics
834
+ break
835
+ default :
817
836
throw PluginError . invalidReponseKind
818
837
}
819
838
@@ -827,7 +846,7 @@ func expandAttachedMacroIPC(
827
846
}
828
847
diagEngine. emit ( diagnostics, messageSuffix: " (from macro ' \( macroName) ') " )
829
848
}
830
- return expandedSources
849
+ return expandedSource
831
850
832
851
} catch let error {
833
852
let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
@@ -861,7 +880,7 @@ func expandAttachedMacroInProcess(
861
880
attachedTo declarationNode: DeclSyntax ,
862
881
parentDeclSourceFilePtr: UnsafePointer < ExportedSourceFile > ? ,
863
882
parentDeclNode: DeclSyntax ?
864
- ) -> [ String ] ? {
883
+ ) -> String ? {
865
884
// Get the macro.
866
885
let macroPtr = macroPtr. bindMemory ( to: ExportedMacro . self, capacity: 1 )
867
886
let macro = macroPtr. pointee. macro
@@ -925,54 +944,3 @@ fileprivate extension SyntaxProtocol {
925
944
return formatted. trimmedDescription ( matching: { $0. isWhitespace } )
926
945
}
927
946
}
928
-
929
- fileprivate func collapse< Node: SyntaxProtocol > (
930
- expansions: [ String ] ,
931
- for role: MacroRole ,
932
- attachedTo declarationNode: Node
933
- ) -> String {
934
- if expansions. isEmpty {
935
- return " "
936
- }
937
-
938
- var expansions = expansions
939
- var separator : String = " \n \n "
940
-
941
- if role == . accessor,
942
- let varDecl = declarationNode. as ( VariableDeclSyntax . self) ,
943
- let binding = varDecl. bindings. first,
944
- binding. accessor == nil {
945
- let indentation = String ( repeating: " " , count: 4 )
946
-
947
- expansions = expansions. map ( { indent ( $0, with: indentation) } )
948
- expansions [ 0 ] = " { \n " + expansions[ 0 ]
949
- expansions [ expansions. count - 1 ] += " \n } "
950
- } else if role == . memberAttribute {
951
- separator = " "
952
- }
953
-
954
- return expansions. joined ( separator: separator)
955
- }
956
-
957
- fileprivate func indent( _ source: String , with indentation: String ) -> String {
958
- if source. isEmpty || indentation. isEmpty {
959
- return source
960
- }
961
-
962
- var indented = " "
963
- var remaining = source [ ... ]
964
- while let nextNewline = remaining. firstIndex ( where: { $0. isNewline } ) {
965
- if nextNewline != remaining. startIndex {
966
- indented += indentation
967
- }
968
- indented += remaining [ ... nextNewline]
969
- remaining = remaining [ remaining. index ( after: nextNewline) ... ]
970
- }
971
-
972
- if !remaining. isEmpty {
973
- indented += indentation
974
- indented += remaining
975
- }
976
-
977
- return indented
978
- }
0 commit comments