Skip to content

Commit

Permalink
[Rgen] Use a roslyn code generator to help generate the flag parsing …
Browse files Browse the repository at this point in the history
…of all attributes. (#22032)
  • Loading branch information
mandel-macaque authored Jan 24, 2025
1 parent b50532b commit 7df9693
Show file tree
Hide file tree
Showing 11 changed files with 569 additions and 91 deletions.
1 change: 1 addition & 0 deletions src/rgen/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generated
4 changes: 3 additions & 1 deletion src/rgen/Microsoft.Macios.Generator/DataModel/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
Expand All @@ -13,7 +14,8 @@ namespace Microsoft.Macios.Generator.DataModel;
/// <summary>
/// Readonly structure that represents a change in a parameter.
/// </summary>
readonly struct Parameter : IEquatable<Parameter> {
[StructLayout (LayoutKind.Auto)]
readonly partial struct Parameter : IEquatable<Parameter> {
/// <summary>
/// Parameter position in the method.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/rgen/Microsoft.Macios.Generator/DataModel/TypeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.Macios.Generator.Attributes;
Expand All @@ -13,6 +14,7 @@ namespace Microsoft.Macios.Generator.DataModel;
/// <summary>
/// Readonly structure that represents a change in a method return type.
/// </summary>
[StructLayout (LayoutKind.Auto)]
readonly partial struct TypeInfo : IEquatable<TypeInfo> {

public static TypeInfo Void = new ("void", SpecialType.System_Void) { Parents = ["System.ValueType", "object"], };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using Microsoft.Macios.Generator.Availability;
using Xamarin.Utils;

namespace Microsoft.Macios.Generator;

partial class TabbedStringBuilder {

public TabbedStringBuilder AppendMemberAvailability (in SymbolAvailability allPlatformsAvailability)
{
foreach (var availability in allPlatformsAvailability.PlatformAvailabilities) {
var platformName = availability.Platform.AsString ().ToLower ();
if (availability.SupportedVersion is not null) {
var versionStr = (PlatformAvailability.IsDefaultVersion (availability.SupportedVersion))
? string.Empty
: availability.SupportedVersion.ToString ();
AppendLine ($"[SupportedOSPlatform (\"{platformName}{versionStr}\")]");
}

// loop over the unsupported versions of the platform
foreach (var (version, message) in availability.UnsupportedVersions) {
var versionStr = (PlatformAvailability.IsDefaultVersion (version)) ? string.Empty : version.ToString ();
if (message is null) {
AppendLine ($"[UnsupportedOSPlatform (\"{platformName}{versionStr}\")]");
} else {
AppendLine ($"[UnsupportedOSPlatform (\"{platformName}{versionStr}\", \"{message}\")]");
}
}

// loop over the obsolete versions of the platform
foreach (var (version, obsoleteInfo) in availability.ObsoletedVersions) {
var versionStr = (PlatformAvailability.IsDefaultVersion (version)) ? string.Empty : version.ToString ();

switch (obsoleteInfo) {
case (null, null):
AppendLine ($"[ObsoletedOSPlatform (\"{platformName}{versionStr}\")]");
break;
case (not null, null):
AppendLine ($"[ObsoletedOSPlatform (\"{platformName}{versionStr}\", \"{obsoleteInfo.Message}\")]");
break;
case (null, not null):
AppendLine ($"[ObsoletedOSPlatform (\"{platformName}{versionStr}\", Url=\"{obsoleteInfo.Url}\")]");
break;
case (not null, not null):
AppendLine (
$"[ObsoletedOSPlatform (\"{platformName}{versionStr}\", \"{obsoleteInfo.Message}\", Url=\"{obsoleteInfo.Url}\")]");
break;
}
}
}

return this;
}
}
49 changes: 1 addition & 48 deletions src/rgen/Microsoft.Macios.Generator/TabbedStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using Microsoft.Macios.Generator.Availability;
using Xamarin.Utils;

namespace Microsoft.Macios.Generator;

Expand Down Expand Up @@ -136,6 +134,7 @@ public TabbedStringBuilder AppendLine (ref DefaultInterpolatedStringHandler hand
return this;
}


/// <summary>
/// Append a new raw literal by prepending the correct indentation.
/// </summary>
Expand Down Expand Up @@ -176,52 +175,6 @@ public TabbedStringBuilder AppendGeneratedCodeAttribute (bool optimizable = true
return this;
}

public TabbedStringBuilder AppendMemberAvailability (in SymbolAvailability allPlatformsAvailability)
{
foreach (var availability in allPlatformsAvailability.PlatformAvailabilities) {
var platformName = availability.Platform.AsString ().ToLower ();
if (availability.SupportedVersion is not null) {
var versionStr = (PlatformAvailability.IsDefaultVersion (availability.SupportedVersion))
? string.Empty
: availability.SupportedVersion.ToString ();
AppendLine ($"[SupportedOSPlatform (\"{platformName}{versionStr}\")]");
}

// loop over the unsupported versions of the platform
foreach (var (version, message) in availability.UnsupportedVersions) {
var versionStr = (PlatformAvailability.IsDefaultVersion (version)) ? string.Empty : version.ToString ();
if (message is null) {
AppendLine ($"[UnsupportedOSPlatform (\"{platformName}{versionStr}\")]");
} else {
AppendLine ($"[UnsupportedOSPlatform (\"{platformName}{versionStr}\", \"{message}\")]");
}
}

// loop over the obsolete versions of the platform
foreach (var (version, obsoleteInfo) in availability.ObsoletedVersions) {
var versionStr = (PlatformAvailability.IsDefaultVersion (version)) ? string.Empty : version.ToString ();

switch (obsoleteInfo) {
case (null, null):
AppendLine ($"[ObsoletedOSPlatform (\"{platformName}{versionStr}\")]");
break;
case (not null, null):
AppendLine ($"[ObsoletedOSPlatform (\"{platformName}{versionStr}\", \"{obsoleteInfo.Message}\")]");
break;
case (null, not null):
AppendLine ($"[ObsoletedOSPlatform (\"{platformName}{versionStr}\", Url=\"{obsoleteInfo.Url}\")]");
break;
case (not null, not null):
AppendLine (
$"[ObsoletedOSPlatform (\"{platformName}{versionStr}\", \"{obsoleteInfo.Message}\", Url=\"{obsoleteInfo.Url}\")]");
break;
}
}
}

return this;
}

public TabbedStringBuilder AppendNotificationAdvice (in string className, in string notification)
{
string attr =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net$(BundledNETCoreAppTargetFrameworkVersion)</TargetFramework>
<IsPackable>false</IsPackable>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>

<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
<IsRoslynComponent>true</IsRoslynComponent>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>

<RootNamespace>Microsoft.Macios.Transformer.Generator</RootNamespace>
<PackageId>Microsoft.Macios.Transformer.Generator</PackageId>
<!-- There is a bug in the roslyn analyzer for roslyn analyzers.... -->
<NoWarn>RS2007;RS1041;APL0003</NoWarn>

</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2"/>
</ItemGroup>

<ItemGroup>
<Compile Include="..\..\Microsoft.Macios.Generator\TabbedStringBuilder.cs">
<Link>TabbedStringBuilder.cs</Link>
</Compile>
</ItemGroup>


</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"DebugRoslynSourceGenerator": {
"commandName": "DebugRoslynComponent",
"targetProject": "../Microsoft.Macios.Transformer.Generator.Sample/Microsoft.Macios.Transformer.Generator.Sample.csproj"
}
}
}
Loading

0 comments on commit 7df9693

Please sign in to comment.