diff --git a/src/bgen/AttributeConversionManager.cs b/src/bgen/AttributeConversionManager.cs index 1e2cf6ec5a6c..c56824ff88fc 100644 --- a/src/bgen/AttributeConversionManager.cs +++ b/src/bgen/AttributeConversionManager.cs @@ -5,7 +5,7 @@ #nullable enable public static class AttributeConversionManager { - public static Attribute ConvertPlatformAttribute (CustomAttributeData attribute, PlatformName platform) + public static Attribute ConvertPlatformAttribute (AttributeManager attributeManager, CustomAttributeData attribute, PlatformName platform) { var constructorArguments = new object [attribute.ConstructorArguments.Count]; for (int i = 0; i < attribute.ConstructorArguments.Count; ++i) @@ -23,7 +23,7 @@ public static Attribute ConvertPlatformAttribute (CustomAttributeData attribute, if (AttributeFactory.ConstructorArguments.TryGetCtorArguments ( constructorArguments, platform, out var ctorValues, out var ctorTypes)) { - return AttributeFactory.CreateNewAttribute (ctorTypes!, ctorValues!); + return AttributeFactory.CreateNewAttribute (attributeManager, ctorTypes!, ctorValues!); } throw new NotImplementedException (unknownFormatError ()); @@ -99,7 +99,7 @@ static ParsedAvailabilityInfo DetermineOldAvailabilityVersion (CustomAttributeNa throw new NotImplementedException ($"Unknown version format \"{enumName}\" in DetermineOldAvailabilityVersion"); } - public static IEnumerable ConvertAvailability (CustomAttributeData attribute) + public static IEnumerable ConvertAvailability (AttributeManager attributeManager, CustomAttributeData attribute) { string? message = null; if (attribute.NamedArguments is null) @@ -112,22 +112,22 @@ public static IEnumerable ConvertAvailability (CustomAttributeData at switch (arg.MemberName) { case "Introduced": { ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg); - yield return AttributeFactory.CreateNewAttribute (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); + yield return AttributeFactory.CreateNewAttribute (attributeManager, availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); continue; } case "Deprecated": { ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg); - yield return AttributeFactory.CreateNewAttribute (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); + yield return AttributeFactory.CreateNewAttribute (attributeManager, availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); continue; } case "Obsoleted": { ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg); - yield return AttributeFactory.CreateNewAttribute (availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); + yield return AttributeFactory.CreateNewAttribute (attributeManager, availInfo.Platform, availInfo.Major, availInfo.Minor, message: message); continue; } case "Unavailable": { ParsedAvailabilityInfo availInfo = DetermineOldAvailabilityVersion (arg); - yield return AttributeFactory.CreateNewAttribute (availInfo.Platform, message: message); + yield return AttributeFactory.CreateNewAttribute (attributeManager, availInfo.Platform, message: message); continue; } case "Message": diff --git a/src/bgen/AttributeFactory.ConstructorArguments.cs b/src/bgen/AttributeFactory.ConstructorArguments.cs index 3d149fe181b2..a207632b6d05 100644 --- a/src/bgen/AttributeFactory.ConstructorArguments.cs +++ b/src/bgen/AttributeFactory.ConstructorArguments.cs @@ -5,6 +5,10 @@ public static partial class AttributeFactory { public static readonly Type PlatformEnum = typeof (PlatformName); + static readonly Type [] ctorTypes_platform_message = new [] { PlatformEnum, typeof (string) }; + static readonly Type [] ctorTypes_platform_major_minor_message = new [] { PlatformEnum, typeof (int), typeof (int), typeof (string) }; + static readonly Type [] ctorTypes_platform_major_minor_build_message = new [] { PlatformEnum, typeof (int), typeof (int), typeof (int), typeof (string) }; + public readonly struct ConstructorArguments { readonly PlatformName platform; readonly int? major; @@ -52,13 +56,12 @@ public ConstructorArguments (PlatformName platformIn, string? messageIn) public Type [] GetCtorTypes () { - if (major is null || minor is null) { - return new [] { PlatformEnum, typeof (string) }; - } + if (major is null || minor is null) + return ctorTypes_platform_message; if (build is null) - return new [] { PlatformEnum, typeof (int), typeof (int), typeof (string) }; - return new [] { PlatformEnum, typeof (int), typeof (int), typeof (int), typeof (string) }; + return ctorTypes_platform_major_minor_message; + return ctorTypes_platform_major_minor_build_message; } public static bool TryGetCtorArguments (object [] constructorArguments, PlatformName platform, [NotNullWhen (true)] out object? []? ctorValues, [NotNullWhen (true)] out Type []? ctorTypes) @@ -71,7 +74,7 @@ public static bool TryGetCtorArguments (object [] constructorArguments, Platform if (constructorArguments [0] is byte && constructorArguments [1] is byte) { ctorValues = new object? [] { (byte) platform, (int) (byte) constructorArguments [0], (int) (byte) constructorArguments [1], null }; - ctorTypes = new [] { PlatformEnum, typeof (int), typeof (int), typeof (string) }; + ctorTypes = ctorTypes_platform_major_minor_message; return true; } @@ -81,7 +84,7 @@ public static bool TryGetCtorArguments (object [] constructorArguments, Platform constructorArguments [1] is byte && constructorArguments [2] is byte) { ctorValues = new object? [] { (byte) platform, (int) (byte) constructorArguments [0], (int) (byte) constructorArguments [1], (int) (byte) constructorArguments [2], null }; - ctorTypes = new [] { PlatformEnum, typeof (int), typeof (int), typeof (int), typeof (string) }; + ctorTypes = ctorTypes_platform_major_minor_build_message; return true; } return false; diff --git a/src/bgen/AttributeFactory.cs b/src/bgen/AttributeFactory.cs index fe9dd2551e59..5ddf43bbc47f 100644 --- a/src/bgen/AttributeFactory.cs +++ b/src/bgen/AttributeFactory.cs @@ -1,49 +1,71 @@ using System.Collections.Generic; using System.Linq; +using System.Reflection; #nullable enable public static partial class AttributeFactory { - public static T CreateNewAttribute (Type [] ctorTypes, object? [] ctorValues) + public static T CreateNewAttribute (AttributeManager attributeManager, Type [] ctorTypes, object? [] ctorValues) where T : Attribute { + var constructorCache = attributeManager.constructorCache; var attribType = typeof (T); - var ctor = attribType.GetConstructor (ctorTypes); - if (ctor is null) - throw ErrorHelper.CreateError (1058, attribType.FullName); + if (!constructorCache.TryGetValue ((attribType, ctorTypes), out var ctor)) { + ctor = attribType.GetConstructor (ctorTypes); + if (ctor is null) + throw ErrorHelper.CreateError (1058, attribType.FullName); + constructorCache [(attribType, ctorTypes)] = ctor; + } return (T) ctor.Invoke (ctorValues); } - public static T CreateNewAttribute (PlatformName platform, int major, int minor, string? message = null) + public static T CreateNewAttribute (AttributeManager attributeManager, PlatformName platform, int major, int minor, string? message = null) where T : Attribute { var args = new ConstructorArguments (platform, major, minor, message); - return CreateNewAttribute (args.GetCtorTypes (), args.GetCtorValues ()); + return CreateNewAttribute (attributeManager, args.GetCtorTypes (), args.GetCtorValues ()); } - public static T CreateNewAttribute (PlatformName platform, int major, int minor, int build, string? message = null) + public static T CreateNewAttribute (AttributeManager attributeManager, PlatformName platform, int major, int minor, int build, string? message = null) where T : Attribute { var args = new ConstructorArguments (platform, major, minor, build, message); - return CreateNewAttribute (args.GetCtorTypes (), args.GetCtorValues ()); + return CreateNewAttribute (attributeManager, args.GetCtorTypes (), args.GetCtorValues ()); } - public static T CreateNewAttribute (PlatformName platform, string? message = null) where T : Attribute + public static T CreateNewAttribute (AttributeManager attributeManager, PlatformName platform, string? message = null) where T : Attribute { var args = new ConstructorArguments (platform, message); - return CreateNewAttribute (args.GetCtorTypes (), args.GetCtorValues ()); + return CreateNewAttribute (attributeManager, args.GetCtorTypes (), args.GetCtorValues ()); } + static readonly IntroducedAttribute [] noVersionSupportedCache = new IntroducedAttribute [] { + new (PlatformName.iOS), + new (PlatformName.TvOS), + new (PlatformName.MacOSX), + new (PlatformName.MacCatalyst), + }; + + static readonly UnavailableAttribute [] unsupportedCache = new UnavailableAttribute [] { + new (PlatformName.iOS), + new (PlatformName.MacCatalyst), + new (PlatformName.MacOSX), + new (PlatformName.TvOS), + }; + public static IntroducedAttribute CreateNoVersionSupportedAttribute (PlatformName platform) { switch (platform) { case PlatformName.iOS: + return noVersionSupportedCache [0]; case PlatformName.TvOS: + return noVersionSupportedCache [1]; case PlatformName.MacOSX: + return noVersionSupportedCache [2]; case PlatformName.MacCatalyst: - return new (platform); + return noVersionSupportedCache [3]; case PlatformName.WatchOS: throw new InvalidOperationException ("CreateNoVersionSupportedAttribute for WatchOS never makes sense"); default: @@ -55,10 +77,13 @@ public static UnavailableAttribute CreateUnsupportedAttribute (PlatformName plat { switch (platform) { case PlatformName.iOS: + return unsupportedCache [0]; case PlatformName.MacCatalyst: + return unsupportedCache [1]; case PlatformName.MacOSX: + return unsupportedCache [2]; case PlatformName.TvOS: - return new (platform); + return unsupportedCache [3]; case PlatformName.WatchOS: throw new InvalidOperationException ("CreateUnsupportedAttribute for WatchOS never makes sense"); default: @@ -68,6 +93,16 @@ public static UnavailableAttribute CreateUnsupportedAttribute (PlatformName plat public static AvailabilityBaseAttribute CloneFromOtherPlatform (AvailabilityBaseAttribute attr, PlatformName platform) { + if (attr.Version is null && string.IsNullOrEmpty (attr.Message)) { + // For no-version, no-message attributes, return the cached singletons + switch (attr.AvailabilityKind) { + case AvailabilityKind.Introduced: + return CreateNoVersionSupportedAttribute (platform); + case AvailabilityKind.Unavailable: + return CreateUnsupportedAttribute (platform); + } + } + if (attr.Version is null) { switch (attr.AvailabilityKind) { case AvailabilityKind.Introduced: @@ -88,8 +123,26 @@ public static AvailabilityBaseAttribute CloneFromOtherPlatform (AvailabilityBase // So determine if the build is -1, and use the 2 or 3 param ctor... var version = attr.Version; var minimum = Xamarin.SdkVersions.GetMinVersion (platform.AsApplePlatform ()); - if (version < minimum) - version = minimum; + if (version <= minimum) { + // If the version is at or below the platform minimum, it's redundant, + // so create a no-version attribute instead of clamping. + switch (attr.AvailabilityKind) { + case AvailabilityKind.Introduced: + return string.IsNullOrEmpty (attr.Message) + ? CreateNoVersionSupportedAttribute (platform) + : new IntroducedAttribute (platform, message: attr.Message); + case AvailabilityKind.Deprecated: + return new DeprecatedAttribute (platform, minimum.Major, minimum.Minor, message: attr.Message); + case AvailabilityKind.Obsoleted: + return new ObsoletedAttribute (platform, minimum.Major, minimum.Minor, message: attr.Message); + case AvailabilityKind.Unavailable: + return string.IsNullOrEmpty (attr.Message) + ? CreateUnsupportedAttribute (platform) + : new UnavailableAttribute (platform, message: attr.Message); + default: + throw new NotImplementedException (); + } + } if (version.Build == -1) { switch (attr.AvailabilityKind) { diff --git a/src/bgen/AttributeManager.cs b/src/bgen/AttributeManager.cs index 4e3b3acbb927..b0a94906eb6a 100644 --- a/src/bgen/AttributeManager.cs +++ b/src/bgen/AttributeManager.cs @@ -21,6 +21,16 @@ public class AttributeManager { // Cache raw GetCustomAttributesData() results per provider to avoid repeated reflection allocations. readonly Dictionary> rawAttributeCache = new (); + // Cache attribute query results per (provider, attribute type) to avoid + // repeated reflection and attribute conversion work. + readonly Dictionary<(ICustomAttributeProvider, System.Type), object> attributeCache = new (); + + // Cache HasAttribute results to avoid repeated GetCustomAttributesData() calls. + readonly Dictionary<(ICustomAttributeProvider, System.Type), bool> hasAttributeCache = new (); + + // Cache ConstructorInfo lookups per (attribute type, constructor arg types) to avoid repeated reflection. + internal readonly Dictionary<(Type, Type []), ConstructorInfo> constructorCache = new (); + TypeCache TypeCache { get; } public AttributeManager (TypeCache typeCache) @@ -275,7 +285,7 @@ Type ConvertTypeToMeta (System.Type type) return rv; } - static IEnumerable ConvertOldAttributes (CustomAttributeData attribute) + IEnumerable ConvertOldAttributes (CustomAttributeData attribute) { switch (attribute.GetAttributeType ().Namespace) { case null: // Root namespace such as PlatformAvailabilityShadow.cs @@ -290,33 +300,33 @@ Type ConvertTypeToMeta (System.Type type) switch (attribute.GetAttributeType ().Name) { case "SinceAttribute": case "iOSAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.iOS).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.iOS).Yield (); case "MacAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.MacOSX).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.MacOSX).Yield (); case "WatchAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.WatchOS).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.WatchOS).Yield (); case "TVAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.TvOS).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.TvOS).Yield (); case "MacCatalystAttribute": - return AttributeConversionManager.ConvertPlatformAttribute (attribute, PlatformName.MacCatalyst).Yield (); + return AttributeConversionManager.ConvertPlatformAttribute (this, attribute, PlatformName.MacCatalyst).Yield (); case "LionAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacOSX, 10, 7).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacOSX, 10, 7).Yield (); case "MountainLionAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacOSX, 10, 8).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacOSX, 10, 8).Yield (); case "MavericksAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacOSX, 10, 9).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacOSX, 10, 9).Yield (); case "NoMacAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacOSX).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacOSX).Yield (); case "NoiOSAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.iOS).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.iOS).Yield (); case "NoWatchAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.WatchOS).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.WatchOS).Yield (); case "NoTVAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.TvOS).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.TvOS).Yield (); case "NoMacCatalystAttribute": - return AttributeFactory.CreateNewAttribute (PlatformName.MacCatalyst).Yield (); + return AttributeFactory.CreateNewAttribute (this, PlatformName.MacCatalyst).Yield (); case "AvailabilityAttribute": - return AttributeConversionManager.ConvertAvailability (attribute); + return AttributeConversionManager.ConvertAvailability (this, attribute); case "ExperimentalAttribute": var earg = attribute.ConstructorArguments [0].Value as string ?? ""; return new System.Diagnostics.CodeAnalysis.ExperimentalAttribute (earg).Yield (); @@ -324,15 +334,15 @@ Type ConvertTypeToMeta (System.Type type) var sarg = attribute.ConstructorArguments [0].Value as string ?? ""; (var sp, var sv) = ParseOSPlatformAttribute (sarg); if (sv is null) - return AttributeFactory.CreateNewAttribute (sp).Yield (); + return AttributeFactory.CreateNewAttribute (this, sp).Yield (); else - return AttributeFactory.CreateNewAttribute (sp, sv.Major, sv.Minor).Yield (); + return AttributeFactory.CreateNewAttribute (this, sp, sv.Major, sv.Minor).Yield (); case "UnsupportedOSPlatformAttribute": var uarg = attribute.ConstructorArguments [0].Value as string ?? ""; (var up, var uv) = ParseOSPlatformAttribute (uarg); // might have been available for a while... if (uv is null) - return AttributeFactory.CreateNewAttribute (up).Yield (); + return AttributeFactory.CreateNewAttribute (this, up).Yield (); else return Enumerable.Empty (); case "ObsoletedOSPlatformAttribute": @@ -340,9 +350,9 @@ Type ConvertTypeToMeta (System.Type type) (var op, var ov) = ParseOSPlatformAttribute (oarg); // might have been available for a while... if (ov is null) - return AttributeFactory.CreateNewAttribute (op).Yield (); + return AttributeFactory.CreateNewAttribute (this, op).Yield (); else - return AttributeFactory.CreateNewAttribute (op, ov.Major, ov.Minor).Yield (); + return AttributeFactory.CreateNewAttribute (this, op, ov.Major, ov.Minor).Yield (); default: return Enumerable.Empty (); } @@ -500,7 +510,16 @@ T [] FilterAttributes (IList? attributes, ICustomAttribu public virtual T [] GetCustomAttributes (ICustomAttributeProvider? provider) where T : System.Attribute { - return FilterAttributes (GetAttributes (provider), provider); + if (provider is null) + return Array.Empty (); + + var key = (provider, typeof (T)); + if (attributeCache.TryGetValue (key, out var cached)) + return (T []) cached; + + var result = FilterAttributes (GetAttributes (provider), provider); + attributeCache [key] = result; + return result; } [return: NotNullIfNotNull (nameof (provider))] @@ -538,21 +557,32 @@ public virtual bool HasAttribute (ICustomAttributeProvider? provider) where T if (provider is null) return false; + var key = (provider, typeof (T)); + if (hasAttributeCache.TryGetValue (key, out var cached)) + return cached; + var attributeType = ConvertTypeToMeta (typeof (T)); var attribs = GetAttributes (provider); - if (attribs is null || attribs.Count == 0) + if (attribs is null || attribs.Count == 0) { + hasAttributeCache [key] = false; return false; + } for (int i = 0; i < attribs.Count; i++) { var attrib = attribs [i]; // == when comparing types uses reference equality, which is what we want here. var currentType = ConvertTypeToMeta (attrib.GetAttributeType ()); - if (currentType == attributeType) + if (currentType == attributeType) { + hasAttributeCache [key] = true; return true; - if (currentType.IsSubclassOf (attributeType)) + } + if (currentType.IsSubclassOf (attributeType)) { + hasAttributeCache [key] = true; return true; + } } + hasAttributeCache [key] = false; return false; } diff --git a/src/bgen/Attributes.cs b/src/bgen/Attributes.cs index 8e47763419eb..06dd32bb3e9c 100644 --- a/src/bgen/Attributes.cs +++ b/src/bgen/Attributes.cs @@ -955,19 +955,12 @@ void GenerateDeprecated (StringBuilder builder) void GenerateSupported (StringBuilder builder) { -#if BGENERATOR - // If the version is less than or equal to the min version for the platform in question, - // the version is redundant, so just skip it. - if (Version is not null && Version <= Xamarin.SdkVersions.GetMinVersion (Platform.AsApplePlatform ())) - Version = null; -#endif - builder.Append ("[SupportedOSPlatform (\""); - GeneratePlatformNameAndVersion (builder); + GeneratePlatformNameAndVersion (builder, skipMinVersion: true); builder.AppendLine ("\")]"); } - void GeneratePlatformNameAndVersion (StringBuilder builder) + void GeneratePlatformNameAndVersion (StringBuilder builder, bool skipMinVersion = false) { switch (Platform) { case PlatformName.iOS: @@ -989,8 +982,17 @@ void GeneratePlatformNameAndVersion (StringBuilder builder) throw new NotSupportedException ($"Unknown platform: {Platform}"); } - if (Version is not null) - builder.Append (Version.ToString (Version.Build >= 0 ? 3 : 2)); + if (Version is null) + return; + +#if BGENERATOR + // If the version is less than or equal to the min version for the platform in question, + // the version is redundant, so just skip it. + if (skipMinVersion && Version <= Xamarin.SdkVersions.GetMinVersion (Platform.AsApplePlatform ())) + return; +#endif + + builder.Append (Version.ToString (Version.Build >= 0 ? 3 : 2)); } /// Returns a human readable version of the availability attribute. diff --git a/src/bgen/Generator.cs b/src/bgen/Generator.cs index f1221a33dafb..b7450adcf612 100644 --- a/src/bgen/Generator.cs +++ b/src/bgen/Generator.cs @@ -1263,12 +1263,21 @@ public ExportAttribute GetOneExportAttribute (MemberInfo mo) return rv; } + readonly Dictionary getterExportCache = new (); + readonly Dictionary setterExportCache = new (); + public ExportAttribute? GetSetterExportAttribute (PropertyInfo pinfo) { + if (setterExportCache.TryGetValue (pinfo, out var cached)) + return cached; var ea = AttributeManager.GetCustomAttribute (pinfo.GetSetMethod ()); - if (ea is not null && ea.Selector is not null) + if (ea is not null && ea.Selector is not null) { + setterExportCache [pinfo] = ea; return ea; - return AttributeManager.GetCustomAttribute (pinfo)?.ToSetter (pinfo); + } + var result = AttributeManager.GetCustomAttribute (pinfo)?.ToSetter (pinfo); + setterExportCache [pinfo] = result; + return result; } public ExportAttribute GetOneSetterExportAttribute (PropertyInfo pinfo) @@ -1281,10 +1290,16 @@ public ExportAttribute GetOneSetterExportAttribute (PropertyInfo pinfo) public ExportAttribute? GetGetterExportAttribute (PropertyInfo pinfo) { + if (getterExportCache.TryGetValue (pinfo, out var cached)) + return cached; var ea = AttributeManager.GetCustomAttribute (pinfo.GetGetMethod ()); - if (ea is not null && ea.Selector is not null) + if (ea is not null && ea.Selector is not null) { + getterExportCache [pinfo] = ea; return ea; - return AttributeManager.GetCustomAttribute (pinfo)?.ToGetter (pinfo); + } + var result = AttributeManager.GetCustomAttribute (pinfo)?.ToGetter (pinfo); + getterExportCache [pinfo] = result; + return result; } public ExportAttribute GetOneGetterExportAttribute (PropertyInfo pinfo) @@ -2548,17 +2563,24 @@ static MemberInfo FindContainingContext (MemberInfo? mi) // The Foo property and the Klass both could have unique or duplicate attributes // We collect them all, starting with the inner most first in the list. // Later on CopyValidAttributes will handle only copying the first valid one down + readonly Dictionary> parentAttributeCache = new (); List GetAllParentAttributes (MemberInfo context) { + if (parentAttributeCache.TryGetValue (context, out var cached)) + return cached; + var parentAvailability = new List (); + var current = context; while (true) { - parentAvailability.AddRange (AttributeManager.GetCustomAttributes (context)); - var parentContext = FindContainingContext (context); - if (context == parentContext) { - return parentAvailability; + parentAvailability.AddRange (AttributeManager.GetCustomAttributes (current)); + var parentContext = FindContainingContext (current); + if (current == parentContext) { + break; } - context = parentContext; + current = parentContext; } + parentAttributeCache [context] = parentAvailability; + return parentAvailability; } public bool PrintPlatformAttributes (MemberInfo? mi, Type? inlinedType = null) diff --git a/tests/bgen/AttributeFactoryTests.cs b/tests/bgen/AttributeFactoryTests.cs index ebc3b2a4ff06..f263347237e9 100644 --- a/tests/bgen/AttributeFactoryTests.cs +++ b/tests/bgen/AttributeFactoryTests.cs @@ -8,50 +8,6 @@ namespace GeneratorTests { [TestFixture] [Parallelizable (ParallelScope.All)] public class AttributeFactoryTests { - static void AssertAttributeCreation (Func callback, PlatformName platform, - int major, int minor, string? message = null) where T : AvailabilityBaseAttribute - { - var typeName = typeof (T).Name; - var attr = callback (platform, major, minor, message) as T; - Assert.That (attr, Is.Not.Null, $"{typeName} attribute type"); - Assert.That (attr.Platform, Is.EqualTo (platform), $"{typeName} Platform"); - Assert.That (attr.Version!.Major, Is.EqualTo (major), $"{typeName} Major"); - Assert.That (attr.Version!.Minor, Is.EqualTo (minor), $"{typeName} Minor"); - Assert.That (attr.Message, Is.EqualTo (message)); - } - - static void AssertAttributeCreationNotVersion (Func callback, PlatformName platform, - string? message = null) where T : AvailabilityBaseAttribute - { - var typeName = typeof (T).Name; - var attr = callback (platform, message) as T; - Assert.That (attr, Is.Not.Null, $"{typeName} attribute type"); - Assert.That (attr.Platform, Is.EqualTo (platform), $"{typeName} Platform"); - Assert.That (attr.Message, Is.EqualTo (message)); - } - - - // simple tests, but we want to test it - [TestCase (PlatformName.iOS, 13, 4, "message")] - [TestCase (PlatformName.iOS, 12, 4, null)] - public void CreateAttributeTest (PlatformName platform, int major, int minor, string? message) - { - // call several times with diff types - AssertAttributeCreation (AttributeFactory.CreateNewAttribute, platform, major, minor, message); - AssertAttributeCreation (AttributeFactory.CreateNewAttribute, platform, major, minor, message); - AssertAttributeCreation (AttributeFactory.CreateNewAttribute, platform, major, minor, message); - } - - [TestCase (PlatformName.iOS, "message")] - [TestCase (PlatformName.iOS, null)] - public void CreateAttributeNoVersionTest (PlatformName platform, string? message) - { - // call several times with diff types - AssertAttributeCreationNotVersion (AttributeFactory.CreateNewAttribute, platform, message); - AssertAttributeCreationNotVersion (AttributeFactory.CreateNewAttribute, platform, message); - AssertAttributeCreationNotVersion (AttributeFactory.CreateNewAttribute, platform, message); - AssertAttributeCreationNotVersion (AttributeFactory.CreateNewAttribute, platform, message); - } [TestCase (PlatformName.iOS)] [TestCase (PlatformName.MacCatalyst)]