From ee09cd814b38886e4eabef6c2415642207850ab1 Mon Sep 17 00:00:00 2001 From: "Wendland, Florian" Date: Fri, 13 Sep 2024 15:04:30 +0200 Subject: [PATCH] Add compilation database options from CPG as CLI option --- .../aisec/codyze/backends/cpg/CPGBackend.kt | 7 +++++++ .../aisec/codyze/backends/cpg/CPGConfiguration.kt | 2 ++ .../aisec/codyze/backends/cpg/CPGOptionGroup.kt | 13 +++++++++++++ .../backends/cpg/cli/BaseCpgBackendCommand.kt | 2 ++ .../backends/cpg/cli/CokoCpgBackendCommand.kt | 2 ++ 5 files changed, 26 insertions(+) diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGBackend.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGBackend.kt index 8f37c367b..3eeaafb3c 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGBackend.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGBackend.kt @@ -23,6 +23,7 @@ import de.fraunhofer.aisec.codyze.core.backend.BackendConfiguration import de.fraunhofer.aisec.cpg.TranslationConfiguration import de.fraunhofer.aisec.cpg.TranslationManager import de.fraunhofer.aisec.cpg.TranslationResult +import de.fraunhofer.aisec.cpg.frontends.CompilationDatabase import io.github.detekt.sarif4k.Artifact import io.github.detekt.sarif4k.ArtifactLocation import io.github.detekt.sarif4k.ToolComponent @@ -72,6 +73,12 @@ open class CPGBackend(config: BackendConfiguration) : Backend { .useUnityBuild(useUnityBuild) .processAnnotations(processAnnotations) + compilationDatabase?.let { + translationConfiguration.useCompilationDatabase( + CompilationDatabase.fromFile(it.toFile(), filterCompilationDatabase) + ) + } + // TODO: very hacky, but needed for the Go frontend source.firstOrNull()?.parent?.toFile().let { translationConfiguration.topLevel(it) } diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGConfiguration.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGConfiguration.kt index d7780e252..7220755bb 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGConfiguration.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGConfiguration.kt @@ -49,6 +49,8 @@ data class CPGConfiguration( val includePaths: List, val includeAllowlist: List, val includeBlocklist: List, + val compilationDatabase: Path? = null, + val filterCompilationDatabase: List = emptyList(), ) : BackendConfiguration { init { logger.info { "Analyzing following sources $source" } 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 5225dd76f..be9ce9d5e 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 @@ -34,6 +34,19 @@ import kotlin.reflect.full.isSuperclassOf */ @Suppress("UNUSED") class CPGOptionGroup : BackendOptions(helpName = "CPG Backend Options") { + + val compilationDatabase: Path? by option( + "--compilationDatabase", + help = "Use compilation database to load project specific files and includes", + ) + .path(mustExist = true, mustBeReadable = true) + + val filterCompilationDatabase: List by option( + "--filterCompilationDatabase", + help = "Filters compilation database for specific components", + ) + .multiple() + private val rawSource: List by option( "-s", "--source", diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt index 0427389e1..26848c946 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/BaseCpgBackendCommand.kt @@ -48,6 +48,8 @@ class BaseCpgBackendCommand : BackendCommand("cpg") { includePaths = includePaths, includeAllowlist = includeAllowlist, includeBlocklist = includeBlocklist, + compilationDatabase = compilationDatabase, + filterCompilationDatabase = filterCompilationDatabase, ).normalize() ) } diff --git a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt index d28262cba..413078bf7 100644 --- a/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt +++ b/codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt @@ -49,6 +49,8 @@ class CokoCpgBackendCommand : BackendCommand("cokoCpg") { includePaths = includePaths, includeAllowlist = includeAllowlist, includeBlocklist = includeBlocklist, + compilationDatabase = compilationDatabase, + filterCompilationDatabase = filterCompilationDatabase, ).normalize() ) }