Skip to content

Commit

Permalink
Improve incrementalism by doing less work when not applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
chsienki authored and eerhardt committed Aug 24, 2021
1 parent 92da59a commit bfa08f6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
33 changes: 32 additions & 1 deletion src/libraries/System.Text.Json/gen/JsonSourceGenerator.Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private sealed class Parser
private readonly Type? _dictionaryType;
private readonly Type? _idictionaryOfTKeyTValueType;
private readonly Type? _ireadonlyDictionaryType;
private readonly Type? _isetType;
private readonly Type? _isetType;
private readonly Type? _stackOfTType;
private readonly Type? _queueOfTType;
private readonly Type? _concurrentStackType;
Expand Down Expand Up @@ -403,6 +403,37 @@ 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? IsSemanticTargetForGeneration(GeneratorSyntaxContext context)
{
var classDeclarationSyntax = (ClassDeclarationSyntax)context.Node;

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

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

if (fullName == "System.Text.Json.Serialization.JsonSerializableAttribute"
|| fullName == "System.Text.Json.Serialization.JsonSourceGenerationOptionsAttribute")
{
return classDeclarationSyntax;
}
}

}

return null;
}

private static JsonSourceGenerationMode? GetJsonSourceGenerationModeEnumVal(SyntaxNode propertyValueMode)
{
IEnumerable<string> enumTokens = propertyValueMode
Expand Down
7 changes: 5 additions & 2 deletions src/libraries/System.Text.Json/gen/JsonSourceGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

//define LAUNCH_DEBUGGER
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
Expand All @@ -23,10 +24,12 @@ public sealed partial class JsonSourceGenerator : IIncrementalGenerator
private const string IJsonOnSerializedFullName = "System.Text.Json.Serialization.IJsonOnSerialized";
private const string IJsonOnSerializingFullName = "System.Text.Json.Serialization.IJsonOnSerializing";


public void Initialize(IncrementalGeneratorInitializationContext context)
{
var classDeclarations = context.SyntaxProvider.CreateSyntaxProvider((s, _) => s is ClassDeclarationSyntax, (s, _) => (ClassDeclarationSyntax)s.Node);

var classDeclarations = context.SyntaxProvider
.CreateSyntaxProvider((s, _) => Parser.IsSyntaxTargetForGeneration(s), (s, _) => Parser.IsSemanticTargetForGeneration(s))
.Where(c => c is object);

var compilationAndClasses = context.CompilationProvider.Combine(classDeclarations.Collect());

Expand Down

0 comments on commit bfa08f6

Please sign in to comment.