Skip to content

Commit cd10609

Browse files
committed
Add sarif results minimization (#490)
1 parent d367407 commit cd10609

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,17 @@ data class SarifResult(
9999
val locations: List<SarifPhysicalLocationWrapper> = listOf(),
100100
val relatedLocations: List<SarifRelatedPhysicalLocationWrapper> = listOf(),
101101
val codeFlows: List<SarifCodeFlow> = listOf()
102-
)
102+
) {
103+
/**
104+
* Returns the total number of locations in all [codeFlows].
105+
*/
106+
fun totalCodeFlowLocations() =
107+
codeFlows.sumBy { codeFlow ->
108+
codeFlow.threadFlows.sumBy { threadFlow ->
109+
threadFlow.locations.size
110+
}
111+
}
112+
}
103113

104114
/**
105115
* The severity of the result. "Error" for detected unchecked exceptions.

utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,25 @@ class SarifReport(
9090
return Sarif.fromRun(
9191
SarifRun(
9292
SarifTool.fromRules(sarifRules.toList()),
93-
sarifResults
93+
minimizeResults(sarifResults)
9494
)
9595
)
9696
}
9797

98+
/**
99+
* Minimizes detected errors and removes duplicates.
100+
* Between two [SarifResult]s with the same `ruleId` and `locations`
101+
* it chooses the one with the shorter length of the execution trace.
102+
*/
103+
private fun minimizeResults(sarifResults: List<SarifResult>): List<SarifResult> =
104+
sarifResults.groupBy { sarifResult ->
105+
Pair(sarifResult.ruleId, sarifResult.locations)
106+
}.map { (_, sarifResultsGroup) ->
107+
sarifResultsGroup.minByOrNull { sarifResult ->
108+
sarifResult.totalCodeFlowLocations()
109+
}!!
110+
}
111+
98112
private fun processUncheckedException(
99113
method: UtMethod<*>,
100114
utExecution: UtExecution,

0 commit comments

Comments
 (0)