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"
@@ -457,6 +458,9 @@ ExternalMacroDefinitionRequest::evaluate(Evaluator &evaluator, ASTContext *ctx,
457
458
// / Adjust the given mangled name for a macro expansion to produce a valid
458
459
// / buffer name.
459
460
static std::string adjustMacroExpansionBufferName (StringRef name) {
461
+ if (name.empty ()) {
462
+ return " <macro-expansion>" ;
463
+ }
460
464
std::string result;
461
465
if (name.startswith (MANGLING_PREFIX_STR)) {
462
466
result += MACRO_EXPANSION_BUFFER_MANGLING_PREFIX;
@@ -689,31 +693,26 @@ Expr *swift::expandMacroExpr(
689
693
if (!sourceFile)
690
694
return nullptr ;
691
695
692
- // Evaluate the macro.
693
- NullTerminatedStringRef evaluatedSource;
694
-
695
696
MacroDecl *macro = cast<MacroDecl>(macroRef.getDecl ());
696
697
697
698
if (isFromExpansionOfMacro (sourceFile, macro, MacroRole::Expression)) {
698
699
ctx.Diags .diagnose (expr->getLoc (), diag::macro_recursive, macro->getName ());
699
700
return nullptr ;
700
701
}
701
702
702
- // / The discriminator used for the macro.
703
- std::string cachedDiscriminator;
704
- auto getDiscriminator = [&]() -> StringRef {
705
- if (!cachedDiscriminator.empty ())
706
- return cachedDiscriminator;
703
+ // Evaluate the macro.
704
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
707
705
706
+ // / The discriminator used for the macro.
707
+ LazyValue<std::string> discriminator ([&]() -> std::string {
708
708
#if SWIFT_SWIFT_PARSER
709
709
if (auto expansionExpr = dyn_cast<MacroExpansionExpr>(expr)) {
710
710
Mangle::ASTMangler mangler;
711
- cachedDiscriminator = mangler.mangleMacroExpansion (expansionExpr);
711
+ return mangler.mangleMacroExpansion (expansionExpr);
712
712
}
713
713
#endif
714
-
715
- return cachedDiscriminator;
716
- };
714
+ return " " ;
715
+ });
717
716
718
717
auto macroDef = macro->getDefinition ();
719
718
switch (macroDef.kind ) {
@@ -735,8 +734,8 @@ Expr *swift::expandMacroExpr(
735
734
// Expand the definition with the given arguments.
736
735
auto result = expandMacroDefinition (
737
736
macroDef.getExpanded (), macro, expr->getArgs ());
738
- llvm::MallocAllocator allocator;
739
- evaluatedSource = NullTerminatedStringRef ( result, allocator );
737
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
738
+ result, adjustMacroExpansionBufferName (*discriminator) );
740
739
break ;
741
740
}
742
741
@@ -770,14 +769,16 @@ Expr *swift::expandMacroExpr(
770
769
ptrdiff_t evaluatedSourceLength;
771
770
swift_ASTGen_expandFreestandingMacro (
772
771
&ctx.Diags , externalDef->opaqueHandle ,
773
- static_cast <uint32_t >(externalDef->kind ), getDiscriminator (). data (),
774
- getDiscriminator (). size (), astGenSourceFile,
772
+ static_cast <uint32_t >(externalDef->kind ), discriminator-> data (),
773
+ discriminator-> size (), astGenSourceFile,
775
774
expr->getStartLoc ().getOpaquePointerValue (), &evaluatedSourceAddress,
776
775
&evaluatedSourceLength);
777
776
if (!evaluatedSourceAddress)
778
777
return nullptr ;
779
- evaluatedSource = NullTerminatedStringRef (evaluatedSourceAddress,
780
- (size_t )evaluatedSourceLength);
778
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
779
+ {evaluatedSourceAddress, (size_t )evaluatedSourceLength},
780
+ adjustMacroExpansionBufferName (*discriminator));
781
+ free ((void *)evaluatedSourceAddress);
781
782
break ;
782
783
#else
783
784
ctx.Diags .diagnose (expr->getLoc (), diag::macro_unsupported);
@@ -786,26 +787,18 @@ Expr *swift::expandMacroExpr(
786
787
}
787
788
}
788
789
789
- // Figure out a reasonable name for the macro expansion buffer.
790
- std::string bufferName;
791
- if (getDiscriminator ().empty ())
792
- bufferName = " macro-expansion" ;
793
- else {
794
- bufferName = adjustMacroExpansionBufferName (getDiscriminator ());
795
- }
796
-
797
790
// Dump macro expansions to standard output, if requested.
798
791
if (ctx.LangOpts .DumpMacroExpansions ) {
799
- llvm::errs () << bufferName << " as " << expandedType.getString ()
792
+ llvm::errs () << evaluatedSource->getBufferIdentifier () << " as "
793
+ << expandedType.getString ()
800
794
<< " \n ------------------------------\n "
801
- << evaluatedSource
795
+ << evaluatedSource-> getBuffer ()
802
796
<< " \n ------------------------------\n " ;
803
797
}
804
798
805
799
// Create a new source buffer with the contents of the expanded macro.
806
- auto macroBuffer =
807
- llvm::MemoryBuffer::getMemBufferCopy (evaluatedSource, bufferName);
808
- unsigned macroBufferID = sourceMgr.addNewSourceBuffer (std::move (macroBuffer));
800
+ unsigned macroBufferID =
801
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
809
802
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
810
803
GeneratedSourceInfo sourceInfo{
811
804
GeneratedSourceInfo::ExpressionMacroExpansion,
@@ -816,7 +809,6 @@ Expr *swift::expandMacroExpr(
816
809
dc
817
810
};
818
811
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
819
- free ((void *)evaluatedSource.data ());
820
812
821
813
// Create a source file to hold the macro buffer. This is automatically
822
814
// registered with the enclosing module.
@@ -878,9 +870,6 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
878
870
if (!sourceFile)
879
871
return None;
880
872
881
- // Evaluate the macro.
882
- NullTerminatedStringRef evaluatedSource;
883
-
884
873
MacroDecl *macro = cast<MacroDecl>(med->getMacroRef ().getDecl ());
885
874
auto macroRoles = macro->getMacroRoles ();
886
875
assert (macroRoles.contains (MacroRole::Declaration) ||
@@ -893,6 +882,19 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
893
882
return None;
894
883
}
895
884
885
+ // Evaluate the macro.
886
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
887
+
888
+ // / The discriminator used for the macro.
889
+ LazyValue<std::string> discriminator ([&]() -> std::string {
890
+ #if SWIFT_SWIFT_PARSER
891
+ Mangle::ASTMangler mangler;
892
+ return mangler.mangleMacroExpansion (med);
893
+ #else
894
+ return " " ;
895
+ #endif
896
+ });
897
+
896
898
auto macroDef = macro->getDefinition ();
897
899
switch (macroDef.kind ) {
898
900
case MacroDefinition::Kind::Undefined:
@@ -912,8 +914,8 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
912
914
// Expand the definition with the given arguments.
913
915
auto result = expandMacroDefinition (
914
916
macroDef.getExpanded (), macro, med->getArgs ());
915
- llvm::MallocAllocator allocator;
916
- evaluatedSource = NullTerminatedStringRef ( result, allocator );
917
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
918
+ result, adjustMacroExpansionBufferName (*discriminator) );
917
919
break ;
918
920
}
919
921
@@ -958,21 +960,20 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
958
960
if (!astGenSourceFile)
959
961
return None;
960
962
961
- Mangle::ASTMangler mangler;
962
- auto discriminator = mangler.mangleMacroExpansion (med);
963
-
964
963
const char *evaluatedSourceAddress;
965
964
ptrdiff_t evaluatedSourceLength;
966
965
swift_ASTGen_expandFreestandingMacro (
967
966
&ctx.Diags , externalDef->opaqueHandle ,
968
- static_cast <uint32_t >(externalDef->kind ), discriminator. data (),
969
- discriminator. size (), astGenSourceFile,
967
+ static_cast <uint32_t >(externalDef->kind ), discriminator-> data (),
968
+ discriminator-> size (), astGenSourceFile,
970
969
med->getStartLoc ().getOpaquePointerValue (), &evaluatedSourceAddress,
971
970
&evaluatedSourceLength);
972
971
if (!evaluatedSourceAddress)
973
972
return None;
974
- evaluatedSource = NullTerminatedStringRef (evaluatedSourceAddress,
975
- (size_t )evaluatedSourceLength);
973
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
974
+ {evaluatedSourceAddress, (size_t )evaluatedSourceLength},
975
+ adjustMacroExpansionBufferName (*discriminator));
976
+ free ((void *)evaluatedSourceAddress);
976
977
break ;
977
978
#else
978
979
med->diagnose (diag::macro_unsupported);
@@ -981,26 +982,17 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
981
982
}
982
983
}
983
984
984
- // Figure out a reasonable name for the macro expansion buffer.
985
- std::string bufferName;
986
- {
987
- Mangle::ASTMangler mangler;
988
- bufferName = adjustMacroExpansionBufferName (
989
- mangler.mangleMacroExpansion (med));
990
- }
991
-
992
985
// Dump macro expansions to standard output, if requested.
993
986
if (ctx.LangOpts .DumpMacroExpansions ) {
994
- llvm::errs () << bufferName
987
+ llvm::errs () << evaluatedSource-> getBufferIdentifier ()
995
988
<< " \n ------------------------------\n "
996
- << evaluatedSource
989
+ << evaluatedSource-> getBuffer ()
997
990
<< " \n ------------------------------\n " ;
998
991
}
999
992
1000
993
// Create a new source buffer with the contents of the expanded macro.
1001
- auto macroBuffer =
1002
- llvm::MemoryBuffer::getMemBufferCopy (evaluatedSource, bufferName);
1003
- unsigned macroBufferID = sourceMgr.addNewSourceBuffer (std::move (macroBuffer));
994
+ unsigned macroBufferID =
995
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
1004
996
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
1005
997
GeneratedSourceInfo sourceInfo{
1006
998
GeneratedSourceInfo::FreestandingDeclMacroExpansion,
@@ -1011,7 +1003,6 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
1011
1003
dc
1012
1004
};
1013
1005
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
1014
- free ((void *)evaluatedSource.data ());
1015
1006
1016
1007
// Create a source file to hold the macro buffer. This is automatically
1017
1008
// registered with the enclosing module.
@@ -1105,9 +1096,18 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1105
1096
}
1106
1097
1107
1098
// Evaluate the macro.
1108
- NullTerminatedStringRef evaluatedSource;
1099
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
1100
+
1101
+ // / The discriminator used for the macro.
1102
+ LazyValue<std::string> discriminator ([&]() -> std::string {
1103
+ #if SWIFT_SWIFT_PARSER
1104
+ Mangle::ASTMangler mangler;
1105
+ return mangler.mangleAttachedMacroExpansion (attachedTo, attr, role);
1106
+ #else
1107
+ return " " ;
1108
+ #endif
1109
+ });
1109
1110
1110
- std::string discriminator;
1111
1111
auto macroDef = macro->getDefinition ();
1112
1112
switch (macroDef.kind ) {
1113
1113
case MacroDefinition::Kind::Undefined:
@@ -1128,7 +1128,8 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1128
1128
auto result = expandMacroDefinition (
1129
1129
macroDef.getExpanded (), macro, attr->getArgs ());
1130
1130
llvm::MallocAllocator allocator;
1131
- evaluatedSource = NullTerminatedStringRef (result, allocator);
1131
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
1132
+ result, adjustMacroExpansionBufferName (*discriminator));
1132
1133
break ;
1133
1134
}
1134
1135
@@ -1174,26 +1175,22 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1174
1175
if (auto var = dyn_cast<VarDecl>(attachedTo))
1175
1176
searchDecl = var->getParentPatternBinding ();
1176
1177
1177
- {
1178
- Mangle::ASTMangler mangler;
1179
- discriminator =
1180
- mangler.mangleAttachedMacroExpansion (attachedTo, attr, role);
1181
- }
1182
-
1183
1178
const char *evaluatedSourceAddress;
1184
1179
ptrdiff_t evaluatedSourceLength;
1185
1180
swift_ASTGen_expandAttachedMacro (
1186
1181
&ctx.Diags , externalDef->opaqueHandle ,
1187
- static_cast <uint32_t >(externalDef->kind ), discriminator. data (),
1188
- discriminator. size (), static_cast <uint32_t >(role), astGenAttrSourceFile ,
1189
- attr->AtLoc .getOpaquePointerValue (), astGenDeclSourceFile ,
1190
- searchDecl->getStartLoc ().getOpaquePointerValue (),
1182
+ static_cast <uint32_t >(externalDef->kind ), discriminator-> data (),
1183
+ discriminator-> size (), static_cast <uint32_t >(role),
1184
+ astGenAttrSourceFile, attr->AtLoc .getOpaquePointerValue (),
1185
+ astGenDeclSourceFile, searchDecl->getStartLoc ().getOpaquePointerValue (),
1191
1186
astGenParentDeclSourceFile, parentDeclLoc, &evaluatedSourceAddress,
1192
1187
&evaluatedSourceLength);
1193
1188
if (!evaluatedSourceAddress)
1194
1189
return nullptr ;
1195
- evaluatedSource = NullTerminatedStringRef (evaluatedSourceAddress,
1196
- (size_t )evaluatedSourceLength);
1190
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
1191
+ {evaluatedSourceAddress, (size_t )evaluatedSourceLength},
1192
+ adjustMacroExpansionBufferName (*discriminator));
1193
+ free ((void *)evaluatedSourceAddress);
1197
1194
break ;
1198
1195
#else
1199
1196
attachedTo->diagnose (diag::macro_unsupported);
@@ -1202,14 +1199,11 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1202
1199
}
1203
1200
}
1204
1201
1205
- // Figure out a reasonable name for the macro expansion buffer.
1206
- std::string bufferName = adjustMacroExpansionBufferName (discriminator);
1207
-
1208
1202
// Dump macro expansions to standard output, if requested.
1209
1203
if (ctx.LangOpts .DumpMacroExpansions ) {
1210
- llvm::errs () << bufferName
1204
+ llvm::errs () << evaluatedSource-> getBufferIdentifier ()
1211
1205
<< " \n ------------------------------\n "
1212
- << evaluatedSource
1206
+ << evaluatedSource-> getBuffer ()
1213
1207
<< " \n ------------------------------\n " ;
1214
1208
}
1215
1209
@@ -1297,9 +1291,8 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1297
1291
}
1298
1292
1299
1293
// Create a new source buffer with the contents of the expanded macro.
1300
- auto macroBuffer =
1301
- llvm::MemoryBuffer::getMemBufferCopy (evaluatedSource, bufferName);
1302
- unsigned macroBufferID = sourceMgr.addNewSourceBuffer (std::move (macroBuffer));
1294
+ unsigned macroBufferID =
1295
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
1303
1296
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
1304
1297
GeneratedSourceInfo sourceInfo{
1305
1298
generatedSourceKind,
@@ -1310,7 +1303,6 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1310
1303
attr
1311
1304
};
1312
1305
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
1313
- free ((void *)evaluatedSource.data ());
1314
1306
1315
1307
// Create a source file to hold the macro buffer. This is automatically
1316
1308
// registered with the enclosing module.
0 commit comments