-
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.
Implement the analyzer for smart enums.
- Loading branch information
1 parent
0a66ce0
commit fa0c0ee
Showing
33 changed files
with
773 additions
and
117 deletions.
There are no files selected for viewing
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
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
80 changes: 64 additions & 16 deletions
80
src/rgen/Microsoft.Macios.Bindings.Analyzer/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.