Skip to content

Commit 92d8a8b

Browse files
Polyfill the incremental generator ForAttributeWithMetadataName from roslyn (for LibraryImportGenerator). (#71652)
Co-authored-by: Jeremy Koritzinsky <[email protected]>
1 parent 840e570 commit 92d8a8b

File tree

3 files changed

+28
-31
lines changed

3 files changed

+28
-31
lines changed

eng/Versions.props

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
This version is a moving target until we ship.
3737
It should never go ahead of the Roslyn version included in the SDK version in dotnet/arcade's global.json to avoid causing breaks in product construction.
3838
-->
39-
<MicrosoftCodeAnalysisVersion_4_X>4.2.0-2.final</MicrosoftCodeAnalysisVersion_4_X>
39+
<MicrosoftCodeAnalysisVersion_4_X>4.3.0-2.final</MicrosoftCodeAnalysisVersion_4_X>
4040
</PropertyGroup>
4141
<PropertyGroup>
4242
<!-- Code analysis dependencies -->
4343
<MicrosoftCodeAnalysisAnalyzersVersion>3.3.3</MicrosoftCodeAnalysisAnalyzersVersion>
44-
<MicrosoftCodeAnalysisCSharpCodeStyleVersion>4.3.0-1.22206.2</MicrosoftCodeAnalysisCSharpCodeStyleVersion>
45-
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.3.0-1.22206.2</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
46-
<MicrosoftCodeAnalysisCSharpVersion>4.3.0-1.22206.2</MicrosoftCodeAnalysisCSharpVersion>
44+
<MicrosoftCodeAnalysisCSharpCodeStyleVersion>4.3.0-2.final</MicrosoftCodeAnalysisCSharpCodeStyleVersion>
45+
<MicrosoftCodeAnalysisCSharpWorkspacesVersion>4.3.0-2.final</MicrosoftCodeAnalysisCSharpWorkspacesVersion>
46+
<MicrosoftCodeAnalysisCSharpVersion>4.3.0-2.final</MicrosoftCodeAnalysisCSharpVersion>
4747
<MicrosoftCodeAnalysisNetAnalyzersVersion>7.0.0-preview1.22362.3</MicrosoftCodeAnalysisNetAnalyzersVersion>
48-
<MicrosoftCodeAnalysisVersion>4.3.0-1.22206.2</MicrosoftCodeAnalysisVersion>
48+
<MicrosoftCodeAnalysisVersion>4.3.0-2.final</MicrosoftCodeAnalysisVersion>
4949
<!--
5050
TODO: Remove pinned version once arcade supplies a compiler that enables the repo to compile.
5151
-->

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.cs

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.CodeAnalysis;
1313
using Microsoft.CodeAnalysis.CSharp;
1414
using Microsoft.CodeAnalysis.CSharp.Syntax;
15+
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;
1516
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1617

1718
[assembly: System.Resources.NeutralResourcesLanguage("en-US")]
@@ -61,19 +62,13 @@ public static class StepNames
6162
public void Initialize(IncrementalGeneratorInitializationContext context)
6263
{
6364
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)
7772
.Where(
7873
static modelData => modelData is not null);
7974

@@ -584,19 +579,6 @@ static ExpressionSyntax CreateEnumExpressionSyntax<T>(T value) where T : Enum
584579
}
585580
}
586581

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-
600582
private static Diagnostic? GetDiagnosticIfInvalidMethodForGeneration(MethodDeclarationSyntax methodSyntax, IMethodSymbol method)
601583
{
602584
// Verify the method has no generic types or defined implementation

src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,19 @@
3535
<ProjectReference Include="..\Microsoft.Interop.SourceGeneration\Microsoft.Interop.SourceGeneration.csproj" Pack="true" PackagePath="analyzers/dotnet/cs" />
3636
</ItemGroup>
3737

38+
<ItemGroup>
39+
<Compile Include="$(CommonPath)Roslyn\GetBestTypeByMetadataName.cs" Link="Common\Roslyn\GetBestTypeByMetadataName.cs" />
40+
<Compile Include="$(CommonPath)Roslyn\Hash.cs" Link="Common\Roslyn\Hash.cs" />
41+
<Compile Include="$(CommonPath)Roslyn\ISyntaxHelper.cs" Link="Common\Roslyn\ISyntaxHelper.cs" />
42+
<Compile Include="$(CommonPath)Roslyn\CSharpSyntaxHelper.cs" Link="Common\Roslyn\CSharpSyntaxHelper.cs" />
43+
<Compile Include="$(CommonPath)Roslyn\GlobalAliases.cs" Link="Common\Roslyn\GlobalAliases.cs" />
44+
<Compile Include="$(CommonPath)Roslyn\SyntaxNodeGrouping.cs" Link="Common\Roslyn\SyntaxNodeGrouping.cs" />
45+
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" Link="Common\Roslyn\SyntaxValueProvider.ImmutableArrayValueComparer.cs" />
46+
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithMetadataName.cs" />
47+
<Compile Include="$(CommonPath)Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" Link="Common\Roslyn\SyntaxValueProvider_ForAttributeWithSimpleName.cs" />
48+
49+
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.cs" Link="Production\ValueListBuilder.cs" />
50+
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ValueListBuilder.Pop.cs" Link="Production\ValueListBuilder.Pop.cs" />
51+
</ItemGroup>
52+
3853
</Project>

0 commit comments

Comments
 (0)