@@ -677,7 +677,7 @@ Expr *swift::expandMacroExpr(
677
677
return nullptr ;
678
678
679
679
// Evaluate the macro.
680
- NullTerminatedStringRef evaluatedSource;
680
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
681
681
682
682
MacroDecl *macro = cast<MacroDecl>(macroRef.getDecl ());
683
683
@@ -722,8 +722,7 @@ Expr *swift::expandMacroExpr(
722
722
// Expand the definition with the given arguments.
723
723
auto result = expandMacroDefinition (
724
724
macroDef.getExpanded (), macro, expr->getArgs ());
725
- llvm::MallocAllocator allocator;
726
- evaluatedSource = NullTerminatedStringRef (result, allocator);
725
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (result);
727
726
break ;
728
727
}
729
728
@@ -763,8 +762,9 @@ Expr *swift::expandMacroExpr(
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
+ free ((void *)evaluatedSourceAddress);
768
768
break ;
769
769
#else
770
770
ctx.Diags .diagnose (expr->getLoc (), diag::macro_unsupported);
@@ -785,14 +785,13 @@ Expr *swift::expandMacroExpr(
785
785
if (ctx.LangOpts .DumpMacroExpansions ) {
786
786
llvm::errs () << bufferName << " as " << expandedType.getString ()
787
787
<< " \n ------------------------------\n "
788
- << evaluatedSource
788
+ << evaluatedSource-> getBuffer ()
789
789
<< " \n ------------------------------\n " ;
790
790
}
791
791
792
792
// 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));
793
+ unsigned macroBufferID =
794
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
796
795
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
797
796
GeneratedSourceInfo sourceInfo{
798
797
GeneratedSourceInfo::ExpressionMacroExpansion,
@@ -803,7 +802,6 @@ Expr *swift::expandMacroExpr(
803
802
dc
804
803
};
805
804
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
806
- free ((void *)evaluatedSource.data ());
807
805
808
806
// Create a source file to hold the macro buffer. This is automatically
809
807
// registered with the enclosing module.
@@ -866,7 +864,7 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
866
864
return None;
867
865
868
866
// Evaluate the macro.
869
- NullTerminatedStringRef evaluatedSource;
867
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
870
868
871
869
MacroDecl *macro = cast<MacroDecl>(med->getMacroRef ().getDecl ());
872
870
auto macroRoles = macro->getMacroRoles ();
@@ -899,8 +897,7 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
899
897
// Expand the definition with the given arguments.
900
898
auto result = expandMacroDefinition (
901
899
macroDef.getExpanded (), macro, med->getArgs ());
902
- llvm::MallocAllocator allocator;
903
- evaluatedSource = NullTerminatedStringRef (result, allocator);
900
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (result);
904
901
break ;
905
902
}
906
903
@@ -958,8 +955,9 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
958
955
&evaluatedSourceLength);
959
956
if (!evaluatedSourceAddress)
960
957
return None;
961
- evaluatedSource = NullTerminatedStringRef (evaluatedSourceAddress,
962
- (size_t )evaluatedSourceLength);
958
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
959
+ {evaluatedSourceAddress, (size_t )evaluatedSourceLength});
960
+ free ((void *)evaluatedSourceAddress);
963
961
break ;
964
962
#else
965
963
med->diagnose (diag::macro_unsupported);
@@ -980,14 +978,13 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
980
978
if (ctx.LangOpts .DumpMacroExpansions ) {
981
979
llvm::errs () << bufferName
982
980
<< " \n ------------------------------\n "
983
- << evaluatedSource
981
+ << evaluatedSource-> getBuffer ()
984
982
<< " \n ------------------------------\n " ;
985
983
}
986
984
987
985
// 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));
986
+ unsigned macroBufferID =
987
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
991
988
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
992
989
GeneratedSourceInfo sourceInfo{
993
990
GeneratedSourceInfo::FreestandingDeclMacroExpansion,
@@ -998,7 +995,6 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
998
995
dc
999
996
};
1000
997
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
1001
- free ((void *)evaluatedSource.data ());
1002
998
1003
999
// Create a source file to hold the macro buffer. This is automatically
1004
1000
// registered with the enclosing module.
@@ -1092,7 +1088,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1092
1088
}
1093
1089
1094
1090
// Evaluate the macro.
1095
- NullTerminatedStringRef evaluatedSource;
1091
+ std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
1096
1092
1097
1093
std::string discriminator;
1098
1094
auto macroDef = macro->getDefinition ();
@@ -1115,7 +1111,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1115
1111
auto result = expandMacroDefinition (
1116
1112
macroDef.getExpanded (), macro, attr->getArgs ());
1117
1113
llvm::MallocAllocator allocator;
1118
- evaluatedSource = NullTerminatedStringRef (result, allocator );
1114
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (result);
1119
1115
break ;
1120
1116
}
1121
1117
@@ -1179,8 +1175,9 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1179
1175
&evaluatedSourceLength);
1180
1176
if (!evaluatedSourceAddress)
1181
1177
return nullptr ;
1182
- evaluatedSource = NullTerminatedStringRef (evaluatedSourceAddress,
1183
- (size_t )evaluatedSourceLength);
1178
+ evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy (
1179
+ {evaluatedSourceAddress, (size_t )evaluatedSourceLength});
1180
+ free ((void *)evaluatedSourceAddress);
1184
1181
break ;
1185
1182
#else
1186
1183
attachedTo->diagnose (diag::macro_unsupported);
@@ -1196,7 +1193,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1196
1193
if (ctx.LangOpts .DumpMacroExpansions ) {
1197
1194
llvm::errs () << bufferName
1198
1195
<< " \n ------------------------------\n "
1199
- << evaluatedSource
1196
+ << evaluatedSource-> getBuffer ()
1200
1197
<< " \n ------------------------------\n " ;
1201
1198
}
1202
1199
@@ -1284,9 +1281,8 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1284
1281
}
1285
1282
1286
1283
// 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));
1284
+ unsigned macroBufferID =
1285
+ sourceMgr.addNewSourceBuffer (std::move (evaluatedSource));
1290
1286
auto macroBufferRange = sourceMgr.getRangeForBuffer (macroBufferID);
1291
1287
GeneratedSourceInfo sourceInfo{
1292
1288
generatedSourceKind,
@@ -1297,7 +1293,6 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
1297
1293
attr
1298
1294
};
1299
1295
sourceMgr.setGeneratedSourceInfo (macroBufferID, sourceInfo);
1300
- free ((void *)evaluatedSource.data ());
1301
1296
1302
1297
// Create a source file to hold the macro buffer. This is automatically
1303
1298
// registered with the enclosing module.
0 commit comments