From 40f3a09296ee4f7339483eaaa16e6d2d4b18c2a4 Mon Sep 17 00:00:00 2001 From: deadlocklogic <57906342+deadlocklogic@users.noreply.github.com> Date: Sat, 11 Nov 2023 16:51:18 +0200 Subject: [PATCH] Minor improvements (#1793) * CppSharp.Generators.Options: added pre/post TranslationUnitPass callback * CppSharp.Generators.Passes.Pass: added TranslationUnitPassGeneratorDependent --- src/Generator/BindingContext.cs | 8 ++++++++ src/Generator/Options.cs | 7 +++++++ src/Generator/Passes/Pass.cs | 10 ++++++++++ 3 files changed, 25 insertions(+) diff --git a/src/Generator/BindingContext.cs b/src/Generator/BindingContext.cs index e7c094c692..cf0cbd3e41 100644 --- a/src/Generator/BindingContext.cs +++ b/src/Generator/BindingContext.cs @@ -2,6 +2,7 @@ using CppSharp.Passes; using CppSharp.Types; using CppSharp.Parser; +using System.Collections.Generic; namespace CppSharp.Generators { @@ -36,14 +37,21 @@ public BindingContext(DriverOptions options, ParserOptions parserOptions = null) public void RunPasses() { + Dictionary passesByType = new Dictionary(); + 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; }); } } diff --git a/src/Generator/Options.cs b/src/Generator/Options.cs index 567153c051..f42236cd67 100644 --- a/src/Generator/Options.cs +++ b/src/Generator/Options.cs @@ -5,6 +5,7 @@ using System.Text; using CppSharp.AST; using CppSharp.Generators; +using CppSharp.Passes; namespace CppSharp { @@ -254,6 +255,12 @@ public bool GenerateSingleCSharpFile /// 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 } diff --git a/src/Generator/Passes/Pass.cs b/src/Generator/Passes/Pass.cs index 877a1116e2..3ea925528d 100644 --- a/src/Generator/Passes/Pass.cs +++ b/src/Generator/Passes/Pass.cs @@ -51,6 +51,16 @@ bool IsDeclExcluded(Declaration decl) } } + public class TranslationUnitPassGeneratorDependent : TranslationUnitPass + { + public Generator Generator { get; } + + public TranslationUnitPassGeneratorDependent(Generator generator) + { + Generator = generator; + } + } + /// /// Used to modify generated output. ///