Skip to content

Commit

Permalink
Clean up (#763)
Browse files Browse the repository at this point in the history
Co-authored-by: Florian Wendland <[email protected]>
Co-authored-by: Robert Haimerl <[email protected]>
  • Loading branch information
3 people authored Apr 10, 2024
1 parent 446b6a3 commit 5002242
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 34 deletions.
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,7 +24,7 @@ import de.fraunhofer.aisec.codyze.core.backend.BackendCommand
/**
* 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()
override val backend = CPGBackend::class

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 ->
this@Op.definitions.map { def -> this@CokoBackend.cpgCallFqn(def.fqn) }.flatten()
this@Op.definitions.flatMap { def -> this@CokoBackend.cpgCallFqn(def.fqn) }
is ConstructorOp -> this@CokoBackend.cpgConstructor(this.classFqn)
}

Expand All @@ -58,24 +58,22 @@ fun Op.cpgGetNodes(): Nodes =
when (this@Op) {
is FunctionOp ->
this@Op.definitions
.map { def ->
.flatMap { def ->
this@CokoBackend.cpgCallFqn(def.fqn) {
def.signatures.any { sig ->
cpgSignature(*sig.parameters.toTypedArray()) &&
sig.unorderedParameters.all { it?.cpgFlowsTo(arguments) ?: false }
}
}
}
.flatten()
is ConstructorOp ->
this@Op.signatures
.map { sig ->
.flatMap { sig ->
this@CokoBackend.cpgConstructor(this@Op.classFqn) {
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 @@ -45,7 +45,6 @@ private fun CokoRule.toReportingDescriptor() = ReportingDescriptor(
map = emptyMap()
)
},
// TODO: add precision, severity
)

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

0 comments on commit 5002242

Please sign in to comment.