|
12 | 12 | using Microsoft.CodeAnalysis; |
13 | 13 | using Microsoft.CodeAnalysis.CSharp; |
14 | 14 | using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 15 | +using Microsoft.CodeAnalysis.DotnetRuntime.Extensions; |
15 | 16 | using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; |
16 | 17 |
|
17 | 18 | [assembly: System.Resources.NeutralResourcesLanguage("en-US")] |
@@ -61,19 +62,13 @@ public static class StepNames |
61 | 62 | public void Initialize(IncrementalGeneratorInitializationContext context) |
62 | 63 | { |
63 | 64 | var attributedMethods = context.SyntaxProvider |
64 | | - .CreateSyntaxProvider( |
65 | | - static (node, ct) => ShouldVisitNode(node), |
66 | | - static (context, ct) => |
67 | | - { |
68 | | - MethodDeclarationSyntax syntax = (MethodDeclarationSyntax)context.Node; |
69 | | - if (context.SemanticModel.GetDeclaredSymbol(syntax, ct) is IMethodSymbol methodSymbol |
70 | | - && methodSymbol.GetAttributes().Any(static attribute => attribute.AttributeClass?.ToDisplayString() == TypeNames.LibraryImportAttribute)) |
71 | | - { |
72 | | - return new { Syntax = syntax, Symbol = methodSymbol }; |
73 | | - } |
74 | | - |
75 | | - return null; |
76 | | - }) |
| 65 | + .ForAttributeWithMetadataName( |
| 66 | + context, |
| 67 | + TypeNames.LibraryImportAttribute, |
| 68 | + static (node, ct) => node is MethodDeclarationSyntax, |
| 69 | + static (context, ct) => context.TargetSymbol is IMethodSymbol methodSymbol |
| 70 | + ? new { Syntax = (MethodDeclarationSyntax)context.TargetNode, Symbol = methodSymbol } |
| 71 | + : null) |
77 | 72 | .Where( |
78 | 73 | static modelData => modelData is not null); |
79 | 74 |
|
@@ -584,19 +579,6 @@ static ExpressionSyntax CreateEnumExpressionSyntax<T>(T value) where T : Enum |
584 | 579 | } |
585 | 580 | } |
586 | 581 |
|
587 | | - private static bool ShouldVisitNode(SyntaxNode syntaxNode) |
588 | | - { |
589 | | - // We only support C# method declarations. |
590 | | - if (syntaxNode.Language != LanguageNames.CSharp |
591 | | - || !syntaxNode.IsKind(SyntaxKind.MethodDeclaration)) |
592 | | - { |
593 | | - return false; |
594 | | - } |
595 | | - |
596 | | - // Filter out methods with no attributes early. |
597 | | - return ((MethodDeclarationSyntax)syntaxNode).AttributeLists.Count > 0; |
598 | | - } |
599 | | - |
600 | 582 | private static Diagnostic? GetDiagnosticIfInvalidMethodForGeneration(MethodDeclarationSyntax methodSyntax, IMethodSymbol method) |
601 | 583 | { |
602 | 584 | // Verify the method has no generic types or defined implementation |
|
0 commit comments