diff --git a/src/Core/Diagnostics.cs b/src/Core/Diagnostics.cs index 0503e9ca..6ca8c168 100644 --- a/src/Core/Diagnostics.cs +++ b/src/Core/Diagnostics.cs @@ -36,10 +36,27 @@ namespace CppSharp void PopIndent(); } - public static class DiagnosticExtensions + public static class Diagnostics { - public static void Debug(this IDiagnostics consumer, - string msg, params object[] args) + public static IDiagnostics Implementation { get; set; } = new ConsoleDiagnostics(); + + public static DiagnosticKind Level + { + get { return Implementation.Level; } + set { Implementation.Level = value; } + } + + public static void PushIndent(int level = 4) + { + Implementation.PushIndent(level); + } + + public static void PopIndent() + { + Implementation.PopIndent(); + } + + public static void Debug(string msg, params object[] args) { var diagInfo = new DiagnosticInfo { @@ -47,11 +64,10 @@ namespace CppSharp Message = string.Format(msg, args) }; - consumer.Emit(diagInfo); + Implementation.Emit(diagInfo); } - public static void Message(this IDiagnostics consumer, - string msg, params object[] args) + public static void Message(string msg, params object[] args) { var diagInfo = new DiagnosticInfo { @@ -59,11 +75,10 @@ namespace CppSharp Message = string.Format(msg, args) }; - consumer.Emit(diagInfo); + Implementation.Emit(diagInfo); } - public static void Warning(this IDiagnostics consumer, - string msg, params object[] args) + public static void Warning(string msg, params object[] args) { var diagInfo = new DiagnosticInfo { @@ -71,11 +86,10 @@ namespace CppSharp Message = string.Format(msg, args) }; - consumer.Emit(diagInfo); + Implementation.Emit(diagInfo); } - public static void Error(this IDiagnostics consumer, - string msg, params object[] args) + public static void Error(string msg, params object[] args) { var diagInfo = new DiagnosticInfo { @@ -83,19 +97,7 @@ namespace CppSharp Message = string.Format(msg, args) }; - consumer.Emit(diagInfo); - } - - public static void Error(this IDiagnostics consumer, - string msg) - { - var diagInfo = new DiagnosticInfo - { - Kind = DiagnosticKind.Error, - Message = msg - }; - - consumer.Emit(diagInfo); + Implementation.Emit(diagInfo); } } diff --git a/src/Generator.Tests/AST/TestAST.cs b/src/Generator.Tests/AST/TestAST.cs index e10a956c..7b98e97d 100644 --- a/src/Generator.Tests/AST/TestAST.cs +++ b/src/Generator.Tests/AST/TestAST.cs @@ -315,7 +315,7 @@ namespace CppSharp.Generator.Tests.AST [Test] public void TestAmbiguity() { - var bindingContext = new BindingContext(new ConsoleDiagnostics(), new DriverOptions(), + var bindingContext = new BindingContext(new DriverOptions(), new ParserOptions()); new CleanUnitPass { Context = bindingContext }.VisitASTContext(AstContext); new CheckAmbiguousFunctions { Context = bindingContext }.VisitASTContext(AstContext); diff --git a/src/Generator.Tests/ASTTestFixture.cs b/src/Generator.Tests/ASTTestFixture.cs index ececa4ac..2c0be5bf 100644 --- a/src/Generator.Tests/ASTTestFixture.cs +++ b/src/Generator.Tests/ASTTestFixture.cs @@ -22,7 +22,7 @@ namespace CppSharp.Generator.Tests Options.Headers.AddRange(files); - Driver = new Driver(Options, new ConsoleDiagnostics()) + Driver = new Driver(Options) { ParserOptions = this.ParserOptions }; diff --git a/src/Generator.Tests/GeneratorTest.cs b/src/Generator.Tests/GeneratorTest.cs index ce61f668..3b3e61ea 100644 --- a/src/Generator.Tests/GeneratorTest.cs +++ b/src/Generator.Tests/GeneratorTest.cs @@ -32,8 +32,8 @@ namespace CppSharp.Utils options.Quiet = true; options.IgnoreParseWarnings = true; - driver.Diagnostics.Message(""); - driver.Diagnostics.Message("Generating bindings for {0} ({1})", + Diagnostics.Message(""); + Diagnostics.Message("Generating bindings for {0} ({1})", options.LibraryName, options.GeneratorKind.ToString()); // Workaround for CLR which does not check for .dll if the @@ -48,7 +48,7 @@ namespace CppSharp.Utils var path = Path.GetFullPath(GetTestsDirectory(name)); parserOptions.AddIncludeDirs(path); - driver.Diagnostics.Message("Looking for tests in: {0}", path); + Diagnostics.Message("Looking for tests in: {0}", path); var files = Directory.EnumerateFiles(path, "*.h"); foreach (var file in files) options.Headers.Add(Path.GetFileName(file)); diff --git a/src/Generator.Tests/ReadNativeDependenciesTest.cs b/src/Generator.Tests/ReadNativeDependenciesTest.cs index 08c2ba70..53ad0440 100644 --- a/src/Generator.Tests/ReadNativeDependenciesTest.cs +++ b/src/Generator.Tests/ReadNativeDependenciesTest.cs @@ -42,7 +42,7 @@ namespace CppSharp.Generator.Tests parserOptions.AddLibraryDirs(GeneratorTest.GetTestsDirectory("Native")); var driverOptions = new DriverOptions(); driverOptions.Libraries.Add(library); - var driver = new Driver(driverOptions, new ConsoleDiagnostics()) + var driver = new Driver(driverOptions) { ParserOptions = parserOptions }; diff --git a/src/Generator/BindingContext.cs b/src/Generator/BindingContext.cs index 77582857..f7cc8254 100644 --- a/src/Generator/BindingContext.cs +++ b/src/Generator/BindingContext.cs @@ -8,8 +8,6 @@ namespace CppSharp.Generators { public class BindingContext { - public IDiagnostics Diagnostics { get; set; } - public DriverOptions Options { get; private set; } public ParserOptions ParserOptions { get; set; } @@ -24,11 +22,9 @@ namespace CppSharp.Generators public Dictionary Delegates { get; private set; } - public BindingContext(IDiagnostics diagnostics, DriverOptions options, - ParserOptions parserOptions = null) + public BindingContext(DriverOptions options, ParserOptions parserOptions = null) { Options = options; - Diagnostics = diagnostics; ParserOptions = parserOptions; Symbols = new SymbolContext(); diff --git a/src/Generator/Driver.cs b/src/Generator/Driver.cs index f879a08f..126e739f 100644 --- a/src/Generator/Driver.cs +++ b/src/Generator/Driver.cs @@ -20,7 +20,6 @@ namespace CppSharp { public class Driver { - public IDiagnostics Diagnostics { get; private set; } public DriverOptions Options { get; private set; } public ParserOptions ParserOptions { get; set; } public Project Project { get; private set; } @@ -29,10 +28,9 @@ namespace CppSharp public bool HasCompilationErrors { get; set; } - public Driver(DriverOptions options, IDiagnostics diagnostics) + public Driver(DriverOptions options) { Options = options; - Diagnostics = diagnostics; Project = new Project(); ParserOptions = new ParserOptions(); } @@ -70,7 +68,7 @@ namespace CppSharp { ValidateOptions(); ParserOptions.SetupIncludes(); - Context = new BindingContext(Diagnostics, Options, ParserOptions); + Context = new BindingContext(Options, ParserOptions); Generator = CreateGeneratorFromKind(Options.GeneratorKind); } @@ -473,31 +471,29 @@ namespace CppSharp public static void Run(ILibrary library) { var options = new DriverOptions(); - - var Log = new ConsoleDiagnostics(); - var driver = new Driver(options, Log); + var driver = new Driver(options); library.Setup(driver); driver.Setup(); if(driver.ParserOptions.Verbose) - Log.Level = DiagnosticKind.Debug; + Diagnostics.Level = DiagnosticKind.Debug; if (!options.Quiet) - Log.Message("Parsing libraries..."); + Diagnostics.Message("Parsing libraries..."); if (!driver.ParseLibraries()) return; if (!options.Quiet) - Log.Message("Parsing code..."); + Diagnostics.Message("Parsing code..."); driver.BuildParseOptions(); if (!driver.ParseCode()) { - Log.Error("CppSharp has encountered an error while parsing code."); + Diagnostics.Error("CppSharp has encountered an error while parsing code."); return; } @@ -505,7 +501,7 @@ namespace CppSharp options.Modules.RemoveAll(m => m != options.SystemModule && !m.Units.GetGenerated().Any()); if (!options.Quiet) - Log.Message("Processing code..."); + Diagnostics.Message("Processing code..."); library.Preprocess(driver, driver.Context.ASTContext); @@ -515,7 +511,7 @@ namespace CppSharp library.Postprocess(driver, driver.Context.ASTContext); if (!options.Quiet) - Log.Message("Generating code..."); + Diagnostics.Message("Generating code..."); var outputs = driver.GenerateCode(); diff --git a/src/Generator/Generators/Template.cs b/src/Generator/Generators/Template.cs index 5d85e1fb..a8bccecd 100644 --- a/src/Generator/Generators/Template.cs +++ b/src/Generator/Generators/Template.cs @@ -7,7 +7,6 @@ namespace CppSharp.Generators { public BindingContext Context { get; private set; } - public IDiagnostics Log { get { return Context.Diagnostics; } } public DriverOptions Options { get { return Context.Options; } } public List TranslationUnits { get; private set; } diff --git a/src/Generator/Passes/CheckDuplicatedNamesPass.cs b/src/Generator/Passes/CheckDuplicatedNamesPass.cs index 7316ca64..0dcfac00 100644 --- a/src/Generator/Passes/CheckDuplicatedNamesPass.cs +++ b/src/Generator/Passes/CheckDuplicatedNamesPass.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Globalization; using System.Linq; using CppSharp.AST; @@ -11,11 +10,9 @@ namespace CppSharp.Passes { private readonly Dictionary methodSignatures; private int Count; - private readonly IDiagnostics diagnostics; - public DeclarationName(IDiagnostics diagnostics) + public DeclarationName() { - this.diagnostics = diagnostics; methodSignatures = new Dictionary(); } @@ -77,12 +74,12 @@ namespace CppSharp.Passes { // TODO: turn into a method; append the original type (say, "signed long") // of the last parameter to the type so that the user knows which overload is called - diagnostics.Warning("Duplicate operator {0} ignored", function.Name); + Diagnostics.Warning("Duplicate operator {0} ignored", function.Name); function.ExplicitlyIgnore(); } else if (method != null && method.IsConstructor) { - diagnostics.Warning("Duplicate constructor {0} ignored", function.Name); + Diagnostics.Warning("Duplicate constructor {0} ignored", function.Name); function.ExplicitlyIgnore(); } else @@ -221,7 +218,7 @@ namespace CppSharp.Passes // If the name is not yet on the map, then add it. if (!names.ContainsKey(fullName)) - names.Add(fullName, new DeclarationName(Diagnostics)); + names.Add(fullName, new DeclarationName()); if (names[fullName].UpdateName(decl)) { diff --git a/src/Generator/Passes/GetterSetterToPropertyPass.cs b/src/Generator/Passes/GetterSetterToPropertyPass.cs index 5f9fb3f8..c51fc5fa 100644 --- a/src/Generator/Passes/GetterSetterToPropertyPass.cs +++ b/src/Generator/Passes/GetterSetterToPropertyPass.cs @@ -15,15 +15,13 @@ namespace CppSharp.Passes { private class PropertyGenerator { - private readonly IDiagnostics Diagnostics; private readonly List getters = new List(); private readonly List setters = new List(); private readonly List setMethods = new List(); private readonly List nonSetters = new List(); - public PropertyGenerator(Class @class, IDiagnostics diags) + public PropertyGenerator(Class @class) { - Diagnostics = diags; foreach (var method in @class.Methods.Where( m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated)) DistributeMethod(method); @@ -354,7 +352,7 @@ namespace CppSharp.Passes if (baseClass.IsClass) VisitClassDecl(baseClass.Class); - new PropertyGenerator(@class, Diagnostics).GenerateProperties(); + new PropertyGenerator(@class).GenerateProperties(); } return false; } diff --git a/src/Generator/Passes/Pass.cs b/src/Generator/Passes/Pass.cs index dd8d5ba3..3f2e185e 100644 --- a/src/Generator/Passes/Pass.cs +++ b/src/Generator/Passes/Pass.cs @@ -12,7 +12,6 @@ namespace CppSharp.Passes { public BindingContext Context { get; set; } - public IDiagnostics Diagnostics { get { return Context.Diagnostics; } } public DriverOptions Options { get { return Context.Options; } } public ASTContext ASTContext { get { return Context.ASTContext; } } public TypeMapDatabase TypeMaps { get { return Context.TypeMaps; } }