@@ -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
@@ -737,10 +754,10 @@ func expandAttachedMacro(
737
754
)
738
755
let discriminator = String ( decoding: discriminatorBuffer, as: UTF8 . self)
739
756
740
- let expandedSources : [ String ] ?
757
+ let expandedSource : String ?
741
758
switch MacroPluginKind ( rawValue: macroKind) ! {
742
759
case . Executable:
743
- expandedSources = expandAttachedMacroIPC (
760
+ expandedSource = expandAttachedMacroIPC (
744
761
diagEnginePtr: diagEnginePtr,
745
762
macroPtr: macroPtr,
746
763
rawMacroRole: rawMacroRole,
@@ -752,7 +769,7 @@ func expandAttachedMacro(
752
769
parentDeclSourceFilePtr: parentDeclSourceFilePtr,
753
770
parentDeclNode: parentDeclNode)
754
771
case . InProcess:
755
- expandedSources = expandAttachedMacroInProcess (
772
+ expandedSource = expandAttachedMacroInProcess (
756
773
diagEnginePtr: diagEnginePtr,
757
774
macroPtr: macroPtr,
758
775
rawMacroRole: rawMacroRole,
@@ -765,26 +782,11 @@ func expandAttachedMacro(
765
782
parentDeclNode: parentDeclNode)
766
783
}
767
784
768
- guard let expandedSources = expandedSources else {
769
- return - 1
770
- }
771
-
772
- // Fixup the source.
773
- var expandedSource : String = collapse ( expansions: expandedSources, for: MacroRole ( rawMacroRole: rawMacroRole) , attachedTo: declarationNode)
774
-
775
- // Form the result buffer for our caller.
776
- expandedSource. withUTF8 { utf8 in
777
- let evaluatedResultPtr = UnsafeMutablePointer< UInt8> . allocate( capacity: utf8. count + 1 )
778
- if let baseAddress = utf8. baseAddress {
779
- evaluatedResultPtr. initialize ( from: baseAddress, count: utf8. count)
780
- }
781
- evaluatedResultPtr [ utf8. count] = 0
782
-
783
- expandedSourcePointer. pointee = UnsafePointer ( evaluatedResultPtr)
784
- expandedSourceLength. pointee = utf8. count
785
- }
786
-
787
- return 0
785
+ return makeExpansionOutputResult (
786
+ expandedSource: expandedSource,
787
+ outputPointer: expandedSourcePointer,
788
+ outputLength: expandedSourceLength
789
+ )
788
790
}
789
791
790
792
func expandAttachedMacroIPC(
@@ -798,7 +800,7 @@ func expandAttachedMacroIPC(
798
800
attachedTo declarationNode: DeclSyntax ,
799
801
parentDeclSourceFilePtr: UnsafePointer < ExportedSourceFile > ? ,
800
802
parentDeclNode: DeclSyntax ?
801
- ) -> [ String ] ? {
803
+ ) -> String ? {
802
804
let macroName : String = customAttrNode. attributeName. description
803
805
let macro = macroPtr. assumingMemoryBound ( to: ExportedExecutableMacro . self) . pointee
804
806
@@ -840,10 +842,27 @@ func expandAttachedMacroIPC(
840
842
declSyntax: declSyntax,
841
843
parentDeclSyntax: parentDeclSyntax)
842
844
do {
843
- let result = try macro. plugin. sendMessageAndWait ( message)
844
- guard
845
- case . expandAttachedMacroResult( let expandedSources, let diagnostics) = result
846
- else {
845
+ let expandedSource : String ?
846
+ let diagnostics : [ PluginMessage . Diagnostic ]
847
+ switch try macro. plugin. sendMessageAndWait ( message) {
848
+ case . expandMacroResult( let _expandedSource, let _diagnostics) :
849
+ expandedSource = _expandedSource
850
+ diagnostics = _diagnostics
851
+
852
+ // Handle legacy result message.
853
+ case . expandAttachedMacroResult( let _expandedSources, let _diagnostics) :
854
+ if let _expandedSources = _expandedSources {
855
+ expandedSource = SwiftSyntaxMacroExpansion . collapse (
856
+ expansions: _expandedSources,
857
+ for: MacroRole ( rawMacroRole: rawMacroRole) ,
858
+ attachedTo: declarationNode
859
+ )
860
+ } else {
861
+ expandedSource = nil
862
+ }
863
+ diagnostics = _diagnostics
864
+ break
865
+ default :
847
866
throw PluginError . invalidReponseKind
848
867
}
849
868
@@ -857,7 +876,7 @@ func expandAttachedMacroIPC(
857
876
}
858
877
diagEngine. emit ( diagnostics, messageSuffix: " (from macro ' \( macroName) ') " )
859
878
}
860
- return expandedSources
879
+ return expandedSource
861
880
862
881
} catch let error {
863
882
let srcMgr = SourceManager ( cxxDiagnosticEngine: diagEnginePtr)
@@ -891,7 +910,7 @@ func expandAttachedMacroInProcess(
891
910
attachedTo declarationNode: DeclSyntax ,
892
911
parentDeclSourceFilePtr: UnsafePointer < ExportedSourceFile > ? ,
893
912
parentDeclNode: DeclSyntax ?
894
- ) -> [ String ] ? {
913
+ ) -> String ? {
895
914
// Get the macro.
896
915
let macroPtr = macroPtr. bindMemory ( to: ExportedMacro . self, capacity: 1 )
897
916
let macro = macroPtr. pointee. macro
@@ -955,54 +974,3 @@ fileprivate extension SyntaxProtocol {
955
974
return formatted. trimmedDescription ( matching: { $0. isWhitespace } )
956
975
}
957
976
}
958
-
959
- fileprivate func collapse< Node: SyntaxProtocol > (
960
- expansions: [ String ] ,
961
- for role: MacroRole ,
962
- attachedTo declarationNode: Node
963
- ) -> String {
964
- if expansions. isEmpty {
965
- return " "
966
- }
967
-
968
- var expansions = expansions
969
- var separator : String = " \n \n "
970
-
971
- if role == . accessor,
972
- let varDecl = declarationNode. as ( VariableDeclSyntax . self) ,
973
- let binding = varDecl. bindings. first,
974
- binding. accessor == nil {
975
- let indentation = String ( repeating: " " , count: 4 )
976
-
977
- expansions = expansions. map ( { indent ( $0, with: indentation) } )
978
- expansions [ 0 ] = " { \n " + expansions[ 0 ]
979
- expansions [ expansions. count - 1 ] += " \n } "
980
- } else if role == . memberAttribute {
981
- separator = " "
982
- }
983
-
984
- return expansions. joined ( separator: separator)
985
- }
986
-
987
- fileprivate func indent( _ source: String , with indentation: String ) -> String {
988
- if source. isEmpty || indentation. isEmpty {
989
- return source
990
- }
991
-
992
- var indented = " "
993
- var remaining = source [ ... ]
994
- while let nextNewline = remaining. firstIndex ( where: { $0. isNewline } ) {
995
- if nextNewline != remaining. startIndex {
996
- indented += indentation
997
- }
998
- indented += remaining [ ... nextNewline]
999
- remaining = remaining [ remaining. index ( after: nextNewline) ... ]
1000
- }
1001
-
1002
- if !remaining. isEmpty {
1003
- indented += indentation
1004
- indented += remaining
1005
- }
1006
-
1007
- return indented
1008
- }
0 commit comments