Skip to content

Commit

Permalink
Minor improvements (#1793)
Browse files Browse the repository at this point in the history
* CppSharp.Generators.Options: added pre/post TranslationUnitPass callback

* CppSharp.Generators.Passes.Pass: added TranslationUnitPassGeneratorDependent
  • Loading branch information
deadlocklogic authored Nov 11, 2023
1 parent b14038a commit 40f3a09
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Generator/BindingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using CppSharp.Passes;
using CppSharp.Types;
using CppSharp.Parser;
using System.Collections.Generic;

namespace CppSharp.Generators
{
Expand Down Expand Up @@ -36,14 +37,21 @@ public BindingContext(DriverOptions options, ParserOptions parserOptions = null)

public void RunPasses()
{
Dictionary<System.Type, int> passesByType = new Dictionary<System.Type, int>();
int index = 0;
TranslationUnitPasses.RunPasses(pass =>
{
int count = passesByType.GetValueOrDefault(pass.GetType(), 0);
Diagnostics.Debug("Pass '{0}'", pass);
Diagnostics.PushIndent();
Options.TranslationUnitPassPreCallBack?.Invoke(pass, index, count);
pass.Context = this;
pass.VisitASTContext(ASTContext);
Options.TranslationUnitPassPostCallBack?.Invoke(pass, index, count);
Diagnostics.PopIndent();
passesByType[pass.GetType()] = count + 1;
index += 1;
});
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/Generator/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Text;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Passes;

namespace CppSharp
{
Expand Down Expand Up @@ -254,6 +255,12 @@ public bool GenerateSingleCSharpFile
/// </summary>
public bool GenerateExternalDataFields { get; set; } = false;

public delegate void TranslationUnitPassCallBack(TranslationUnitPass pass, int index, int count);

public TranslationUnitPassCallBack TranslationUnitPassPreCallBack { get; set; }

public TranslationUnitPassCallBack TranslationUnitPassPostCallBack { get; set; }

#endregion
}

Expand Down
10 changes: 10 additions & 0 deletions src/Generator/Passes/Pass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ bool IsDeclExcluded(Declaration decl)
}
}

public class TranslationUnitPassGeneratorDependent : TranslationUnitPass
{
public Generator Generator { get; }

public TranslationUnitPassGeneratorDependent(Generator generator)
{
Generator = generator;
}
}

/// <summary>
/// Used to modify generated output.
/// </summary>
Expand Down

0 comments on commit 40f3a09

Please sign in to comment.