Browse Source

Optimised the pass for fixing invalid names by skipping already visited decarations.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/523/merge
Dimitar Dobrev 11 years ago
parent
commit
b09d5a897e
  1. 2
      src/AST/Declaration.cs
  2. 58
      src/Generator/Passes/CleanInvalidDeclNamesPass.cs
  3. 11
      src/Generator/Utils/Utils.cs

2
src/AST/Declaration.cs

@ -94,7 +94,7 @@ namespace CppSharp.AST
set set
{ {
name = value; name = value;
if (string.IsNullOrEmpty(OriginalName)) if (OriginalName == null)
OriginalName = name; OriginalName = name;
} }
} }

58
src/Generator/Passes/CleanInvalidDeclNamesPass.cs

@ -1,9 +1,8 @@
using System; using System.Linq;
using System.Linq; using System.Text;
using CppSharp.AST; using CppSharp.AST;
using CppSharp.Generators.CLI; using CppSharp.Generators.CLI;
using CppSharp.Generators.CSharp; using CppSharp.Generators.CSharp;
using System.Text;
namespace CppSharp.Passes namespace CppSharp.Passes
{ {
@ -28,6 +27,9 @@ namespace CppSharp.Passes
public override bool VisitDeclaration(Declaration decl) public override bool VisitDeclaration(Declaration decl)
{ {
if (!base.VisitDeclaration(decl))
return false;
// Do not clean up namespace names since it can mess up with the // Do not clean up namespace names since it can mess up with the
// names of anonymous or the global namespace. // names of anonymous or the global namespace.
if (decl is Namespace) if (decl is Namespace)
@ -38,7 +40,7 @@ namespace CppSharp.Passes
{ {
decl.Name = decl.Namespace.Name == "_" ? "__" : "_"; decl.Name = decl.Namespace.Name == "_" ? "__" : "_";
decl.ExplicitlyIgnore(); decl.ExplicitlyIgnore();
return false; return true;
} }
Function function = decl as Function; Function function = decl as Function;
@ -46,7 +48,7 @@ namespace CppSharp.Passes
decl.Name = CheckName(decl.Name); decl.Name = CheckName(decl.Name);
StringHelpers.CleanupText(ref decl.DebugText); StringHelpers.CleanupText(ref decl.DebugText);
return base.VisitDeclaration(decl); return true;
} }
public override bool VisitParameterDecl(Parameter parameter) public override bool VisitParameterDecl(Parameter parameter)
@ -56,10 +58,10 @@ namespace CppSharp.Passes
public override bool VisitClassDecl(Class @class) public override bool VisitClassDecl(Class @class)
{ {
var currentUniqueName = this.uniqueName; var currentUniqueName = uniqueName;
this.uniqueName = 0; uniqueName = 0;
var ret = base.VisitClassDecl(@class); base.VisitClassDecl(@class);
this.uniqueName = currentUniqueName; uniqueName = currentUniqueName;
if (@class.Namespace.Classes.Any(d => d != @class && d.Name == @class.Name)) if (@class.Namespace.Classes.Any(d => d != @class && d.Name == @class.Name))
{ {
@ -72,25 +74,25 @@ namespace CppSharp.Passes
@class.Name = str.ToString(); @class.Name = str.ToString();
} }
return ret; return true;
} }
public override bool VisitFunctionDecl(Function function) public override bool VisitFunctionDecl(Function function)
{ {
var currentUniqueName = this.uniqueName; var currentUniqueName = uniqueName;
this.uniqueName = 0; uniqueName = 0;
var ret = base.VisitFunctionDecl(function); var ret = base.VisitFunctionDecl(function);
this.uniqueName = currentUniqueName; uniqueName = currentUniqueName;
return ret; return ret;
} }
public override bool VisitEvent(Event @event) public override bool VisitEvent(Event @event)
{ {
var currentUniqueName = this.uniqueName; var currentUniqueName = uniqueName;
this.uniqueName = 0; uniqueName = 0;
var ret = base.VisitEvent(@event); var ret = base.VisitEvent(@event);
this.uniqueName = currentUniqueName; uniqueName = currentUniqueName;
return ret; return ret;
} }
@ -108,6 +110,9 @@ namespace CppSharp.Passes
public override bool VisitTypedefDecl(TypedefDecl typedef) public override bool VisitTypedefDecl(TypedefDecl typedef)
{ {
if (base.VisitTypedefDecl(typedef))
return false;
var @class = typedef.Namespace.FindClass(typedef.Name); var @class = typedef.Namespace.FindClass(typedef.Name);
// Clang will walk the typedef'd tag decl and the typedef decl, // Clang will walk the typedef'd tag decl and the typedef decl,
@ -119,7 +124,7 @@ namespace CppSharp.Passes
if (typedef.Type == null) if (typedef.Type == null)
typedef.ExplicitlyIgnore(); typedef.ExplicitlyIgnore();
return base.VisitTypedefDecl(typedef); return true;
} }
private static void CheckEnumName(Enumeration @enum) private static void CheckEnumName(Enumeration @enum)
@ -127,7 +132,7 @@ namespace CppSharp.Passes
// If we still do not have a valid name, then try to guess one // If we still do not have a valid name, then try to guess one
// based on the enum value names. // based on the enum value names.
if (!String.IsNullOrWhiteSpace(@enum.Name)) if (!string.IsNullOrWhiteSpace(@enum.OriginalName))
return; return;
var prefix = @enum.Items.Select(item => item.Name) var prefix = @enum.Items.Select(item => item.Name)
@ -137,24 +142,33 @@ namespace CppSharp.Passes
if (prefix.Length < 3) if (prefix.Length < 3)
return; return;
prefix = prefix.Trim().Trim(new char[] { '_' }); prefix = prefix.Trim().Trim('_');
@enum.Name = prefix; @enum.Name = prefix;
} }
public override bool VisitEnumDecl(Enumeration @enum) public override bool VisitEnumDecl(Enumeration @enum)
{ {
if (!base.VisitEnumDecl(@enum))
return false;
CheckEnumName(@enum); CheckEnumName(@enum);
return base.VisitEnumDecl(@enum); return true;
} }
public override bool VisitEnumItem(Enumeration.Item item) public override bool VisitEnumItem(Enumeration.Item item)
{ {
if (!base.VisitEnumItem(item))
return false;
item.Name = CheckName(item.Name); item.Name = CheckName(item.Name);
return base.VisitEnumItem(item); return true;
} }
public override bool VisitFieldDecl(Field field) public override bool VisitFieldDecl(Field field)
{ {
if (!base.VisitFieldDecl(field))
return false;
if (field.Class.Fields.Count(c => c.Name.Equals(field.Name)) > 1) if (field.Class.Fields.Count(c => c.Name.Equals(field.Name)) > 1)
{ {
StringBuilder str = new StringBuilder(); StringBuilder str = new StringBuilder();
@ -165,7 +179,7 @@ namespace CppSharp.Passes
} while (field.Class.Fields.Any(c => c.Name.Equals(str.ToString()))); } while (field.Class.Fields.Any(c => c.Name.Equals(str.ToString())));
field.Name = str.ToString(); field.Name = str.ToString();
} }
return base.VisitFieldDecl(field); return true;
} }
} }
} }

11
src/Generator/Utils/Utils.cs

@ -127,12 +127,15 @@ namespace CppSharp
public static void CleanupText(ref string debugText) public static void CleanupText(ref string debugText)
{ {
// Strip off newlines from the debug text. // Strip off newlines from the debug text.
if (String.IsNullOrWhiteSpace(debugText)) if (string.IsNullOrWhiteSpace(debugText))
debugText = String.Empty; {
debugText = string.Empty;
return;
}
// TODO: Make this transformation in the output. // TODO: Make this transformation in the output.
debugText = Regex.Replace(debugText, " ( )+", " "); debugText = Regex.Replace(debugText, " {2,}", " ");
debugText = Regex.Replace(debugText, "\n", ""); debugText = debugText.Replace("\n", "");
} }
public static string[] SplitCamelCase(string input) public static string[] SplitCamelCase(string input)

Loading…
Cancel
Save