From 66598ec05a77a3abd959e75f1922eaec5e98fa91 Mon Sep 17 00:00:00 2001 From: Sebastien Pouliot Date: Wed, 2 Nov 2016 21:52:56 -0400 Subject: [PATCH] [generator] Copy [Obsolete] attributes on smart enums. Fixes #46292 Covers attributes on the type itself and on its members. Reference: * https://bugzilla.xamarin.com/show_bug.cgi?id=46292 --- src/generator-enums.cs | 8 ++++++++ tests/generator/Makefile | 11 +++++++++++ tests/generator/bug46292.cs | 13 +++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 tests/generator/bug46292.cs diff --git a/src/generator-enums.cs b/src/generator-enums.cs index 9436d5d9247..32582328145 100644 --- a/src/generator-enums.cs +++ b/src/generator-enums.cs @@ -55,6 +55,12 @@ static string GetCSharpTypeName (Type type) } } + void CopyObsolete (ICustomAttributeProvider provider) + { + foreach (ObsoleteAttribute oa in provider.GetCustomAttributes (typeof (ObsoleteAttribute), false)) + print ("[Obsolete (\"{0}\", {1})]", oa.Message, oa.IsError ? "true" : "false"); + } + // caller already: // - setup the header and namespace // - call/emit PrintPlatformAttributes on the type @@ -70,6 +76,7 @@ void GenerateEnum (Type type) else print ("[Native (\"{0}\")]", native.NativeName); } + CopyObsolete (type); var unique_constants = new HashSet (); var fields = new Dictionary (); @@ -83,6 +90,7 @@ void GenerateEnum (Type type) if (f.IsSpecialName) continue; PrintPlatformAttributes (f); + CopyObsolete (f); print ("{0} = {1},", f.Name, f.GetRawConstantValue ()); var fa = GetAttribute (f); if (fa == null) diff --git a/tests/generator/Makefile b/tests/generator/Makefile index d5f69ae1632..ca5df7cd2f4 100644 --- a/tests/generator/Makefile +++ b/tests/generator/Makefile @@ -121,6 +121,17 @@ bug35176: echo "Error: Expected 4 Introduced attributes in generated code."; exit 1; \ fi +bug46292: + @rm -Rf $@.tmpdir + @mkdir -p $@.tmpdir + $(if $(V),,@echo "$@";) $(IOS_GENERATOR) --sourceonly:$@.source -tmpdir=$@.tmpdir $@.cs --process-enums + @if ! grep -r Obsolete bug46292.tmpdir/BindingTests > /dev/null; then \ + echo "error: Could not find Obsolete attribute in generated code."; exit 1; \ + fi + @if [ `grep -r Obsolete bug46292.tmpdir/BindingTests | wc -l` -ne 2 ]; then \ + echo "Error: Expected 2 Obsolete attributes in generated code."; exit 1; \ + fi + forcedtype: @rm -Rf $@.tmpdir @mkdir -p $@.tmpdir diff --git a/tests/generator/bug46292.cs b/tests/generator/bug46292.cs new file mode 100644 index 00000000000..57b59a46f65 --- /dev/null +++ b/tests/generator/bug46292.cs @@ -0,0 +1,13 @@ +using System; +using MonoTouch.Foundation; + +namespace BindingTests { + [Obsolete ("Type level obsolete must also be copied")] + public enum HMAccessoryCategoryType : int { + [Field ("HMAccessoryCategoryTypeGarageDoorOpener")] + GarageDoorOpener = 0, + + [Obsolete ("Use GarageDoorOpener")] + DoorOpener = GarageDoorOpener, + } +} \ No newline at end of file