Browse Source

Added the option to have attributes generated.

Signed-off-by: Dimitar Dobrev <dpldobrev@yahoo.com>
pull/129/head
Dimitar Dobrev 12 years ago
parent
commit
6dcb8fa593
  1. 18
      src/AST/Attribute.cs
  2. 3
      src/AST/Declaration.cs
  3. 13
      src/Generator/Generators/CSharp/CSharpTextTemplate.cs
  4. 1
      src/Generator/Types/TypeMap.cs
  5. 12
      tests/CSharpTemp/CSharpTemp.Tests.cs
  6. 5
      tests/CSharpTemp/CSharpTemp.cpp
  7. 22
      tests/CSharpTemp/CSharpTemp.cs
  8. 1
      tests/CSharpTemp/CSharpTemp.h

18
src/AST/Attribute.cs

@ -0,0 +1,18 @@ @@ -0,0 +1,18 @@
namespace CppSharp.AST
{
/// <summary>
/// Gives the ability to specify attributes to generate, for example ObsoleteAttribute.
/// </summary>
public class Attribute
{
public Attribute()
{
Type = typeof(object);
Value = string.Empty;
}
public System.Type Type { get; set; }
public string Value { get; set; }
}
}

3
src/AST/Declaration.cs

@ -200,12 +200,15 @@ namespace CppSharp.AST @@ -200,12 +200,15 @@ namespace CppSharp.AST
public IntPtr OriginalPtr;
private string originalName;
public List<Attribute> Attributes { get; private set; }
protected Declaration()
{
Access = AccessSpecifier.Public;
IgnoreFlags = IgnoreFlags.None;
ExcludeFromPasses = new HashSet<System.Type>();
PreprocessedEntities = new List<PreprocessedEntity>();
Attributes = new List<Attribute>();
}
protected Declaration(string name)

13
src/Generator/Generators/CSharp/CSharpTextTemplate.cs

@ -8,6 +8,7 @@ using System.Text.RegularExpressions; @@ -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 @@ -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)

1
src/Generator/Types/TypeMap.cs

@ -5,6 +5,7 @@ using CppSharp.Generators; @@ -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

12
tests/CSharpTemp/CSharpTemp.Tests.cs

@ -1,4 +1,6 @@ @@ -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 @@ -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));
}
}

5
tests/CSharpTemp/CSharpTemp.cpp

@ -46,6 +46,11 @@ int Qux::farAwayFunc() const @@ -46,6 +46,11 @@ int Qux::farAwayFunc() const
return 20;
}
void Qux::obsolete()
{
}
int Bar::method()
{
return 2;

22
tests/CSharpTemp/CSharpTemp.cs

@ -1,9 +1,11 @@ @@ -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 @@ -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 @@ -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)

1
tests/CSharpTemp/CSharpTemp.h

@ -25,6 +25,7 @@ public: @@ -25,6 +25,7 @@ public:
Qux(Foo foo);
int farAwayFunc() const;
int array[3];
void obsolete();
};
class DLL_API Bar : public Qux

Loading…
Cancel
Save