Skip to content

Commit 93d4fe4

Browse files
committed
[Macros] Create llvm::MemoryBuffer directly from macro expansion result
Saves one memory copy. No need to create a temporary NullTerminatedStringRef.
1 parent 43eabf1 commit 93d4fe4

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ Expr *swift::expandMacroExpr(
677677
return nullptr;
678678

679679
// Evaluate the macro.
680-
NullTerminatedStringRef evaluatedSource;
680+
std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
681681

682682
MacroDecl *macro = cast<MacroDecl>(macroRef.getDecl());
683683

@@ -722,8 +722,7 @@ Expr *swift::expandMacroExpr(
722722
// Expand the definition with the given arguments.
723723
auto result = expandMacroDefinition(
724724
macroDef.getExpanded(), macro, expr->getArgs());
725-
llvm::MallocAllocator allocator;
726-
evaluatedSource = NullTerminatedStringRef(result, allocator);
725+
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(result);
727726
break;
728727
}
729728

@@ -763,8 +762,9 @@ Expr *swift::expandMacroExpr(
763762
&evaluatedSourceLength);
764763
if (!evaluatedSourceAddress)
765764
return nullptr;
766-
evaluatedSource = NullTerminatedStringRef(evaluatedSourceAddress,
767-
(size_t)evaluatedSourceLength);
765+
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(
766+
{evaluatedSourceAddress, (size_t)evaluatedSourceLength});
767+
free((void *)evaluatedSourceAddress);
768768
break;
769769
#else
770770
ctx.Diags.diagnose(expr->getLoc(), diag::macro_unsupported);
@@ -785,14 +785,13 @@ Expr *swift::expandMacroExpr(
785785
if (ctx.LangOpts.DumpMacroExpansions) {
786786
llvm::errs() << bufferName << " as " << expandedType.getString()
787787
<< "\n------------------------------\n"
788-
<< evaluatedSource
788+
<< evaluatedSource->getBuffer()
789789
<< "\n------------------------------\n";
790790
}
791791

792792
// 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));
796795
auto macroBufferRange = sourceMgr.getRangeForBuffer(macroBufferID);
797796
GeneratedSourceInfo sourceInfo{
798797
GeneratedSourceInfo::ExpressionMacroExpansion,
@@ -803,7 +802,6 @@ Expr *swift::expandMacroExpr(
803802
dc
804803
};
805804
sourceMgr.setGeneratedSourceInfo(macroBufferID, sourceInfo);
806-
free((void*)evaluatedSource.data());
807805

808806
// Create a source file to hold the macro buffer. This is automatically
809807
// registered with the enclosing module.
@@ -866,7 +864,7 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
866864
return None;
867865

868866
// Evaluate the macro.
869-
NullTerminatedStringRef evaluatedSource;
867+
std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
870868

871869
MacroDecl *macro = cast<MacroDecl>(med->getMacroRef().getDecl());
872870
auto macroRoles = macro->getMacroRoles();
@@ -899,8 +897,7 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
899897
// Expand the definition with the given arguments.
900898
auto result = expandMacroDefinition(
901899
macroDef.getExpanded(), macro, med->getArgs());
902-
llvm::MallocAllocator allocator;
903-
evaluatedSource = NullTerminatedStringRef(result, allocator);
900+
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(result);
904901
break;
905902
}
906903

@@ -958,8 +955,9 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
958955
&evaluatedSourceLength);
959956
if (!evaluatedSourceAddress)
960957
return None;
961-
evaluatedSource = NullTerminatedStringRef(evaluatedSourceAddress,
962-
(size_t)evaluatedSourceLength);
958+
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(
959+
{evaluatedSourceAddress, (size_t)evaluatedSourceLength});
960+
free((void *)evaluatedSourceAddress);
963961
break;
964962
#else
965963
med->diagnose(diag::macro_unsupported);
@@ -987,7 +985,8 @@ swift::expandFreestandingMacro(MacroExpansionDecl *med) {
987985
// Create a new source buffer with the contents of the expanded macro.
988986
auto macroBuffer =
989987
llvm::MemoryBuffer::getMemBufferCopy(evaluatedSource, bufferName);
990-
unsigned macroBufferID = sourceMgr.addNewSourceBuffer(std::move(macroBuffer));
988+
unsigned macroBufferID =
989+
sourceMgr.addNewSourceBuffer(std::move(evaluatedSource));
991990
auto macroBufferRange = sourceMgr.getRangeForBuffer(macroBufferID);
992991
GeneratedSourceInfo sourceInfo{
993992
GeneratedSourceInfo::FreestandingDeclMacroExpansion,
@@ -1092,7 +1091,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
10921091
}
10931092

10941093
// Evaluate the macro.
1095-
NullTerminatedStringRef evaluatedSource;
1094+
std::unique_ptr<llvm::MemoryBuffer> evaluatedSource;
10961095

10971096
std::string discriminator;
10981097
auto macroDef = macro->getDefinition();
@@ -1115,7 +1114,7 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
11151114
auto result = expandMacroDefinition(
11161115
macroDef.getExpanded(), macro, attr->getArgs());
11171116
llvm::MallocAllocator allocator;
1118-
evaluatedSource = NullTerminatedStringRef(result, allocator);
1117+
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(result);
11191118
break;
11201119
}
11211120

@@ -1179,8 +1178,9 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
11791178
&evaluatedSourceLength);
11801179
if (!evaluatedSourceAddress)
11811180
return nullptr;
1182-
evaluatedSource = NullTerminatedStringRef(evaluatedSourceAddress,
1183-
(size_t)evaluatedSourceLength);
1181+
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(
1182+
{evaluatedSourceAddress, (size_t)evaluatedSourceLength});
1183+
free((void *)evaluatedSourceAddress);
11841184
break;
11851185
#else
11861186
attachedTo->diagnose(diag::macro_unsupported);
@@ -1286,7 +1286,8 @@ evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, CustomAttr *attr,
12861286
// Create a new source buffer with the contents of the expanded macro.
12871287
auto macroBuffer =
12881288
llvm::MemoryBuffer::getMemBufferCopy(evaluatedSource, bufferName);
1289-
unsigned macroBufferID = sourceMgr.addNewSourceBuffer(std::move(macroBuffer));
1289+
unsigned macroBufferID =
1290+
sourceMgr.addNewSourceBuffer(std::move(evaluatedSource));
12901291
auto macroBufferRange = sourceMgr.getRangeForBuffer(macroBufferID);
12911292
GeneratedSourceInfo sourceInfo{
12921293
generatedSourceKind,

0 commit comments

Comments
 (0)