Skip to content

Commit 9610320

Browse files
Polyfill the incremental generator ForAttributeWithMetadataName from roslyn (for LoggingGenerator). (#71651)
* Polyfill the incremental generator ForAttributeWithMetadataName from roslyn (for LoggingGenerator). * Simplify * Fix project file * Remove * revert
1 parent 5544d9b commit 9610320

File tree

4 files changed

+53
-34
lines changed

4 files changed

+53
-34
lines changed

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Parser.cs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public partial class LoggerMessageGenerator
1818
{
1919
internal sealed class Parser
2020
{
21-
private const string LoggerMessageAttribute = "Microsoft.Extensions.Logging.LoggerMessageAttribute";
21+
internal const string LoggerMessageAttribute = "Microsoft.Extensions.Logging.LoggerMessageAttribute";
2222

2323
private readonly CancellationToken _cancellationToken;
2424
private readonly Compilation _compilation;
@@ -31,36 +31,6 @@ public Parser(Compilation compilation, Action<Diagnostic> reportDiagnostic, Canc
3131
_reportDiagnostic = reportDiagnostic;
3232
}
3333

34-
internal static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
35-
node is MethodDeclarationSyntax m && m.AttributeLists.Count > 0;
36-
37-
internal static ClassDeclarationSyntax? GetSemanticTargetForGeneration(GeneratorSyntaxContext context)
38-
{
39-
var methodDeclarationSyntax = (MethodDeclarationSyntax)context.Node;
40-
41-
foreach (AttributeListSyntax attributeListSyntax in methodDeclarationSyntax.AttributeLists)
42-
{
43-
foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes)
44-
{
45-
IMethodSymbol attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol as IMethodSymbol;
46-
if (attributeSymbol == null)
47-
{
48-
continue;
49-
}
50-
51-
INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType;
52-
string fullName = attributeContainingTypeSymbol.ToDisplayString();
53-
54-
if (fullName == LoggerMessageAttribute)
55-
{
56-
return methodDeclarationSyntax.Parent as ClassDeclarationSyntax;
57-
}
58-
}
59-
}
60-
61-
return null;
62-
}
63-
6434
/// <summary>
6535
/// Gets the set of logging classes containing methods to output.
6636
/// </summary>

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Roslyn3.11.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,45 @@ internal static ISyntaxContextReceiver Create()
4949

5050
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
5151
{
52-
if (Parser.IsSyntaxTargetForGeneration(context.Node))
52+
if (IsSyntaxTargetForGeneration(context.Node))
5353
{
54-
ClassDeclarationSyntax classSyntax = Parser.GetSemanticTargetForGeneration(context);
54+
ClassDeclarationSyntax classSyntax = GetSemanticTargetForGeneration(context);
5555
if (classSyntax != null)
5656
{
5757
ClassDeclarations.Add(classSyntax);
5858
}
5959
}
6060
}
61+
62+
private static bool IsSyntaxTargetForGeneration(SyntaxNode node) =>
63+
node is MethodDeclarationSyntax m && m.AttributeLists.Count > 0;
64+
65+
private static ClassDeclarationSyntax? GetSemanticTargetForGeneration(GeneratorSyntaxContext context)
66+
{
67+
var methodDeclarationSyntax = (MethodDeclarationSyntax)context.Node;
68+
69+
foreach (AttributeListSyntax attributeListSyntax in methodDeclarationSyntax.AttributeLists)
70+
{
71+
foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes)
72+
{
73+
IMethodSymbol attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol as IMethodSymbol;
74+
if (attributeSymbol == null)
75+
{
76+
continue;
77+
}
78+
79+
INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType;
80+
string fullName = attributeContainingTypeSymbol.ToDisplayString();
81+
82+
if (fullName == Parser.LoggerMessageAttribute)
83+
{
84+
return methodDeclarationSyntax.Parent as ClassDeclarationSyntax;
85+
}
86+
}
87+
}
88+
89+
return null;
90+
}
6191
}
6292
}
6393
}

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/LoggerMessageGenerator.Roslyn4.0.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Text;
88
using Microsoft.CodeAnalysis;
99
using Microsoft.CodeAnalysis.CSharp.Syntax;
10+
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;
1011
using Microsoft.CodeAnalysis.Text;
1112

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

2530
IncrementalValueProvider<(Compilation, ImmutableArray<ClassDeclarationSyntax>)> compilationAndClasses =

src/libraries/Microsoft.Extensions.Logging.Abstractions/gen/Microsoft.Extensions.Logging.Generators.Roslyn4.0.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,20 @@
88

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

11+
<ItemGroup>
12+
<Compile Include="$(CommonPath)Roslyn\Hash.cs" Link="Common\Roslyn\Hash.cs" />
13+
<Compile Include="$(CommonPath)Roslyn\ISyntaxHelper.cs" Link="Common\Roslyn\ISyntaxHelper.cs" />
14+
<Compile Include="$(CommonPath)Roslyn\CSharpSyntaxHelper.cs" Link="Common\Roslyn\CSharpSyntaxHelper.cs" />
15+
<Compile Include="$(CommonPath)Roslyn\GlobalAliases.cs" Link="Common\Roslyn\GlobalAliases.cs" />
16+
<Compile Include="$(CommonPath)Roslyn\SyntaxNodeGrouping.cs" Link="Common\Roslyn\SyntaxNodeGrouping.cs" />
17+
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" Link="Common\Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" />
18+
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" />
19+
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" />
20+
21+
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
22+
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" />
23+
</ItemGroup>
24+
1125
<ItemGroup>
1226
<Compile Remove="LoggerMessageGenerator.Roslyn3.11.cs" />
1327
</ItemGroup>

0 commit comments

Comments
 (0)