From 2799085052d142c58aae3b011d4be00442c0d036 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 5 Jul 2022 10:52:29 -0700 Subject: [PATCH 1/5] Polyfill the incremental generator ForAttributeWithMetadataName from roslyn (for JsonGenerator). --- .../gen/JsonSourceGenerator.Parser.cs | 35 ++---------------- .../gen/JsonSourceGenerator.Roslyn3.11.cs | 36 +++++++++++++++++-- .../gen/JsonSourceGenerator.Roslyn4.0.cs | 8 +++-- ...ext.Json.SourceGeneration.Roslyn4.0.csproj | 14 ++++++++ .../Generic/ValueListBuilder.Pop.cs | 20 +++++++++++ 5 files changed, 76 insertions(+), 37 deletions(-) create mode 100644 src/libraries/System.Text.Json/src/System/Collections/Generic/ValueListBuilder.Pop.cs diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs index bd5686edbb9549..5c58ec985cee01 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs @@ -41,9 +41,10 @@ private sealed class Parser private const string JsonPropertyNameAttributeFullName = "System.Text.Json.Serialization.JsonPropertyNameAttribute"; private const string JsonPropertyOrderAttributeFullName = "System.Text.Json.Serialization.JsonPropertyOrderAttribute"; private const string JsonSerializerContextFullName = "System.Text.Json.Serialization.JsonSerializerContext"; - private const string JsonSerializableAttributeFullName = "System.Text.Json.Serialization.JsonSerializableAttribute"; private const string JsonSourceGenerationOptionsAttributeFullName = "System.Text.Json.Serialization.JsonSourceGenerationOptionsAttribute"; + internal const string JsonSerializableAttributeFullName = "System.Text.Json.Serialization.JsonSerializableAttribute"; + private const string DateOnlyFullName = "System.DateOnly"; private const string TimeOnlyFullName = "System.TimeOnly"; private const string IAsyncEnumerableFullName = "System.Collections.Generic.IAsyncEnumerable`1"; @@ -550,38 +551,6 @@ private static bool TryGetClassDeclarationList(INamedTypeSymbol typeSymbol, [Not return typeGenerationSpec; } - internal static bool IsSyntaxTargetForGeneration(SyntaxNode node) => node is ClassDeclarationSyntax { AttributeLists.Count: > 0, BaseList.Types.Count: > 0 }; - - internal static ClassDeclarationSyntax? GetSemanticTargetForGeneration(GeneratorSyntaxContext context, CancellationToken cancellationToken) - { - var classDeclarationSyntax = (ClassDeclarationSyntax)context.Node; - - foreach (AttributeListSyntax attributeListSyntax in classDeclarationSyntax.AttributeLists) - { - foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes) - { - cancellationToken.ThrowIfCancellationRequested(); - - IMethodSymbol attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax, cancellationToken).Symbol as IMethodSymbol; - if (attributeSymbol == null) - { - continue; - } - - INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType; - string fullName = attributeContainingTypeSymbol.ToDisplayString(); - - if (fullName == JsonSerializableAttributeFullName) - { - return classDeclarationSyntax; - } - } - - } - - return null; - } - private static JsonSourceGenerationMode? GetJsonSourceGenerationModeEnumVal(SyntaxNode propertyValueMode) { IEnumerable enumTokens = propertyValueMode diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn3.11.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn3.11.cs index 68228029cf1846..e64735d1d959e0 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn3.11.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn3.11.cs @@ -78,15 +78,47 @@ public SyntaxContextReceiver(CancellationToken cancellationToken) public void OnVisitSyntaxNode(GeneratorSyntaxContext context) { - if (Parser.IsSyntaxTargetForGeneration(context.Node)) + if (IsSyntaxTargetForGeneration(context.Node)) { - ClassDeclarationSyntax classSyntax = Parser.GetSemanticTargetForGeneration(context, _cancellationToken); + ClassDeclarationSyntax classSyntax = GetSemanticTargetForGeneration(context, _cancellationToken); if (classSyntax != null) { (ClassDeclarationSyntaxList ??= new List()).Add(classSyntax); } } } + + private static bool IsSyntaxTargetForGeneration(SyntaxNode node) => node is ClassDeclarationSyntax { AttributeLists.Count: > 0, BaseList.Types.Count: > 0 }; + + private static ClassDeclarationSyntax? GetSemanticTargetForGeneration(GeneratorSyntaxContext context, CancellationToken cancellationToken) + { + var classDeclarationSyntax = (ClassDeclarationSyntax)context.Node; + + foreach (AttributeListSyntax attributeListSyntax in classDeclarationSyntax.AttributeLists) + { + foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes) + { + cancellationToken.ThrowIfCancellationRequested(); + + IMethodSymbol attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeSyntax, cancellationToken).Symbol as IMethodSymbol; + if (attributeSymbol == null) + { + continue; + } + + INamedTypeSymbol attributeContainingTypeSymbol = attributeSymbol.ContainingType; + string fullName = attributeContainingTypeSymbol.ToDisplayString(); + + if (fullName == Parser.JsonSerializableAttributeFullName) + { + return classDeclarationSyntax; + } + } + + } + + return null; + } } /// diff --git a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn4.0.cs b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn4.0.cs index 765e3adb9e30de..8f4cd15e2dfb9d 100644 --- a/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn4.0.cs +++ b/src/libraries/System.Text.Json/gen/JsonSourceGenerator.Roslyn4.0.cs @@ -13,6 +13,7 @@ using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; using Microsoft.CodeAnalysis.Text; +using Microsoft.CodeAnalysis.DotnetRuntime.Extensions; namespace System.Text.Json.SourceGeneration { @@ -25,8 +26,11 @@ public sealed partial class JsonSourceGenerator : IIncrementalGenerator public void Initialize(IncrementalGeneratorInitializationContext context) { IncrementalValuesProvider classDeclarations = context.SyntaxProvider - .CreateSyntaxProvider(static (s, _) => Parser.IsSyntaxTargetForGeneration(s), static (s, c) => Parser.GetSemanticTargetForGeneration(s, c)) - .Where(static c => c is not null); + .ForAttributeWithMetadataName( + context, + Parser.JsonSerializableAttributeFullName, + (node, _) => node is ClassDeclarationSyntax, + (context, _) => (ClassDeclarationSyntax)context.TargetNode); IncrementalValueProvider<(Compilation, ImmutableArray)> compilationAndClasses = context.CompilationProvider.Combine(classDeclarations.Collect()); diff --git a/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj b/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj index 5e50e1e72e1d90..032bdcbb2e5c72 100644 --- a/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj +++ b/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj @@ -12,4 +12,18 @@ + + + + + + + + + + + + + + diff --git a/src/libraries/System.Text.Json/src/System/Collections/Generic/ValueListBuilder.Pop.cs b/src/libraries/System.Text.Json/src/System/Collections/Generic/ValueListBuilder.Pop.cs new file mode 100644 index 00000000000000..c5aa6bbff3a6db --- /dev/null +++ b/src/libraries/System.Text.Json/src/System/Collections/Generic/ValueListBuilder.Pop.cs @@ -0,0 +1,20 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Runtime.CompilerServices; + +namespace System.Collections.Generic +{ + /// + /// These public methods are required by RegexWriter. + /// + internal ref partial struct ValueListBuilder + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public T Pop() + { + _pos--; + return _span[_pos]; + } + } +} From a2a96581675c5341f09938690e314e8fe89a4ad1 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 5 Jul 2022 11:06:10 -0700 Subject: [PATCH 2/5] Move common code to shared location --- .../Generic/ValueListBuilder.Pop.cs | 0 .../LibraryImportGenerator.csproj | 38 ------------------- ...m.Text.RegularExpressions.Generator.csproj | 2 +- .../Generic/ValueListBuilder.Pop.cs | 20 ---------- 4 files changed, 1 insertion(+), 59 deletions(-) rename src/libraries/{System.Text.Json => System.Private.CoreLib}/src/System/Collections/Generic/ValueListBuilder.Pop.cs (100%) delete mode 100644 src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj delete mode 100644 src/libraries/System.Text.RegularExpressions/src/System/Collections/Generic/ValueListBuilder.Pop.cs diff --git a/src/libraries/System.Text.Json/src/System/Collections/Generic/ValueListBuilder.Pop.cs b/src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.Pop.cs similarity index 100% rename from src/libraries/System.Text.Json/src/System/Collections/Generic/ValueListBuilder.Pop.cs rename to src/libraries/System.Private.CoreLib/src/System/Collections/Generic/ValueListBuilder.Pop.cs diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj deleted file mode 100644 index 5806bf43f581ca..00000000000000 --- a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj +++ /dev/null @@ -1,38 +0,0 @@ - - - - Microsoft.Interop.LibraryImportGenerator - netstandard2.0 - true - Microsoft.Interop - true - - RS2008;$(NoWarn) - cs - - - - false - false - true - https://github.com/dotnet/runtime/tree/main/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator - LibraryImportGenerator - LibraryImportGenerator, analyzers - - - - - - - - - - - - - - - - - diff --git a/src/libraries/System.Text.RegularExpressions/gen/System.Text.RegularExpressions.Generator.csproj b/src/libraries/System.Text.RegularExpressions/gen/System.Text.RegularExpressions.Generator.csproj index ff5a56cee85696..bf14a855c9014e 100644 --- a/src/libraries/System.Text.RegularExpressions/gen/System.Text.RegularExpressions.Generator.csproj +++ b/src/libraries/System.Text.RegularExpressions/gen/System.Text.RegularExpressions.Generator.csproj @@ -34,7 +34,7 @@ - + diff --git a/src/libraries/System.Text.RegularExpressions/src/System/Collections/Generic/ValueListBuilder.Pop.cs b/src/libraries/System.Text.RegularExpressions/src/System/Collections/Generic/ValueListBuilder.Pop.cs deleted file mode 100644 index c5aa6bbff3a6db..00000000000000 --- a/src/libraries/System.Text.RegularExpressions/src/System/Collections/Generic/ValueListBuilder.Pop.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.CompilerServices; - -namespace System.Collections.Generic -{ - /// - /// These public methods are required by RegexWriter. - /// - internal ref partial struct ValueListBuilder - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public T Pop() - { - _pos--; - return _span[_pos]; - } - } -} From e8acee5c7f52e505e4cc6984db5387b6c6284d31 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Tue, 5 Jul 2022 11:43:12 -0700 Subject: [PATCH 3/5] Update projects --- .../src/System.Text.RegularExpressions.csproj | 2 +- .../UnitTests/System.Text.RegularExpressions.Unit.Tests.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Text.RegularExpressions/src/System.Text.RegularExpressions.csproj b/src/libraries/System.Text.RegularExpressions/src/System.Text.RegularExpressions.csproj index 952ab67d9f9c92..56a71e7e8126f6 100644 --- a/src/libraries/System.Text.RegularExpressions/src/System.Text.RegularExpressions.csproj +++ b/src/libraries/System.Text.RegularExpressions/src/System.Text.RegularExpressions.csproj @@ -5,7 +5,6 @@ - @@ -91,6 +90,7 @@ + diff --git a/src/libraries/System.Text.RegularExpressions/tests/UnitTests/System.Text.RegularExpressions.Unit.Tests.csproj b/src/libraries/System.Text.RegularExpressions/tests/UnitTests/System.Text.RegularExpressions.Unit.Tests.csproj index eb35b0bcd294a2..62948390630d38 100644 --- a/src/libraries/System.Text.RegularExpressions/tests/UnitTests/System.Text.RegularExpressions.Unit.Tests.csproj +++ b/src/libraries/System.Text.RegularExpressions/tests/UnitTests/System.Text.RegularExpressions.Unit.Tests.csproj @@ -24,7 +24,7 @@ - + From c21298921211a2a3f9bc8f2554c60a0a4dcb0349 Mon Sep 17 00:00:00 2001 From: Cyrus Najmabadi Date: Wed, 6 Jul 2022 08:01:24 -0700 Subject: [PATCH 4/5] Add back --- .../LibraryImportGenerator.csproj | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj diff --git a/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj new file mode 100644 index 00000000000000..5806bf43f581ca --- /dev/null +++ b/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator/LibraryImportGenerator.csproj @@ -0,0 +1,38 @@ + + + + Microsoft.Interop.LibraryImportGenerator + netstandard2.0 + true + Microsoft.Interop + true + + RS2008;$(NoWarn) + cs + + + + false + false + true + https://github.com/dotnet/runtime/tree/main/src/libraries/System.Runtime.InteropServices/gen/LibraryImportGenerator + LibraryImportGenerator + LibraryImportGenerator, analyzers + + + + + + + + + + + + + + + + + From 2b5a5a15875824a934a82584d379a15bca85a087 Mon Sep 17 00:00:00 2001 From: CyrusNajmabadi Date: Wed, 6 Jul 2022 08:01:57 -0700 Subject: [PATCH 5/5] Update src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj --- .../gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj b/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj index 032bdcbb2e5c72..638e4a6e4b5373 100644 --- a/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj +++ b/src/libraries/System.Text.Json/gen/System.Text.Json.SourceGeneration.Roslyn4.0.csproj @@ -23,7 +23,7 @@ - +