-
Notifications
You must be signed in to change notification settings - Fork 518
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Rgen] Add parsing of the ThreadSafe attr to the transformer.
- Loading branch information
1 parent
010103d
commit 71ed12c
Showing
3 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/rgen/Microsoft.Macios.Transformer/Attributes/ThreadSafeData.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,68 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.Macios.Transformer.Attributes; | ||
|
||
readonly struct ThreadSafeData : IEquatable<ThreadSafeData> { | ||
|
||
public bool Safe { get; } = true; | ||
|
||
public ThreadSafeData () : this (true) {} | ||
|
||
public ThreadSafeData (bool safe) | ||
{ | ||
Safe = safe; | ||
} | ||
|
||
public static bool TryParse (AttributeData attributeData, | ||
[NotNullWhen (true)] out ThreadSafeData? data) | ||
{ | ||
data = null; | ||
var count = attributeData.ConstructorArguments.Length; | ||
if (count == 0) { | ||
data = new(); | ||
return true; | ||
} | ||
bool safe = true; | ||
switch (count) { | ||
case 1: | ||
safe = (bool) attributeData.ConstructorArguments [0].Value!; | ||
break; | ||
default: | ||
// 0 should not be an option.. | ||
return false; | ||
} | ||
|
||
data = new ThreadSafeData (safe); | ||
return true; | ||
} | ||
|
||
public bool Equals (ThreadSafeData other) | ||
=> Safe == other.Safe; | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals (object? obj) | ||
{ | ||
return obj is ThreadSafeData other && Equals (other); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode () | ||
=> HashCode.Combine (Safe); | ||
|
||
public static bool operator == (ThreadSafeData x, ThreadSafeData y) | ||
{ | ||
return x.Equals (y); | ||
} | ||
|
||
public static bool operator != (ThreadSafeData x, ThreadSafeData y) | ||
{ | ||
return !(x == y); | ||
} | ||
|
||
public override string ToString () | ||
=> $"{{ ThreadSafe: {Safe} }}"; | ||
} |
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
92 changes: 92 additions & 0 deletions
92
tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/ThreadSafeDataTests.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,92 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Collections; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.Macios.Generator.Extensions; | ||
using Microsoft.Macios.Transformer.Attributes; | ||
using Xamarin.Tests; | ||
using Xamarin.Utils; | ||
|
||
namespace Microsoft.Macios.Transformer.Tests.Attributes; | ||
|
||
public class ThreadSafeDataTests : BaseTransformerTestClass { | ||
|
||
class TestDataTryCreate : IEnumerable<object []> { | ||
public IEnumerator<object []> GetEnumerator () | ||
{ | ||
var path = "/some/random/path.cs"; | ||
|
||
const string threadSageMethod = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
using UIKit; | ||
namespace Test; | ||
[NoTV] | ||
[MacCatalyst (13, 1)] | ||
[DisableDefaultCtor] | ||
[Abstract] | ||
[BaseType (typeof (NSObject))] | ||
interface UIFeedbackGenerator : UIInteraction { | ||
[Export (""prepare""), ThreadSafe] | ||
void Prepare (); | ||
} | ||
"; | ||
|
||
yield return [(Source: threadSageMethod, Path: path), new ThreadSafeData()]; | ||
|
||
const string notThreadSafeMethod = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
using UIKit; | ||
namespace Test; | ||
[NoTV] | ||
[MacCatalyst (13, 1)] | ||
[DisableDefaultCtor] | ||
[Abstract] | ||
[BaseType (typeof (NSObject))] | ||
interface UIFeedbackGenerator : UIInteraction { | ||
[Export (""prepare""), ThreadSafe (false)] | ||
void Prepare (); | ||
} | ||
"; | ||
|
||
yield return [(Source: notThreadSafeMethod, Path: path), new ThreadSafeData(false)]; | ||
|
||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); | ||
} | ||
|
||
[Theory] | ||
[AllSupportedPlatformsClassData<TestDataTryCreate>] | ||
void TryCreateTests (ApplePlatform platform, (string Source, string Path) source, ThreadSafeData expectedData) | ||
{ | ||
// create a compilation used to create the transformer | ||
var compilation = CreateCompilation (platform, sources: source); | ||
var syntaxTree = compilation.SyntaxTrees.ForSource (source); | ||
Assert.NotNull (syntaxTree); | ||
|
||
var semanticModel = compilation.GetSemanticModel (syntaxTree); | ||
Assert.NotNull (semanticModel); | ||
|
||
var declaration = syntaxTree.GetRoot () | ||
.DescendantNodes ().OfType<MethodDeclarationSyntax> () | ||
.FirstOrDefault (); | ||
Assert.NotNull (declaration); | ||
|
||
var symbol = semanticModel.GetDeclaredSymbol (declaration); | ||
Assert.NotNull (symbol); | ||
var attribute = symbol.GetAttribute<ThreadSafeData> (AttributesNames.ThreadSafeAttribute, ThreadSafeData.TryParse); | ||
Assert.Equal (expectedData, attribute); | ||
} | ||
} |