diff --git a/src/rgen/Microsoft.Macios.Bindings.Analyzer/DiagnosticInfo.cs b/src/rgen/Microsoft.Macios.Bindings.Analyzer/DiagnosticInfo.cs index 95e9f8c3ab6..90e208c6f83 100644 --- a/src/rgen/Microsoft.Macios.Bindings.Analyzer/DiagnosticInfo.cs +++ b/src/rgen/Microsoft.Macios.Bindings.Analyzer/DiagnosticInfo.cs @@ -10,8 +10,8 @@ public struct DiagnosticInfo ( string category) { public string Id { get; } = id; - public LocalizableResourceString Title { get; } = title; - public LocalizableResourceString MessageFormat { get; } = messageFormat; + public LocalizableResourceString Title { get; } = title; + public LocalizableResourceString MessageFormat { get; } = messageFormat; public LocalizableResourceString Description { get; } = description; public string Category { get; } = category; } diff --git a/src/rgen/Microsoft.Macios.Bindings.Analyzer/Extensions/IBindingTypeAnalyzerExtensions.cs b/src/rgen/Microsoft.Macios.Bindings.Analyzer/Extensions/IBindingTypeAnalyzerExtensions.cs index a08eb31c692..e0d6a0e57ba 100644 --- a/src/rgen/Microsoft.Macios.Bindings.Analyzer/Extensions/IBindingTypeAnalyzerExtensions.cs +++ b/src/rgen/Microsoft.Macios.Bindings.Analyzer/Extensions/IBindingTypeAnalyzerExtensions.cs @@ -31,7 +31,7 @@ public static void AnalyzeBindingType (this IBindingTypeAnalyzer self, Syn switch (attributeType) { case AttributesNames.BindingAttribute: // validate that the class is partial, else we need to report an error - var diagnostics= self.Analyze (context.Compilation.GetCurrentPlatform (), + var diagnostics = self.Analyze (context.Compilation.GetCurrentPlatform (), declarationNode, declaredSymbol); foreach (var diagnostic in diagnostics) context.ReportDiagnostic (diagnostic); diff --git a/src/rgen/Microsoft.Macios.Bindings.Analyzer/IBindingTypeAnalyzer.cs b/src/rgen/Microsoft.Macios.Bindings.Analyzer/IBindingTypeAnalyzer.cs index 42e7759bf5f..71855d16413 100644 --- a/src/rgen/Microsoft.Macios.Bindings.Analyzer/IBindingTypeAnalyzer.cs +++ b/src/rgen/Microsoft.Macios.Bindings.Analyzer/IBindingTypeAnalyzer.cs @@ -7,6 +7,6 @@ namespace Microsoft.Macios.Bindings.Analyzer; /// /// Interface to be implemented by those analyzer that will be looking at BindingTypes. /// -public interface IBindingTypeAnalyzer where T : BaseTypeDeclarationSyntax { +public interface IBindingTypeAnalyzer where T : BaseTypeDeclarationSyntax { ImmutableArray Analyze (PlatformName platformName, T declarationNode, INamedTypeSymbol symbol); } diff --git a/src/rgen/Microsoft.Macios.Generator/Attributes/FieldData.cs b/src/rgen/Microsoft.Macios.Generator/Attributes/FieldData.cs index 933cc0f082c..35abd82623d 100644 --- a/src/rgen/Microsoft.Macios.Generator/Attributes/FieldData.cs +++ b/src/rgen/Microsoft.Macios.Generator/Attributes/FieldData.cs @@ -21,10 +21,10 @@ public static bool TryParse (SyntaxNode attributeSyntax, AttributeData attribute var count = attributeData.ConstructorArguments.Length; switch (count) { case 1: - data = new((string) attributeData.ConstructorArguments [0].Value!); + data = new ((string) attributeData.ConstructorArguments [0].Value!); break; case 2: - data = new((string) attributeData.ConstructorArguments [0].Value!, + data = new ((string) attributeData.ConstructorArguments [0].Value!, (string) attributeData.ConstructorArguments [1].Value!); break; default: diff --git a/src/rgen/Microsoft.Macios.Generator/BindingSourceGeneratorGenerator.cs b/src/rgen/Microsoft.Macios.Generator/BindingSourceGeneratorGenerator.cs index 834202e71a3..231d3625212 100644 --- a/src/rgen/Microsoft.Macios.Generator/BindingSourceGeneratorGenerator.cs +++ b/src/rgen/Microsoft.Macios.Generator/BindingSourceGeneratorGenerator.cs @@ -156,11 +156,11 @@ static void GenerateCode (SourceProductionContext context, Compilation compil CollectUsingStatements (baseTypeDeclarationSyntax.SyntaxTree, sb, emitter); - if (emitter.TryEmit(out var diagnostics)) { - // only add file when we do generate code - var code = sb.ToString (); - context.AddSource ($"{symbolBindingContext.Namespace}/{emitter.SymbolName}.g.cs", - SourceText.From (code, Encoding.UTF8)); + if (emitter.TryEmit (out var diagnostics)) { + // only add file when we do generate code + var code = sb.ToString (); + context.AddSource ($"{symbolBindingContext.Namespace}/{emitter.SymbolName}.g.cs", + SourceText.From (code, Encoding.UTF8)); } else { // add to the diagnostics and continue to the next possible candidate foreach (Diagnostic diagnostic in diagnostics) { diff --git a/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs b/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs index 91a91047d78..efcb3b59ae2 100644 --- a/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs +++ b/src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs @@ -19,7 +19,7 @@ void Emit (TabbedStringBuilder classBlock, (IFieldSymbol Symbol, FieldData Field { var typeNamespace = enumField.Symbol.ContainingType.ContainingNamespace.Name; if (!context.RootBindingContext.TryComputeLibraryName (enumField.FieldData.LibraryName, typeNamespace, - out string? libraryName, out string? libraryPath)) { + out string? libraryName, out string? libraryPath)) { return; } @@ -111,34 +111,34 @@ void Emit (TabbedStringBuilder classBlock, INamedTypeSymbol enumSymbol, }}"); } - public bool TryEmit ([NotNullWhen (false)] out ImmutableArray? diagnostics) - { - diagnostics = null; - if (!context.Symbol.TryGetEnumFields (out var members, - out diagnostics) || members.Value.Length == 0) { - diagnostics = new ImmutableArray (); - return false; - } - // in the old generator we had to copy over the enum, in this new approach the only code - // we need to create is the extension class for the enum that is backed by fields - builder.AppendLine (); - builder.AppendLine ($"namespace {context.Namespace};"); - builder.AppendLine (); - - builder.AppendGeneratedCodeAttribute (); - using (var classBlock = builder.CreateBlock ($"static public partial class {SymbolName}", true)) { - classBlock.AppendLine (); - classBlock.AppendLine ($"static IntPtr[] values = new IntPtr [{members.Value.Length}];"); - // foreach member in the enum we need to create a field that holds the value, the property emitter - // will take care of generating the property. Do not order by name to keep the order of the enum - Emit (classBlock, members.Value); - classBlock.AppendLine (); - - // emit the extension methods that will be used to get the values from the enum - Emit (classBlock, context.Symbol, members); - classBlock.AppendLine (); - } - - return true; - } + public bool TryEmit ([NotNullWhen (false)] out ImmutableArray? diagnostics) + { + diagnostics = null; + if (!context.Symbol.TryGetEnumFields (out var members, + out diagnostics) || members.Value.Length == 0) { + diagnostics = new ImmutableArray (); + return false; + } + // in the old generator we had to copy over the enum, in this new approach the only code + // we need to create is the extension class for the enum that is backed by fields + builder.AppendLine (); + builder.AppendLine ($"namespace {context.Namespace};"); + builder.AppendLine (); + + builder.AppendGeneratedCodeAttribute (); + using (var classBlock = builder.CreateBlock ($"static public partial class {SymbolName}", true)) { + classBlock.AppendLine (); + classBlock.AppendLine ($"static IntPtr[] values = new IntPtr [{members.Value.Length}];"); + // foreach member in the enum we need to create a field that holds the value, the property emitter + // will take care of generating the property. Do not order by name to keep the order of the enum + Emit (classBlock, members.Value); + classBlock.AppendLine (); + + // emit the extension methods that will be used to get the values from the enum + Emit (classBlock, context.Symbol, members); + classBlock.AppendLine (); + } + + return true; + } } diff --git a/src/rgen/Microsoft.Macios.Generator/Extensions/TypeSymbolExtensions.cs b/src/rgen/Microsoft.Macios.Generator/Extensions/TypeSymbolExtensions.cs index 24c048e4d4d..f4a2c65a370 100644 --- a/src/rgen/Microsoft.Macios.Generator/Extensions/TypeSymbolExtensions.cs +++ b/src/rgen/Microsoft.Macios.Generator/Extensions/TypeSymbolExtensions.cs @@ -30,7 +30,7 @@ public static Dictionary GetAttributeData (this ISymbol s var boundAttributes = symbol.GetAttributes (); if (boundAttributes.Length == 0) { // return an empty dictionary if there are no attributes - return new(); + return new (); } var attributes = new Dictionary (); diff --git a/tests/rgen/Microsoft.Macios.Bindings.Analyzer.Tests/SmartEnumSemanticAnalyzerTests.cs b/tests/rgen/Microsoft.Macios.Bindings.Analyzer.Tests/SmartEnumSemanticAnalyzerTests.cs index d234d8e4c12..d380ba5dec0 100644 --- a/tests/rgen/Microsoft.Macios.Bindings.Analyzer.Tests/SmartEnumSemanticAnalyzerTests.cs +++ b/tests/rgen/Microsoft.Macios.Bindings.Analyzer.Tests/SmartEnumSemanticAnalyzerTests.cs @@ -158,7 +158,7 @@ public enum AVCaptureSystemPressureExampleLevel { public async Task SmartEnumAppleFrameworkNotLibrary (ApplePlatform platform, string inputText) { var compilation = CreateCompilation (nameof (SmartEnumSemanticAnalyzerTests), platform, inputText); - var diagnostics = await RunAnalyzer (new SmartEnumSemanticAnalyzer(), compilation); + var diagnostics = await RunAnalyzer (new SmartEnumSemanticAnalyzer (), compilation); var analyzerDiagnotics = diagnostics .Where (d => d.Id == SmartEnumSemanticAnalyzer.RBI0005.Id).ToArray (); @@ -245,7 +245,7 @@ public enum CustomLibraryEnum { public async Task SmartEnumThirdPartyLibrary (ApplePlatform platform, string inputText) { var compilation = CreateCompilation (nameof (SmartEnumSemanticAnalyzerTests), platform, inputText); - var diagnostics = await RunAnalyzer (new SmartEnumSemanticAnalyzer(), compilation); + var diagnostics = await RunAnalyzer (new SmartEnumSemanticAnalyzer (), compilation); var analyzerDiagnotics = diagnostics .Where (d => d.Id == SmartEnumSemanticAnalyzer.RBI0004.Id).ToArray (); Assert.Single (analyzerDiagnotics); diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/BaseGeneratorTestClass.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/BaseGeneratorTestClass.cs index a183d4206fc..f5200c6c591 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/BaseGeneratorTestClass.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/BaseGeneratorTestClass.cs @@ -20,7 +20,7 @@ public class BaseGeneratorTestClass { // list of the defines for each platform, this is passed to the parser to ensure that // we are testing the platforms as if they were being compiled. - readonly Dictionary platformDefines = new() { + readonly Dictionary platformDefines = new () { { ApplePlatform.iOS, new [] { "__IOS__" } }, { ApplePlatform.TVOS, new [] { "__TVOS__" } }, { ApplePlatform.MacOSX, new [] { "__MACOS__" } }, @@ -56,7 +56,7 @@ protected Compilation CreateCompilation (string name, ApplePlatform platform, pa throw new InvalidOperationException ($"Could not find platform dll for {platform}"); } - var parseOptions = new CSharpParseOptions (LanguageVersion.Latest, DocumentationMode.None, preprocessorSymbols: platformDefines[platform]);; + var parseOptions = new CSharpParseOptions (LanguageVersion.Latest, DocumentationMode.None, preprocessorSymbols: platformDefines [platform]); ; var trees = sources.Select (s => CSharpSyntaxTree.ParseText (s, parseOptions)); var options = new CSharpCompilationOptions (OutputKind.NetModule) diff --git a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/SmartEnumTests.cs b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/SmartEnumTests.cs index 25320b39e49..00984563e09 100644 --- a/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/SmartEnumTests.cs +++ b/tests/rgen/Microsoft.Macios.Generator.Tests/SmartEnum/SmartEnumTests.cs @@ -9,11 +9,9 @@ namespace Microsoft.Macios.Generator.Tests.SmartEnum; /// /// Test all the field generation code. /// -public class SmartEnumTests : BaseGeneratorTestClass -{ - public class TestDataGenerator : BaseTestDataGenerator, IEnumerable - { - readonly List<(ApplePlatform Platform, string ClassName, string BindingFile, string OutputFile)> _data = new() +public class SmartEnumTests : BaseGeneratorTestClass { + public class TestDataGenerator : BaseTestDataGenerator, IEnumerable { + readonly List<(ApplePlatform Platform, string ClassName, string BindingFile, string OutputFile)> _data = new () { (ApplePlatform.iOS, "AVCaptureDeviceTypeExtensions", "AVCaptureDeviceTypeEnum.cs", "ExpectedAVCaptureDeviceTypeEnum.cs" ), (ApplePlatform.iOS, "AVCaptureSystemPressureLevelExtensions", "AVCaptureSystemPressureLevel.cs", "ExpectedAVCaptureSystemPressureLevel.cs" ), @@ -22,25 +20,24 @@ public class TestDataGenerator : BaseTestDataGenerator, IEnumerable (ApplePlatform.MacOSX, "CustomLibraryEnumExtensions", "CustomLibraryEnum.cs", "ExpectedCustomLibraryEnum.cs" ), }; - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator () { - foreach (var testData in _data) - { + foreach (var testData in _data) { yield return [ testData.Platform, testData.ClassName, testData.BindingFile, - ReadFileAsString(testData.BindingFile), + ReadFileAsString (testData.BindingFile), testData.OutputFile, - ReadFileAsString(testData.OutputFile)]; + ReadFileAsString (testData.OutputFile)]; } } - IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); } [Theory] - [ClassData(typeof(TestDataGenerator))] + [ClassData (typeof (TestDataGenerator))] public void ExtensionGenerationTests (ApplePlatform platform, string className, string inputFileName, string inputText, string outputFileName, string expectedOutputText) - => CompareGeneratedCode(platform, className, inputFileName, inputText, outputFileName, expectedOutputText); + => CompareGeneratedCode (platform, className, inputFileName, inputText, outputFileName, expectedOutputText); }