From 6dcb8fa5934781d2aea13e657310ef5c891c4473 Mon Sep 17 00:00:00 2001 From: Dimitar Dobrev Date: Wed, 13 Nov 2013 19:04:26 +0200 Subject: [PATCH] Added the option to have attributes generated. Signed-off-by: Dimitar Dobrev --- src/AST/Attribute.cs | 18 +++++++++++++++ src/AST/Declaration.cs | 3 +++ .../Generators/CSharp/CSharpTextTemplate.cs | 13 ++++++----- src/Generator/Types/TypeMap.cs | 1 + tests/CSharpTemp/CSharpTemp.Tests.cs | 12 +++++++++- tests/CSharpTemp/CSharpTemp.cpp | 5 +++++ tests/CSharpTemp/CSharpTemp.cs | 22 ++++++++++++++++++- tests/CSharpTemp/CSharpTemp.h | 1 + 8 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 src/AST/Attribute.cs diff --git a/src/AST/Attribute.cs b/src/AST/Attribute.cs new file mode 100644 index 00000000..886b293d --- /dev/null +++ b/src/AST/Attribute.cs @@ -0,0 +1,18 @@ +namespace CppSharp.AST +{ + /// + /// Gives the ability to specify attributes to generate, for example ObsoleteAttribute. + /// + public class Attribute + { + public Attribute() + { + Type = typeof(object); + Value = string.Empty; + } + + public System.Type Type { get; set; } + + public string Value { get; set; } + } +} diff --git a/src/AST/Declaration.cs b/src/AST/Declaration.cs index ffd292ef..fb8dcf64 100644 --- a/src/AST/Declaration.cs +++ b/src/AST/Declaration.cs @@ -200,12 +200,15 @@ namespace CppSharp.AST public IntPtr OriginalPtr; private string originalName; + public List Attributes { get; private set; } + protected Declaration() { Access = AccessSpecifier.Public; IgnoreFlags = IgnoreFlags.None; ExcludeFromPasses = new HashSet(); PreprocessedEntities = new List(); + Attributes = new List(); } protected Declaration(string name) diff --git a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs index b775a652..4ab5f56d 100644 --- a/src/Generator/Generators/CSharp/CSharpTextTemplate.cs +++ b/src/Generator/Generators/CSharp/CSharpTextTemplate.cs @@ -8,6 +8,7 @@ using System.Text.RegularExpressions; using System.Web.Util; using CppSharp.AST; using CppSharp.Utils; +using Attribute = CppSharp.AST.Attribute; using Type = CppSharp.AST.Type; namespace CppSharp.Generators.CSharp @@ -280,11 +281,13 @@ namespace CppSharp.Generators.CSharp public void GenerateDeclarationCommon(Declaration decl) { - if (decl.Comment == null) - return; - - GenerateComment(decl.Comment); - GenerateDebug(decl); + if (decl.Comment != null) + { + GenerateComment(decl.Comment); + GenerateDebug(decl); + } + foreach (Attribute attribute in decl.Attributes) + WriteLine("[{0}({1})]", attribute.Type.FullName, attribute.Value); } public void GenerateDebug(Declaration decl) diff --git a/src/Generator/Types/TypeMap.cs b/src/Generator/Types/TypeMap.cs index 066433e8..ce0a4ca0 100644 --- a/src/Generator/Types/TypeMap.cs +++ b/src/Generator/Types/TypeMap.cs @@ -5,6 +5,7 @@ using CppSharp.Generators; using CppSharp.Generators.AST; using CppSharp.Generators.CLI; using CppSharp.Generators.CSharp; +using Attribute = System.Attribute; using Type = CppSharp.AST.Type; namespace CppSharp.Types diff --git a/tests/CSharpTemp/CSharpTemp.Tests.cs b/tests/CSharpTemp/CSharpTemp.Tests.cs index 84a923cd..5859863d 100644 --- a/tests/CSharpTemp/CSharpTemp.Tests.cs +++ b/tests/CSharpTemp/CSharpTemp.Tests.cs @@ -1,4 +1,6 @@ -using System.Reflection; +using System; +using System.Linq; +using System.Reflection; using CSharpTemp; using NUnit.Framework; using Foo = CSharpTemp.Foo; @@ -76,4 +78,12 @@ public class CSharpTempTests Assert.That(p.Test, Is.True); Assert.That(p.IsBool, Is.False); } + + [Test] + public void TestAttributes() + { + Assert.That(typeof(Qux).GetMethod("Obsolete") + .GetCustomAttributes(typeof(ObsoleteAttribute), false).Length, + Is.GreaterThan(0)); + } } \ No newline at end of file diff --git a/tests/CSharpTemp/CSharpTemp.cpp b/tests/CSharpTemp/CSharpTemp.cpp index 0585226c..c14d9e62 100644 --- a/tests/CSharpTemp/CSharpTemp.cpp +++ b/tests/CSharpTemp/CSharpTemp.cpp @@ -46,6 +46,11 @@ int Qux::farAwayFunc() const return 20; } +void Qux::obsolete() +{ + +} + int Bar::method() { return 2; diff --git a/tests/CSharpTemp/CSharpTemp.cs b/tests/CSharpTemp/CSharpTemp.cs index bb473b11..c54d2713 100644 --- a/tests/CSharpTemp/CSharpTemp.cs +++ b/tests/CSharpTemp/CSharpTemp.cs @@ -1,9 +1,11 @@ -using CppSharp.AST; +using System; +using CppSharp.AST; using CppSharp.Generators; using CppSharp.Generators.CSharp; using CppSharp.Passes; using CppSharp.Types; using CppSharp.Utils; +using Attribute = CppSharp.AST.Attribute; namespace CppSharp.Tests { @@ -28,6 +30,23 @@ namespace CppSharp.Tests } } + public class TestAttributesPass : TranslationUnitPass + { + public override bool VisitFunctionDecl(Function function) + { + if (!AlreadyVisited(function) && function.Name == "obsolete") + { + Attribute attribute = new Attribute + { + Type = typeof(ObsoleteAttribute), + Value = string.Format("\"{0} is obsolete.\"", function.Name) + }; + function.Attributes.Add(attribute); + } + return base.VisitFunctionDecl(function); + } + } + public class CSharpTempTests : LibraryTest { public CSharpTempTests(GeneratorKind kind) @@ -40,6 +59,7 @@ namespace CppSharp.Tests driver.Options.GenerateInterfacesForMultipleInheritance = true; driver.Options.GenerateProperties = true; driver.Options.GenerateVirtualTables = true; + driver.TranslationUnitPasses.AddPass(new TestAttributesPass()); } public override void Postprocess(Driver driver, ASTContext lib) diff --git a/tests/CSharpTemp/CSharpTemp.h b/tests/CSharpTemp/CSharpTemp.h index a573c6ed..98151bf4 100644 --- a/tests/CSharpTemp/CSharpTemp.h +++ b/tests/CSharpTemp/CSharpTemp.h @@ -25,6 +25,7 @@ public: Qux(Foo foo); int farAwayFunc() const; int array[3]; + void obsolete(); }; class DLL_API Bar : public Qux