Skip to content

Commit

Permalink
[Rgen] Add parsing of the ThreadSafe attr to the transformer.
Browse files Browse the repository at this point in the history
  • Loading branch information
mandel-macaque committed Jan 27, 2025
1 parent 010103d commit 71ed12c
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/rgen/Microsoft.Macios.Transformer/Attributes/ThreadSafeData.cs
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} }}";
}
5 changes: 5 additions & 0 deletions src/rgen/Microsoft.Macios.Transformer/AttributesNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ static class AttributesNames {
/// </summary>
[BindingFlag (AttributeTargets.Class | AttributeTargets.Interface)]
public const string TargetAttribute = "TargetAttribute";

/// <summary>
/// Flags the object as being thread safe.
/// </summary>
[BindingAttribute(typeof(BackingFieldTypeData), AttributeTargets.All)]
public const string ThreadSafeAttribute = "ThreadSafeAttribute";

/// <summary>
Expand Down
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);
}
}

0 comments on commit 71ed12c

Please sign in to comment.