-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
11 additions
and
38 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -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; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
IMethodSymbol? regexMethodSymbol = sm.GetDeclaredSymbol(methodSyntax, cancellationToken) as IMethodSymbol; | ||
IMethodSymbol? regexMethodSymbol = context.TargetSymbol as IMethodSymbol; | ||
This comment has been minimized.
Sorry, something went wrong.
CyrusNajmabadi
Member
|
||
if (regexMethodSymbol is null) | ||
{ | ||
return null; | ||
} | ||
|
||
ImmutableArray<AttributeData>? 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)) | ||
{ | ||
|
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
nit: this syntaqctic check could move into IsSyntaxTargetForGeneration instead.