-
Notifications
You must be signed in to change notification settings - Fork 260
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
Support logging verification results in JSON #4951
Merged
keyboardDrummer
merged 11 commits into
dafny-lang:master
from
atomb:json-verification-logger
Jan 11, 2024
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7e04b42
First cut at JSON verification logger
atomb 97672e2
Rename TextLogger -> TextVerificationLogger
atomb 9cfcb57
Make fields readonly
atomb 35c22a3
Fix test
atomb 115b45d
Merge remote-tracking branch 'upstream/master' into json-verification…
atomb 106857d
Apply suggestion from @keyboardDrummer
atomb fd928f7
Change output ordering to reflect code change
atomb eda4c5a
Make proof dependency tests less fragile
atomb 6e8ed74
Add missing file
atomb da3adb0
Merge branch 'master' into json-verification-logger
atomb ae2d2bb
Fix expected coverage report output
atomb 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
There are no files selected for viewing
This file contains 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 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 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,106 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text.Json.Nodes; | ||
using DafnyCore.Verifier; | ||
using Microsoft.Boogie; | ||
using VC; | ||
|
||
namespace Microsoft.Dafny; | ||
|
||
public class JsonVerificationLogger { | ||
private TextWriter tw; | ||
private readonly TextWriter outWriter; | ||
private readonly ProofDependencyManager depManager; | ||
|
||
public JsonVerificationLogger(ProofDependencyManager depManager, TextWriter outWriter) { | ||
this.depManager = depManager; | ||
this.outWriter = outWriter; | ||
} | ||
|
||
public void Initialize(Dictionary<string, string> parameters) { | ||
tw = parameters.TryGetValue("LogFileName", out string filename) ? new StreamWriter(filename) : outWriter; | ||
} | ||
|
||
private static JsonNode SerializeAssertion(Microsoft.Boogie.IToken tok, string description) { | ||
return new JsonObject { | ||
["filename"] = tok.filename, | ||
["line"] = tok.line, | ||
["col"] = tok.col, | ||
["description"] = description | ||
}; | ||
} | ||
|
||
private JsonNode SerializeProofDependency(ProofDependency dependency) { | ||
return new JsonObject { | ||
["startFile"] = dependency.Range.StartToken.Filepath, | ||
["startLine"] = dependency.Range.StartToken.line, | ||
["startCol"] = dependency.Range.StartToken.col, | ||
["endFile"] = dependency.Range.EndToken.Filepath, | ||
["endLine"] = dependency.Range.EndToken.line, | ||
["endCol"] = dependency.Range.EndToken.col, | ||
["description"] = dependency.Description, | ||
["originalText"] = dependency.OriginalString() | ||
}; | ||
} | ||
|
||
private JsonNode SerializeVcResult(IEnumerable<ProofDependency> potentialDependencies, DafnyConsolePrinter.VCResultLogEntry vcResult) { | ||
var result = new JsonObject { | ||
["vcNum"] = vcResult.VCNum, | ||
["outcome"] = SerializeOutcome(vcResult.Outcome), | ||
["runTime"] = SerializeTimeSpan(vcResult.RunTime), | ||
["resourceCount"] = vcResult.ResourceCount, | ||
["assertions"] = new JsonArray(vcResult.Asserts.Select(x => SerializeAssertion(x.Tok, x.Description)).ToArray()), | ||
}; | ||
if (potentialDependencies is not null) { | ||
var fullDependencies = depManager.GetOrderedFullDependencies(vcResult.CoveredElements); | ||
var fullDependencySet = fullDependencies.ToHashSet(); | ||
var unusedDependencies = potentialDependencies.Where(dep => !fullDependencySet.Contains(dep)); | ||
result["coveredElements"] = new JsonArray(fullDependencies.Select(SerializeProofDependency).ToArray()); | ||
result["uncoveredElements"] = new JsonArray(unusedDependencies.Select(SerializeProofDependency).ToArray()); | ||
} | ||
return result; | ||
} | ||
|
||
private static JsonNode SerializeTimeSpan(TimeSpan timeSpan) { | ||
return timeSpan.ToString(); | ||
} | ||
|
||
private static JsonNode SerializeOutcome(ProverInterface.Outcome outcome) { | ||
return outcome.ToString(); | ||
} | ||
private static JsonNode SerializeOutcome(ConditionGeneration.Outcome outcome) { | ||
return outcome.ToString(); | ||
} | ||
|
||
private JsonNode SerializeVerificationResult(DafnyConsolePrinter.ConsoleLogEntry logEntry) { | ||
var (impl, verificationResult) = logEntry; | ||
var trackProofDependencies = | ||
verificationResult.Outcome == ConditionGeneration.Outcome.Correct && | ||
verificationResult.VCResults.Any(vcResult => vcResult.CoveredElements.Any()); | ||
var potentialDependencies = | ||
trackProofDependencies ? depManager.GetPotentialDependenciesForDefinition(impl.Name) : null; | ||
var result = new JsonObject { | ||
["name"] = impl.Name, | ||
["outcome"] = SerializeOutcome(verificationResult.Outcome), | ||
["runTime"] = SerializeTimeSpan(verificationResult.RunTime), | ||
["resourceCount"] = verificationResult.ResourceCount, | ||
["vcResults"] = new JsonArray(verificationResult.VCResults.Select(r => SerializeVcResult(potentialDependencies, r)).ToArray()) | ||
}; | ||
if (potentialDependencies is not null) { | ||
result["programElements"] = new JsonArray(potentialDependencies.Select(SerializeProofDependency).ToArray()); | ||
} | ||
return result; | ||
} | ||
|
||
private JsonObject SerializeVerificationResults(IEnumerable<DafnyConsolePrinter.ConsoleLogEntry> verificationResults) { | ||
return new JsonObject { | ||
["verificationResults"] = new JsonArray(verificationResults.Select(SerializeVerificationResult).ToArray()) | ||
}; | ||
} | ||
|
||
public void LogResults(IEnumerable<DafnyConsolePrinter.ConsoleLogEntry> verificationResults) { | ||
tw.Write(SerializeVerificationResults(verificationResults).ToJsonString()); | ||
} | ||
} |
This file contains 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 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
10 changes: 10 additions & 0 deletions
10
Source/IntegrationTests/TestFiles/LitTests/LitTest/logger/JsonLogger.dfy
This file contains 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,10 @@ | ||
// RUN: %exits-with 4 %baredafny verify --show-snippets:false --log-format:json --isolate-assertions "%s" > "%t" | ||
// RUN: %OutputCheck --file-to-check "%t" "%s" | ||
// CHECK: vcNum.:1,.outcome.:.Valid.*vcNum.:3,.outcome.:.Invalid | ||
method M(x: int, y: int) | ||
requires y > 0 | ||
requires x > 0 | ||
{ | ||
var d := x / y; | ||
assert(d * y == x); // Should fail | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find ordering by RangeString to be strange. Why not order on the unprinted object:
dep.Range.StartToken
ordep.Range
?Also, Instead of the tuple, you can also use the C# chaining ordering API,
OrderBy(dep => dep.Range.StartToken).ThenBy(dep => dep.Description)
, which I think is a little easier to read.