From 74483f8572e9995e514e42135c9231c975b53e5c Mon Sep 17 00:00:00 2001 From: Nikita Stroganov Date: Tue, 31 Jan 2023 13:20:47 +0300 Subject: [PATCH 1/2] Add hard constraints to the start line field in sarif report --- .../src/main/kotlin/org/utbot/sarif/DataClasses.kt | 9 +++++++-- .../src/main/kotlin/org/utbot/sarif/SarifReport.kt | 6 +++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt b/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt index e71294914d..6ec59fec61 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt @@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue +import kotlin.math.max /** * Useful links: @@ -203,7 +204,9 @@ data class SarifArtifact( val uriBaseId: String = "%SRCROOT%" ) -// all fields should be one-based +/** + * All fields should be one-based. + */ data class SarifRegion( val startLine: Int, val endLine: Int? = null, @@ -214,13 +217,15 @@ data class SarifRegion( /** * Makes [startColumn] the first non-whitespace character in [startLine] in the [text]. * If the [text] contains less than [startLine] lines, [startColumn] == null. + * @param startLine should be one-based */ fun withStartLine(text: String, startLine: Int): SarifRegion { val neededLine = text.split('\n').getOrNull(startLine - 1) // to zero-based val startColumn = neededLine?.run { takeWhile { it.toString().isBlank() }.length + 1 // to one-based } - return SarifRegion(startLine = startLine, startColumn = startColumn) + val safeStartLine = max(1, startLine) // we don't want to fail if for some reason startLine < 1 + return SarifRegion(startLine = safeStartLine, startColumn = startColumn) } } } diff --git a/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt b/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt index 94de6244cc..1295c28cd1 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt @@ -328,7 +328,7 @@ class SarifReport( message = Message("$classFqn.$methodName($sourceFileName:$lineNumber)"), physicalLocation = SarifPhysicalLocation( SarifArtifact(uri = sourceFilePath), - SarifRegion(startLine = lineNumber) + SarifRegion(startLine = lineNumber) // lineNumber is one-based ) )) } @@ -440,7 +440,7 @@ class SarifReport( val lastCoveredInstruction = coveredInstructions?.lastOrNull() if (lastCoveredInstruction != null) return Pair( - lastCoveredInstruction.lineNumber, + lastCoveredInstruction.lineNumber, // .lineNumber is one-based lastCoveredInstruction.className.replace('/', '.') ) @@ -448,7 +448,7 @@ class SarifReport( val lastPathElementLineNumber = try { // path/fullPath might be empty when engine executes in another process - // soot entities cannot be passed to the main process because kryo cannot deserialize them - (utExecution as? UtSymbolicExecution)?.path?.lastOrNull()?.stmt?.javaSourceStartLineNumber + (utExecution as? UtSymbolicExecution)?.path?.lastOrNull()?.stmt?.javaSourceStartLineNumber // one-based } catch (t: Throwable) { null } From 270272188c04d08b6c61070fded6f8396d6ef7fd Mon Sep 17 00:00:00 2001 From: Nikita Stroganov Date: Tue, 31 Jan 2023 15:19:19 +0300 Subject: [PATCH 2/2] Add information to the log --- .../src/main/kotlin/org/utbot/sarif/DataClasses.kt | 8 ++++++-- .../src/main/kotlin/org/utbot/sarif/SarifReport.kt | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt b/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt index 6ec59fec61..01ed9ca331 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt @@ -8,7 +8,6 @@ import com.fasterxml.jackson.databind.JsonNode import com.fasterxml.jackson.databind.module.SimpleModule import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper import com.fasterxml.jackson.module.kotlin.readValue -import kotlin.math.max /** * Useful links: @@ -224,7 +223,12 @@ data class SarifRegion( val startColumn = neededLine?.run { takeWhile { it.toString().isBlank() }.length + 1 // to one-based } - val safeStartLine = max(1, startLine) // we don't want to fail if for some reason startLine < 1 + val safeStartLine = if (startLine < 1) { + logger.warn { "For some reason startLine < 1, so now it is equal to 1" } + 1 // we don't want to fail, so just set the line number to 1 + } else { + startLine + } return SarifRegion(startLine = safeStartLine, startColumn = startColumn) } } diff --git a/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt b/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt index 1295c28cd1..1df35ccd1e 100644 --- a/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt +++ b/utbot-framework/src/main/kotlin/org/utbot/sarif/SarifReport.kt @@ -1,5 +1,6 @@ package org.utbot.sarif +import mu.KotlinLogging import org.utbot.common.PathUtil.fileExtension import org.utbot.common.PathUtil.toPath import org.utbot.framework.UtSettings @@ -7,6 +8,8 @@ import org.utbot.framework.plugin.api.* import java.nio.file.Path import kotlin.io.path.nameWithoutExtension +internal val logger = KotlinLogging.logger {} + /** * Used for the SARIF report creation by given test cases and generated tests code. * SARIF is a JSON-based format for presenting static analyzer results.