Skip to content
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

feat: Execution branch coverage report #4755

Merged
merged 17 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve encapsulation
  • Loading branch information
robin-aws committed Nov 8, 2023
commit e40be50f48843706f65290ec00c2d4b2245abf6d
4 changes: 4 additions & 0 deletions Source/DafnyCore/Compilers/CSharp/CsharpBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ private class CSharpCompilationResult {
return RunProcess(psi, outputWriter, errorWriter) == 0;
}

public override void PopulateCoverageReport(CoverageReport coverageReport) {
compiler.Coverage.PopulateCoverageReport(coverageReport);
}

public CsharpBackend(DafnyOptions options) : base(options) {
}
}
21 changes: 12 additions & 9 deletions Source/DafnyCore/Compilers/CoverageInstrumenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,15 +94,18 @@ public class CoverageInstrumenter {
}
}

public void PopulateCoverageReport(CoverageReport report) {
var tallies = File.ReadLines(talliesFilePath).Select(int.Parse).ToArray();
foreach (var ((token, _), tally) in legend.Zip(tallies)) {
var label = tally == 0 ? CoverageLabel.NotCovered : CoverageLabel.FullyCovered;
// For now we only identify branches at the line granularity,
// which matches what `dafny generate-tests ... --coverage-report` does as well.
var rangeToken = new RangeToken(new Token(token.line, 1), new Token(token.line + 1, 0));
rangeToken.Uri = token.Uri;
report.LabelCode(rangeToken, label);
public void PopulateCoverageReport(CoverageReport coverageReport) {
var coverageReportDir = compiler.Options?.Get(CommonOptionBag.ExecutionCoverageReport);
if (coverageReportDir != null) {
var tallies = File.ReadLines(talliesFilePath).Select(int.Parse).ToArray();
foreach (var ((token, _), tally) in legend.Zip(tallies)) {
var label = tally == 0 ? CoverageLabel.NotCovered : CoverageLabel.FullyCovered;
// For now we only identify branches at the line granularity,
// which matches what `dafny generate-tests ... --coverage-report` does as well.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice because then merging actual and expected coverage would work well. The merge-coverage-reports command could perhaps use a --diff flag that would produce a report indicating where two coverage reports differed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My thoughts exactly, and although I do suspect that the actual and expected coverage don't 100% agree on coverable things yet, I don't think it will take much to align them in the future.

var rangeToken = new RangeToken(new Token(token.line, 1), new Token(token.line + 1, 0));
rangeToken.Uri = token.Uri;
coverageReport.LabelCode(rangeToken, label);
}
}
}

Expand Down
4 changes: 0 additions & 4 deletions Source/DafnyCore/Compilers/ExecutableBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ public abstract class ExecutableBackend : IExecutableBackend {
Compiler.Coverage.WriteLegendFile();
}

public override CoverageInstrumenter GetCoverageAfterRun() {
return Compiler.Coverage;
}

protected abstract SinglePassCompiler CreateCompiler();

public override string PublicIdProtect(string name) {
Expand Down
6 changes: 4 additions & 2 deletions Source/DafnyCore/Plugins/IExecutableBackend.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,6 @@ public abstract class IExecutableBackend {
/// </summary>
public virtual void OnPostCompile() { }

public abstract CoverageInstrumenter GetCoverageAfterRun();

/// <summary>
/// Remove previously generated source files. This is only applicable to compilers that put sources in a separate
/// directory (e.g. Java). For other compilers, this method should do nothing.
Expand Down Expand Up @@ -200,4 +198,8 @@ public abstract class IExecutableBackend {
public virtual Command GetCommand() {
return new Command(TargetId, $"Translate Dafny sources to {TargetName} source and build files.");
}

public virtual void PopulateCoverageReport(CoverageReport coverageReport) {
throw new NotImplementedException();
}
}
7 changes: 1 addition & 6 deletions Source/DafnyDriver/CompilerDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,8 @@ private record TargetPaths(string Directory, string Filename) {
if (compiledCorrectly) {
var coverageReportDir = options.Get(CommonOptionBag.ExecutionCoverageReport);
if (coverageReportDir != null) {
var coverage = compiler.GetCoverageAfterRun();
var coverageReport = new CoverageReport("Execution Coverage", "Branches", "_tests_actual", dafnyProgram);
coverage.PopulateCoverageReport(coverageReport);
compiler.PopulateCoverageReport(coverageReport);
new CoverageReporter(options).SerializeCoverageReports(coverageReport, coverageReportDir);
}
}
Expand Down Expand Up @@ -630,10 +629,6 @@ class NoExecutableBackend : IExecutableBackend {
throw new NotSupportedException();
}

public override CoverageInstrumenter GetCoverageAfterRun() {
throw new NotImplementedException();
}

public NoExecutableBackend([NotNull] DafnyOptions options) : base(options) {
}
}
Expand Down
Loading