Skip to content

Commit ad31005

Browse files
authored
[bgen] Generate Obsolete + EditorBrowsable attributes in a few more cases. (#21135)
* Forward [Obsolete] from api definition interfaces. * Generate [EditorBrowsable] whenever an api definition member as either [EditorBrowsable] or [Obsolete].
1 parent 73c035a commit ad31005

File tree

6 files changed

+75
-159
lines changed

6 files changed

+75
-159
lines changed

src/avfoundation.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ enum AVMediaTypes {
221221
}
222222

223223
#if !NET
224-
[Obsolete ("Use AVMediaTypes enum values")]
224+
[Obsolete ("Use AVMediaTypes enum values.")]
225225
[NoWatch]
226226
[BaseType (typeof (NSObject))]
227227
[Static]
@@ -420,7 +420,7 @@ enum AVMediaCharacteristics {
420420

421421
#if !NET
422422
[NoWatch]
423-
[Obsolete ("Use AVMediaCharacteristics enum values")]
423+
[Obsolete ("Use AVMediaCharacteristics enum values.")]
424424
[BaseType (typeof (NSObject))]
425425
[Static]
426426
interface AVMediaCharacteristic {
@@ -598,7 +598,7 @@ enum AVFileTypes {
598598
[NoWatch]
599599
[BaseType (typeof (NSObject))]
600600
[Static]
601-
[Obsolete ("Use AVFileTypes enum values")]
601+
[Obsolete ("Use AVFileTypes enum values.")]
602602
interface AVFileType {
603603
[Field ("AVFileTypeQuickTimeMovie")]
604604
NSString QuickTimeMovie { get; }

src/bgen/Enums.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ void GenerateEnum (Type type)
8282
sb.Append ("]");
8383
print (sb.ToString ());
8484
}
85-
PrintObsoleteAttributes (type);
8685
CopyNativeName (type);
8786

8887
var unique_constants = new HashSet<string> ();

src/bgen/Generator.cs

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3866,18 +3866,47 @@ bool DoesPropertyNeedDirtyCheck (PropertyInfo pi, ExportAttribute ea)
38663866
}
38673867
}
38683868

3869-
void PrintObsoleteAttributes (ICustomAttributeProvider provider, bool already_has_editor_browsable_attribute = false)
3869+
void PrintObsoleteAttributes (ICustomAttributeProvider provider)
38703870
{
38713871
var obsoleteAttributes = AttributeManager.GetCustomAttributes<ObsoleteAttribute> (provider);
38723872

38733873
foreach (var oa in obsoleteAttributes) {
38743874
print ("[Obsolete (\"{0}\", {1})]", oa.Message, oa.IsError ? "true" : "false");
38753875
}
38763876

3877-
if (!already_has_editor_browsable_attribute && obsoleteAttributes.Any ())
3877+
var printEditorBrowsableAttribute = TryGetPrintEditorBrowsableAttribute (provider, out var editorBrowsableAttribute);
3878+
if (!printEditorBrowsableAttribute && obsoleteAttributes.Any ()) {
3879+
printEditorBrowsableAttribute = true;
3880+
editorBrowsableAttribute = "[EditorBrowsable (EditorBrowsableState.Never)]";
3881+
}
3882+
if (printEditorBrowsableAttribute)
38783883
print ("[EditorBrowsable (EditorBrowsableState.Never)]");
38793884
}
38803885

3886+
bool TryGetPrintEditorBrowsableAttribute (ICustomAttributeProvider provider, out string attribute)
3887+
{
3888+
attribute = string.Empty;
3889+
foreach (var ea in AttributeManager.GetCustomAttributes<EditorBrowsableAttribute> (provider)) {
3890+
switch (ea.State) {
3891+
case EditorBrowsableState.Always:
3892+
attribute = "[EditorBrowsable (EditorBrowsableState.Always)]";
3893+
break;
3894+
case EditorBrowsableState.Never:
3895+
attribute = "[EditorBrowsable (EditorBrowsableState.Never)]";
3896+
break;
3897+
case EditorBrowsableState.Advanced:
3898+
attribute = "[EditorBrowsable (EditorBrowsableState.Advanced)]";
3899+
break;
3900+
default:
3901+
attribute = $"[EditorBrowsable (EditorBrowsableState.{ea.State})]";
3902+
break;
3903+
}
3904+
return true;
3905+
}
3906+
3907+
return false;
3908+
}
3909+
38813910
void PrintPropertyAttributes (PropertyInfo pi, Type type, bool skipTypeInjection = false)
38823911
{
38833912
var minfo = new MemberInformation (this, this, pi, type);
@@ -4418,21 +4447,11 @@ void GenerateAsyncMethod (MemberInformation original_minfo, AsyncMethodKind asyn
44184447
void PrintMethodAttributes (MemberInformation minfo)
44194448
{
44204449
MethodInfo mi = minfo.Method;
4421-
var editor_browsable_attribute = false;
44224450

44234451
foreach (var sa in AttributeManager.GetCustomAttributes<ThreadSafeAttribute> (mi))
44244452
print (sa.Safe ? "[ThreadSafe]" : "[ThreadSafe (false)]");
44254453

4426-
foreach (var ea in AttributeManager.GetCustomAttributes<EditorBrowsableAttribute> (mi)) {
4427-
if (ea.State == EditorBrowsableState.Always) {
4428-
print ("[EditorBrowsable]");
4429-
} else {
4430-
print ("[EditorBrowsable (EditorBrowsableState.{0})]", ea.State);
4431-
}
4432-
editor_browsable_attribute = true;
4433-
}
4434-
4435-
PrintObsoleteAttributes (mi, editor_browsable_attribute);
4454+
PrintObsoleteAttributes (mi);
44364455

44374456
if (minfo.is_return_release)
44384457
print ("[return: ReleaseAttribute ()]");
@@ -5507,7 +5526,7 @@ public void PrintBindAsAttribute (ICustomAttributeProvider mi, StringBuilder sb
55075526
// Not adding the experimental attribute is bad (it would mean that an API
55085527
// we meant to be experimental ended up being released as stable), so it's
55095528
// opt-out instead of opt-in.
5510-
public void PrintAttributes (ICustomAttributeProvider mi, bool platform = false, bool preserve = false, bool advice = false, bool notImplemented = false, bool bindAs = false, bool requiresSuper = false, Type inlinedType = null, bool experimental = true)
5529+
public void PrintAttributes (ICustomAttributeProvider mi, bool platform = false, bool preserve = false, bool advice = false, bool notImplemented = false, bool bindAs = false, bool requiresSuper = false, Type inlinedType = null, bool experimental = true, bool obsolete = false)
55115530
{
55125531
if (platform)
55135532
PrintPlatformAttributes (mi as MemberInfo, inlinedType);
@@ -5523,6 +5542,8 @@ public void PrintAttributes (ICustomAttributeProvider mi, bool platform = false,
55235542
PrintRequiresSuperAttribute (mi);
55245543
if (experimental)
55255544
PrintExperimentalAttribute (mi);
5545+
if (obsolete)
5546+
PrintObsoleteAttributes (mi);
55265547
}
55275548

55285549
public void PrintExperimentalAttribute (ICustomAttributeProvider mi)
@@ -5754,7 +5775,7 @@ public void Generate (Type type)
57545775
print ("[Model]");
57555776
}
57565777

5757-
PrintAttributes (type, platform: true, preserve: true, advice: true);
5778+
PrintAttributes (type, platform: true, preserve: true, advice: true, obsolete: true);
57585779

57595780
if (type.IsEnum) {
57605781
GenerateEnum (type);

0 commit comments

Comments
 (0)