From 909709afc558ae370078514d05f553e5dfe8e4e5 Mon Sep 17 00:00:00 2001 From: Jared Parsons Date: Wed, 8 Jun 2022 15:35:20 -0700 Subject: [PATCH] Generator changes --- .../gen/RegexGenerator.Parser.cs | 40 +++---------------- .../gen/RegexGenerator.cs | 9 +++-- 2 files changed, 11 insertions(+), 38 deletions(-) diff --git a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Parser.cs b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Parser.cs index 0829ccf42ff174..a9179fb7b056a4 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Parser.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Parser.cs @@ -22,37 +22,15 @@ public partial class RegexGenerator private const string RegexGeneratorAttributeName = "System.Text.RegularExpressions.RegexGeneratorAttribute"; private static bool IsSyntaxTargetForGeneration(SyntaxNode node, CancellationToken cancellationToken) => - // We don't have a semantic model here, so the best we can do is say whether there are any attributes. - node is MethodDeclarationSyntax { AttributeLists: { Count: > 0 } }; - - private static bool IsSemanticTargetForGeneration(SemanticModel semanticModel, MethodDeclarationSyntax methodDeclarationSyntax, CancellationToken cancellationToken) - { - foreach (AttributeListSyntax attributeListSyntax in methodDeclarationSyntax.AttributeLists) - { - foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes) - { - if (semanticModel.GetSymbolInfo(attributeSyntax, cancellationToken).Symbol is IMethodSymbol attributeSymbol && - attributeSymbol.ContainingType.ToDisplayString() == RegexGeneratorAttributeName) - { - return true; - } - } - } - - return false; - } + node is MethodDeclarationSyntax; // Returns null if nothing to do, Diagnostic if there's an error to report, or RegexType if the type was analyzed successfully. - private static object? GetSemanticTargetForGeneration(GeneratorSyntaxContext context, CancellationToken cancellationToken) + private static object? GetSemanticTargetForGeneration(GeneratorAttributeSyntaxContext context, CancellationToken cancellationToken) { - var methodSyntax = (MethodDeclarationSyntax)context.Node; + Debug.Assert(!context.Attributes.IsDefaultOrEmpty); + var methodSyntax = (MethodDeclarationSyntax)context.TargetNode; SemanticModel sm = context.SemanticModel; - if (!IsSemanticTargetForGeneration(sm, methodSyntax, cancellationToken)) - { - return null; - } - Compilation compilation = sm.Compilation; INamedTypeSymbol? regexSymbol = compilation.GetBestTypeByMetadataName(RegexName); INamedTypeSymbol? regexGeneratorAttributeSymbol = compilation.GetBestTypeByMetadataName(RegexGeneratorAttributeName); @@ -69,23 +47,17 @@ private static bool IsSemanticTargetForGeneration(SemanticModel semanticModel, M return null; } - IMethodSymbol? regexMethodSymbol = sm.GetDeclaredSymbol(methodSyntax, cancellationToken) as IMethodSymbol; + IMethodSymbol? regexMethodSymbol = context.TargetSymbol as IMethodSymbol; if (regexMethodSymbol is null) { return null; } - ImmutableArray? boundAttributes = regexMethodSymbol.GetAttributes(); - if (boundAttributes is null || boundAttributes.Value.Length == 0) - { - return null; - } - bool attributeFound = false; string? pattern = null; int? options = null; int? matchTimeout = null; - foreach (AttributeData attributeData in boundAttributes) + foreach (AttributeData attributeData in context.Attributes) { if (!SymbolEqualityComparer.Default.Equals(attributeData.AttributeClass, regexGeneratorAttributeSymbol)) { diff --git a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs index 8aac6281fa0022..8441e875efc20c 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs +++ b/src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.cs @@ -48,10 +48,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context) // - (RegexMethod regexMethod, string runnerFactoryImplementation, Dictionary requiredHelpers) in the case of valid regex // - (RegexMethod regexMethod, string reason, Diagnostic diagnostic) in the case of a limited-support regex IncrementalValueProvider> codeOrDiagnostics = - context.SyntaxProvider - - // Find all MethodDeclarationSyntax nodes attributed with RegexGenerator and gather the required information. - .CreateSyntaxProvider(IsSyntaxTargetForGeneration, GetSemanticTargetForGeneration) + context + .ForAttributeWithMetadataName( + RegexName, + IsSyntaxTargetForGeneration, + GetSemanticTargetForGeneration) .Where(static m => m is not null) // Generate the RunnerFactory for each regex, if possible. This is where the bulk of the implementation occurs.