-
Notifications
You must be signed in to change notification settings - Fork 511
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rgen] Implement SmartEnums code emittion.
This change brings the following: 1. We now generate the Extension classes for the SmartEnums. 2. Bumped the roslyn version, the one we were using had a bug when using dir separators in the name hint for a generated file. 3. Generated code is added under the expected directory for bgen to allow the API diff.
- Loading branch information
1 parent
dbbdf51
commit 4a0295c
Showing
44 changed files
with
2,411 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/rgen/Microsoft.Macios.Binding.Common/Microsoft.Macios.Binding.Common.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="..\..\..\tools\common\ApplePlatform.cs"> | ||
<Link>external\ApplePlatform.cs</Link> | ||
</Compile> | ||
<Compile Include="..\..\..\tools\common\SdkVersions.cs"> | ||
<Link>external\SdkVersions.cs</Link> | ||
</Compile> | ||
<Compile Include="..\..\..\tools\common\TargetFramework.cs"> | ||
<Link>external\TargetFramework.cs</Link> | ||
</Compile> | ||
<Compile Include="../../../tools/common/Frameworks.cs" > | ||
<Link>external\Frameworks.cs</Link> | ||
</Compile> | ||
<Compile Include="..\..\bgen\PlatformName.cs" > | ||
<Link>external\PlatformName.cs</Link> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 7 additions & 2 deletions
9
src/rgen/Microsoft.Macios.Bindings.Analyzer/AnalyzerReleases.Unshipped.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
### New Rules | ||
|
||
| Rule ID | Category | Severity | Notes | | ||
|---------|----------|----------|-------| | ||
| Rule ID | Category | Severity | Notes | | ||
|---------|----------|----------|------------------------------------------------------------| | ||
| RBI0001 | Usage | Error | Binding types should be declared as partial classes. | | ||
| RBI0002 | Usage | Error | Smart enum values must be tagged with an FieldAttribute. | | ||
| RBI0003 | Usage | Error | Smart enum backing field cannot be an empty string. | | ||
| RBI0004 | Usage | Error | Non Apple framework bindings must provide a library name. | | ||
| RBI0005 | Usage | Warning | Do not provide the LibraryName for known Apple frameworks. | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/rgen/Microsoft.Macios.Bindings.Analyzer/DiagnosticInfo.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.Macios.Bindings.Analyzer; | ||
|
||
public struct DiagnosticInfo ( | ||
string id, | ||
LocalizableResourceString title, | ||
LocalizableResourceString messageFormat, | ||
LocalizableResourceString description, | ||
string category) { | ||
|
||
public string Id { get; } = id; | ||
public LocalizableResourceString Title { get; } = title; | ||
public LocalizableResourceString MessageFormat { get; } = messageFormat; | ||
public LocalizableResourceString Description { get; } = description; | ||
public string Category { get; } = category; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/rgen/Microsoft.Macios.Bindings.Analyzer/Extensions/IBindingTypeAnalyzerExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.Macios.Generator; | ||
using Microsoft.Macios.Generator.Extensions; | ||
using Diagnostic = Microsoft.CodeAnalysis.Diagnostic; | ||
|
||
namespace Microsoft.Macios.Bindings.Analyzer.Extensions; | ||
|
||
public static class BindingTypeAnalyzerExtensions { | ||
public static void AnalyzeBindingType<T> (this IBindingTypeAnalyzer<T> self, SyntaxNodeAnalysisContext context) where T : BaseTypeDeclarationSyntax | ||
{ | ||
// calculate the current compilation platform name | ||
if (context.Node is not T declarationNode) | ||
return; | ||
|
||
var declaredSymbol = context.SemanticModel.GetDeclaredSymbol (declarationNode); | ||
if (declaredSymbol is null) | ||
return; | ||
|
||
var boundAttributes = declaredSymbol.GetAttributes (); | ||
if (boundAttributes.Length == 0) { | ||
// do nothing since our generator only cares about declared types with the BindingType attribute | ||
return; | ||
} | ||
|
||
// the c# syntax is a a list of lists of attributes. That is why we need to iterate through the list of lists | ||
foreach (var attributeData in boundAttributes) { | ||
// based on the type use the correct parser to retrieve the data | ||
var attributeType = attributeData.AttributeClass?.ToDisplayString (); | ||
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 (), | ||
declarationNode, declaredSymbol); | ||
foreach (var diagnostic in diagnostics) | ||
context.ReportDiagnostic (diagnostic); | ||
break; | ||
} | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/rgen/Microsoft.Macios.Bindings.Analyzer/Extensions/PlatformNameExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Xamarin.Utils; | ||
|
||
namespace Microsoft.Macios.Bindings.Analyzer.Extensions; | ||
|
||
public static class PlatformNameExtensions { | ||
|
||
public static ApplePlatform ToApplePlatform (this PlatformName platformName) | ||
=> platformName switch { | ||
PlatformName.iOS => ApplePlatform.iOS, | ||
PlatformName.MacCatalyst => ApplePlatform.MacCatalyst, | ||
PlatformName.MacOSX => ApplePlatform.MacOSX, | ||
PlatformName.TvOS => ApplePlatform.TVOS, | ||
_ => ApplePlatform.None, | ||
}; | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
src/rgen/Microsoft.Macios.Bindings.Analyzer/IBindingTypeAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System.Collections.Immutable; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
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 { | ||
ImmutableArray<Diagnostic> Analyze (PlatformName platformName, T declarationNode, INamedTypeSymbol symbol); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.