Skip to content

Commit

Permalink
Auto-format source code
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub Actions Autoformatter committed Oct 18, 2024
1 parent bb16529 commit 4a07007
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/rgen/Microsoft.Macios.Bindings.Analyzer/DiagnosticInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static void AnalyzeBindingType<T> (this IBindingTypeAnalyzer<T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ namespace Microsoft.Macios.Bindings.Analyzer;
/// <summary>
/// Interface to be implemented by those analyzer that will be looking at BindingTypes.
/// </summary>
public interface IBindingTypeAnalyzer<T> where T : BaseTypeDeclarationSyntax {
public interface IBindingTypeAnalyzer<T> where T : BaseTypeDeclarationSyntax {
ImmutableArray<Diagnostic> Analyze (PlatformName platformName, T declarationNode, INamedTypeSymbol symbol);
}
4 changes: 2 additions & 2 deletions src/rgen/Microsoft.Macios.Generator/Attributes/FieldData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ static void GenerateCode<T> (SourceProductionContext context, Compilation compil

CollectUsingStatements<T> (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) {
Expand Down
64 changes: 32 additions & 32 deletions src/rgen/Microsoft.Macios.Generator/Emitters/EnumEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -111,35 +111,35 @@ void Emit (TabbedStringBuilder classBlock, INamedTypeSymbol enumSymbol,
}}");
}

public bool TryEmit ([NotNullWhen (false)] out ImmutableArray<Diagnostic>? diagnostics)
{
diagnostics = null;
if (!context.Symbol.TryGetEnumFields (out var members,
out diagnostics) || members.Value.Length == 0) {
// return true to indicate that we did generate code, even if it's empty, the analyzer will take care
// of the rest
return true;
}
// 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<Diagnostic>? diagnostics)
{
diagnostics = null;
if (!context.Symbol.TryGetEnumFields (out var members,
out diagnostics) || members.Value.Length == 0) {
// return true to indicate that we did generate code, even if it's empty, the analyzer will take care
// of the rest
return true;
}
// 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static Dictionary<string, AttributeData> 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<string, AttributeData> ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -277,7 +277,7 @@ public enum CustomLibraryEnum {
}
";
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.RBI0002.Id).ToArray ();
// we should have a diagnostic for each enum value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApplePlatform, string[]> platformDefines = new() {
readonly Dictionary<ApplePlatform, string []> platformDefines = new () {
{ ApplePlatform.iOS, new [] { "__IOS__" } },
{ ApplePlatform.TVOS, new [] { "__TVOS__" } },
{ ApplePlatform.MacOSX, new [] { "__MACOS__" } },
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ namespace Microsoft.Macios.Generator.Tests.SmartEnum;
/// <summary>
/// Test all the field generation code.
/// </summary>
public class SmartEnumTests : BaseGeneratorTestClass
{
public class TestDataGenerator : BaseTestDataGenerator, IEnumerable<object[]>
{
readonly List<(ApplePlatform Platform, string ClassName, string BindingFile, string OutputFile)> _data = new()
public class SmartEnumTests : BaseGeneratorTestClass {
public class TestDataGenerator : BaseTestDataGenerator, IEnumerable<object []> {
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" ),
Expand All @@ -22,25 +20,24 @@ public class TestDataGenerator : BaseTestDataGenerator, IEnumerable<object[]>
(ApplePlatform.MacOSX, "CustomLibraryEnumExtensions", "CustomLibraryEnum.cs", "ExpectedCustomLibraryEnum.cs" ),
};

public IEnumerator<object[]> GetEnumerator()
public IEnumerator<object []> 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);
}

0 comments on commit 4a07007

Please sign in to comment.