Skip to content

Commit ee046ee

Browse files
authored
[generator] Copy [Obsolete] attributes on smart enums. Fixes #46292 (#1104)
Covers attributes on the type itself and on its members. Reference: * https://bugzilla.xamarin.com/show_bug.cgi?id=46292
1 parent aedf9cd commit ee046ee

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/generator-enums.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ static string GetCSharpTypeName (Type type)
5555
}
5656
}
5757

58+
void CopyObsolete (ICustomAttributeProvider provider)
59+
{
60+
foreach (ObsoleteAttribute oa in provider.GetCustomAttributes (typeof (ObsoleteAttribute), false))
61+
print ("[Obsolete (\"{0}\", {1})]", oa.Message, oa.IsError ? "true" : "false");
62+
}
63+
5864
// caller already:
5965
// - setup the header and namespace
6066
// - call/emit PrintPlatformAttributes on the type
@@ -70,6 +76,7 @@ void GenerateEnum (Type type)
7076
else
7177
print ("[Native (\"{0}\")]", native.NativeName);
7278
}
79+
CopyObsolete (type);
7380

7481
var unique_constants = new HashSet<string> ();
7582
var fields = new Dictionary<FieldInfo, FieldAttribute> ();
@@ -83,6 +90,7 @@ void GenerateEnum (Type type)
8390
if (f.IsSpecialName)
8491
continue;
8592
PrintPlatformAttributes (f);
93+
CopyObsolete (f);
8694
print ("{0} = {1},", f.Name, f.GetRawConstantValue ());
8795
var fa = GetAttribute<FieldAttribute> (f);
8896
if (fa == null)

tests/generator/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ bug35176:
121121
echo "Error: Expected 4 Introduced attributes in generated code."; exit 1; \
122122
fi
123123

124+
bug46292:
125+
@rm -Rf $@.tmpdir
126+
@mkdir -p $@.tmpdir
127+
$(if $(V),,@echo "$@";) $(IOS_GENERATOR) --sourceonly:$@.source -tmpdir=$@.tmpdir $@.cs --process-enums
128+
@if ! grep -r Obsolete bug46292.tmpdir/BindingTests > /dev/null; then \
129+
echo "error: Could not find Obsolete attribute in generated code."; exit 1; \
130+
fi
131+
@if [ `grep -r Obsolete bug46292.tmpdir/BindingTests | wc -l` -ne 2 ]; then \
132+
echo "Error: Expected 2 Obsolete attributes in generated code."; exit 1; \
133+
fi
134+
124135
forcedtype:
125136
@rm -Rf $@.tmpdir
126137
@mkdir -p $@.tmpdir

tests/generator/bug46292.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using MonoTouch.Foundation;
3+
4+
namespace BindingTests {
5+
[Obsolete ("Type level obsolete must also be copied")]
6+
public enum HMAccessoryCategoryType : int {
7+
[Field ("HMAccessoryCategoryTypeGarageDoorOpener")]
8+
GarageDoorOpener = 0,
9+
10+
[Obsolete ("Use GarageDoorOpener")]
11+
DoorOpener = GarageDoorOpener,
12+
}
13+
}

0 commit comments

Comments
 (0)