Browse Source

Fix leaking the memory of an entire Clang AST

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1362/head
Dimitar Dobrev 5 years ago committed by João Matos
parent
commit
bcdc2cf667
  1. 23
      src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/CppSharp.CppParser.cs
  2. 23
      src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/CppSharp.CppParser.cs
  3. 23
      src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/CppSharp.CppParser.cs
  4. 23
      src/CppParser/Bindings/CSharp/x86_64-linux-gnu-cxx11abi/CppSharp.CppParser.cs
  5. 23
      src/CppParser/Bindings/CSharp/x86_64-linux-gnu/CppSharp.CppParser.cs
  6. 23
      src/CppParser/Bindings/CSharp/x86_64-pc-win32-msvc/CppSharp.CppParser.cs
  7. 4
      src/CppParser/CppParser.cpp
  8. 1
      src/CppParser/CppParser.h
  9. 35
      src/CppParser/Parser.cpp
  10. 5
      src/CppParser/Parser.h
  11. 36
      src/Generator/Driver.cs

23
src/CppParser/Bindings/CSharp/i686-apple-darwin12.4.0/CppSharp.CppParser.cs

@ -48212,7 +48212,7 @@ namespace CppSharp @@ -48212,7 +48212,7 @@ namespace CppSharp
public unsafe partial class ParserResult : IDisposable
{
[StructLayout(LayoutKind.Explicit, Size = 28)]
[StructLayout(LayoutKind.Explicit, Size = 24)]
public partial struct __Internal
{
[FieldOffset(0)]
@ -48227,9 +48227,6 @@ namespace CppSharp @@ -48227,9 +48227,6 @@ namespace CppSharp
[FieldOffset(20)]
internal global::System.IntPtr targetInfo;
[FieldOffset(24)]
internal global::System.IntPtr codeParser;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN8CppSharp9CppParser12ParserResultC2Ev")]
@ -48410,24 +48407,6 @@ namespace CppSharp @@ -48410,24 +48407,6 @@ namespace CppSharp
}
}
public global::CppSharp.Parser.Parser CodeParser
{
get
{
global::CppSharp.Parser.Parser __result0;
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
return __result0;
}
set
{
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
}
}
public uint DiagnosticsCount
{
get

23
src/CppParser/Bindings/CSharp/i686-pc-win32-msvc/CppSharp.CppParser.cs

@ -48212,7 +48212,7 @@ namespace CppSharp @@ -48212,7 +48212,7 @@ namespace CppSharp
public unsafe partial class ParserResult : IDisposable
{
[StructLayout(LayoutKind.Explicit, Size = 28)]
[StructLayout(LayoutKind.Explicit, Size = 24)]
public partial struct __Internal
{
[FieldOffset(0)]
@ -48227,9 +48227,6 @@ namespace CppSharp @@ -48227,9 +48227,6 @@ namespace CppSharp
[FieldOffset(20)]
internal global::System.IntPtr targetInfo;
[FieldOffset(24)]
internal global::System.IntPtr codeParser;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall,
EntryPoint="??0ParserResult@CppParser@CppSharp@@QAE@XZ")]
@ -48410,24 +48407,6 @@ namespace CppSharp @@ -48410,24 +48407,6 @@ namespace CppSharp
}
}
public global::CppSharp.Parser.Parser CodeParser
{
get
{
global::CppSharp.Parser.Parser __result0;
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
return __result0;
}
set
{
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
}
}
public uint DiagnosticsCount
{
get

23
src/CppParser/Bindings/CSharp/x86_64-apple-darwin12.4.0/CppSharp.CppParser.cs

@ -48211,7 +48211,7 @@ namespace CppSharp @@ -48211,7 +48211,7 @@ namespace CppSharp
public unsafe partial class ParserResult : IDisposable
{
[StructLayout(LayoutKind.Explicit, Size = 56)]
[StructLayout(LayoutKind.Explicit, Size = 48)]
public partial struct __Internal
{
[FieldOffset(0)]
@ -48226,9 +48226,6 @@ namespace CppSharp @@ -48226,9 +48226,6 @@ namespace CppSharp
[FieldOffset(40)]
internal global::System.IntPtr targetInfo;
[FieldOffset(48)]
internal global::System.IntPtr codeParser;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN8CppSharp9CppParser12ParserResultC2Ev")]
@ -48409,24 +48406,6 @@ namespace CppSharp @@ -48409,24 +48406,6 @@ namespace CppSharp
}
}
public global::CppSharp.Parser.Parser CodeParser
{
get
{
global::CppSharp.Parser.Parser __result0;
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
return __result0;
}
set
{
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
}
}
public uint DiagnosticsCount
{
get

23
src/CppParser/Bindings/CSharp/x86_64-linux-gnu-cxx11abi/CppSharp.CppParser.cs

@ -48211,7 +48211,7 @@ namespace CppSharp @@ -48211,7 +48211,7 @@ namespace CppSharp
public unsafe partial class ParserResult : IDisposable
{
[StructLayout(LayoutKind.Explicit, Size = 56)]
[StructLayout(LayoutKind.Explicit, Size = 48)]
public partial struct __Internal
{
[FieldOffset(0)]
@ -48226,9 +48226,6 @@ namespace CppSharp @@ -48226,9 +48226,6 @@ namespace CppSharp
[FieldOffset(40)]
internal global::System.IntPtr targetInfo;
[FieldOffset(48)]
internal global::System.IntPtr codeParser;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN8CppSharp9CppParser12ParserResultC2Ev")]
@ -48409,24 +48406,6 @@ namespace CppSharp @@ -48409,24 +48406,6 @@ namespace CppSharp
}
}
public global::CppSharp.Parser.Parser CodeParser
{
get
{
global::CppSharp.Parser.Parser __result0;
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
return __result0;
}
set
{
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
}
}
public uint DiagnosticsCount
{
get

23
src/CppParser/Bindings/CSharp/x86_64-linux-gnu/CppSharp.CppParser.cs

@ -48211,7 +48211,7 @@ namespace CppSharp @@ -48211,7 +48211,7 @@ namespace CppSharp
public unsafe partial class ParserResult : IDisposable
{
[StructLayout(LayoutKind.Explicit, Size = 56)]
[StructLayout(LayoutKind.Explicit, Size = 48)]
public partial struct __Internal
{
[FieldOffset(0)]
@ -48226,9 +48226,6 @@ namespace CppSharp @@ -48226,9 +48226,6 @@ namespace CppSharp
[FieldOffset(40)]
internal global::System.IntPtr targetInfo;
[FieldOffset(48)]
internal global::System.IntPtr codeParser;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="_ZN8CppSharp9CppParser12ParserResultC2Ev")]
@ -48409,24 +48406,6 @@ namespace CppSharp @@ -48409,24 +48406,6 @@ namespace CppSharp
}
}
public global::CppSharp.Parser.Parser CodeParser
{
get
{
global::CppSharp.Parser.Parser __result0;
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
return __result0;
}
set
{
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
}
}
public uint DiagnosticsCount
{
get

23
src/CppParser/Bindings/CSharp/x86_64-pc-win32-msvc/CppSharp.CppParser.cs

@ -48212,7 +48212,7 @@ namespace CppSharp @@ -48212,7 +48212,7 @@ namespace CppSharp
public unsafe partial class ParserResult : IDisposable
{
[StructLayout(LayoutKind.Explicit, Size = 56)]
[StructLayout(LayoutKind.Explicit, Size = 48)]
public partial struct __Internal
{
[FieldOffset(0)]
@ -48227,9 +48227,6 @@ namespace CppSharp @@ -48227,9 +48227,6 @@ namespace CppSharp
[FieldOffset(40)]
internal global::System.IntPtr targetInfo;
[FieldOffset(48)]
internal global::System.IntPtr codeParser;
[SuppressUnmanagedCodeSecurity]
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
EntryPoint="??0ParserResult@CppParser@CppSharp@@QEAA@XZ")]
@ -48410,24 +48407,6 @@ namespace CppSharp @@ -48410,24 +48407,6 @@ namespace CppSharp
}
}
public global::CppSharp.Parser.Parser CodeParser
{
get
{
global::CppSharp.Parser.Parser __result0;
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
return __result0;
}
set
{
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
}
}
public uint DiagnosticsCount
{
get

4
src/CppParser/CppParser.cpp

@ -42,7 +42,6 @@ DEF_VECTOR_STRING(CppParserOptions, SupportedStdTypes) @@ -42,7 +42,6 @@ DEF_VECTOR_STRING(CppParserOptions, SupportedStdTypes)
ParserResult::ParserResult()
: library(0)
, targetInfo(0)
, codeParser(0)
{
}
@ -51,13 +50,10 @@ ParserResult::ParserResult(const ParserResult& rhs) @@ -51,13 +50,10 @@ ParserResult::ParserResult(const ParserResult& rhs)
, Diagnostics(rhs.Diagnostics)
, library(rhs.library)
, targetInfo(rhs.targetInfo)
, codeParser(rhs.codeParser)
{}
ParserResult::~ParserResult()
{
if (codeParser)
delete codeParser;
if (library)
delete library;
}

1
src/CppParser/CppParser.h

@ -96,7 +96,6 @@ struct CS_API ParserResult @@ -96,7 +96,6 @@ struct CS_API ParserResult
NativeLibrary* library;
ParserTargetInfo* targetInfo;
Parser* codeParser;
};
enum class SourceLocationKind

35
src/CppParser/Parser.cpp

@ -3292,7 +3292,7 @@ void Parser::WalkFunction(const clang::FunctionDecl* FD, Function* F, @@ -3292,7 +3292,7 @@ void Parser::WalkFunction(const clang::FunctionDecl* FD, Function* F,
return;
}
auto& CGInfo = GetCodeGenFunctionInfo(codeGenTypes, FD);
auto& CGInfo = GetCodeGenFunctionInfo(codeGenTypes.get(), FD);
F->isReturnIndirect = CGInfo.getReturnInfo().isIndirect() ||
CGInfo.getReturnInfo().isInAlloca();
@ -4173,9 +4173,7 @@ void Parser::SetupLLVMCodegen() @@ -4173,9 +4173,7 @@ void Parser::SetupLLVMCodegen()
c->getHeaderSearchOpts(), c->getPreprocessorOpts(),
c->getCodeGenOpts(), *LLVMModule, c->getDiagnostics()));
CGT.reset(new clang::CodeGen::CodeGenTypes(*CGM.get()));
codeGenTypes = CGT.get();
codeGenTypes.reset(new clang::CodeGen::CodeGenTypes(*CGM.get()));
}
bool Parser::SetupSourceFiles(const std::vector<std::string>& SourceFiles,
@ -4241,7 +4239,7 @@ void SemaConsumer::HandleTranslationUnit(clang::ASTContext& Ctx) @@ -4241,7 +4239,7 @@ void SemaConsumer::HandleTranslationUnit(clang::ASTContext& Ctx)
Parser.WalkAST(TU);
}
ParserResult* Parser::ParseHeader(const std::vector<std::string>& SourceFiles)
ParserResult* Parser::Parse(const std::vector<std::string>& SourceFiles)
{
assert(opts->ASTContext && "Expected a valid ASTContext");
@ -4507,9 +4505,30 @@ ParserResult* ClangParser::ParseHeader(CppParserOptions* Opts) @@ -4507,9 +4505,30 @@ ParserResult* ClangParser::ParseHeader(CppParserOptions* Opts)
if (!Opts)
return nullptr;
auto res = new ParserResult();
res->codeParser = new Parser(Opts);
return res->codeParser->ParseHeader(Opts->SourceFiles);
auto& Headers = Opts->SourceFiles;
if (Opts->unityBuild)
{
Parser parser(Opts);
return parser.Parse(Headers);
}
ParserResult* res = 0;
std::vector<Parser*> parsers;
for (size_t i = 0; i < Headers.size(); i++)
{
auto parser = new Parser(Opts);
parsers.push_back(parser);
std::vector<std::string> Header(&Headers[i], &Headers[i + 1]);
if (i < Headers.size() - 1)
delete parser->Parse(Header);
else
res = parser->Parse(Header);
}
for (auto parser : parsers)
delete parser;
return res;
}
ParserResult* ClangParser::ParseLibrary(CppParserOptions* Opts)

5
src/CppParser/Parser.h

@ -53,7 +53,7 @@ public: @@ -53,7 +53,7 @@ public:
Parser(CppParserOptions* Opts);
void Setup();
ParserResult* ParseHeader(const std::vector<std::string>& SourceFiles);
ParserResult* Parse(const std::vector<std::string>& SourceFiles);
ParserResult* ParseLibrary(const std::string& File);
void WalkAST(clang::TranslationUnitDecl* TU);
@ -172,8 +172,7 @@ private: @@ -172,8 +172,7 @@ private:
llvm::LLVMContext LLVMCtx;
std::unique_ptr<llvm::Module> LLVMModule;
std::unique_ptr<clang::CodeGen::CodeGenModule> CGM;
std::unique_ptr<clang::CodeGen::CodeGenTypes> CGT;
clang::CodeGen::CodeGenTypes* codeGenTypes;
std::unique_ptr<clang::CodeGen::CodeGenTypes> codeGenTypes;
std::unordered_map<const clang::TemplateTypeParmDecl*, TypeTemplateParameter*> walkedTypeTemplateParameters;
std::unordered_map<const clang::TemplateTemplateParmDecl*, TemplateTemplateParameter*> walkedTemplateTemplateParameters;
std::unordered_map<const clang::NonTypeTemplateParmDecl*, NonTypeTemplateParameter*> walkedNonTypeTemplateParameters;

36
src/Generator/Driver.cs

@ -131,35 +131,15 @@ namespace CppSharp @@ -131,35 +131,15 @@ namespace CppSharp
var sourceFiles = Options.Modules.SelectMany(m => m.Headers);
if (ParserOptions.UnityBuild)
using (ParserOptions parserOptions = ParserOptions.BuildForSourceFile(
Options.Modules))
{
using (var parserOptions = ParserOptions.BuildForSourceFile(
Options.Modules))
{
using (var result = parser.ParseSourceFiles(
sourceFiles, parserOptions))
Context.TargetInfo = result.TargetInfo;
if (string.IsNullOrEmpty(ParserOptions.TargetTriple))
ParserOptions.TargetTriple = parserOptions.TargetTriple;
}
}
else
{
foreach (var sourceFile in sourceFiles)
{
using (var parserOptions = ParserOptions.BuildForSourceFile(
Options.Modules, sourceFile))
{
using (ParserResult result = parser.ParseSourceFile(
sourceFile, parserOptions))
if (Context.TargetInfo == null)
Context.TargetInfo = result.TargetInfo;
else if (result.TargetInfo != null)
result.TargetInfo.Dispose();
if (string.IsNullOrEmpty(ParserOptions.TargetTriple))
ParserOptions.TargetTriple = parserOptions.TargetTriple;
}
}
using (ParserResult result = parser.ParseSourceFiles(
sourceFiles, parserOptions))
Context.TargetInfo = result.TargetInfo;
if (string.IsNullOrEmpty(ParserOptions.TargetTriple))
ParserOptions.TargetTriple = parserOptions.TargetTriple;
}
Context.ASTContext = ClangParser.ConvertASTContext(astContext);

Loading…
Cancel
Save