Skip to content

Commit be82d0b

Browse files
authored
Merge pull request #68271 from DougGregor/no-free-for-you
2 parents 8dc55b8 + 0ae2906 commit be82d0b

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

lib/AST/CASTBridging.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ using namespace swift;
1717

1818
namespace {
1919
struct BridgedDiagnosticImpl {
20+
typedef llvm::MallocAllocator Allocator;
21+
2022
InFlightDiagnostic inFlight;
2123
std::vector<StringRef> textBlobs;
2224

@@ -31,8 +33,10 @@ struct BridgedDiagnosticImpl {
3133

3234
~BridgedDiagnosticImpl() {
3335
inFlight.flush();
36+
37+
Allocator allocator;
3438
for (auto text : textBlobs) {
35-
free((void *)text.data());
39+
allocator.Deallocate(text.data(), text.size());
3640
}
3741
}
3842
};
@@ -100,8 +104,8 @@ BridgedDiagnostic Diagnostic_create(BridgedDiagnosticEngine cDiags,
100104
BridgedSourceLoc cLoc,
101105
BridgedString cText) {
102106
StringRef origText = convertString(cText);
103-
llvm::MallocAllocator mallocAlloc;
104-
StringRef text = origText.copy(mallocAlloc);
107+
BridgedDiagnosticImpl::Allocator alloc;
108+
StringRef text = origText.copy(alloc);
105109

106110
SourceLoc loc = convertSourceLoc(cLoc);
107111

@@ -148,8 +152,8 @@ void Diagnostic_fixItReplace(BridgedDiagnostic cDiag,
148152
SourceLoc endLoc = convertSourceLoc(cEndLoc);
149153

150154
StringRef origReplaceText = convertString(cReplaceText);
151-
llvm::MallocAllocator mallocAlloc;
152-
StringRef replaceText = origReplaceText.copy(mallocAlloc);
155+
BridgedDiagnosticImpl::Allocator alloc;
156+
StringRef replaceText = origReplaceText.copy(alloc);
153157

154158
BridgedDiagnosticImpl *diag = convertDiagnostic(cDiag);
155159
diag->textBlobs.push_back(replaceText);

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,11 @@ func allocateUTF8String(
170170
}
171171
}
172172

173+
@_cdecl("swift_ASTGen_freeString")
174+
public func freeString(pointer: UnsafePointer<UInt8>?) {
175+
pointer?.deallocate()
176+
}
177+
173178
/// Diagnostics produced here.
174179
enum ASTGenMacroDiagnostic: DiagnosticMessage, FixItMessage {
175180
case thrownError(Error)
@@ -413,6 +418,14 @@ func checkMacroDefinition(
413418
}
414419
}
415420

421+
@_cdecl("swift_ASTGen_freeExpansionReplacements")
422+
public func freeExpansionReplacements(
423+
pointer: UnsafeMutablePointer<Int>?,
424+
numReplacements: Int
425+
) {
426+
UnsafeMutableBufferPointer(start: pointer, count: numReplacements).deallocate()
427+
}
428+
416429
// Make an expansion result for '@_cdecl' function caller.
417430
func makeExpansionOutputResult(
418431
expandedSource: String?,

lib/Sema/TypeCheckMacros.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ using namespace swift;
4949
extern "C" void *swift_ASTGen_resolveMacroType(const void *macroType);
5050
extern "C" void swift_ASTGen_destroyMacro(void *macro);
5151

52+
extern "C" void swift_ASTGen_freeString(const char *str);
53+
5254
extern "C" void *swift_ASTGen_resolveExecutableMacro(
5355
const char *moduleName, ptrdiff_t moduleNameLength,
5456
const char *typeName, ptrdiff_t typeNameLength,
@@ -64,6 +66,9 @@ extern "C" ptrdiff_t swift_ASTGen_checkMacroDefinition(
6466
ptrdiff_t **replacementsPtr,
6567
ptrdiff_t *numReplacements
6668
);
69+
extern "C" void swift_ASTGen_freeExpansionReplacements(
70+
ptrdiff_t *replacementsPtr,
71+
ptrdiff_t numReplacements);
6772

6873
extern "C" ptrdiff_t swift_ASTGen_expandFreestandingMacro(
6974
void *diagEngine, void *macro, uint8_t externalKind,
@@ -197,8 +202,8 @@ MacroDefinition MacroDefinitionRequest::evaluate(
197202

198203
// Clean up after the call.
199204
SWIFT_DEFER {
200-
free(externalMacroNamePtr);
201-
free(replacements);
205+
swift_ASTGen_freeString(externalMacroNamePtr);
206+
swift_ASTGen_freeExpansionReplacements(replacements, numReplacements);
202207
};
203208

204209
if (checkResult < 0 && ctx.CompletionCallback) {
@@ -1065,7 +1070,7 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion,
10651070
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(
10661071
{evaluatedSourceAddress, (size_t)evaluatedSourceLength},
10671072
adjustMacroExpansionBufferName(*discriminator));
1068-
free((void *)evaluatedSourceAddress);
1073+
swift_ASTGen_freeString(evaluatedSourceAddress);
10691074
break;
10701075
#else
10711076
ctx.Diags.diagnose(loc, diag::macro_unsupported);
@@ -1343,7 +1348,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo,
13431348
evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy(
13441349
{evaluatedSourceAddress, (size_t)evaluatedSourceLength},
13451350
adjustMacroExpansionBufferName(*discriminator));
1346-
free((void *)evaluatedSourceAddress);
1351+
swift_ASTGen_freeString(evaluatedSourceAddress);
13471352
break;
13481353
#else
13491354
attachedTo->diagnose(diag::macro_unsupported);

0 commit comments

Comments
 (0)