From 9035f8ceb9cf6eeca571bdca984b1e23cab727a1 Mon Sep 17 00:00:00 2001 From: TechPizza <23627133+TechPizzaDev@users.noreply.github.com> Date: Sat, 11 Feb 2023 20:17:29 +0100 Subject: [PATCH 1/7] Fix truncated message in generated Obsolete attributes --- sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index c72b5388..dfa5b9fc 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -5958,9 +5958,9 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = 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 From 1e27bb8d621057a7cfa8d733dba72888d4bf52b8 Mon Sep 17 00:00:00 2001 From: TechPizza <23627133+TechPizzaDev@users.noreply.github.com> Date: Fri, 24 Feb 2023 20:59:46 +0100 Subject: [PATCH 2/7] Fixed deprecated->Obsolete for anonymous typedef --- .../PInvokeGenerator.cs | 19 ++++++++++++++++--- sources/ClangSharp/Cursors/Decls/TagDecl.cs | 6 +++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index dfa5b9fc..c75ff8a1 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -5936,9 +5936,22 @@ 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 RecordDecl recordDecl) + { + var typedefName = recordDecl.TypedefNameForAnonDecl; + if (typedefName != null && typedefName.HasAttrs) + { + declAttrs = declAttrs.Concat(typedefName.Attrs); + } + } + + foreach (var attr in declAttrs) { switch (attr.Kind) { @@ -6241,7 +6254,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; } From 4e6d57d3821a82cd88e5819621bc779a79c230f9 Mon Sep 17 00:00:00 2001 From: TechPizza <23627133+TechPizzaDev@users.noreply.github.com> Date: Fri, 24 Feb 2023 21:20:25 +0100 Subject: [PATCH 3/7] Initial tests for deprecated->Obsolete --- .../Base/DeprecatedToObsoleteTest.cs | 44 +++ .../DeprecatedToObsoleteTest.cs | 311 ++++++++++++++++++ 2 files changed, 355 insertions(+) create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..b08f0994 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs @@ -0,0 +1,44 @@ +// 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); + + 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); +} diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs new file mode 100644 index 00000000..80b721e1 --- /dev/null +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs @@ -0,0 +1,311 @@ +// 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); + } +} From 65dc41ca08577470f14568892c91794248751d29 Mon Sep 17 00:00:00 2001 From: TechPizza <23627133+TechPizzaDev@users.noreply.github.com> Date: Fri, 24 Feb 2023 22:28:29 +0100 Subject: [PATCH 4/7] Added deprecated->Obsolete tests for functions --- .../Base/DeprecatedToObsoleteTest.cs | 20 ++ .../DeprecatedToObsoleteTest.cs | 206 ++++++++++++++++++ 2 files changed, 226 insertions(+) diff --git a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs index b08f0994..f062c262 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/Base/DeprecatedToObsoleteTest.cs @@ -28,6 +28,18 @@ public abstract class DeprecatedToObsoleteTest : PInvokeGeneratorTest [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(); @@ -41,4 +53,12 @@ public abstract class DeprecatedToObsoleteTest : PInvokeGeneratorTest 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/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs index 80b721e1..fb57b664 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs @@ -304,6 +304,212 @@ public static partial class Methods 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"")] + public IntPtr _callback; + } + + public partial struct MyStruct2 + { + [NativeTypeName(""Callback2"")] + 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); From fadc5eacb519e0753d9aea229c317e0195acf9ad Mon Sep 17 00:00:00 2001 From: TechPizza <23627133+TechnologicalPizza@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:08:55 +0100 Subject: [PATCH 5/7] Propagate attributes from field type --- .../PInvokeGenerator.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs index c75ff8a1..07c7af2e 100644 --- a/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs +++ b/sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs @@ -5942,7 +5942,14 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = ? namedDecl.Attrs : Enumerable.Empty(); - if (namedDecl is RecordDecl recordDecl) + 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) @@ -5951,6 +5958,8 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = } } + var obsoleteEmitted = false; + foreach (var attr in declAttrs) { switch (attr.Kind) @@ -5966,6 +5975,11 @@ 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('"'); @@ -5980,6 +5994,7 @@ private void WithAttributes(NamedDecl namedDecl, bool onlySupportedOSPlatform = { outputBuilder.WriteCustomAttribute($"Obsolete"); } + obsoleteEmitted = true; break; } From c2fe96e96d40aa3419903a5a1c520e16a96395b9 Mon Sep 17 00:00:00 2001 From: TechPizza <23627133+TechnologicalPizza@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:11:15 +0100 Subject: [PATCH 6/7] Added remaining C# tests for deprecated->obsolete --- .../DeprecatedToObsoleteTest.cs | 519 ++++++++++++++++++ .../DeprecatedToObsoleteTest.cs | 2 + .../DeprecatedToObsoleteTest.cs | 504 +++++++++++++++++ .../DeprecatedToObsoleteTest.cs | 504 +++++++++++++++++ .../DeprecatedToObsoleteTest.cs | 504 +++++++++++++++++ .../DeprecatedToObsoleteTest.cs | 504 +++++++++++++++++ 6 files changed, 2537 insertions(+) create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleUnix/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestUnix/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpLatestWindows/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewUnix/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpPreviewWindows/DeprecatedToObsoleteTest.cs 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 index fb57b664..bce728df 100644 --- a/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs +++ b/tests/ClangSharp.PInvokeGenerator.UnitTests/CSharpCompatibleWindows/DeprecatedToObsoleteTest.cs @@ -460,12 +460,14 @@ public partial struct MyStruct0 public partial struct MyStruct1 { [NativeTypeName(""Callback1"")] + [Obsolete] public IntPtr _callback; } public partial struct MyStruct2 { [NativeTypeName(""Callback2"")] + [Obsolete(""This is obsolete."")] public IntPtr _callback; } 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); + } +} From d7bf480564d4793b15ac4b2b38ed1af2745225e7 Mon Sep 17 00:00:00 2001 From: TechPizza <23627133+TechnologicalPizza@users.noreply.github.com> Date: Sat, 25 Feb 2023 18:40:52 +0100 Subject: [PATCH 7/7] Added XML tests for deprecated->Obsolete --- .../DeprecatedToObsoleteTest.cs | 549 ++++++++++++++++++ .../DeprecatedToObsoleteTest.cs | 549 ++++++++++++++++++ .../XmlLatestUnix/DeprecatedToObsoleteTest.cs | 535 +++++++++++++++++ .../DeprecatedToObsoleteTest.cs | 535 +++++++++++++++++ .../DeprecatedToObsoleteTest.cs | 535 +++++++++++++++++ .../DeprecatedToObsoleteTest.cs | 535 +++++++++++++++++ 6 files changed, 3238 insertions(+) create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleUnix/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/XmlCompatibleWindows/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestUnix/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/XmlLatestWindows/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewUnix/DeprecatedToObsoleteTest.cs create mode 100644 tests/ClangSharp.PInvokeGenerator.UnitTests/XmlPreviewWindows/DeprecatedToObsoleteTest.cs 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); + } +}