Skip to content

Commit 40f3a09

Browse files
Minor improvements (#1793)
* CppSharp.Generators.Options: added pre/post TranslationUnitPass callback * CppSharp.Generators.Passes.Pass: added TranslationUnitPassGeneratorDependent
1 parent b14038a commit 40f3a09

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

src/Generator/BindingContext.cs

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using CppSharp.Passes;
33
using CppSharp.Types;
44
using CppSharp.Parser;
5+
using System.Collections.Generic;
56

67
namespace CppSharp.Generators
78
{
@@ -36,14 +37,21 @@ public BindingContext(DriverOptions options, ParserOptions parserOptions = null)
3637

3738
public void RunPasses()
3839
{
40+
Dictionary<System.Type, int> passesByType = new Dictionary<System.Type, int>();
41+
int index = 0;
3942
TranslationUnitPasses.RunPasses(pass =>
4043
{
44+
int count = passesByType.GetValueOrDefault(pass.GetType(), 0);
4145
Diagnostics.Debug("Pass '{0}'", pass);
4246

4347
Diagnostics.PushIndent();
48+
Options.TranslationUnitPassPreCallBack?.Invoke(pass, index, count);
4449
pass.Context = this;
4550
pass.VisitASTContext(ASTContext);
51+
Options.TranslationUnitPassPostCallBack?.Invoke(pass, index, count);
4652
Diagnostics.PopIndent();
53+
passesByType[pass.GetType()] = count + 1;
54+
index += 1;
4755
});
4856
}
4957
}

src/Generator/Options.cs

+7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Text;
66
using CppSharp.AST;
77
using CppSharp.Generators;
8+
using CppSharp.Passes;
89

910
namespace CppSharp
1011
{
@@ -254,6 +255,12 @@ public bool GenerateSingleCSharpFile
254255
/// </summary>
255256
public bool GenerateExternalDataFields { get; set; } = false;
256257

258+
public delegate void TranslationUnitPassCallBack(TranslationUnitPass pass, int index, int count);
259+
260+
public TranslationUnitPassCallBack TranslationUnitPassPreCallBack { get; set; }
261+
262+
public TranslationUnitPassCallBack TranslationUnitPassPostCallBack { get; set; }
263+
257264
#endregion
258265
}
259266

src/Generator/Passes/Pass.cs

+10
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ bool IsDeclExcluded(Declaration decl)
5151
}
5252
}
5353

54+
public class TranslationUnitPassGeneratorDependent : TranslationUnitPass
55+
{
56+
public Generator Generator { get; }
57+
58+
public TranslationUnitPassGeneratorDependent(Generator generator)
59+
{
60+
Generator = generator;
61+
}
62+
}
63+
5464
/// <summary>
5565
/// Used to modify generated output.
5666
/// </summary>

0 commit comments

Comments
 (0)