From 3d72c2fca4cdde6df10cd5d28f3a524f8c65e2c3 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Tue, 6 Jan 2015 04:06:40 +0200 Subject: [PATCH] Added renaming of variables. Signed-off-by: Dimitar Dobrev --- src/AST/ASTVisitor.cs | 3 +++ .../Passes/CleanInvalidDeclNamesPass.cs | 6 ------ src/Generator/Passes/RenamePass.cs | 16 +++++++++++++++- tests/Basic/Basic.Tests.cs | 6 ++++++ tests/Basic/Basic.h | 1 + tests/CSharpTemp/CSharpTemp.Tests.cs | 6 ++++++ tests/CSharpTemp/CSharpTemp.cs | 2 +- tests/CSharpTemp/CSharpTemp.h | 2 +- 8 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/AST/ASTVisitor.cs b/src/AST/ASTVisitor.cs index 44ea798c..0b07ae8c 100644 --- a/src/AST/ASTVisitor.cs +++ b/src/AST/ASTVisitor.cs @@ -346,6 +346,9 @@ namespace CppSharp.AST public virtual bool VisitVariableDecl(Variable variable) { + if (!VisitDeclaration(variable)) + return false; + return variable.Type.Visit(this, variable.QualifiedType.Qualifiers); } diff --git a/src/Generator/Passes/CleanInvalidDeclNamesPass.cs b/src/Generator/Passes/CleanInvalidDeclNamesPass.cs index 9bd526e9..70ecfc9b 100644 --- a/src/Generator/Passes/CleanInvalidDeclNamesPass.cs +++ b/src/Generator/Passes/CleanInvalidDeclNamesPass.cs @@ -123,12 +123,6 @@ namespace CppSharp.Passes return base.VisitTypedefDecl(typedef); } - public override bool VisitVariableDecl(Variable variable) - { - variable.Name = CheckName(variable.Name); - return base.VisitVariableDecl(variable); - } - private static void CheckEnumName(Enumeration @enum) { // If we still do not have a valid name, then try to guess one diff --git a/src/Generator/Passes/RenamePass.cs b/src/Generator/Passes/RenamePass.cs index 6cd3eae7..059ace93 100644 --- a/src/Generator/Passes/RenamePass.cs +++ b/src/Generator/Passes/RenamePass.cs @@ -55,6 +55,7 @@ namespace CppSharp.Passes if (decl is Event) return true; if (decl is TypedefDecl) return true; if (decl is Namespace && !(decl is TranslationUnit)) return true; + if (decl is Variable) return true; return false; } @@ -213,6 +214,14 @@ namespace CppSharp.Passes return base.VisitEvent(@event); } + + public override bool VisitVariableDecl(Variable variable) + { + if (!Targets.HasFlag(RenameTargets.Variable)) + return false; + + return base.VisitVariableDecl(variable); + } } [Flags] @@ -228,7 +237,8 @@ namespace CppSharp.Passes Event = 1 << 7, Property = 1 << 8, Delegate = 1 << 9, - Any = Function | Method | Parameter | Class | Field | Enum | EnumItem | Event | Property | Delegate, + Variable = 1 << 10, + Any = Function | Method | Parameter | Class | Field | Enum | EnumItem | Event | Property | Delegate | Variable } /// @@ -311,6 +321,10 @@ namespace CppSharp.Passes /// string static string ConvertCaseString(string phrase, RenameCasePattern pattern) { + // check if it's been renamed to avoid a keyword + if (phrase.StartsWith("@")) + phrase = phrase.Substring(1); + var splittedPhrase = phrase.Split(' ', '-', '.'); var sb = new StringBuilder(); diff --git a/tests/Basic/Basic.Tests.cs b/tests/Basic/Basic.Tests.cs index b5debc4f..94566714 100644 --- a/tests/Basic/Basic.Tests.cs +++ b/tests/Basic/Basic.Tests.cs @@ -483,5 +483,11 @@ public class BasicTests : GeneratorTestFixture Assert.IsTrue(differentConstOverloads == new DifferentConstOverloads()); Assert.IsFalse(differentConstOverloads == 5); } + + [Test] + public void TestRenamingVariableNamedAfterKeyword() + { + Assert.AreEqual(10, Foo.@unsafe); + } } \ No newline at end of file diff --git a/tests/Basic/Basic.h b/tests/Basic/Basic.h index ec6f533e..994f6311 100644 --- a/tests/Basic/Basic.h +++ b/tests/Basic/Basic.h @@ -33,6 +33,7 @@ public: IgnoredType ignoredType; int fixedArray[3]; void* ptr; + static const int unsafe = 10; const char* GetANSI(); // TODO: VC++ does not support char16 diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index 17884705..a591064d 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -182,4 +182,10 @@ public class CSharpTempTests : GeneratorTestFixture Assert.AreEqual(10, structWithPrivateFields.SimplePrivateField); Assert.AreEqual(5, structWithPrivateFields.ComplexPrivateField.A); } + + [Test] + public void TestRenamingVariable() + { + Assert.AreEqual(5, Foo.Rename); + } } \ No newline at end of file diff --git a/tests/CSharpTemp/CSharpTemp.cs b/tests/CSharpTemp/CSharpTemp.cs index 40129936..1dba61ac 100644 --- a/tests/CSharpTemp/CSharpTemp.cs +++ b/tests/CSharpTemp/CSharpTemp.cs @@ -95,7 +95,7 @@ namespace CppSharp.Tests public override void Postprocess(Driver driver, ASTContext ctx) { new CaseRenamePass( - RenameTargets.Function | RenameTargets.Method | RenameTargets.Property | RenameTargets.Delegate, + RenameTargets.Function | RenameTargets.Method | RenameTargets.Property | RenameTargets.Delegate | RenameTargets.Variable, RenameCasePattern.UpperCamelCase).VisitLibrary(driver.ASTContext); } diff --git a/tests/CSharpTemp/CSharpTemp.h b/tests/CSharpTemp/CSharpTemp.h index 2f2018e3..2a4812d8 100644 --- a/tests/CSharpTemp/CSharpTemp.h +++ b/tests/CSharpTemp/CSharpTemp.h @@ -10,7 +10,7 @@ public: int& operator[](int i); int A; - static int null; + static const int rename = 5; protected: int P;