Browse Source

Refactor the diagnostics system.

pull/750/head
Joao Matos 9 years ago
parent
commit
fbf43067d7
  1. 50
      src/Core/Diagnostics.cs
  2. 2
      src/Generator.Tests/AST/TestAST.cs
  3. 2
      src/Generator.Tests/ASTTestFixture.cs
  4. 6
      src/Generator.Tests/GeneratorTest.cs
  5. 2
      src/Generator.Tests/ReadNativeDependenciesTest.cs
  6. 6
      src/Generator/BindingContext.cs
  7. 22
      src/Generator/Driver.cs
  8. 1
      src/Generator/Generators/Template.cs
  9. 13
      src/Generator/Passes/CheckDuplicatedNamesPass.cs
  10. 6
      src/Generator/Passes/GetterSetterToPropertyPass.cs
  11. 1
      src/Generator/Passes/Pass.cs

50
src/Core/Diagnostics.cs

@ -36,66 +36,68 @@ namespace CppSharp @@ -36,66 +36,68 @@ 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
{
var diagInfo = new DiagnosticInfo
get { return Implementation.Level; }
set { Implementation.Level = value; }
}
public static void PushIndent(int level = 4)
{
Kind = DiagnosticKind.Debug,
Message = string.Format(msg, args)
};
Implementation.PushIndent(level);
}
consumer.Emit(diagInfo);
public static void PopIndent()
{
Implementation.PopIndent();
}
public static void Message(this IDiagnostics consumer,
string msg, params object[] args)
public static void Debug(string msg, params object[] args)
{
var diagInfo = new DiagnosticInfo
{
Kind = DiagnosticKind.Message,
Kind = DiagnosticKind.Debug,
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 Message(string msg, params object[] args)
{
var diagInfo = new DiagnosticInfo
{
Kind = DiagnosticKind.Warning,
Kind = DiagnosticKind.Message,
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 Warning(string msg, params object[] args)
{
var diagInfo = new DiagnosticInfo
{
Kind = DiagnosticKind.Error,
Kind = DiagnosticKind.Warning,
Message = string.Format(msg, args)
};
consumer.Emit(diagInfo);
Implementation.Emit(diagInfo);
}
public static void Error(this IDiagnostics consumer,
string msg)
public static void Error(string msg, params object[] args)
{
var diagInfo = new DiagnosticInfo
{
Kind = DiagnosticKind.Error,
Message = msg
Message = string.Format(msg, args)
};
consumer.Emit(diagInfo);
Implementation.Emit(diagInfo);
}
}

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

@ -315,7 +315,7 @@ namespace CppSharp.Generator.Tests.AST @@ -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);

2
src/Generator.Tests/ASTTestFixture.cs

@ -22,7 +22,7 @@ namespace CppSharp.Generator.Tests @@ -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
};

6
src/Generator.Tests/GeneratorTest.cs

@ -32,8 +32,8 @@ namespace CppSharp.Utils @@ -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 @@ -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));

2
src/Generator.Tests/ReadNativeDependenciesTest.cs

@ -42,7 +42,7 @@ namespace CppSharp.Generator.Tests @@ -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
};

6
src/Generator/BindingContext.cs

@ -8,8 +8,6 @@ namespace CppSharp.Generators @@ -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 @@ -24,11 +22,9 @@ namespace CppSharp.Generators
public Dictionary<Function, DelegatesPass.DelegateDefinition> 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();

22
src/Generator/Driver.cs

@ -20,7 +20,6 @@ namespace CppSharp @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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();

1
src/Generator/Generators/Template.cs

@ -7,7 +7,6 @@ namespace CppSharp.Generators @@ -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<TranslationUnit> TranslationUnits { get; private set; }

13
src/Generator/Passes/CheckDuplicatedNamesPass.cs

@ -1,5 +1,4 @@ @@ -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 @@ -11,11 +10,9 @@ namespace CppSharp.Passes
{
private readonly Dictionary<string, int> methodSignatures;
private int Count;
private readonly IDiagnostics diagnostics;
public DeclarationName(IDiagnostics diagnostics)
public DeclarationName()
{
this.diagnostics = diagnostics;
methodSignatures = new Dictionary<string, int>();
}
@ -77,12 +74,12 @@ namespace CppSharp.Passes @@ -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 @@ -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))
{

6
src/Generator/Passes/GetterSetterToPropertyPass.cs

@ -15,15 +15,13 @@ namespace CppSharp.Passes @@ -15,15 +15,13 @@ namespace CppSharp.Passes
{
private class PropertyGenerator
{
private readonly IDiagnostics Diagnostics;
private readonly List<Method> getters = new List<Method>();
private readonly List<Method> setters = new List<Method>();
private readonly List<Method> setMethods = new List<Method>();
private readonly List<Method> nonSetters = new List<Method>();
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 @@ -354,7 +352,7 @@ namespace CppSharp.Passes
if (baseClass.IsClass)
VisitClassDecl(baseClass.Class);
new PropertyGenerator(@class, Diagnostics).GenerateProperties();
new PropertyGenerator(@class).GenerateProperties();
}
return false;
}

1
src/Generator/Passes/Pass.cs

@ -12,7 +12,6 @@ namespace CppSharp.Passes @@ -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; } }

Loading…
Cancel
Save