Skip to content

Commit

Permalink
Do not emit unused fields in RDG source
Browse files Browse the repository at this point in the history
Do not emit unused private fields for HTTP verbs that are not used by any of the user-code endpoints.
Resolves #48381.
  • Loading branch information
martincostello committed Jun 9, 2023
1 parent 6575b28 commit e4c1c32
Show file tree
Hide file tree
Showing 40 changed files with 60 additions and 181 deletions.
26 changes: 18 additions & 8 deletions src/Http/Http.Extensions/gen/RequestDelegateGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Analyzers.Infrastructure;
using Microsoft.AspNetCore.App.Analyzers.Infrastructure;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Operations;
using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel.Emitters;
using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel;
using Microsoft.AspNetCore.Http.RequestDelegateGenerator.StaticRouteHandlerModel.Emitters;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Operations;

namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator;

Expand Down Expand Up @@ -125,7 +126,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
.Collect()
.Select((endpoints, _) =>
{
var dedupedByDelegate = endpoints.Distinct<Endpoint>(EndpointDelegateComparer.Instance);
var dedupedByDelegate = endpoints.Distinct(EndpointDelegateComparer.Instance);
var verbs = new HashSet<string>();
using var stringWriter = new StringWriter(CultureInfo.InvariantCulture);
using var codeWriter = new CodeWriter(stringWriter, baseIndent: 2);
foreach (var endpoint in dedupedByDelegate)
Expand Down Expand Up @@ -161,15 +163,22 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
{
codeWriter.WriteLine($"{SymbolDisplay.FormatLiteral("{*path:nonfile}", true)},");
}
(var verbSymbol, var verbHttp) = endpoint.EmitVerb();
if (verbHttp is { })
{
verbs.Add(verbHttp);
}
codeWriter.WriteLine("handler,");
codeWriter.WriteLine($"{endpoint.EmitVerb()},");
codeWriter.WriteLine($"{verbSymbol},");
codeWriter.WriteLine("filePath,");
codeWriter.WriteLine("lineNumber);");
codeWriter.Indent--;
codeWriter.EndBlock();
}
return stringWriter.ToString();
return (stringWriter.ToString(), verbs);
});

var endpointHelpers = endpoints
Expand Down Expand Up @@ -283,7 +292,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

context.RegisterSourceOutput(thunksAndEndpoints, (context, sources) =>
{
var (((thunks, endpointsCode), helperMethods), helperTypes) = sources;
var (((thunks, (endpointsCode, verbs)), helperMethods), helperTypes) = sources;
if (thunks.IsDefaultOrEmpty || string.IsNullOrEmpty(endpointsCode))
{
Expand All @@ -301,7 +310,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
thunks: thunksCode.ToString(),
endpoints: endpointsCode,
helperMethods: helperMethods ?? string.Empty,
helperTypes: helperTypes ?? string.Empty);
helperTypes: helperTypes ?? string.Empty,
verbs: verbs);
context.AddSource("GeneratedRouteBuilderExtensions.g.cs", code);
});
Expand Down
42 changes: 33 additions & 9 deletions src/Http/Http.Extensions/gen/RequestDelegateGeneratorSources.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Text;
using Microsoft.CodeAnalysis.CSharp;

namespace Microsoft.AspNetCore.Http.RequestDelegateGenerator;

internal static class RequestDelegateGeneratorSources
Expand Down Expand Up @@ -500,7 +503,7 @@ public override bool IsDefined(Type attributeType, bool inherit)
}
""";
public static string GetGeneratedRouteBuilderExtensionsSource(string genericThunks, string thunks, string endpoints, string helperMethods, string helperTypes) => $$"""
public static string GetGeneratedRouteBuilderExtensionsSource(string genericThunks, string thunks, string endpoints, string helperMethods, string helperTypes, HashSet<string> verbs) => $$"""
{{SourceHeader}}
namespace Microsoft.AspNetCore.Builder
Expand All @@ -518,7 +521,7 @@ public SourceKey(string path, int line)
}
}
{{GetEndpoints(endpoints)}}
{{GetEndpoints(endpoints, verbs)}}
}
namespace Microsoft.AspNetCore.Http.Generated
Expand Down Expand Up @@ -641,7 +644,15 @@ internal static RouteHandlerBuilder MapCore(
}
""" : string.Empty;
private static string GetEndpoints(string endpoints) => endpoints != string.Empty ? $$"""
private static string GetEndpoints(string endpoints, HashSet<string> verbs)
{
if (endpoints == string.Empty)
{
return string.Empty;
}
var builder = new StringBuilder();
builder.Append($$"""
// This class needs to be internal so that the compiled application
// has access to the strongly-typed endpoint definitions that are
// generated by the compiler so that they will be favored by
Expand All @@ -650,13 +661,26 @@ private static string GetEndpoints(string endpoints) => endpoints != string.Empt
{{GeneratedCodeAttribute}}
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };
""");
foreach (string verb in verbs)
{
builder.AppendLine($$"""
private static readonly string[] {{verb}}Verb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.{{verb}} };
""");
}
if (verbs.Count > 0)
{
builder.AppendLine();
}
builder.Append($$"""
{{endpoints}}
}
""" : string.Empty;
""");
return builder.ToString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public static string EmitSourceKey(this Endpoint endpoint)
return $@"(@""{endpoint.Location.File}"", {endpoint.Location.LineNumber})";
}

public static string EmitVerb(this Endpoint endpoint)
public static (string VerbSymbol, string? VerbHttp) EmitVerb(this Endpoint endpoint)
{
return endpoint.HttpMethod switch
{
"MapGet" => "GetVerb",
"MapPut" => "PutVerb",
"MapPost" => "PostVerb",
"MapDelete" => "DeleteVerb",
"MapPatch" => "PatchVerb",
"MapMethods" => "httpMethods",
"Map" => "null",
"MapFallback" => "null",
"MapGet" => ("GetVerb", "Get"),
"MapPut" => ("PutVerb", "Put"),
"MapPost" => ("PostVerb", "Post"),
"MapDelete" => ("DeleteVerb", "Delete"),
"MapPatch" => ("PatchVerb", "Patch"),
"MapMethods" => ("httpMethods", null),
"Map" => ("null", null),
"MapFallback" => ("null", null),
_ => throw new ArgumentException($"Received unexpected HTTP method: {endpoint.HttpMethod}")
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ namespace Microsoft.AspNetCore.Builder
%GENERATEDCODEATTRIBUTE%
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapPost(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ namespace Microsoft.AspNetCore.Builder
internal static class GenerateRouteBuilderEndpoints
{
private static readonly string[] GetVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Get };
private static readonly string[] PostVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Post };
private static readonly string[] PutVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Put };
private static readonly string[] DeleteVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Delete };
private static readonly string[] PatchVerb = new[] { global::Microsoft.AspNetCore.Http.HttpMethods.Patch };

internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder MapGet(
this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints,
Expand Down
Loading

0 comments on commit e4c1c32

Please sign in to comment.