Skip to content

Commit

Permalink
[generator] Copy [Obsolete] attributes on smart enums. Fixes #46292 (#…
Browse files Browse the repository at this point in the history
…1104)

Covers attributes on the type itself and on its members.

Reference:
* https://bugzilla.xamarin.com/show_bug.cgi?id=46292
  • Loading branch information
spouliot authored Nov 3, 2016
1 parent aedf9cd commit ee046ee
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/generator-enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -70,6 +76,7 @@ void GenerateEnum (Type type)
else
print ("[Native (\"{0}\")]", native.NativeName);
}
CopyObsolete (type);

var unique_constants = new HashSet<string> ();
var fields = new Dictionary<FieldInfo, FieldAttribute> ();
Expand All @@ -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<FieldAttribute> (f);
if (fa == null)
Expand Down
11 changes: 11 additions & 0 deletions tests/generator/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/generator/bug46292.cs
Original file line number Diff line number Diff line change
@@ -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,
}
}

0 comments on commit ee046ee

Please sign in to comment.