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;
@@ -677,7 +681,7 @@ Expr *swift::expandMacroExpr(
677
681
return nullptr ;
678
682
679
683
// Evaluate the macro.
680
- NullTerminatedStringRef evaluatedSource;
684
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
681
685
682
686
MacroDecl *macro = cast<MacroDecl>(macroRef.getDecl ());
683
687
@@ -687,20 +691,15 @@ Expr *swift::expandMacroExpr(
687
691
}
688
692
689
693
// / The discriminator used for the macro.
690
- std::string cachedDiscriminator;
691
- auto getDiscriminator = [&]() -> StringRef {
692
- if (!cachedDiscriminator.empty ())
693
- return cachedDiscriminator;
694
-
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.
@@ -866,7 +858,7 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
866
858
return None;
867
859
868
860
// Evaluate the macro.
869
- NullTerminatedStringRef evaluatedSource;
861
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
870
862
871
863
MacroDecl *macro = cast<MacroDecl>(med->getMacroRef ().getDecl ());
872
864
auto macroRoles = macro->getMacroRoles ();
@@ -880,6 +872,16 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
880
872
return None;
881
873
}
882
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,16 @@ 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;
1096
1087
1097
- std::string discriminator;
1088
+ LazyValue<std::string> discriminator ([&]() -> std::string {
1089
+ #if SWIFT_SWIFT_PARSER
1090
+ Mangle::ASTMangler mangler;
1091
+ return mangler.mangleAttachedMacroExpansion (attachedTo, attr, role);
1092
+ #else
1093
+ return " " ;
1094
+ #endif
1095
+ });
1098
1096
auto macroDef = macro->getDefinition ();
1099
1097
switch (macroDef.kind ) {
1100
1098
case MacroDefinition::Kind::Undefined:
@@ -1115,7 +1113,8 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1115
1113
auto result = expandMacroDefinition (
1116
1114
macroDef.getExpanded (), macro, attr->getArgs ());
1117
1115
llvm::MallocAllocator allocator;
1118
- evaluatedSource = NullTerminatedStringRef (result, allocator);
1116
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
1117
+ result, adjustMacroExpansionBufferName (*discriminator));
1119
1118
break ;
1120
1119
}
1121
1120
@@ -1161,26 +1160,22 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1161
1160
if (auto var = dyn_cast<VarDecl>(attachedTo))
1162
1161
searchDecl = var->getParentPatternBinding ();
1163
1162
1164
- {
1165
- Mangle::ASTMangler mangler;
1166
- discriminator =
1167
- mangler.mangleAttachedMacroExpansion (attachedTo, attr, role);
1168
- }
1169
-
1170
1163
const char *evaluatedSourceAddress;
1171
1164
ptrdiff_t evaluatedSourceLength;
1172
1165
swift_ASTGen_expandAttachedMacro (
1173
1166
&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 (),
1167
+ static_cast <uint32_t >(externalDef->kind ), discriminator-> data (),
1168
+ discriminator-> size (), static_cast <uint32_t >(role),
1169
+ astGenAttrSourceFile, attr->AtLoc .getOpaquePointerValue (),
1170
+ astGenDeclSourceFile, searchDecl->getStartLoc ().getOpaquePointerValue (),
1178
1171
astGenParentDeclSourceFile, parentDeclLoc, &evaluatedSourceAddress,
1179
1172
&evaluatedSourceLength);
1180
1173
if (!evaluatedSourceAddress)
1181
1174
return nullptr ;
1182
- evaluatedSource = NullTerminatedStringRef (evaluatedSourceAddress,
1183
- (size_t )evaluatedSourceLength);
1175
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
1176
+ {evaluatedSourceAddress, (size_t )evaluatedSourceLength},
1177
+ adjustMacroExpansionBufferName (*discriminator));
1178
+ free ((void *)evaluatedSourceAddress);
1184
1179
break ;
1185
1180
#else
1186
1181
attachedTo->diagnose (diag::macro_unsupported);
@@ -1189,14 +1184,11 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1189
1184
}
1190
1185
}
1191
1186
1192
- // Figure out a reasonable name for the macro expansion buffer.
1193
- std::string bufferName = adjustMacroExpansionBufferName (discriminator);
1194
-
1195
1187
// Dump macro expansions to standard output, if requested.
1196
1188
if (ctx.LangOpts .DumpMacroExpansions ) {
1197
- llvm::errs () << bufferName
1189
+ llvm::errs () << evaluatedSource-> getBufferIdentifier ()
1198
1190
<< " \n ------------------------------\n "
1199
- << evaluatedSource
1191
+ << evaluatedSource-> getBuffer ()
1200
1192
<< " \n ------------------------------\n " ;
1201
1193
}
1202
1194
@@ -1284,9 +1276,8 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1284
1276
}
1285
1277
1286
1278
// 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));
1279
+ unsigned macroBufferID =
1280
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
1290
1281
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
1291
1282
GeneratedSourceInfo sourceInfo{
1292
1283
generatedSourceKind,
@@ -1297,7 +1288,6 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1297
1288
attr
1298
1289
};
1299
1290
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
1300
- free ((void *)evaluatedSource.data ());
1301
1291
1302
1292
// Create a source file to hold the macro buffer. This is automatically
1303
1293
// registered with the enclosing module.
0 commit comments