30
30
#include " swift/AST/SourceFile.h"
31
31
#include " swift/AST/TypeCheckRequests.h"
32
32
#include " swift/Basic/Defer.h"
33
+ #include " swift/Basic/Lazy.h"
33
34
#include " swift/Basic/SourceManager.h"
34
35
#include " swift/Basic/StringExtras.h"
35
36
#include " swift/Demangling/Demangler.h"
@@ -441,6 +442,9 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
441
442
// / Adjust the given mangled name for a macro expansion to produce a valid
442
443
// / buffer name.
443
444
static std::string adjustMacroExpansionBufferName (StringRef name) {
445
+ if (name.empty ()) {
446
+ return " <macro-expansion>" ;
447
+ }
444
448
std::string result;
445
449
if (name.startswith (MANGLING_PREFIX_STR)) {
446
450
result += MACRO_EXPANSION_BUFFER_MANGLING_PREFIX;
@@ -676,31 +680,26 @@ Expr *swift::expandMacroExpr(
676
680
if (!sourceFile)
677
681
return nullptr ;
678
682
679
- // Evaluate the macro.
680
- NullTerminatedStringRef evaluatedSource;
681
-
682
683
MacroDecl *macro = cast<MacroDecl>(macroRef.getDecl ());
683
684
684
685
if (isFromExpansionOfMacro (sourceFile, macro, MacroRole::Expression)) {
685
686
ctx.Diags .diagnose (expr->getLoc (), diag::macro_recursive, macro->getName ());
686
687
return nullptr ;
687
688
}
688
689
689
- // / The discriminator used for the macro.
690
- std::string cachedDiscriminator;
691
- auto getDiscriminator = [&]() -> StringRef {
692
- if (!cachedDiscriminator.empty ())
693
- return cachedDiscriminator;
690
+ // Evaluate the macro.
691
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
694
692
693
+ // / The discriminator used for the macro.
694
+ LazyValue<std::string> discriminator ([&]() -> std::string {
695
695
#if SWIFT_SWIFT_PARSER
696
696
if (auto expansionExpr = dyn_cast<MacroExpansionExpr>(expr)) {
697
697
Mangle::ASTMangler mangler;
698
- cachedDiscriminator = mangler.mangleMacroExpansion (expansionExpr);
698
+ return mangler.mangleMacroExpansion (expansionExpr);
699
699
}
700
700
#endif
701
-
702
- return cachedDiscriminator;
703
- };
701
+ return " " ;
702
+ });
704
703
705
704
auto macroDef = macro->getDefinition ();
706
705
switch (macroDef.kind ) {
@@ -722,8 +721,8 @@ Expr *swift::expandMacroExpr(
722
721
// Expand the definition with the given arguments.
723
722
auto result = expandMacroDefinition (
724
723
macroDef.getExpanded (), macro, expr->getArgs ());
725
- llvm::MallocAllocator allocator;
726
- evaluatedSource = NullTerminatedStringRef ( result, allocator );
724
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
725
+ result, adjustMacroExpansionBufferName (*discriminator) );
727
726
break ;
728
727
}
729
728
@@ -757,14 +756,16 @@ Expr *swift::expandMacroExpr(
757
756
ptrdiff_t evaluatedSourceLength;
758
757
swift_ASTGen_expandFreestandingMacro (
759
758
&ctx.Diags , externalDef->opaqueHandle ,
760
- static_cast <uint32_t >(externalDef->kind ), getDiscriminator (). data (),
761
- getDiscriminator (). size (), astGenSourceFile,
759
+ static_cast <uint32_t >(externalDef->kind ), discriminator-> data (),
760
+ discriminator-> size (), astGenSourceFile,
762
761
expr->getStartLoc ().getOpaquePointerValue (), &evaluatedSourceAddress,
763
762
&evaluatedSourceLength);
764
763
if (!evaluatedSourceAddress)
765
764
return nullptr ;
766
- evaluatedSource = NullTerminatedStringRef (evaluatedSourceAddress,
767
- (size_t )evaluatedSourceLength);
765
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
766
+ {evaluatedSourceAddress, (size_t )evaluatedSourceLength},
767
+ adjustMacroExpansionBufferName (*discriminator));
768
+ free ((void *)evaluatedSourceAddress);
768
769
break ;
769
770
#else
770
771
ctx.Diags .diagnose (expr->getLoc (), diag::macro_unsupported);
@@ -773,26 +774,18 @@ Expr *swift::expandMacroExpr(
773
774
}
774
775
}
775
776
776
- // Figure out a reasonable name for the macro expansion buffer.
777
- std::string bufferName;
778
- if (getDiscriminator ().empty ())
779
- bufferName = " macro-expansion" ;
780
- else {
781
- bufferName = adjustMacroExpansionBufferName (getDiscriminator ());
782
- }
783
-
784
777
// Dump macro expansions to standard output, if requested.
785
778
if (ctx.LangOpts .DumpMacroExpansions ) {
786
- llvm::errs () << bufferName << " as " << expandedType.getString ()
779
+ llvm::errs () << evaluatedSource->getBufferIdentifier () << " as "
780
+ << expandedType.getString ()
787
781
<< " \n ------------------------------\n "
788
- << evaluatedSource
782
+ << evaluatedSource-> getBuffer ()
789
783
<< " \n ------------------------------\n " ;
790
784
}
791
785
792
786
// Create a new source buffer with the contents of the expanded macro.
793
- auto macroBuffer =
794
- llvm::MemoryBuffer::getMemBufferCopy (evaluatedSource, bufferName);
795
- unsigned macroBufferID = sourceMgr.addNewSourceBuffer (std::move (macroBuffer));
787
+ unsigned macroBufferID =
788
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
796
789
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
797
790
GeneratedSourceInfo sourceInfo{
798
791
GeneratedSourceInfo::ExpressionMacroExpansion,
@@ -803,7 +796,6 @@ Expr *swift::expandMacroExpr(
803
796
dc
804
797
};
805
798
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
806
- free ((void *)evaluatedSource.data ());
807
799
808
800
// Create a source file to hold the macro buffer. This is automatically
809
801
// registered with the enclosing module.
@@ -865,9 +857,6 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
865
857
if (!sourceFile)
866
858
return None;
867
859
868
- // Evaluate the macro.
869
- NullTerminatedStringRef evaluatedSource;
870
-
871
860
MacroDecl *macro = cast<MacroDecl>(med->getMacroRef ().getDecl ());
872
861
auto macroRoles = macro->getMacroRoles ();
873
862
assert (macroRoles.contains (MacroRole::Declaration) ||
@@ -880,6 +869,19 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
880
869
return None;
881
870
}
882
871
872
+ // Evaluate the macro.
873
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
874
+
875
+ // / The discriminator used for the macro.
876
+ LazyValue<std::string> discriminator ([&]() -> std::string {
877
+ #if SWIFT_SWIFT_PARSER
878
+ Mangle::ASTMangler mangler;
879
+ return mangler.mangleMacroExpansion (med);
880
+ #else
881
+ return " " ;
882
+ #endif
883
+ });
884
+
883
885
auto macroDef = macro->getDefinition ();
884
886
switch (macroDef.kind ) {
885
887
case MacroDefinition::Kind::Undefined:
@@ -899,8 +901,8 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
899
901
// Expand the definition with the given arguments.
900
902
auto result = expandMacroDefinition (
901
903
macroDef.getExpanded (), macro, med->getArgs ());
902
- llvm::MallocAllocator allocator;
903
- evaluatedSource = NullTerminatedStringRef ( result, allocator );
904
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
905
+ result, adjustMacroExpansionBufferName (*discriminator) );
904
906
break ;
905
907
}
906
908
@@ -945,21 +947,20 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
945
947
if (!astGenSourceFile)
946
948
return None;
947
949
948
- Mangle::ASTMangler mangler;
949
- auto discriminator = mangler.mangleMacroExpansion (med);
950
-
951
950
const char *evaluatedSourceAddress;
952
951
ptrdiff_t evaluatedSourceLength;
953
952
swift_ASTGen_expandFreestandingMacro (
954
953
&ctx.Diags , externalDef->opaqueHandle ,
955
- static_cast <uint32_t >(externalDef->kind ), discriminator. data (),
956
- discriminator. size (), astGenSourceFile,
954
+ static_cast <uint32_t >(externalDef->kind ), discriminator-> data (),
955
+ discriminator-> size (), astGenSourceFile,
957
956
med->getStartLoc ().getOpaquePointerValue (), &evaluatedSourceAddress,
958
957
&evaluatedSourceLength);
959
958
if (!evaluatedSourceAddress)
960
959
return None;
961
- evaluatedSource = NullTerminatedStringRef (evaluatedSourceAddress,
962
- (size_t )evaluatedSourceLength);
960
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
961
+ {evaluatedSourceAddress, (size_t )evaluatedSourceLength},
962
+ adjustMacroExpansionBufferName (*discriminator));
963
+ free ((void *)evaluatedSourceAddress);
963
964
break ;
964
965
#else
965
966
med->diagnose (diag::macro_unsupported);
@@ -968,26 +969,17 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
968
969
}
969
970
}
970
971
971
- // Figure out a reasonable name for the macro expansion buffer.
972
- std::string bufferName;
973
- {
974
- Mangle::ASTMangler mangler;
975
- bufferName = adjustMacroExpansionBufferName (
976
- mangler.mangleMacroExpansion (med));
977
- }
978
-
979
972
// Dump macro expansions to standard output, if requested.
980
973
if (ctx.LangOpts .DumpMacroExpansions ) {
981
- llvm::errs () << bufferName
974
+ llvm::errs () << evaluatedSource-> getBufferIdentifier ()
982
975
<< " \n ------------------------------\n "
983
- << evaluatedSource
976
+ << evaluatedSource-> getBuffer ()
984
977
<< " \n ------------------------------\n " ;
985
978
}
986
979
987
980
// Create a new source buffer with the contents of the expanded macro.
988
- auto macroBuffer =
989
- llvm::MemoryBuffer::getMemBufferCopy (evaluatedSource, bufferName);
990
- unsigned macroBufferID = sourceMgr.addNewSourceBuffer (std::move (macroBuffer));
981
+ unsigned macroBufferID =
982
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
991
983
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
992
984
GeneratedSourceInfo sourceInfo{
993
985
GeneratedSourceInfo::FreestandingDeclMacroExpansion,
@@ -998,7 +990,6 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
998
990
dc
999
991
};
1000
992
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
1001
- free ((void *)evaluatedSource.data ());
1002
993
1003
994
// Create a source file to hold the macro buffer. This is automatically
1004
995
// registered with the enclosing module.
@@ -1092,9 +1083,18 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1092
1083
}
1093
1084
1094
1085
// Evaluate the macro.
1095
- NullTerminatedStringRef evaluatedSource;
1086
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
1087
+
1088
+ // / The discriminator used for the macro.
1089
+ LazyValue<std::string> discriminator ([&]() -> std::string {
1090
+ #if SWIFT_SWIFT_PARSER
1091
+ Mangle::ASTMangler mangler;
1092
+ return mangler.mangleAttachedMacroExpansion (attachedTo, attr, role);
1093
+ #else
1094
+ return " " ;
1095
+ #endif
1096
+ });
1096
1097
1097
- std::string discriminator;
1098
1098
auto macroDef = macro->getDefinition ();
1099
1099
switch (macroDef.kind ) {
1100
1100
case MacroDefinition::Kind::Undefined:
@@ -1115,7 +1115,8 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1115
1115
auto result = expandMacroDefinition (
1116
1116
macroDef.getExpanded (), macro, attr->getArgs ());
1117
1117
llvm::MallocAllocator allocator;
1118
- evaluatedSource = NullTerminatedStringRef (result, allocator);
1118
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
1119
+ result, adjustMacroExpansionBufferName (*discriminator));
1119
1120
break ;
1120
1121
}
1121
1122
@@ -1161,26 +1162,22 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1161
1162
if (auto var = dyn_cast<VarDecl>(attachedTo))
1162
1163
searchDecl = var->getParentPatternBinding ();
1163
1164
1164
- {
1165
- Mangle::ASTMangler mangler;
1166
- discriminator =
1167
- mangler.mangleAttachedMacroExpansion (attachedTo, attr, role);
1168
- }
1169
-
1170
1165
const char *evaluatedSourceAddress;
1171
1166
ptrdiff_t evaluatedSourceLength;
1172
1167
swift_ASTGen_expandAttachedMacro (
1173
1168
&ctx.Diags , externalDef->opaqueHandle ,
1174
- static_cast <uint32_t >(externalDef->kind ), discriminator. data (),
1175
- discriminator. size (), static_cast <uint32_t >(role), astGenAttrSourceFile ,
1176
- attr->AtLoc .getOpaquePointerValue (), astGenDeclSourceFile ,
1177
- searchDecl->getStartLoc ().getOpaquePointerValue (),
1169
+ static_cast <uint32_t >(externalDef->kind ), discriminator-> data (),
1170
+ discriminator-> size (), static_cast <uint32_t >(role),
1171
+ astGenAttrSourceFile, attr->AtLoc .getOpaquePointerValue (),
1172
+ astGenDeclSourceFile, searchDecl->getStartLoc ().getOpaquePointerValue (),
1178
1173
astGenParentDeclSourceFile, parentDeclLoc, &evaluatedSourceAddress,
1179
1174
&evaluatedSourceLength);
1180
1175
if (!evaluatedSourceAddress)
1181
1176
return nullptr ;
1182
- evaluatedSource = NullTerminatedStringRef (evaluatedSourceAddress,
1183
- (size_t )evaluatedSourceLength);
1177
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
1178
+ {evaluatedSourceAddress, (size_t )evaluatedSourceLength},
1179
+ adjustMacroExpansionBufferName (*discriminator));
1180
+ free ((void *)evaluatedSourceAddress);
1184
1181
break ;
1185
1182
#else
1186
1183
attachedTo->diagnose (diag::macro_unsupported);
@@ -1189,14 +1186,11 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1189
1186
}
1190
1187
}
1191
1188
1192
- // Figure out a reasonable name for the macro expansion buffer.
1193
- std::string bufferName = adjustMacroExpansionBufferName (discriminator);
1194
-
1195
1189
// Dump macro expansions to standard output, if requested.
1196
1190
if (ctx.LangOpts .DumpMacroExpansions ) {
1197
- llvm::errs () << bufferName
1191
+ llvm::errs () << evaluatedSource-> getBufferIdentifier ()
1198
1192
<< " \n ------------------------------\n "
1199
- << evaluatedSource
1193
+ << evaluatedSource-> getBuffer ()
1200
1194
<< " \n ------------------------------\n " ;
1201
1195
}
1202
1196
@@ -1284,9 +1278,8 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1284
1278
}
1285
1279
1286
1280
// Create a new source buffer with the contents of the expanded macro.
1287
- auto macroBuffer =
1288
- llvm::MemoryBuffer::getMemBufferCopy (evaluatedSource, bufferName);
1289
- unsigned macroBufferID = sourceMgr.addNewSourceBuffer (std::move (macroBuffer));
1281
+ unsigned macroBufferID =
1282
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
1290
1283
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
1291
1284
GeneratedSourceInfo sourceInfo{
1292
1285
generatedSourceKind,
@@ -1297,7 +1290,6 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1297
1290
attr
1298
1291
};
1299
1292
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
1300
- free ((void *)evaluatedSource.data ());
1301
1293
1302
1294
// Create a source file to hold the macro buffer. This is automatically
1303
1295
// registered with the enclosing module.
0 commit comments