-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support logging verification results in JSON (#4951)
### Description Implement support for `dafny verify --log-format json` to log the information provided in the `text` format logger in JSON format. Fixes #4907 ### How has this been tested? `logger/JsonLogger.dfy` By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.
- Loading branch information
Showing
17 changed files
with
2,020 additions
and
1,738 deletions.
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
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.