-
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 code needed to parse the several snippet attrs.
Allow the transformer to detect this. We will need to think a way to port them to the new API style.
- Loading branch information
1 parent
d79592d
commit 694a975
Showing
3 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
src/rgen/Microsoft.Macios.Transformer/Attributes/SnippetData.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,97 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace Microsoft.Macios.Transformer.Attributes; | ||
|
||
readonly struct SnippetData : IEquatable<SnippetData> { | ||
|
||
public string Code { get; } | ||
|
||
public bool Optimizable { get; } | ||
|
||
public SnippetData (string code) | ||
{ | ||
Code = code; | ||
} | ||
|
||
public SnippetData (string code, bool optimizable) | ||
{ | ||
Code = code; | ||
Optimizable = optimizable; | ||
} | ||
|
||
public static bool TryParse (AttributeData attributeData, | ||
[NotNullWhen (true)] out SnippetData? data) | ||
{ | ||
data = null; | ||
var count = attributeData.ConstructorArguments.Length; | ||
string code; | ||
var optimizable = false; | ||
|
||
// custom marshal directive values | ||
|
||
switch (count) { | ||
case 1: | ||
code = (string) attributeData.ConstructorArguments [0].Value!; | ||
break; | ||
default: | ||
// 0 should not be an option.. | ||
return false; | ||
} | ||
|
||
if (attributeData.NamedArguments.Length == 0) { | ||
data = new (code); | ||
return true; | ||
} | ||
|
||
foreach (var (argumentName, value) in attributeData.NamedArguments) { | ||
switch (argumentName) { | ||
case "Code": | ||
code = (string?) value.Value!; | ||
break; | ||
case "Optimizable": | ||
optimizable = (bool) value.Value!; | ||
break; | ||
default: | ||
data = null; | ||
return false; | ||
} | ||
} | ||
|
||
data = new (code, optimizable); | ||
return true; | ||
} | ||
|
||
public bool Equals (SnippetData other) | ||
{ | ||
if (Code != other.Code) | ||
return false; | ||
return Optimizable == other.Optimizable; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override bool Equals (object? obj) | ||
{ | ||
return obj is SnippetData other && Equals (other); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override int GetHashCode () | ||
=> HashCode.Combine (Code, Optimizable); | ||
|
||
public static bool operator == (SnippetData x, SnippetData y) | ||
{ | ||
return x.Equals (y); | ||
} | ||
|
||
public static bool operator != (SnippetData x, SnippetData y) | ||
{ | ||
return !(x == y); | ||
} | ||
|
||
public override string ToString () | ||
=> $"{{ Code: '{Code}' Optimizable: {Optimizable} }}"; | ||
} |
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
62 changes: 62 additions & 0 deletions
62
tests/rgen/Microsoft.Macios.Transformer.Tests/Attributes/SnippetDataTests.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,62 @@ | ||
// 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 SnippetDataTests : BaseTransformerTestClass { | ||
class TestDataTryCreate : IEnumerable<object []> { | ||
|
||
public IEnumerator<object []> GetEnumerator () | ||
{ | ||
const string path = "/some/random/path.cs"; | ||
|
||
const string disposeAttribute = @" | ||
using System; | ||
using Foundation; | ||
using ObjCRuntime; | ||
using UIKit; | ||
namespace Test; | ||
[BaseType (typeof (NSControl))] | ||
[Dispose (""dispatcher = null;"", Optimizable = true)] | ||
interface NSButton { } | ||
"; | ||
|
||
yield return [(Source: disposeAttribute, Path: path), new SnippetData("dispatcher = null;", true)]; | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); | ||
} | ||
|
||
[Theory] | ||
[AllSupportedPlatformsClassData<TestDataTryCreate>] | ||
void TryCreateTests (ApplePlatform platform, (string Source, string Path) source, SnippetData 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<BaseTypeDeclarationSyntax> () | ||
.FirstOrDefault (); | ||
Assert.NotNull (declaration); | ||
|
||
var symbol = semanticModel.GetDeclaredSymbol (declaration); | ||
Assert.NotNull (symbol); | ||
var attribute = symbol.GetAttribute<SnippetData> (AttributesNames.DisposeAttribute, SnippetData.TryParse); | ||
Assert.Equal (expectedData, attribute); | ||
} | ||
} |