Browse Source

Fix memory leaks when using the driver

Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
pull/1391/head
Dimitar Dobrev 5 years ago
parent
commit
1d9a07b693
  1. 2
      src/Generator.Tests/AST/TestAST.cs
  2. 16
      src/Generator.Tests/ASTTestFixture.cs
  3. 12
      src/Generator.Tests/ReadNativeDependenciesTest.cs
  4. 12
      src/Generator.Tests/ReadNativeSymbolsTest.cs
  5. 102
      src/Generator/Driver.cs

2
src/Generator.Tests/AST/TestAST.cs

@ -32,7 +32,7 @@ namespace CppSharp.Generator.Tests.AST @@ -32,7 +32,7 @@ namespace CppSharp.Generator.Tests.AST
[OneTimeTearDown]
public void CleanUp()
{
ParserOptions.Dispose();
Driver.Dispose();
}
[Test]

16
src/Generator.Tests/ASTTestFixture.cs

@ -10,25 +10,23 @@ namespace CppSharp.Generator.Tests @@ -10,25 +10,23 @@ namespace CppSharp.Generator.Tests
public class ASTTestFixture
{
protected Driver Driver;
protected DriverOptions Options;
protected ParserOptions ParserOptions;
protected ASTContext AstContext;
protected void ParseLibrary(params string[] files)
{
Options = new DriverOptions { GeneratorKind = GeneratorKind.CSharp };
ParserOptions = new ParserOptions();
var options = new DriverOptions { GeneratorKind = GeneratorKind.CSharp };
var parserOptions = new ParserOptions();
var testsPath = GeneratorTest.GetTestsDirectory("Native");
ParserOptions.AddIncludeDirs(testsPath);
ParserOptions.SkipPrivateDeclarations = true;
parserOptions.AddIncludeDirs(testsPath);
parserOptions.SkipPrivateDeclarations = true;
var module = Options.AddModule("Test");
var module = options.AddModule("Test");
module.Headers.AddRange(files);
Driver = new Driver(Options)
Driver = new Driver(options)
{
ParserOptions = this.ParserOptions
ParserOptions = parserOptions
};
Driver.Setup();

12
src/Generator.Tests/ReadNativeDependenciesTest.cs

@ -39,14 +39,12 @@ namespace CppSharp.Generator.Tests @@ -39,14 +39,12 @@ namespace CppSharp.Generator.Tests
var driverOptions = new DriverOptions();
var module = driverOptions.AddModule("Test");
module.Libraries.Add(library);
var driver = new Driver(driverOptions)
using (var driver = new Driver(driverOptions) { ParserOptions = parserOptions })
{
ParserOptions = parserOptions
};
driver.Setup();
Assert.IsTrue(driver.ParseLibraries());
var dependencies = driver.Context.Symbols.Libraries[0].Dependencies;
return dependencies;
driver.Setup();
Assert.IsTrue(driver.ParseLibraries());
return driver.Context.Symbols.Libraries[0].Dependencies;
}
}
}
}

12
src/Generator.Tests/ReadNativeSymbolsTest.cs

@ -311,14 +311,12 @@ namespace CppSharp.Generator.Tests @@ -311,14 +311,12 @@ namespace CppSharp.Generator.Tests
var driverOptions = new DriverOptions();
var module = driverOptions.AddModule("Test");
module.Libraries.Add(library);
var driver = new Driver(driverOptions)
using (var driver = new Driver(driverOptions) { ParserOptions = parserOptions })
{
ParserOptions = parserOptions
};
driver.Setup();
Assert.IsTrue(driver.ParseLibraries());
var symbols = driver.Context.Symbols.Libraries[0].Symbols;
return symbols;
driver.Setup();
Assert.IsTrue(driver.ParseLibraries());
return driver.Context.Symbols.Libraries[0].Symbols;
}
}
}
}

102
src/Generator/Driver.cs

@ -17,7 +17,7 @@ using CppSharp.Generators.Cpp; @@ -17,7 +17,7 @@ using CppSharp.Generators.Cpp;
namespace CppSharp
{
public class Driver
public class Driver : IDisposable
{
public DriverOptions Options { get; private set; }
public ParserOptions ParserOptions { get; set; }
@ -399,6 +399,13 @@ namespace CppSharp @@ -399,6 +399,13 @@ namespace CppSharp
Context.GeneratorOutputPasses.AddPass(pass);
}
public void Dispose()
{
Generator.Dispose();
Context.TargetInfo?.Dispose();
ParserOptions.Dispose();
}
private bool hasParsingErrors;
}
@ -407,72 +414,69 @@ namespace CppSharp @@ -407,72 +414,69 @@ namespace CppSharp
public static void Run(ILibrary library)
{
var options = new DriverOptions();
var driver = new Driver(options);
library.Setup(driver);
driver.Setup();
using (var driver = new Driver(options))
{
library.Setup(driver);
if(driver.Options.Verbose)
Diagnostics.Level = DiagnosticKind.Debug;
driver.Setup();
if (!options.Quiet)
Diagnostics.Message("Parsing libraries...");
if (driver.Options.Verbose)
Diagnostics.Level = DiagnosticKind.Debug;
if (!driver.ParseLibraries())
return;
if (!options.Quiet)
Diagnostics.Message("Parsing libraries...");
if (!options.Quiet)
Diagnostics.Message("Parsing code...");
if (!driver.ParseLibraries())
return;
if (!driver.ParseCode())
{
Diagnostics.Error("CppSharp has encountered an error while parsing code.");
return;
}
if (!options.Quiet)
Diagnostics.Message("Parsing code...");
new CleanUnitPass { Context = driver.Context }.VisitASTContext(driver.Context.ASTContext);
options.Modules.RemoveAll(m => m != options.SystemModule && !m.Units.GetGenerated().Any());
if (!driver.ParseCode())
{
Diagnostics.Error("CppSharp has encountered an error while parsing code.");
return;
}
if (!options.Quiet)
Diagnostics.Message("Processing code...");
new CleanUnitPass { Context = driver.Context }.VisitASTContext(driver.Context.ASTContext);
options.Modules.RemoveAll(m => m != options.SystemModule && !m.Units.GetGenerated().Any());
driver.SetupPasses(library);
driver.SetupTypeMaps();
if (!options.Quiet)
Diagnostics.Message("Processing code...");
library.Preprocess(driver, driver.Context.ASTContext);
driver.SetupPasses(library);
driver.SetupTypeMaps();
driver.ProcessCode();
library.Postprocess(driver, driver.Context.ASTContext);
library.Preprocess(driver, driver.Context.ASTContext);
if (!options.Quiet)
Diagnostics.Message("Generating code...");
driver.ProcessCode();
library.Postprocess(driver, driver.Context.ASTContext);
if (!options.DryRun)
{
var outputs = driver.GenerateCode();
if (!options.Quiet)
Diagnostics.Message("Generating code...");
foreach (var output in outputs)
if (!options.DryRun)
{
foreach (var pass in driver.Context.GeneratorOutputPasses.Passes)
{
pass.VisitGeneratorOutput(output);
}
}
var outputs = driver.GenerateCode();
driver.SaveCode(outputs);
if (driver.Options.IsCSharpGenerator && driver.Options.CompileCode)
foreach (var module in driver.Options.Modules)
foreach (var output in outputs)
{
driver.CompileCode(module);
if (driver.HasCompilationErrors)
break;
foreach (var pass in driver.Context.GeneratorOutputPasses.Passes)
{
pass.VisitGeneratorOutput(output);
}
}
}
driver.Generator.Dispose();
driver.Context.TargetInfo.Dispose();
driver.ParserOptions.Dispose();
driver.SaveCode(outputs);
if (driver.Options.IsCSharpGenerator && driver.Options.CompileCode)
foreach (var module in driver.Options.Modules)
{
driver.CompileCode(module);
if (driver.HasCompilationErrors)
break;
}
}
}
}
}
}

Loading…
Cancel
Save