diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index c72b5388..07c7af2e 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -5936,9 +5936,31 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = } } - if (!isTestOutput && namedDecl.HasAttrs) + if (!isTestOutput) { - foreach (var attr in namedDecl.Attrs) + var declAttrs = namedDecl.HasAttrs + ? namedDecl.Attrs + : Enumerable.Empty(); + + if (namedDecl is FieldDecl fieldDecl) + { + if (fieldDecl.Type is TypedefType defType && defType.Decl.HasAttrs) + { + declAttrs = declAttrs.Concat(defType.Decl.Attrs); + } + } + else if (namedDecl is RecordDecl recordDecl) + { + var typedefName = recordDecl.TypedefNameForAnonDecl; + if (typedefName != null && typedefName.HasAttrs) + { + declAttrs = declAttrs.Concat(typedefName.Attrs); + } + } + + var obsoleteEmitted = false; + + foreach (var attr in declAttrs) { switch (attr.Kind) { @@ -5953,20 +5975,26 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = case CX_AttrKind.CX_AttrKind_Deprecated: { + if (obsoleteEmitted) + { + break; + } + var attrText = GetSourceRangeContents(namedDecl.TranslationUnit.Handle, attr.Extent); var textStart = attrText.IndexOf('"'); var textLength = attrText.LastIndexOf('"') - textStart; - if (textLength > 2) + if (textLength > 1) { - var text = attrText.AsSpan(textStart + 1, textLength - 2); + var text = attrText.AsSpan(textStart + 1, textLength - 1); outputBuilder.WriteCustomAttribute($"Obsolete(\"{text}\")"); } else { outputBuilder.WriteCustomAttribute($"Obsolete"); } + obsoleteEmitted = true; break; } @@ -6241,7 +6269,7 @@ private void WithTestAssertEqual(string expected, string actual) _testOutputBuilder.WriteIndented("Assert.Equal"); _testOutputBuilder.Write('('); _testOutputBuilder.Write(expected); - _testOutputBuilder.Write(", ");; + _testOutputBuilder.Write(", "); _testOutputBuilder.Write(actual); _testOutputBuilder.Write(')'); _testOutputBuilder.WriteSemicolon(); diff --git a/sources/ClangSharp/Cursors/Decls/TagDecl.cs b/sources/ClangSharp/Cursors/Decls/TagDecl.cs index c41be351..9431b89f 100644 --- a/sources/ClangSharp/Cursors/Decls/TagDecl.cs +++ b/sources/ClangSharp/Cursors/Decls/TagDecl.cs @@ -10,7 +10,7 @@ public unsafe class TagDecl : TypeDecl, IDeclContext, IRedeclarable { private readonly Lazy _definition; private readonly Lazy>> _templateParameterLists; - private readonly Lazy _typedefNameForAnonDecl; + private readonly Lazy _typedefNameForAnonDecl; private protected TagDecl(CXCursor handle, CXCursorKind expectedCursorKind, CX_DeclKind expectedDeclKind) : base(handle, expectedCursorKind, expectedDeclKind) { @@ -42,7 +42,7 @@ private protected TagDecl(CXCursor handle, CXCursorKind expectedCursorKind, CX_D return templateParameterLists; }); - _typedefNameForAnonDecl = new Lazy(() => TranslationUnit.GetOrCreate(Handle.TypedefNameForAnonDecl)); + _typedefNameForAnonDecl = new Lazy(() => !Handle.TypedefNameForAnonDecl.IsNull ? TranslationUnit.GetOrCreate(Handle.TypedefNameForAnonDecl) : null); } public new TagDecl CanonicalDecl => (TagDecl)base.CanonicalDecl; @@ -65,5 +65,5 @@ private protected TagDecl(CXCursor handle, CXCursorKind expectedCursorKind, CX_D public IReadOnlyList> TemplateParameterLists => _templateParameterLists.Value; - public TypedefNameDecl TypedefNameForAnonDecl => _typedefNameForAnonDecl.Value; + public TypedefNameDecl? TypedefNameForAnonDecl => _typedefNameForAnonDecl.Value; } diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..f062c262 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs @@ -0,0 +1,64 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; +using NUnit.Framework; + +namespace ClangSharp.UnitTests; + +public abstract class DeprecatedToObsoleteTest : PInvokeGeneratorTest +{ + [TestCase("int", "int")] + public Task SimpleStructMembers(string nativeType, string expectedManagedType) => SimpleStructMembersImpl(nativeType, expectedManagedType); + + [Test] + public Task StructDecl() => StructDeclImpl(); + + [TestCase("int", "int")] + public Task SimpleTypedefStructMembers(string nativeType, string expectedManagedType) => SimpleTypedefStructMembersImpl(nativeType, expectedManagedType); + + [Test] + public Task TypedefStructDecl() => TypedefStructDeclImpl(); + + [Test] + public Task SimpleEnumMembers() => SimpleEnumMembersImpl(); + + [Test] + public Task EnumDecl() => EnumDeclImpl(); + + [TestCase("int", "int")] + public Task SimpleVarDecl(string nativeType, string expectedManagedType) => SimpleVarDeclImpl(nativeType, expectedManagedType); + + [Test] + public Task FuncDecl() => FuncDeclImpl(); + + [Test] + public Task InstanceFunc() => InstanceFuncImpl(); + + [Test] + public Task FuncPtrDecl() => FuncPtrDeclImpl(); + + [Test] + public Task FuncDeclDllImport() => FuncDllImportImpl(); + + protected abstract Task SimpleStructMembersImpl(string nativeType, string expectedManagedType); + + protected abstract Task StructDeclImpl(); + + protected abstract Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType); + + protected abstract Task TypedefStructDeclImpl(); + + protected abstract Task SimpleEnumMembersImpl(); + + protected abstract Task EnumDeclImpl(); + + protected abstract Task SimpleVarDeclImpl(string nativeType, string expectedManagedType); + + protected abstract Task FuncDeclImpl(); + + protected abstract Task InstanceFuncImpl(); + + protected abstract Task FuncPtrDeclImpl(); + + protected abstract Task FuncDllImportImpl(); +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..87a1f500 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/DeprecatedToObsoleteTest.cs @@ -0,0 +1,519 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class CSharpCompatibleUnix_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum + { + MyEnum_Value0, + [Obsolete] + MyEnum_Value1, + [Obsolete(""This is obsolete."")] + MyEnum_Value2, + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum0 + { + MyEnum_Value0, + } + + [Obsolete] + public enum MyEnum1 + { + MyEnum_Value1, + } + + [Obsolete(""This is obsolete."")] + public enum MyEnum2 + { + MyEnum_Value2, + } + + public enum MyEnum3 + { + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public static partial class Methods + {{ + public static {expectedManagedType} MyVariable0 = 0; + + [Obsolete] + public static {expectedManagedType} MyVariable1 = 0; + + [Obsolete(""This is obsolete."")] + public static {expectedManagedType} MyVariable2 = 0; + + public static {expectedManagedType} MyVariable3 = 0; + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + public static void MyFunction0() + { + } + + [Obsolete] + public static void MyFunction1() + { + } + + [Obsolete(""This is obsolete."")] + public static void MyFunction2() + { + } + + public static void MyFunction3() + { + } + } +} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public int MyFunction0() + {{ + return 0; + }} + + [Obsolete] + public int MyFunction1() + {{ + return 0; + }} + + [Obsolete(""This is obsolete."")] + public int MyFunction2() + {{ + return 0; + }} + + public int MyFunction3() + {{ + return 0; + }} + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate void Callback0(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [Obsolete] + public delegate void Callback1(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [Obsolete(""This is obsolete."")] + public delegate void Callback2(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate void Callback3(); + + public partial struct MyStruct0 + { + [NativeTypeName(""Callback0"")] + public IntPtr _callback; + } + + public partial struct MyStruct1 + { + [NativeTypeName(""Callback1"")] + [Obsolete] + public IntPtr _callback; + } + + public partial struct MyStruct2 + { + [NativeTypeName(""Callback2"")] + [Obsolete(""This is obsolete."")] + public IntPtr _callback; + } + + public partial struct MyStruct3 + { + [NativeTypeName(""Callback3"")] + public IntPtr _callback; + } +} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction0(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete] + public static extern void MyFunction1(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete(""This is obsolete."")] + public static extern void MyFunction2(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction3(); + } +} +"; + + return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..bce728df --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs @@ -0,0 +1,519 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class CSharpCompatibleWindows_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum + { + MyEnum_Value0, + [Obsolete] + MyEnum_Value1, + [Obsolete(""This is obsolete."")] + MyEnum_Value2, + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum0 + { + MyEnum_Value0, + } + + [Obsolete] + public enum MyEnum1 + { + MyEnum_Value1, + } + + [Obsolete(""This is obsolete."")] + public enum MyEnum2 + { + MyEnum_Value2, + } + + public enum MyEnum3 + { + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public static partial class Methods + {{ + public static {expectedManagedType} MyVariable0 = 0; + + [Obsolete] + public static {expectedManagedType} MyVariable1 = 0; + + [Obsolete(""This is obsolete."")] + public static {expectedManagedType} MyVariable2 = 0; + + public static {expectedManagedType} MyVariable3 = 0; + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + public static void MyFunction0() + { + } + + [Obsolete] + public static void MyFunction1() + { + } + + [Obsolete(""This is obsolete."")] + public static void MyFunction2() + { + } + + public static void MyFunction3() + { + } + } +} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public int MyFunction0() + {{ + return 0; + }} + + [Obsolete] + public int MyFunction1() + {{ + return 0; + }} + + [Obsolete(""This is obsolete."")] + public int MyFunction2() + {{ + return 0; + }} + + public int MyFunction3() + {{ + return 0; + }} + }} +}} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate void Callback0(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [Obsolete] + public delegate void Callback1(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + [Obsolete(""This is obsolete."")] + public delegate void Callback2(); + + [UnmanagedFunctionPointer(CallingConvention.Cdecl)] + public delegate void Callback3(); + + public partial struct MyStruct0 + { + [NativeTypeName(""Callback0"")] + public IntPtr _callback; + } + + public partial struct MyStruct1 + { + [NativeTypeName(""Callback1"")] + [Obsolete] + public IntPtr _callback; + } + + public partial struct MyStruct2 + { + [NativeTypeName(""Callback2"")] + [Obsolete(""This is obsolete."")] + public IntPtr _callback; + } + + public partial struct MyStruct3 + { + [NativeTypeName(""Callback3"")] + public IntPtr _callback; + } +} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction0(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete] + public static extern void MyFunction1(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete(""This is obsolete."")] + public static extern void MyFunction2(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction3(); + } +} +"; + + return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..1c79e4bd --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/DeprecatedToObsoleteTest.cs @@ -0,0 +1,504 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class CSharpLatestUnix_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum + { + MyEnum_Value0, + [Obsolete] + MyEnum_Value1, + [Obsolete(""This is obsolete."")] + MyEnum_Value2, + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum0 + { + MyEnum_Value0, + } + + [Obsolete] + public enum MyEnum1 + { + MyEnum_Value1, + } + + [Obsolete(""This is obsolete."")] + public enum MyEnum2 + { + MyEnum_Value2, + } + + public enum MyEnum3 + { + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public static partial class Methods + {{ + public static {expectedManagedType} MyVariable0 = 0; + + [Obsolete] + public static {expectedManagedType} MyVariable1 = 0; + + [Obsolete(""This is obsolete."")] + public static {expectedManagedType} MyVariable2 = 0; + + public static {expectedManagedType} MyVariable3 = 0; + }} +}} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + public static void MyFunction0() + { + } + + [Obsolete] + public static void MyFunction1() + { + } + + [Obsolete(""This is obsolete."")] + public static void MyFunction2() + { + } + + public static void MyFunction3() + { + } + } +} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public int MyFunction0() + {{ + return 0; + }} + + [Obsolete] + public int MyFunction1() + {{ + return 0; + }} + + [Obsolete(""This is obsolete."")] + public int MyFunction2() + {{ + return 0; + }} + + public int MyFunction3() + {{ + return 0; + }} + }} +}} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public unsafe partial struct MyStruct0 + { + [NativeTypeName(""Callback0"")] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct1 + { + [NativeTypeName(""Callback1"")] + [Obsolete] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct2 + { + [NativeTypeName(""Callback2"")] + [Obsolete(""This is obsolete."")] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct3 + { + [NativeTypeName(""Callback3"")] + public delegate* unmanaged[Cdecl] _callback; + } +} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction0(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete] + public static extern void MyFunction1(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete(""This is obsolete."")] + public static extern void MyFunction2(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction3(); + } +} +"; + + return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..c6c16574 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/DeprecatedToObsoleteTest.cs @@ -0,0 +1,504 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class CSharpLatestWindows_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum + { + MyEnum_Value0, + [Obsolete] + MyEnum_Value1, + [Obsolete(""This is obsolete."")] + MyEnum_Value2, + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum0 + { + MyEnum_Value0, + } + + [Obsolete] + public enum MyEnum1 + { + MyEnum_Value1, + } + + [Obsolete(""This is obsolete."")] + public enum MyEnum2 + { + MyEnum_Value2, + } + + public enum MyEnum3 + { + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public static partial class Methods + {{ + public static {expectedManagedType} MyVariable0 = 0; + + [Obsolete] + public static {expectedManagedType} MyVariable1 = 0; + + [Obsolete(""This is obsolete."")] + public static {expectedManagedType} MyVariable2 = 0; + + public static {expectedManagedType} MyVariable3 = 0; + }} +}} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + public static void MyFunction0() + { + } + + [Obsolete] + public static void MyFunction1() + { + } + + [Obsolete(""This is obsolete."")] + public static void MyFunction2() + { + } + + public static void MyFunction3() + { + } + } +} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public int MyFunction0() + {{ + return 0; + }} + + [Obsolete] + public int MyFunction1() + {{ + return 0; + }} + + [Obsolete(""This is obsolete."")] + public int MyFunction2() + {{ + return 0; + }} + + public int MyFunction3() + {{ + return 0; + }} + }} +}} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public unsafe partial struct MyStruct0 + { + [NativeTypeName(""Callback0"")] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct1 + { + [NativeTypeName(""Callback1"")] + [Obsolete] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct2 + { + [NativeTypeName(""Callback2"")] + [Obsolete(""This is obsolete."")] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct3 + { + [NativeTypeName(""Callback3"")] + public delegate* unmanaged[Cdecl] _callback; + } +} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction0(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete] + public static extern void MyFunction1(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete(""This is obsolete."")] + public static extern void MyFunction2(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction3(); + } +} +"; + + return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..c7219b8b --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/DeprecatedToObsoleteTest.cs @@ -0,0 +1,504 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class CSharpPreviewUnix_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum + { + MyEnum_Value0, + [Obsolete] + MyEnum_Value1, + [Obsolete(""This is obsolete."")] + MyEnum_Value2, + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum0 + { + MyEnum_Value0, + } + + [Obsolete] + public enum MyEnum1 + { + MyEnum_Value1, + } + + [Obsolete(""This is obsolete."")] + public enum MyEnum2 + { + MyEnum_Value2, + } + + public enum MyEnum3 + { + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public static partial class Methods + {{ + public static {expectedManagedType} MyVariable0 = 0; + + [Obsolete] + public static {expectedManagedType} MyVariable1 = 0; + + [Obsolete(""This is obsolete."")] + public static {expectedManagedType} MyVariable2 = 0; + + public static {expectedManagedType} MyVariable3 = 0; + }} +}} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + public static void MyFunction0() + { + } + + [Obsolete] + public static void MyFunction1() + { + } + + [Obsolete(""This is obsolete."")] + public static void MyFunction2() + { + } + + public static void MyFunction3() + { + } + } +} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public int MyFunction0() + {{ + return 0; + }} + + [Obsolete] + public int MyFunction1() + {{ + return 0; + }} + + [Obsolete(""This is obsolete."")] + public int MyFunction2() + {{ + return 0; + }} + + public int MyFunction3() + {{ + return 0; + }} + }} +}} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public unsafe partial struct MyStruct0 + { + [NativeTypeName(""Callback0"")] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct1 + { + [NativeTypeName(""Callback1"")] + [Obsolete] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct2 + { + [NativeTypeName(""Callback2"")] + [Obsolete(""This is obsolete."")] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct3 + { + [NativeTypeName(""Callback3"")] + public delegate* unmanaged[Cdecl] _callback; + } +} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction0(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete] + public static extern void MyFunction1(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete(""This is obsolete."")] + public static extern void MyFunction2(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction3(); + } +} +"; + + return ValidateGeneratedCSharpPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..e29bc04e --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/DeprecatedToObsoleteTest.cs @@ -0,0 +1,504 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class CSharpPreviewWindows_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public {expectedManagedType} r; + + [Obsolete] + public {expectedManagedType} g; + + [Obsolete(""This is obsolete."")] + public {expectedManagedType} b; + + public {expectedManagedType} a; + }} +}} +"; + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct0 + {{ + public int r; + }} + + [Obsolete] + public partial struct MyStruct1 + {{ + public int r; + }} + + [Obsolete(""This is obsolete."")] + public partial struct MyStruct2 + {{ + public int r; + }} + + public partial struct MyStruct3 + {{ + public int r; + }} +}} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum + { + MyEnum_Value0, + [Obsolete] + MyEnum_Value1, + [Obsolete(""This is obsolete."")] + MyEnum_Value2, + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public enum MyEnum0 + { + MyEnum_Value0, + } + + [Obsolete] + public enum MyEnum1 + { + MyEnum_Value1, + } + + [Obsolete(""This is obsolete."")] + public enum MyEnum2 + { + MyEnum_Value2, + } + + public enum MyEnum3 + { + MyEnum_Value3, + } +} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public static partial class Methods + {{ + public static {expectedManagedType} MyVariable0 = 0; + + [Obsolete] + public static {expectedManagedType} MyVariable1 = 0; + + [Obsolete(""This is obsolete."")] + public static {expectedManagedType} MyVariable2 = 0; + + public static {expectedManagedType} MyVariable3 = 0; + }} +}} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + public static void MyFunction0() + { + } + + [Obsolete] + public static void MyFunction1() + { + } + + [Obsolete(""This is obsolete."")] + public static void MyFunction2() + { + } + + public static void MyFunction3() + { + } + } +} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@"using System; + +namespace ClangSharp.Test +{{ + public partial struct MyStruct + {{ + public int MyFunction0() + {{ + return 0; + }} + + [Obsolete] + public int MyFunction1() + {{ + return 0; + }} + + [Obsolete(""This is obsolete."")] + public int MyFunction2() + {{ + return 0; + }} + + public int MyFunction3() + {{ + return 0; + }} + }} +}} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @"using System; + +namespace ClangSharp.Test +{ + public unsafe partial struct MyStruct0 + { + [NativeTypeName(""Callback0"")] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct1 + { + [NativeTypeName(""Callback1"")] + [Obsolete] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct2 + { + [NativeTypeName(""Callback2"")] + [Obsolete(""This is obsolete."")] + public delegate* unmanaged[Cdecl] _callback; + } + + public unsafe partial struct MyStruct3 + { + [NativeTypeName(""Callback3"")] + public delegate* unmanaged[Cdecl] _callback; + } +} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @"using System; +using System.Runtime.InteropServices; + +namespace ClangSharp.Test +{ + public static partial class Methods + { + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction0(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete] + public static extern void MyFunction1(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + [Obsolete(""This is obsolete."")] + public static extern void MyFunction2(); + + [DllImport(""ClangSharpPInvokeGenerator"", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)] + public static extern void MyFunction3(); + } +} +"; + + return ValidateGeneratedCSharpPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..148de6d8 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/DeprecatedToObsoleteTest.cs @@ -0,0 +1,549 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class XmlCompatibleUnix_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + + Obsolete + int + + int + + + + Obsolete(""This is obsolete."") + int + + int + + + + int + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@" + + + + + int + + 0 + + + + Obsolete + int + + 0 + + + + Obsolete(""This is obsolete."") + int + + 0 + + + + int + + 0 + + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @" + + + + + void + + + + Obsolete + void + + + + Obsolete(""This is obsolete."") + void + + + + void + + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@" + + + + + int + return 0; + + + Obsolete + int + return 0; + + + Obsolete(""This is obsolete."") + int + return 0; + + + int + return 0; + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @" + + + + void + + + Obsolete + void + + + Obsolete(""This is obsolete."") + void + + + void + + + + IntPtr + + + + + Obsolete + IntPtr + + + + + Obsolete(""This is obsolete."") + IntPtr + + + + + IntPtr + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @" + + + + + void + + + Obsolete + void + + + Obsolete(""This is obsolete."") + void + + + void + + + + +"; + + return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..3b547a53 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/DeprecatedToObsoleteTest.cs @@ -0,0 +1,549 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class XmlCompatibleWindows_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + + Obsolete + int + + int + + + + Obsolete(""This is obsolete."") + int + + int + + + + int + + int + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@" + + + + + int + + 0 + + + + Obsolete + int + + 0 + + + + Obsolete(""This is obsolete."") + int + + 0 + + + + int + + 0 + + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @" + + + + + void + + + + Obsolete + void + + + + Obsolete(""This is obsolete."") + void + + + + void + + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@" + + + + + int + return 0; + + + Obsolete + int + return 0; + + + Obsolete(""This is obsolete."") + int + return 0; + + + int + return 0; + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @" + + + + void + + + Obsolete + void + + + Obsolete(""This is obsolete."") + void + + + void + + + + IntPtr + + + + + Obsolete + IntPtr + + + + + Obsolete(""This is obsolete."") + IntPtr + + + + + IntPtr + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @" + + + + + void + + + Obsolete + void + + + Obsolete(""This is obsolete."") + void + + + void + + + + +"; + + return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..54dca338 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/DeprecatedToObsoleteTest.cs @@ -0,0 +1,535 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class XmlLatestUnix_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + + Obsolete + int + + int + + + + Obsolete(""This is obsolete."") + int + + int + + + + int + + int + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@" + + + + + int + + 0 + + + + Obsolete + int + + 0 + + + + Obsolete(""This is obsolete."") + int + + 0 + + + + int + + 0 + + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @" + + + + + void + + + + Obsolete + void + + + + Obsolete(""This is obsolete."") + void + + + + void + + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@" + + + + + int + return 0; + + + Obsolete + int + return 0; + + + Obsolete(""This is obsolete."") + int + return 0; + + + int + return 0; + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @" + + + + + delegate* unmanaged[Cdecl]<void> + + + + + Obsolete + delegate* unmanaged[Cdecl]<void> + + + + + Obsolete(""This is obsolete."") + delegate* unmanaged[Cdecl]<void> + + + + + delegate* unmanaged[Cdecl]<void> + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @" + + + + + void + + + Obsolete + void + + + Obsolete(""This is obsolete."") + void + + + void + + + + +"; + + return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..1daae4dc --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/DeprecatedToObsoleteTest.cs @@ -0,0 +1,535 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class XmlLatestWindows_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + + Obsolete + int + + int + + + + Obsolete(""This is obsolete."") + int + + int + + + + int + + int + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@" + + + + + int + + 0 + + + + Obsolete + int + + 0 + + + + Obsolete(""This is obsolete."") + int + + 0 + + + + int + + 0 + + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @" + + + + + void + + + + Obsolete + void + + + + Obsolete(""This is obsolete."") + void + + + + void + + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@" + + + + + int + return 0; + + + Obsolete + int + return 0; + + + Obsolete(""This is obsolete."") + int + return 0; + + + int + return 0; + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @" + + + + + delegate* unmanaged[Cdecl]<void> + + + + + Obsolete + delegate* unmanaged[Cdecl]<void> + + + + + Obsolete(""This is obsolete."") + delegate* unmanaged[Cdecl]<void> + + + + + delegate* unmanaged[Cdecl]<void> + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @" + + + + + void + + + Obsolete + void + + + Obsolete(""This is obsolete."") + void + + + void + + + + +"; + + return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..7df42fb6 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/DeprecatedToObsoleteTest.cs @@ -0,0 +1,535 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class XmlPreviewUnix_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + + Obsolete + int + + int + + + + Obsolete(""This is obsolete."") + int + + int + + + + int + + int + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@" + + + + + int + + 0 + + + + Obsolete + int + + 0 + + + + Obsolete(""This is obsolete."") + int + + 0 + + + + int + + 0 + + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @" + + + + + void + + + + Obsolete + void + + + + Obsolete(""This is obsolete."") + void + + + + void + + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@" + + + + + int + return 0; + + + Obsolete + int + return 0; + + + Obsolete(""This is obsolete."") + int + return 0; + + + int + return 0; + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @" + + + + + delegate* unmanaged[Cdecl]<void> + + + + + Obsolete + delegate* unmanaged[Cdecl]<void> + + + + + Obsolete(""This is obsolete."") + delegate* unmanaged[Cdecl]<void> + + + + + delegate* unmanaged[Cdecl]<void> + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @" + + + + + void + + + Obsolete + void + + + Obsolete(""This is obsolete."") + void + + + void + + + + +"; + + return ValidateGeneratedXmlPreviewUnixBindingsAsync(inputContents, expectedOutputContents); + } +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..b16803a4 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/DeprecatedToObsoleteTest.cs @@ -0,0 +1,535 @@ +// Copyright (c) .NET Foundation and Contributors. All Rights Reserved. Licensed under the MIT License (MIT). See License.md in the repository root for more information. + +using System.Threading.Tasks; + +namespace ClangSharp.UnitTests; + +public sealed class XmlPreviewWindows_DeprecatedToObsoleteTest : DeprecatedToObsoleteTest +{ + protected override Task SimpleStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"struct MyStruct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task StructDeclImpl() + { + var inputContents = $@"struct MyStruct0 +{{ + int r; +}}; + +struct [[deprecated]] MyStruct1 +{{ + int r; +}}; + +struct [[deprecated(""This is obsolete."")]] MyStruct2 +{{ + int r; +}}; + +struct MyStruct3 +{{ + int r; +}}; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleTypedefStructMembersImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@"typedef struct +{{ + {nativeType} r; + + [[deprecated]] + {nativeType} g; + + [[deprecated(""This is obsolete."")]] + {nativeType} b; + + {nativeType} a; +}} MyStruct; +"; + + var expectedOutputContents = $@" + + + + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task TypedefStructDeclImpl() + { + var inputContents = $@"typedef struct +{{ + int r; +}} MyStruct0; + +[[deprecated]] typedef struct +{{ + int r; +}} MyStruct1; + +[[deprecated(""This is obsolete."")]] typedef struct +{{ + int r; +}} MyStruct2; + +typedef struct +{{ + int r; +}} MyStruct3; +"; + + var expectedOutputContents = $@" + + + + + int + + + + Obsolete + + int + + + + Obsolete(""This is obsolete."") + + int + + + + + int + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleEnumMembersImpl() + { + var inputContents = $@"enum MyEnum : int +{{ + MyEnum_Value0, + MyEnum_Value1 [[deprecated]], + MyEnum_Value2 [[deprecated(""This is obsolete."")]], + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + Obsolete + int + + + Obsolete(""This is obsolete."") + int + + + int + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task EnumDeclImpl() + { + var inputContents = $@"enum MyEnum0 : int +{{ + MyEnum_Value0, +}}; + +enum [[deprecated]] MyEnum1 : int +{{ + MyEnum_Value1, +}}; + +enum [[deprecated(""This is obsolete."")]] MyEnum2 : int +{{ + MyEnum_Value2, +}}; + + +enum MyEnum3 : int +{{ + MyEnum_Value3, +}}; +"; + + var expectedOutputContents = @" + + + + int + + int + + + + Obsolete + int + + int + + + + Obsolete(""This is obsolete."") + int + + int + + + + int + + int + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task SimpleVarDeclImpl(string nativeType, string expectedManagedType) + { + var inputContents = $@" +{nativeType} MyVariable0 = 0; + +[[deprecated]] +{nativeType} MyVariable1 = 0; + +[[deprecated(""This is obsolete."")]] +{nativeType} MyVariable2 = 0; + +{nativeType} MyVariable3 = 0;"; + + var expectedOutputContents = $@" + + + + + int + + 0 + + + + Obsolete + int + + 0 + + + + Obsolete(""This is obsolete."") + int + + 0 + + + + int + + 0 + + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDeclImpl() + { + var inputContents = @" +void MyFunction0() +{ +} + +[[deprecated]] +void MyFunction1() +{ +} + +[[deprecated(""This is obsolete."")]] +void MyFunction2() +{ +} + +void MyFunction3() +{ +} +"; + + var expectedOutputContents = @" + + + + + void + + + + Obsolete + void + + + + Obsolete(""This is obsolete."") + void + + + + void + + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task InstanceFuncImpl() + { + var inputContents = @"struct MyStruct +{ + int MyFunction0() { return 0; } + + [[deprecated]] + int MyFunction1() { return 0; } + + [[deprecated(""This is obsolete."")]] + int MyFunction2() { return 0; } + + int MyFunction3() { return 0; } +};"; + + var expectedOutputContents = $@" + + + + + int + return 0; + + + Obsolete + int + return 0; + + + Obsolete(""This is obsolete."") + int + return 0; + + + int + return 0; + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncPtrDeclImpl() + { + var inputContents = @" +typedef void (*Callback0)(); +[[deprecated]] typedef void (*Callback1)(); +[[deprecated(""This is obsolete."")]] typedef void (*Callback2)(); +typedef void (*Callback3)(); + +struct MyStruct0 { + Callback0 _callback; +}; +struct MyStruct1 { + Callback1 _callback; +}; +struct MyStruct2 { + Callback2 _callback; +}; +struct MyStruct3 { + Callback3 _callback; +}; +"; + + var expectedOutputContents = @" + + + + + delegate* unmanaged[Cdecl]<void> + + + + + Obsolete + delegate* unmanaged[Cdecl]<void> + + + + + Obsolete(""This is obsolete."") + delegate* unmanaged[Cdecl]<void> + + + + + delegate* unmanaged[Cdecl]<void> + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } + + protected override Task FuncDllImportImpl() + { + var inputContents = @" +extern ""C"" void MyFunction0(); +extern ""C"" [[deprecated]] void MyFunction1(); +extern ""C"" [[deprecated(""This is obsolete."")]] void MyFunction2(); +extern ""C"" void MyFunction3();"; + + var expectedOutputContents = @" + + + + + void + + + Obsolete + void + + + Obsolete(""This is obsolete."") + void + + + void + + + + +"; + + return ValidateGeneratedXmlPreviewWindowsBindingsAsync(inputContents, expectedOutputContents); + } +}