Skip to content

Add constraints to the start line field in SARIF #1762

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

Merged
merged 4 commits into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 11 additions & 2 deletions utbot-framework/src/main/kotlin/org/utbot/sarif/DataClasses.kt
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,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,
Expand All @@ -214,13 +216,20 @@ 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 = 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)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.utbot.sarif

import mu.KotlinLogging
import org.utbot.common.PathUtil.fileExtension
import org.utbot.common.PathUtil.toPath
import org.utbot.framework.UtSettings
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.
Expand Down Expand Up @@ -328,7 +331,7 @@ class SarifReport(
message = Message("$classFqn.$methodName($sourceFileName:$lineNumber)"),
physicalLocation = SarifPhysicalLocation(
SarifArtifact(uri = sourceFilePath),
SarifRegion(startLine = lineNumber)
SarifRegion(startLine = lineNumber) // lineNumber is one-based
)
))
}
Expand Down Expand Up @@ -440,15 +443,15 @@ class SarifReport(
val lastCoveredInstruction = coveredInstructions?.lastOrNull()
if (lastCoveredInstruction != null)
return Pair(
lastCoveredInstruction.lineNumber,
lastCoveredInstruction.lineNumber, // .lineNumber is one-based
lastCoveredInstruction.className.replace('/', '.')
)

// if for some reason we can't extract the last line from the coverage
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
}
Expand Down