Repository navigation
feat: Compare generated code against reference files for tests. Provide metrics for some aspects of the generated code #2348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| namespace Spice86.Tests; | ||
|
|
||
| using System.Collections.Immutable; | ||
| using System.Linq; | ||
|
|
||
| /// <summary> | ||
| /// Readability metrics for a single generated-code fixture, computed from the Roslyn syntax tree and the | ||
| /// compilation diagnostics. Used by the code-generator quality harness to track how later phases change the | ||
| /// generated output. No assertions are made on these numbers yet; they run dark and are recorded to a file. | ||
| /// </summary> | ||
| internal sealed record GeneratedCodeMetrics { | ||
| public required string Fixture { get; init; } | ||
| public required int Methods { get; init; } | ||
| public required int Lines { get; init; } | ||
| public required int Labels { get; init; } | ||
| public required int GotoStatements { get; init; } | ||
| public required int DistinctGotoTargets { get; init; } | ||
| public required int CheckExternalEvents { get; init; } | ||
| public required int VerifySpeculativeEntryOrFail { get; init; } | ||
| public required int FailAsUntested { get; init; } | ||
| public required int UncheckedExpressions { get; init; } | ||
| public required int Casts { get; init; } | ||
| public required int TryStatements { get; init; } | ||
| public required int Warnings { get; init; } | ||
| public required ImmutableArray<string> WarningIds { get; init; } | ||
|
|
||
| // Fixed column widths so rows stay aligned regardless of the order fixtures are appended in. The | ||
| // last column (warning ids) is left unpadded because it is free text of varying length. | ||
| private const int FixtureWidth = 48; | ||
| private const int NumberWidth = 9; | ||
|
|
||
| private static readonly (string Header, int Width)[] Columns = [ | ||
| ("fixture", FixtureWidth), | ||
| ("methods", NumberWidth), | ||
| ("lines", NumberWidth), | ||
| ("labels", NumberWidth), | ||
| ("gotos", NumberWidth), | ||
| ("gotoTgts", NumberWidth), | ||
| ("extEvents", NumberWidth), | ||
| ("specGuard", NumberWidth), | ||
| ("untested", NumberWidth), | ||
| ("unchecked", NumberWidth), | ||
| ("casts", NumberWidth), | ||
| ("tries", NumberWidth), | ||
| ("warnings", NumberWidth), | ||
| ("warningIds", 0) | ||
| ]; | ||
|
|
||
| public static string Header() { | ||
| return string.Concat(Columns.Select(column => Pad(column.Header, column.Width))); | ||
| } | ||
|
|
||
| public string ToLine() { | ||
| string warningIds = WarningIds.IsDefaultOrEmpty ? "" : string.Join(",", WarningIds); | ||
| string[] values = [ | ||
| Fixture, | ||
| Methods.ToString(), | ||
| Lines.ToString(), | ||
| Labels.ToString(), | ||
| GotoStatements.ToString(), | ||
| DistinctGotoTargets.ToString(), | ||
| CheckExternalEvents.ToString(), | ||
| VerifySpeculativeEntryOrFail.ToString(), | ||
| FailAsUntested.ToString(), | ||
| UncheckedExpressions.ToString(), | ||
| Casts.ToString(), | ||
| TryStatements.ToString(), | ||
| Warnings.ToString(), | ||
| warningIds | ||
| ]; | ||
| return string.Concat(values.Select((value, index) => Pad(value, Columns[index].Width))); | ||
| } | ||
|
|
||
| private static string Pad(string value, int width) { | ||
| if (width == 0) { | ||
| return value; | ||
| } | ||
| // One trailing space guarantees a separator even when a value is wider than its column. | ||
| return value.PadRight(width - 1) + " "; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| namespace Spice86.Tests; | ||
|
|
||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
| using Microsoft.CodeAnalysis.Text; | ||
|
|
||
| using System.Collections.Immutable; | ||
|
|
||
| /// <summary> | ||
| /// Computes <see cref="GeneratedCodeMetrics"/> from a parsed syntax tree and compilation diagnostics. | ||
| /// Counting is done on the syntax tree rather than with text regexes so it stays robust to the formatting | ||
| /// changes later phases make to the generated output. | ||
| /// </summary> | ||
| internal static class GeneratedCodeMetricsCollector { | ||
| private const string CheckExternalEventsName = "CheckExternalEvents"; | ||
| private const string VerifySpeculativeEntryOrFailName = "VerifySpeculativeEntryOrFail"; | ||
| private const string FailAsUntestedName = "FailAsUntested"; | ||
|
|
||
| public static GeneratedCodeMetrics Collect(string fixture, SyntaxTree tree, ImmutableArray<Diagnostic> diagnostics) { | ||
| SyntaxNode root = tree.GetRoot(); | ||
| MetricsWalker walker = new(); | ||
| walker.Visit(root); | ||
|
|
||
| SourceText text = tree.GetText(); | ||
| int lines = text.Lines.Count; | ||
|
|
||
| ImmutableArray<Diagnostic> warnings = diagnostics | ||
| .Where(diagnostic => diagnostic.Severity == DiagnosticSeverity.Warning) | ||
| .ToImmutableArray(); | ||
| ImmutableArray<string> warningIds = warnings | ||
| .Select(diagnostic => diagnostic.Id) | ||
| .Distinct(StringComparer.Ordinal) | ||
| .OrderBy(id => id, StringComparer.Ordinal) | ||
| .ToImmutableArray(); | ||
|
|
||
| return new GeneratedCodeMetrics { | ||
| Fixture = fixture, | ||
| Methods = walker.Methods, | ||
| Lines = lines, | ||
| Labels = walker.Labels, | ||
| GotoStatements = walker.GotoStatements, | ||
| DistinctGotoTargets = walker.GotoTargets.Count, | ||
| CheckExternalEvents = walker.CheckExternalEvents, | ||
| VerifySpeculativeEntryOrFail = walker.VerifySpeculativeEntryOrFail, | ||
| FailAsUntested = walker.FailAsUntested, | ||
| UncheckedExpressions = walker.UncheckedExpressions, | ||
| Casts = walker.Casts, | ||
| TryStatements = walker.TryStatements, | ||
| Warnings = warnings.Length, | ||
| WarningIds = warningIds | ||
| }; | ||
| } | ||
|
|
||
| private sealed class MetricsWalker : CSharpSyntaxWalker { | ||
| public int Methods { get; private set; } | ||
| public int Labels { get; private set; } | ||
| public int GotoStatements { get; private set; } | ||
| public HashSet<string> GotoTargets { get; } = new(StringComparer.Ordinal); | ||
| public int CheckExternalEvents { get; private set; } | ||
| public int VerifySpeculativeEntryOrFail { get; private set; } | ||
| public int FailAsUntested { get; private set; } | ||
| public int UncheckedExpressions { get; private set; } | ||
| public int Casts { get; private set; } | ||
| public int TryStatements { get; private set; } | ||
|
|
||
| public override void VisitMethodDeclaration(MethodDeclarationSyntax node) { | ||
| Methods++; | ||
| base.VisitMethodDeclaration(node); | ||
| } | ||
|
|
||
| public override void VisitLabeledStatement(LabeledStatementSyntax node) { | ||
| Labels++; | ||
| base.VisitLabeledStatement(node); | ||
| } | ||
|
|
||
| public override void VisitGotoStatement(GotoStatementSyntax node) { | ||
| GotoStatements++; | ||
| if (node.Expression is IdentifierNameSyntax identifier) { | ||
| GotoTargets.Add(identifier.Identifier.ValueText); | ||
| } | ||
| base.VisitGotoStatement(node); | ||
| } | ||
|
|
||
| public override void VisitInvocationExpression(InvocationExpressionSyntax node) { | ||
| string? name = GetInvokedName(node.Expression); | ||
| if (name == CheckExternalEventsName) { | ||
| CheckExternalEvents++; | ||
| } else if (name == VerifySpeculativeEntryOrFailName) { | ||
| VerifySpeculativeEntryOrFail++; | ||
| } else if (name == FailAsUntestedName) { | ||
| FailAsUntested++; | ||
| } | ||
| base.VisitInvocationExpression(node); | ||
| } | ||
|
|
||
| public override void VisitCheckedExpression(CheckedExpressionSyntax node) { | ||
| if (node.Keyword.IsKind(SyntaxKind.UncheckedKeyword)) { | ||
| UncheckedExpressions++; | ||
| } | ||
| base.VisitCheckedExpression(node); | ||
| } | ||
|
|
||
| public override void VisitCastExpression(CastExpressionSyntax node) { | ||
| Casts++; | ||
| base.VisitCastExpression(node); | ||
| } | ||
|
|
||
| public override void VisitTryStatement(TryStatementSyntax node) { | ||
| TryStatements++; | ||
| base.VisitTryStatement(node); | ||
| } | ||
|
|
||
| private static string? GetInvokedName(ExpressionSyntax expression) { | ||
| if (expression is IdentifierNameSyntax identifier) { | ||
| return identifier.Identifier.ValueText; | ||
| } | ||
| if (expression is MemberAccessExpressionSyntax memberAccess) { | ||
| return memberAccess.Name.Identifier.ValueText; | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| namespace Spice86.Tests; | ||
|
|
||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.Emit; | ||
|
|
||
| /// <summary> | ||
| /// Result of parsing and emitting generated C# source: the parsed syntax tree, the emit result | ||
| /// (including diagnostics, which are always populated regardless of success), and the emitted PE stream. | ||
| /// </summary> | ||
| internal sealed class GeneratedCompilation { | ||
| public GeneratedCompilation(string source, SyntaxTree syntaxTree, EmitResult emitResult, MemoryStream peStream) { | ||
| Source = source; | ||
| SyntaxTree = syntaxTree; | ||
| EmitResult = emitResult; | ||
| PeStream = peStream; | ||
| } | ||
|
|
||
| public string Source { get; } | ||
| public SyntaxTree SyntaxTree { get; } | ||
| public EmitResult EmitResult { get; } | ||
| public MemoryStream PeStream { get; } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.