From 366fb42c14acd45aa05fe25341697235996be2e3 Mon Sep 17 00:00:00 2001 From: Manuel de la Pena Saenz Date: Sun, 26 Jan 2025 18:31:48 -0500 Subject: [PATCH] [Rgen] Fix a bug in how the target flags are managed. We cannot use an or a & we need to provide a list, that leater is tested against a flag that has been either | or &. --- .../XamarinBindingAPIGenerator.cs | 29 ++++++++++--------- .../AttributesNames.cs | 2 ++ 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/rgen/Microsoft.Macios.Transformer.Generator/Microsoft.Macios.Transformer.Generator/XamarinBindingAPIGenerator.cs b/src/rgen/Microsoft.Macios.Transformer.Generator/Microsoft.Macios.Transformer.Generator/XamarinBindingAPIGenerator.cs index df6dedc052a..91588e180ed 100644 --- a/src/rgen/Microsoft.Macios.Transformer.Generator/Microsoft.Macios.Transformer.Generator/XamarinBindingAPIGenerator.cs +++ b/src/rgen/Microsoft.Macios.Transformer.Generator/Microsoft.Macios.Transformer.Generator/XamarinBindingAPIGenerator.cs @@ -68,15 +68,16 @@ public void Initialize (IncrementalGeneratorInitializationContext context) } static string [] GetFlagsForTarget (Dictionary flags, - AttributeTargets targets) - => flags.Where (kv => kv.Value.Targets.HasFlag (targets)) + AttributeTargets[] targets) + => flags.Where (kv => targets.Any (t => kv.Value.Targets.HasFlag (t))) .Select (kv => kv.Key) .ToArray (); static (string AttributeFullName, string AttributeName, BindingAttributeData Data) [] GetAttributesForTarget ( Dictionary dataAttribute, - AttributeTargets targets) - => dataAttribute.Where (kv => kv.Value.Data.Target.HasFlag (targets)) + AttributeTargets [] targets) + // return all the attributes that have at least one of the targets + => dataAttribute.Where (kv => targets.Any (t => kv.Value.Data.Target.HasFlag (t))) .Select (kv => kv.Value) .ToArray (); @@ -165,9 +166,9 @@ static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, s } static void GenerateModelExtension (TabbedStringBuilder sb, string dataModel, - Dictionary flags, - Dictionary attributes, - AttributeTargets targets, + Dictionary flags, + Dictionary attributes, + AttributeTargets[] targets, SourceProductionContext context) { var methodFlags = GetFlagsForTarget (flags, targets); @@ -238,13 +239,13 @@ void GenerateCode (SourceProductionContext context, Compilation compilation, #pragma warning disable format // generate the extra methods for the data model, group the fields by the model type based on the target - var models = new (string Model, AttributeTargets Target) [] { - ("EnumMember", AttributeTargets.Field), - ("Parameter", AttributeTargets.Parameter), - ("Property", AttributeTargets.Property), - ("Method", AttributeTargets.Method), - ("Binding", AttributeTargets.Interface), - ("TypeInfo", AttributeTargets.Parameter) + var models = new (string Model, AttributeTargets[] Targets) [] { + ("EnumMember", [AttributeTargets.Field]), + ("Parameter", [AttributeTargets.Parameter]), + ("Property", [AttributeTargets.Property]), + ("Method", [AttributeTargets.Method]), + ("Binding", [AttributeTargets.Interface, AttributeTargets.Class, AttributeTargets.Enum, AttributeTargets.Struct]), + ("TypeInfo", [AttributeTargets.Parameter]) }; #pragma warning restore format diff --git a/src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs b/src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs index a3ca39d41f3..1d8a5186630 100644 --- a/src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs +++ b/src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs @@ -145,7 +145,9 @@ static class AttributesNames { /// [BindingFlag (AttributeTargets.Class | AttributeTargets.Interface)] public const string ModelAttribute = "Foundation.ModelAttribute"; + [BindingAttribute(typeof(NativeData), AttributeTargets.Enum)] public const string NativeAttribute = "ObjCRuntime.NativeAttribute"; + [BindingAttribute(typeof(NativeData), AttributeTargets.Enum | AttributeTargets.Struct)] public const string NativeNameAttribute = "ObjCRuntime.NativeNameAttribute"; ///