Skip to content

Commit bcdc2cf

Browse files
ddobrevtritao
authored andcommitted
Fix leaking the memory of an entire Clang AST
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 5d55a57 commit bcdc2cf

File tree

11 files changed

+43
-176
lines changed

11 files changed

+43
-176
lines changed

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48212,7 +48212,7 @@ public int ColumnNumber
4821248212

4821348213
public unsafe partial class ParserResult : IDisposable
4821448214
{
48215-
[StructLayout(LayoutKind.Explicit, Size = 28)]
48215+
[StructLayout(LayoutKind.Explicit, Size = 24)]
4821648216
public partial struct __Internal
4821748217
{
4821848218
[FieldOffset(0)]
@@ -48227,9 +48227,6 @@ public partial struct __Internal
4822748227
[FieldOffset(20)]
4822848228
internal global::System.IntPtr targetInfo;
4822948229

48230-
[FieldOffset(24)]
48231-
internal global::System.IntPtr codeParser;
48232-
4823348230
[SuppressUnmanagedCodeSecurity]
4823448231
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
4823548232
EntryPoint="_ZN8CppSharp9CppParser12ParserResultC2Ev")]
@@ -48410,24 +48407,6 @@ public void ClearDiagnostics()
4841048407
}
4841148408
}
4841248409

48413-
public global::CppSharp.Parser.Parser CodeParser
48414-
{
48415-
get
48416-
{
48417-
global::CppSharp.Parser.Parser __result0;
48418-
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
48419-
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
48420-
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
48421-
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
48422-
return __result0;
48423-
}
48424-
48425-
set
48426-
{
48427-
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
48428-
}
48429-
}
48430-
4843148410
public uint DiagnosticsCount
4843248411
{
4843348412
get

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48212,7 +48212,7 @@ public int ColumnNumber
4821248212

4821348213
public unsafe partial class ParserResult : IDisposable
4821448214
{
48215-
[StructLayout(LayoutKind.Explicit, Size = 28)]
48215+
[StructLayout(LayoutKind.Explicit, Size = 24)]
4821648216
public partial struct __Internal
4821748217
{
4821848218
[FieldOffset(0)]
@@ -48227,9 +48227,6 @@ public partial struct __Internal
4822748227
[FieldOffset(20)]
4822848228
internal global::System.IntPtr targetInfo;
4822948229

48230-
[FieldOffset(24)]
48231-
internal global::System.IntPtr codeParser;
48232-
4823348230
[SuppressUnmanagedCodeSecurity]
4823448231
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.ThisCall,
4823548232
EntryPoint="??0ParserResult@CppParser@CppSharp@@QAE@XZ")]
@@ -48410,24 +48407,6 @@ public void ClearDiagnostics()
4841048407
}
4841148408
}
4841248409

48413-
public global::CppSharp.Parser.Parser CodeParser
48414-
{
48415-
get
48416-
{
48417-
global::CppSharp.Parser.Parser __result0;
48418-
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
48419-
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
48420-
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
48421-
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
48422-
return __result0;
48423-
}
48424-
48425-
set
48426-
{
48427-
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
48428-
}
48429-
}
48430-
4843148410
public uint DiagnosticsCount
4843248411
{
4843348412
get

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48211,7 +48211,7 @@ public int ColumnNumber
4821148211

4821248212
public unsafe partial class ParserResult : IDisposable
4821348213
{
48214-
[StructLayout(LayoutKind.Explicit, Size = 56)]
48214+
[StructLayout(LayoutKind.Explicit, Size = 48)]
4821548215
public partial struct __Internal
4821648216
{
4821748217
[FieldOffset(0)]
@@ -48226,9 +48226,6 @@ public partial struct __Internal
4822648226
[FieldOffset(40)]
4822748227
internal global::System.IntPtr targetInfo;
4822848228

48229-
[FieldOffset(48)]
48230-
internal global::System.IntPtr codeParser;
48231-
4823248229
[SuppressUnmanagedCodeSecurity]
4823348230
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
4823448231
EntryPoint="_ZN8CppSharp9CppParser12ParserResultC2Ev")]
@@ -48409,24 +48406,6 @@ public void ClearDiagnostics()
4840948406
}
4841048407
}
4841148408

48412-
public global::CppSharp.Parser.Parser CodeParser
48413-
{
48414-
get
48415-
{
48416-
global::CppSharp.Parser.Parser __result0;
48417-
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
48418-
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
48419-
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
48420-
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
48421-
return __result0;
48422-
}
48423-
48424-
set
48425-
{
48426-
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
48427-
}
48428-
}
48429-
4843048409
public uint DiagnosticsCount
4843148410
{
4843248411
get

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48211,7 +48211,7 @@ public int ColumnNumber
4821148211

4821248212
public unsafe partial class ParserResult : IDisposable
4821348213
{
48214-
[StructLayout(LayoutKind.Explicit, Size = 56)]
48214+
[StructLayout(LayoutKind.Explicit, Size = 48)]
4821548215
public partial struct __Internal
4821648216
{
4821748217
[FieldOffset(0)]
@@ -48226,9 +48226,6 @@ public partial struct __Internal
4822648226
[FieldOffset(40)]
4822748227
internal global::System.IntPtr targetInfo;
4822848228

48229-
[FieldOffset(48)]
48230-
internal global::System.IntPtr codeParser;
48231-
4823248229
[SuppressUnmanagedCodeSecurity]
4823348230
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
4823448231
EntryPoint="_ZN8CppSharp9CppParser12ParserResultC2Ev")]
@@ -48409,24 +48406,6 @@ public void ClearDiagnostics()
4840948406
}
4841048407
}
4841148408

48412-
public global::CppSharp.Parser.Parser CodeParser
48413-
{
48414-
get
48415-
{
48416-
global::CppSharp.Parser.Parser __result0;
48417-
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
48418-
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
48419-
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
48420-
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
48421-
return __result0;
48422-
}
48423-
48424-
set
48425-
{
48426-
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
48427-
}
48428-
}
48429-
4843048409
public uint DiagnosticsCount
4843148410
{
4843248411
get

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48211,7 +48211,7 @@ public int ColumnNumber
4821148211

4821248212
public unsafe partial class ParserResult : IDisposable
4821348213
{
48214-
[StructLayout(LayoutKind.Explicit, Size = 56)]
48214+
[StructLayout(LayoutKind.Explicit, Size = 48)]
4821548215
public partial struct __Internal
4821648216
{
4821748217
[FieldOffset(0)]
@@ -48226,9 +48226,6 @@ public partial struct __Internal
4822648226
[FieldOffset(40)]
4822748227
internal global::System.IntPtr targetInfo;
4822848228

48229-
[FieldOffset(48)]
48230-
internal global::System.IntPtr codeParser;
48231-
4823248229
[SuppressUnmanagedCodeSecurity]
4823348230
[DllImport("CppSharp.CppParser", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
4823448231
EntryPoint="_ZN8CppSharp9CppParser12ParserResultC2Ev")]
@@ -48409,24 +48406,6 @@ public void ClearDiagnostics()
4840948406
}
4841048407
}
4841148408

48412-
public global::CppSharp.Parser.Parser CodeParser
48413-
{
48414-
get
48415-
{
48416-
global::CppSharp.Parser.Parser __result0;
48417-
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
48418-
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
48419-
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
48420-
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
48421-
return __result0;
48422-
}
48423-
48424-
set
48425-
{
48426-
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
48427-
}
48428-
}
48429-
4843048409
public uint DiagnosticsCount
4843148410
{
4843248411
get

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

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48212,7 +48212,7 @@ public int ColumnNumber
4821248212

4821348213
public unsafe partial class ParserResult : IDisposable
4821448214
{
48215-
[StructLayout(LayoutKind.Explicit, Size = 56)]
48215+
[StructLayout(LayoutKind.Explicit, Size = 48)]
4821648216
public partial struct __Internal
4821748217
{
4821848218
[FieldOffset(0)]
@@ -48227,9 +48227,6 @@ public partial struct __Internal
4822748227
[FieldOffset(40)]
4822848228
internal global::System.IntPtr targetInfo;
4822948229

48230-
[FieldOffset(48)]
48231-
internal global::System.IntPtr codeParser;
48232-
4823348230
[SuppressUnmanagedCodeSecurity]
4823448231
[DllImport("CppSharp.CppParser.dll", CallingConvention = global::System.Runtime.InteropServices.CallingConvention.Cdecl,
4823548232
EntryPoint="??0ParserResult@CppParser@CppSharp@@QEAA@XZ")]
@@ -48410,24 +48407,6 @@ public void ClearDiagnostics()
4841048407
}
4841148408
}
4841248409

48413-
public global::CppSharp.Parser.Parser CodeParser
48414-
{
48415-
get
48416-
{
48417-
global::CppSharp.Parser.Parser __result0;
48418-
if (((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser == IntPtr.Zero) __result0 = null;
48419-
else if (global::CppSharp.Parser.Parser.NativeToManagedMap.ContainsKey(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser))
48420-
__result0 = (global::CppSharp.Parser.Parser) global::CppSharp.Parser.Parser.NativeToManagedMap[((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser];
48421-
else __result0 = global::CppSharp.Parser.Parser.__CreateInstance(((global::CppSharp.Parser.ParserResult.__Internal*) __Instance)->codeParser);
48422-
return __result0;
48423-
}
48424-
48425-
set
48426-
{
48427-
((global::CppSharp.Parser.ParserResult.__Internal*)__Instance)->codeParser = ReferenceEquals(value, null) ? global::System.IntPtr.Zero : value.__Instance;
48428-
}
48429-
}
48430-
4843148410
public uint DiagnosticsCount
4843248411
{
4843348412
get

src/CppParser/CppParser.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ DEF_VECTOR_STRING(CppParserOptions, SupportedStdTypes)
4242
ParserResult::ParserResult()
4343
: library(0)
4444
, targetInfo(0)
45-
, codeParser(0)
4645
{
4746
}
4847

@@ -51,13 +50,10 @@ ParserResult::ParserResult(const ParserResult& rhs)
5150
, Diagnostics(rhs.Diagnostics)
5251
, library(rhs.library)
5352
, targetInfo(rhs.targetInfo)
54-
, codeParser(rhs.codeParser)
5553
{}
5654

5755
ParserResult::~ParserResult()
5856
{
59-
if (codeParser)
60-
delete codeParser;
6157
if (library)
6258
delete library;
6359
}

src/CppParser/CppParser.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ struct CS_API ParserResult
9696

9797
NativeLibrary* library;
9898
ParserTargetInfo* targetInfo;
99-
Parser* codeParser;
10099
};
101100

102101
enum class SourceLocationKind

src/CppParser/Parser.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3292,7 +3292,7 @@ void Parser::WalkFunction(const clang::FunctionDecl* FD, Function* F,
32923292
return;
32933293
}
32943294

3295-
auto& CGInfo = GetCodeGenFunctionInfo(codeGenTypes, FD);
3295+
auto& CGInfo = GetCodeGenFunctionInfo(codeGenTypes.get(), FD);
32963296
F->isReturnIndirect = CGInfo.getReturnInfo().isIndirect() ||
32973297
CGInfo.getReturnInfo().isInAlloca();
32983298

@@ -4173,9 +4173,7 @@ void Parser::SetupLLVMCodegen()
41734173
c->getHeaderSearchOpts(), c->getPreprocessorOpts(),
41744174
c->getCodeGenOpts(), *LLVMModule, c->getDiagnostics()));
41754175

4176-
CGT.reset(new clang::CodeGen::CodeGenTypes(*CGM.get()));
4177-
4178-
codeGenTypes = CGT.get();
4176+
codeGenTypes.reset(new clang::CodeGen::CodeGenTypes(*CGM.get()));
41794177
}
41804178

41814179
bool Parser::SetupSourceFiles(const std::vector<std::string>& SourceFiles,
@@ -4241,7 +4239,7 @@ void SemaConsumer::HandleTranslationUnit(clang::ASTContext& Ctx)
42414239
Parser.WalkAST(TU);
42424240
}
42434241

4244-
ParserResult* Parser::ParseHeader(const std::vector<std::string>& SourceFiles)
4242+
ParserResult* Parser::Parse(const std::vector<std::string>& SourceFiles)
42454243
{
42464244
assert(opts->ASTContext && "Expected a valid ASTContext");
42474245

@@ -4507,9 +4505,30 @@ ParserResult* ClangParser::ParseHeader(CppParserOptions* Opts)
45074505
if (!Opts)
45084506
return nullptr;
45094507

4510-
auto res = new ParserResult();
4511-
res->codeParser = new Parser(Opts);
4512-
return res->codeParser->ParseHeader(Opts->SourceFiles);
4508+
auto& Headers = Opts->SourceFiles;
4509+
if (Opts->unityBuild)
4510+
{
4511+
Parser parser(Opts);
4512+
return parser.Parse(Headers);
4513+
}
4514+
4515+
ParserResult* res = 0;
4516+
std::vector<Parser*> parsers;
4517+
for (size_t i = 0; i < Headers.size(); i++)
4518+
{
4519+
auto parser = new Parser(Opts);
4520+
parsers.push_back(parser);
4521+
std::vector<std::string> Header(&Headers[i], &Headers[i + 1]);
4522+
if (i < Headers.size() - 1)
4523+
delete parser->Parse(Header);
4524+
else
4525+
res = parser->Parse(Header);
4526+
}
4527+
4528+
for (auto parser : parsers)
4529+
delete parser;
4530+
4531+
return res;
45134532
}
45144533

45154534
ParserResult* ClangParser::ParseLibrary(CppParserOptions* Opts)

src/CppParser/Parser.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class Parser
5353
Parser(CppParserOptions* Opts);
5454

5555
void Setup();
56-
ParserResult* ParseHeader(const std::vector<std::string>& SourceFiles);
56+
ParserResult* Parse(const std::vector<std::string>& SourceFiles);
5757
ParserResult* ParseLibrary(const std::string& File);
5858

5959
void WalkAST(clang::TranslationUnitDecl* TU);
@@ -172,8 +172,7 @@ class Parser
172172
llvm::LLVMContext LLVMCtx;
173173
std::unique_ptr<llvm::Module> LLVMModule;
174174
std::unique_ptr<clang::CodeGen::CodeGenModule> CGM;
175-
std::unique_ptr<clang::CodeGen::CodeGenTypes> CGT;
176-
clang::CodeGen::CodeGenTypes* codeGenTypes;
175+
std::unique_ptr<clang::CodeGen::CodeGenTypes> codeGenTypes;
177176
std::unordered_map<const clang::TemplateTypeParmDecl*, TypeTemplateParameter*> walkedTypeTemplateParameters;
178177
std::unordered_map<const clang::TemplateTemplateParmDecl*, TemplateTemplateParameter*> walkedTemplateTemplateParameters;
179178
std::unordered_map<const clang::NonTypeTemplateParmDecl*, NonTypeTemplateParameter*> walkedNonTypeTemplateParameters;

0 commit comments

Comments
 (0)