Skip to content

Commit

Permalink
Add compilation database options from CPG as CLI option
Browse files Browse the repository at this point in the history
  • Loading branch information
fwendland committed Sep 13, 2024
1 parent 3868a52 commit ee09cd8
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -72,6 +73,12 @@ open class CPGBackend(config: BackendConfiguration) : Backend {
.useUnityBuild(useUnityBuild)
.processAnnotations(processAnnotations)

compilationDatabase?.let {
translationConfiguration.useCompilationDatabase(
CompilationDatabase.fromFile(it.toFile(), filterCompilationDatabase)

Check warning on line 78 in codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGBackend.kt

View check run for this annotation

Codecov / codecov/patch

codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/CPGBackend.kt#L77-L78

Added lines #L77 - L78 were not covered by tests
)
}

// TODO: very hacky, but needed for the Go frontend
source.firstOrNull()?.parent?.toFile().let { translationConfiguration.topLevel(it) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ data class CPGConfiguration(
val includePaths: List<Path>,
val includeAllowlist: List<Path>,
val includeBlocklist: List<Path>,
val compilationDatabase: Path? = null,
val filterCompilationDatabase: List<String> = emptyList<String>(),
) : BackendConfiguration {
init {
logger.info { "Analyzing following sources $source" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> by option(
"--filterCompilationDatabase",
help = "Filters compilation database for specific components",
)
.multiple()

private val rawSource: List<Path> by option(
"-s",
"--source",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class BaseCpgBackendCommand : BackendCommand<CPGBackend>("cpg") {
includePaths = includePaths,
includeAllowlist = includeAllowlist,
includeBlocklist = includeBlocklist,
compilationDatabase = compilationDatabase,
filterCompilationDatabase = filterCompilationDatabase,

Check warning on line 52 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#L51-L52

Added lines #L51 - L52 were not covered by tests
).normalize()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class CokoCpgBackendCommand : BackendCommand<CokoBackend>("cokoCpg") {
includePaths = includePaths,
includeAllowlist = includeAllowlist,
includeBlocklist = includeBlocklist,
compilationDatabase = compilationDatabase,
filterCompilationDatabase = filterCompilationDatabase,

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

View check run for this annotation

Codecov / codecov/patch

codyze-backends/cpg/src/main/kotlin/de/fraunhofer/aisec/codyze/backends/cpg/cli/CokoCpgBackendCommand.kt#L52-L53

Added lines #L52 - L53 were not covered by tests
).normalize()
)
}
Expand Down

0 comments on commit ee09cd8

Please sign in to comment.