From 20e8d017620636af54dc5714280415d8d230bfc7 Mon Sep 17 00:00:00 2001 From: Selina Lin Date: Wed, 10 Apr 2024 12:48:43 +0200 Subject: [PATCH] Clean up (#763) Co-authored-by: Florian Wendland Co-authored-by: Robert Haimerl --- .../aisec/codyze/backends/cpg/CPGOptionGroup.kt | 6 +++--- .../{BaseCpgBackend.kt => BaseCpgBackendCommand.kt} | 2 +- .../{CokoCpgBackend.kt => CokoCpgBackendCommand.kt} | 2 +- .../backends/cpg/coko/dsl/ImplementationDsl.kt | 8 +++----- .../backends/cpg/coko/evaluators/OnlyEvaluator.kt | 6 ++---- .../de/fraunhofer/aisec/codyze/cli/KoinModules.kt | 8 ++++---- .../specificationLanguages/coko/core/dsl/Op.kt | 8 ++------ .../coko/core/modelling/OpComponents.kt | 12 +++--------- .../coko/dsl/CokoSarifBuilder.kt | 1 - 9 files changed, 19 insertions(+), 34 deletions(-) rename codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/{BaseCpgBackend.kt => BaseCpgBackendCommand.kt} (96%) rename codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/{CokoCpgBackend.kt => CokoCpgBackendCommand.kt} (96%) diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGOptionGroup.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGOptionGroup.kt index c77aa418b..5225dd76f 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGOptionGroup.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGOptionGroup.kt @@ -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 @@ -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") { diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackend.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt similarity index 96% rename from codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackend.kt rename to codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt index c082e65e0..0427389e1 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackend.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt @@ -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("cpg") { +class BaseCpgBackendCommand : BackendCommand("cpg") { val backendOptions by CPGOptionGroup() override val backend = CPGBackend::class diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackend.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt similarity index 96% rename from codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackend.kt rename to codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt index b86ea74f6..d28262cba 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackend.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt @@ -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("cokoCpg") { +class CokoCpgBackendCommand : BackendCommand("cokoCpg") { val backendOptions by CPGOptionGroup() override val backend = CokoBackend::class diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/ImplementationDsl.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/ImplementationDsl.kt index e67d37584..8423a9758 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/ImplementationDsl.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/dsl/ImplementationDsl.kt @@ -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) } @@ -58,7 +58,7 @@ 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()) && @@ -66,16 +66,14 @@ fun Op.cpgGetNodes(): Nodes = } } } - .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. */ diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/evaluators/OnlyEvaluator.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/evaluators/OnlyEvaluator.kt index 6d6f33c95..bec29b8ec 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/evaluators/OnlyEvaluator.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/coko/evaluators/OnlyEvaluator.kt @@ -40,14 +40,12 @@ class OnlyEvaluator(val ops: List) : Evaluator { override fun evaluate(context: EvaluationContext): List { 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` diff --git a/codyze-cli/src/main/kotlin/de/fraunhofer/aisec/codyze/cli/KoinModules.kt b/codyze-cli/src/main/kotlin/de/fraunhofer/aisec/codyze/cli/KoinModules.kt index c455e943e..dd72e0152 100644 --- a/codyze-cli/src/main/kotlin/de/fraunhofer/aisec/codyze/cli/KoinModules.kt +++ b/codyze-cli/src/main/kotlin/de/fraunhofer/aisec/codyze/cli/KoinModules.kt @@ -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 @@ -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) } /** diff --git a/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/dsl/Op.kt b/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/dsl/Op.kt index bef09a401..8be75c30f 100644 --- a/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/dsl/Op.kt +++ b/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/dsl/Op.kt @@ -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 { @@ -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 { diff --git a/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/OpComponents.kt b/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/OpComponents.kt index 38a8f41b9..8e91964a7 100644 --- a/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/OpComponents.kt +++ b/codyze-specification-languages/coko/coko-core/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/core/modelling/OpComponents.kt @@ -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 { @@ -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 { @@ -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 { diff --git a/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoSarifBuilder.kt b/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoSarifBuilder.kt index a26cd24b4..668f82518 100644 --- a/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoSarifBuilder.kt +++ b/codyze-specification-languages/coko/coko-dsl/src/main/kotlin/de/fraunhofer/aisec/codyze/specificationLanguages/coko/dsl/CokoSarifBuilder.kt @@ -45,7 +45,6 @@ private fun CokoRule.toReportingDescriptor() = ReportingDescriptor( map = emptyMap() ) }, - // TODO: add precision, severity ) class CokoSarifBuilder(val rules: List, val backend: Backend) {