Skip to content
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

Clean up #763

Merged
merged 14 commits into from
Apr 10, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package de.fraunhofer.aisec.codyze.backends.cpg

import com.github.ajalt.clikt.parameters.options.*
import com.github.ajalt.clikt.parameters.types.*
import de.fraunhofer.aisec.codyze.backends.cpg.cli.BaseCpgBackend
import de.fraunhofer.aisec.codyze.backends.cpg.cli.CokoCpgBackend
import de.fraunhofer.aisec.codyze.backends.cpg.cli.BaseCpgBackendCommand
import de.fraunhofer.aisec.codyze.backends.cpg.cli.CokoCpgBackendCommand
import de.fraunhofer.aisec.codyze.core.backend.BackendOptions
import de.fraunhofer.aisec.codyze.core.config.combineSources
import de.fraunhofer.aisec.codyze.core.config.resolvePaths
Expand All @@ -30,7 +30,7 @@ import kotlin.reflect.full.isSuperclassOf

/**
* Holds the common CLI options for all CPG based Codyze backends.
* Used in e.g., [BaseCpgBackend] and [CokoCpgBackend].
* Used in e.g., [BaseCpgBackendCommand] and [CokoCpgBackendCommand].
*/
@Suppress("UNUSED")
class CPGOptionGroup : BackendOptions(helpName = "CPG Backend Options") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
/**
* The [CliktCommand] to add the plain cpg backend to the codyze-cli.
*/
class BaseCpgBackend : BackendCommand<CPGBackend>("cpg") {
class BaseCpgBackendCommand : BackendCommand<CPGBackend>("cpg") {
val backendOptions by CPGOptionGroup()

Check warning on line 28 in codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt

View check run for this annotation

Codecov / codecov/patch

codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt#L27-L28

Added lines #L27 - L28 were not covered by tests
override val backend = CPGBackend::class

override fun getBackend() = with(backendOptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import de.fraunhofer.aisec.codyze.specificationLanguages.coko.core.CokoBackend
/**
* The [CliktCommand] to add the cokoCpg backend to the codyze-cli.
*/
class CokoCpgBackend : BackendCommand<CokoBackend>("cokoCpg") {
class CokoCpgBackendCommand : BackendCommand<CokoBackend>("cokoCpg") {
val backendOptions by CPGOptionGroup()
override val backend = CokoBackend::class

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ context(CokoBackend)
fun Op.cpgGetAllNodes(): Nodes =
when (this@Op) {
is FunctionOp ->
[email protected].map { def -> [email protected](def.fqn) }.flatten()
[email protected].flatMap { def -> [email protected](def.fqn) }
is ConstructorOp -> [email protected](this.classFqn)
}

Expand All @@ -58,24 +58,22 @@ fun Op.cpgGetNodes(): Nodes =
when (this@Op) {
is FunctionOp ->
[email protected]
.map { def ->
.flatMap { def ->
[email protected](def.fqn) {
def.signatures.any { sig ->
cpgSignature(*sig.parameters.toTypedArray()) &&
sig.unorderedParameters.all { it?.cpgFlowsTo(arguments) ?: false }
}
}
}
.flatten()
is ConstructorOp ->
[email protected]
.map { sig ->
.flatMap { sig ->
[email protected]([email protected]) {
cpgSignature(*sig.parameters.toTypedArray()) &&
sig.unorderedParameters.all { it?.cpgFlowsTo(arguments) ?: false }
}
}
.flatten()
}

/** Returns a list of [ValueDeclaration]s with the matching name. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,12 @@ class OnlyEvaluator(val ops: List<Op>) : Evaluator {

override fun evaluate(context: EvaluationContext): List<CpgFinding> {
val correctNodes =
with(this@CokoCpgBackend) { ops.map { it.cpgGetNodes() } }
.flatten()
with(this@CokoCpgBackend) { ops.flatMap { it.cpgGetNodes() } }
.toSet()

val distinctOps = ops.toSet()
val allNodes =
with(this@CokoCpgBackend) { distinctOps.map { it.cpgGetAllNodes() } }
.flatten()
with(this@CokoCpgBackend) { distinctOps.flatMap { it.cpgGetAllNodes() } }
.toSet()

// `correctNodes` is a subset of `allNodes`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
package de.fraunhofer.aisec.codyze.cli

import de.fraunhofer.aisec.codyze.backends.cpg.cli.BaseCpgBackend
import de.fraunhofer.aisec.codyze.backends.cpg.cli.CokoCpgBackend
import de.fraunhofer.aisec.codyze.backends.cpg.cli.BaseCpgBackendCommand
import de.fraunhofer.aisec.codyze.backends.cpg.cli.CokoCpgBackendCommand
import de.fraunhofer.aisec.codyze.core.backend.Backend
import de.fraunhofer.aisec.codyze.core.backend.BackendCommand
import de.fraunhofer.aisec.codyze.core.executor.Executor
Expand All @@ -32,8 +32,8 @@ import org.koin.dsl.module
* Every [Backend] must provide [BackendCommand] to be selectable in the CLI.
*/
val backendCommands = module {
factoryOf(::BaseCpgBackend) bind(BackendCommand::class)
factoryOf(::CokoCpgBackend) bind(BackendCommand::class)
factoryOf(::BaseCpgBackendCommand) bind(BackendCommand::class)
factoryOf(::CokoCpgBackendCommand) bind(BackendCommand::class)
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ class FunctionOp internal constructor(

other as FunctionOp

if (definitions != other.definitions) return false

return true
return definitions == other.definitions
}

override fun hashCode(): Int {
Expand Down Expand Up @@ -100,9 +98,7 @@ class ConstructorOp internal constructor(
if (this === other) return true
if (other !is ConstructorOp) return false

if (signatures != other.signatures) return false

return true
return signatures == other.signatures
}

override fun hashCode(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class ParameterGroup {

other as ParameterGroup

if (parameters != other.parameters) return false

return true
return parameters == other.parameters
}

override fun hashCode(): Int {
Expand Down Expand Up @@ -74,9 +72,7 @@ class Definition(val fqn: String) {
other as Definition

if (fqn != other.fqn) return false
if (signatures != other.signatures) return false

return true
return signatures == other.signatures
}

override fun hashCode(): Int {
Expand Down Expand Up @@ -147,9 +143,7 @@ class Signature {
other as Signature

if (parameters != other.parameters) return false
if (unorderedParameters != other.unorderedParameters) return false

return true
return unorderedParameters == other.unorderedParameters
}

override fun hashCode(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ private fun CokoRule.toReportingDescriptor() = ReportingDescriptor(
defaultConfiguration = ReportingConfiguration(level = findAnnotation<Rule>()?.severity?.toResultLevel()),
help = findAnnotation<Rule>()?.help?.let { desc -> MultiformatMessageString(text = desc) },
properties = PropertyBag(
tags = findAnnotation<Rule>()?.tags?.toList()
tags = findAnnotation<Rule>()?.tags?.toList(),

)
// TODO: add precision, severity
)

class CokoSarifBuilder(val rules: List<CokoRule>, val backend: Backend) {
Expand Down