Skip to content

Commit

Permalink
Polyfill the incremental generator ForAttributeWithMetadataName from …
Browse files Browse the repository at this point in the history
…roslyn (for LoggingGenerator). (#71651)

* Polyfill the incremental generator ForAttributeWithMetadataName from roslyn (for LoggingGenerator).

* Simplify

* Fix project file

* Remove

* revert
  • Loading branch information
CyrusNajmabadi committed Jul 7, 2022
1 parent 5544d9b commit 9610320
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public partial class LoggerMessageGenerator
{
internal sealed class Parser
{
private const string LoggerMessageAttribute = "Microsoft.Extensions.Logging.LoggerMessageAttribute";
internal const string LoggerMessageAttribute = "Microsoft.Extensions.Logging.LoggerMessageAttribute";

private readonly CancellationToken _cancellationToken;
private readonly Compilation _compilation;
Expand All @@ -31,36 +31,6 @@ public Parser(Compilation compilation, Action<Diagnostic> reportDiagnostic, Canc
_reportDiagnostic = reportDiagnostic;
}

internal static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
node is MethodDeclarationSyntax m && m.AttributeLists.Count > 0;

internal static ClassDeclarationSyntax? GetSemanticTargetForGeneration(GeneratorSyntaxContext context)
{
var methodDeclarationSyntax = (MethodDeclarationSyntax)context.Node;

foreach (AttributeListSyntax attributeListSyntax in methodDeclarationSyntax.AttributeLists)
{
foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes)
{
IMethodSymbol attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol as IMethodSymbol;
if (attributeSymbol == null)
{
continue;
}

INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType;
string fullName = attributeContainingTypeSymbol.ToDisplayString();

if (fullName == LoggerMessageAttribute)
{
return methodDeclarationSyntax.Parent as ClassDeclarationSyntax;
}
}
}

return null;
}

/// <summary>
/// Gets the set of logging classes containing methods to output.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,45 @@ internal static ISyntaxContextReceiver Create()

public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
if (Parser.IsSyntaxTargetForGeneration(context.Node))
if (IsSyntaxTargetForGeneration(context.Node))
{
ClassDeclarationSyntax classSyntax = Parser.GetSemanticTargetForGeneration(context);
ClassDeclarationSyntax classSyntax = GetSemanticTargetForGeneration(context);
if (classSyntax != null)
{
ClassDeclarations.Add(classSyntax);
}
}
}

private static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
node is MethodDeclarationSyntax m && m.AttributeLists.Count > 0;

private static ClassDeclarationSyntax? GetSemanticTargetForGeneration(GeneratorSyntaxContext context)
{
var methodDeclarationSyntax = (MethodDeclarationSyntax)context.Node;

foreach (AttributeListSyntax attributeListSyntax in methodDeclarationSyntax.AttributeLists)
{
foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes)
{
IMethodSymbol attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol as IMethodSymbol;
if (attributeSymbol == null)
{
continue;
}

INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType;
string fullName = attributeContainingTypeSymbol.ToDisplayString();

if (fullName == Parser.LoggerMessageAttribute)
{
return methodDeclarationSyntax.Parent as ClassDeclarationSyntax;
}
}
}

return null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;
using Microsoft.CodeAnalysis.Text;

[assembly: System.Resources.NeutralResourcesLanguage("en-us")]
Expand All @@ -19,7 +20,11 @@ public partial class LoggerMessageGenerator : IIncrementalGenerator
public void Initialize(IncrementalGeneratorInitializationContext context)
{
IncrementalValuesProvider<ClassDeclarationSyntax> classDeclarations = context.SyntaxProvider
.CreateSyntaxProvider(static (s, _) => Parser.IsSyntaxTargetForGeneration(s), static (ctx, _) => Parser.GetSemanticTargetForGeneration(ctx))
.ForAttributeWithMetadataName(
context,
Parser.LoggerMessageAttribute,
(node, _) => node is MethodDeclarationSyntax,
(context, _) => context.TargetNode.Parent as ClassDeclarationSyntax)
.Where(static m => m is not null);

IncrementalValueProvider<(Compilation, ImmutableArray<ClassDeclarationSyntax>)> compilationAndClasses =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@

<Import Project="Microsoft.Extensions.Logging.Generators.targets" />

<ItemGroup>
<Compile Include="$(CommonPath)Roslyn\Hash.cs" Link="Common\Roslyn\Hash.cs" />
<Compile Include="$(CommonPath)Roslyn\ISyntaxHelper.cs" Link="Common\Roslyn\ISyntaxHelper.cs" />
<Compile Include="$(CommonPath)Roslyn\CSharpSyntaxHelper.cs" Link="Common\Roslyn\CSharpSyntaxHelper.cs" />
<Compile Include="$(CommonPath)Roslyn\GlobalAliases.cs" Link="Common\Roslyn\GlobalAliases.cs" />
<Compile Include="$(CommonPath)Roslyn\SyntaxNodeGrouping.cs" Link="Common\Roslyn\SyntaxNodeGrouping.cs" />
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" Link="Common\Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" />
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" />
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" />

<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" />
</ItemGroup>

<ItemGroup>
<Compile Remove="LoggerMessageGenerator.Roslyn3.11.cs" />
</ItemGroup>
Expand Down

0 comments on commit 9610320

Please sign in to comment.