diff --git a/lib/AST/CASTBridging.cpp b/lib/AST/CASTBridging.cpp index e629b4057b0e9..dacb9bbc62f65 100644 --- a/lib/AST/CASTBridging.cpp +++ b/lib/AST/CASTBridging.cpp @@ -17,6 +17,8 @@ using namespace swift; namespace { struct BridgedDiagnosticImpl { + typedef llvm::MallocAllocator Allocator; + InFlightDiagnostic inFlight; std::vector textBlobs; @@ -31,8 +33,10 @@ struct BridgedDiagnosticImpl { ~BridgedDiagnosticImpl() { inFlight.flush(); + + Allocator allocator; for (auto text : textBlobs) { - free((void *)text.data()); + allocator.Deallocate(text.data(), text.size()); } } }; @@ -100,8 +104,8 @@ BridgedDiagnostic Diagnostic_create(BridgedDiagnosticEngine cDiags, BridgedSourceLoc cLoc, BridgedString cText) { StringRef origText = convertString(cText); - llvm::MallocAllocator mallocAlloc; - StringRef text = origText.copy(mallocAlloc); + BridgedDiagnosticImpl::Allocator alloc; + StringRef text = origText.copy(alloc); SourceLoc loc = convertSourceLoc(cLoc); @@ -148,8 +152,8 @@ void Diagnostic_fixItReplace(BridgedDiagnostic cDiag, SourceLoc endLoc = convertSourceLoc(cEndLoc); StringRef origReplaceText = convertString(cReplaceText); - llvm::MallocAllocator mallocAlloc; - StringRef replaceText = origReplaceText.copy(mallocAlloc); + BridgedDiagnosticImpl::Allocator alloc; + StringRef replaceText = origReplaceText.copy(alloc); BridgedDiagnosticImpl *diag = convertDiagnostic(cDiag); diag->textBlobs.push_back(replaceText); diff --git a/lib/ASTGen/Sources/ASTGen/Macros.swift b/lib/ASTGen/Sources/ASTGen/Macros.swift index 96205f1d149e9..39d623207e286 100644 --- a/lib/ASTGen/Sources/ASTGen/Macros.swift +++ b/lib/ASTGen/Sources/ASTGen/Macros.swift @@ -170,6 +170,11 @@ func allocateUTF8String( } } +@_cdecl("swift_ASTGen_freeString") +public func freeString(pointer: UnsafePointer?) { + pointer?.deallocate() +} + /// Diagnostics produced here. enum ASTGenMacroDiagnostic: DiagnosticMessage, FixItMessage { case thrownError(Error) @@ -413,6 +418,14 @@ func checkMacroDefinition( } } +@_cdecl("swift_ASTGen_freeExpansionReplacements") +public func freeExpansionReplacements( + pointer: UnsafeMutablePointer?, + numReplacements: Int +) { + UnsafeMutableBufferPointer(start: pointer, count: numReplacements).deallocate() +} + // Make an expansion result for '@_cdecl' function caller. func makeExpansionOutputResult( expandedSource: String?, diff --git a/lib/Sema/TypeCheckMacros.cpp b/lib/Sema/TypeCheckMacros.cpp index b25177e43cd8d..c73a6b5c6a374 100644 --- a/lib/Sema/TypeCheckMacros.cpp +++ b/lib/Sema/TypeCheckMacros.cpp @@ -49,6 +49,8 @@ using namespace swift; extern "C" void *swift_ASTGen_resolveMacroType(const void *macroType); extern "C" void swift_ASTGen_destroyMacro(void *macro); +extern "C" void swift_ASTGen_freeString(const char *str); + extern "C" void *swift_ASTGen_resolveExecutableMacro( const char *moduleName, ptrdiff_t moduleNameLength, const char *typeName, ptrdiff_t typeNameLength, @@ -64,6 +66,9 @@ extern "C" ptrdiff_t swift_ASTGen_checkMacroDefinition( ptrdiff_t **replacementsPtr, ptrdiff_t *numReplacements ); +extern "C" void swift_ASTGen_freeExpansionReplacements( + ptrdiff_t *replacementsPtr, + ptrdiff_t numReplacements); extern "C" ptrdiff_t swift_ASTGen_expandFreestandingMacro( void *diagEngine, void *macro, uint8_t externalKind, @@ -197,8 +202,8 @@ MacroDefinition MacroDefinitionRequest::evaluate( // Clean up after the call. SWIFT_DEFER { - free(externalMacroNamePtr); - free(replacements); + swift_ASTGen_freeString(externalMacroNamePtr); + swift_ASTGen_freeExpansionReplacements(replacements, numReplacements); }; if (checkResult < 0 && ctx.CompletionCallback) { @@ -1065,7 +1070,7 @@ evaluateFreestandingMacro(FreestandingMacroExpansion *expansion, evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy( {evaluatedSourceAddress, (size_t)evaluatedSourceLength}, adjustMacroExpansionBufferName(*discriminator)); - free((void *)evaluatedSourceAddress); + swift_ASTGen_freeString(evaluatedSourceAddress); break; #else ctx.Diags.diagnose(loc, diag::macro_unsupported); @@ -1343,7 +1348,7 @@ static SourceFile *evaluateAttachedMacro(MacroDecl *macro, Decl *attachedTo, evaluatedSource = llvm::MemoryBuffer::getMemBufferCopy( {evaluatedSourceAddress, (size_t)evaluatedSourceLength}, adjustMacroExpansionBufferName(*discriminator)); - free((void *)evaluatedSourceAddress); + swift_ASTGen_freeString(evaluatedSourceAddress); break; #else attachedTo->diagnose(diag::macro_unsupported);