-
Notifications
You must be signed in to change notification settings - Fork 519
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rgen] Add a new struc that will represent the needed trivia for a sy…
…mbol. (#22017) If we are generating code, specially in the transformer, that does not have a specific platform, we need to generate the correct trivia to ensure that methods are not generated in the wrong platform. --------- Co-authored-by: GitHub Actions Autoformatter <[email protected]>
- Loading branch information
1 parent
efd6ff9
commit 3a0fb1a
Showing
7 changed files
with
206 additions
and
13 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
59 changes: 59 additions & 0 deletions
59
src/rgen/Microsoft.Macios.Generator/Availability/AvailabilityTrivia.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,59 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.Macios.Generator.Extensions; | ||
using Xamarin.Utils; | ||
|
||
namespace Microsoft.Macios.Generator.Availability; | ||
|
||
readonly struct AvailabilityTrivia { | ||
|
||
/// <summary> | ||
/// Retrieve the trivia to add before the symbol declaration. | ||
/// </summary> | ||
public string? Start { get; } | ||
|
||
/// <summary> | ||
/// Retrieve the trivia to add after the symbol declaration. | ||
/// </summary> | ||
public string? End { get; } | ||
|
||
public AvailabilityTrivia (SymbolAvailability availability) | ||
{ | ||
// trivia is calculated based on the availability of the symbol in each platform | ||
// we will check each of the platforms and decide for the shorts #if possible | ||
var supportedPlatforms = new HashSet<string> (); | ||
var unsupportedPlatforms = new HashSet<string> (); | ||
foreach (var platformAvailability in availability.PlatformAvailabilities) { | ||
var platformDefine = platformAvailability.Platform.ToPlatformDefine (); | ||
if (platformDefine is null) | ||
continue; | ||
|
||
if (platformAvailability.IsSupported) | ||
supportedPlatforms.Add (platformDefine); | ||
else | ||
unsupportedPlatforms.Add (platformDefine); | ||
} | ||
|
||
// if all platforms are supported, we don't need any trivia | ||
if (unsupportedPlatforms.Count == 0) { | ||
// all platforms are supported | ||
Start = null; | ||
End = null; | ||
return; | ||
} | ||
|
||
if (supportedPlatforms.Count > unsupportedPlatforms.Count) { | ||
// we have more supported platforms than unsupported ones | ||
// we will use #if to exclude the unsupported platforms | ||
Start = $"#if !{string.Join (" && !", unsupportedPlatforms)}"; | ||
End = "#endif"; | ||
} else { | ||
// we have more unsupported platforms than supported ones | ||
// we will use #if to include the supported platforms | ||
Start = $"#if {string.Join (" || ", supportedPlatforms)}"; | ||
End = "#endif"; | ||
} | ||
} | ||
} |
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
22 changes: 22 additions & 0 deletions
22
src/rgen/Microsoft.Macios.Generator/Extensions/ApplePlatformExtensions.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,22 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using Xamarin.Utils; | ||
|
||
namespace Microsoft.Macios.Generator.Extensions; | ||
|
||
public static class ApplePlatformExtensions { | ||
|
||
/// <summary> | ||
/// Return the platform define for the given ApplePlatform for use in #if directives. | ||
/// </summary> | ||
/// <param name="self">Apple platform.</param> | ||
/// <returns>the platform define</returns> | ||
public static string? ToPlatformDefine (this ApplePlatform self) => self switch { | ||
ApplePlatform.iOS => "IOS", | ||
ApplePlatform.TVOS => "TVOS", | ||
ApplePlatform.MacOSX => "MONOMAC", | ||
ApplePlatform.MacCatalyst => "__MACCATALYST__", | ||
_ => null | ||
}; | ||
} |
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
89 changes: 89 additions & 0 deletions
89
tests/rgen/Microsoft.Macios.Generator.Tests/Availability/AvailabilityTriviaTests.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,89 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using Microsoft.Macios.Generator.Attributes; | ||
using Microsoft.Macios.Generator.Availability; | ||
using Xunit; | ||
|
||
namespace Microsoft.Macios.Generator.Tests.Availability; | ||
|
||
public class AvailabilityTriviaTests { | ||
|
||
[Fact] | ||
public void AllSupportedPlatforms () | ||
{ | ||
SymbolAvailability.Builder builder = SymbolAvailability.CreateBuilder (); | ||
builder.Add (new SupportedOSPlatformData ("ios12.0")); | ||
builder.Add (new SupportedOSPlatformData ("tvos12.0")); | ||
builder.Add (new SupportedOSPlatformData ("macos10.14")); | ||
builder.Add (new SupportedOSPlatformData ("macCatalyst13.0")); | ||
var availability = builder.ToImmutable (); | ||
var trivia = new AvailabilityTrivia (availability); | ||
Assert.Null (trivia.Start); | ||
Assert.Null (trivia.End); | ||
Assert.Null (availability.Trivia); | ||
} | ||
|
||
[Fact] | ||
public void SomeUnsupportedVersions () | ||
{ | ||
SymbolAvailability.Builder builder = SymbolAvailability.CreateBuilder (); | ||
builder.Add (new SupportedOSPlatformData ("ios12.0")); | ||
builder.Add (new UnsupportedOSPlatformData ("ios9.0")); | ||
builder.Add (new SupportedOSPlatformData ("tvos12.0")); | ||
builder.Add (new SupportedOSPlatformData ("macos10.14")); | ||
builder.Add (new SupportedOSPlatformData ("maccatalyst13.0")); | ||
var availability = builder.ToImmutable (); | ||
var trivia = new AvailabilityTrivia (availability); | ||
Assert.Null (trivia.Start); | ||
Assert.Null (trivia.End); | ||
Assert.Null (availability.Trivia); | ||
} | ||
|
||
[Fact] | ||
public void SingleFullyUnsupportedPlatform () | ||
{ | ||
SymbolAvailability.Builder builder = SymbolAvailability.CreateBuilder (); | ||
builder.Add (new UnsupportedOSPlatformData ("ios")); | ||
builder.Add (new SupportedOSPlatformData ("tvos12.0")); | ||
builder.Add (new SupportedOSPlatformData ("macos10.14")); | ||
builder.Add (new SupportedOSPlatformData ("maccatalyst13.0")); | ||
var availability = builder.ToImmutable (); | ||
var trivia = new AvailabilityTrivia (availability); | ||
Assert.Equal ("#if !IOS", trivia.Start); | ||
Assert.Equal ("#endif", trivia.End); | ||
Assert.NotNull (availability.Trivia); | ||
} | ||
|
||
[Fact] | ||
public void DoubleUnsupportedPlatform () | ||
{ | ||
SymbolAvailability.Builder builder = SymbolAvailability.CreateBuilder (); | ||
builder.Add (new SupportedOSPlatformData ("ios")); | ||
builder.Add (new SupportedOSPlatformData ("tvos12.0")); | ||
builder.Add (new UnsupportedOSPlatformData ("macos")); | ||
builder.Add (new UnsupportedOSPlatformData ("maccatalyst")); | ||
var availability = builder.ToImmutable (); | ||
var trivia = new AvailabilityTrivia (availability); | ||
Assert.Equal ("#if IOS || TVOS", trivia.Start); | ||
Assert.Equal ("#endif", trivia.End); | ||
Assert.NotNull (availability.Trivia); | ||
} | ||
|
||
[Fact] | ||
public void SingleSupportedPlatform () | ||
{ | ||
SymbolAvailability.Builder builder = SymbolAvailability.CreateBuilder (); | ||
builder.Add (new SupportedOSPlatformData ("ios")); | ||
builder.Add (new UnsupportedOSPlatformData ("tvos")); | ||
builder.Add (new UnsupportedOSPlatformData ("macos")); | ||
builder.Add (new UnsupportedOSPlatformData ("maccatalyst")); | ||
var availability = builder.ToImmutable (); | ||
var trivia = new AvailabilityTrivia (availability); | ||
Assert.Equal ("#if IOS", trivia.Start); | ||
Assert.Equal ("#endif", trivia.End); | ||
Assert.NotNull (availability.Trivia); | ||
} | ||
|
||
} |