diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt index d2b7f83db7..fb4b3674e8 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt @@ -117,8 +117,8 @@ private constructor( matchCommentsToNodes: Boolean, addIncludesToGraph: Boolean, passConfigurations: Map>, PassConfiguration>, - /** This list contains the directories which should be excluded from the analysis. */ - val excludedDirectories: List + /** A list of exclusion patterns used to filter files and directories. */ + val exclusionPatterns: List ) { /** This list contains all languages which we want to translate. */ val languages: List> @@ -259,7 +259,7 @@ private constructor( private var useDefaultPasses = false private var passConfigurations: MutableMap>, PassConfiguration> = mutableMapOf() - private val excludedDirectories = mutableListOf() + private val exclusionPatterns = mutableListOf() fun symbols(symbols: Map): Builder { this.symbols = symbols @@ -456,9 +456,17 @@ private constructor( return this.configurePass(T::class, config) } - /** Adds the directories to the [excludedDirectories] list. */ - fun excludedDirs(dirs: List): Builder { - excludedDirectories.addAll(dirs.map { Path.of(it) }) + /** + * Sets a list of exclusion patterns for filtering files and directories. + * + * @param patterns List of exclusion patterns: strings are treated as Regex patterns. + * Example: + * ``` + * exclusionPatterns(listOf(".*test(s)?", "tests")) + * ``` + */ + fun exclusionPatterns(patterns: List): Builder { + exclusionPatterns.addAll(patterns.map { Regex(it) }) return this } @@ -657,7 +665,7 @@ private constructor( matchCommentsToNodes, addIncludesToGraph, passConfigurations, - excludedDirectories + exclusionPatterns ) } diff --git a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt index efd584c87c..06edf734b3 100644 --- a/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt +++ b/cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt @@ -148,8 +148,8 @@ private constructor( sourceLocations = sourceLocations.filter { file -> - ctx.config.excludedDirectories.none { excludedDir -> - file.toPath().contains(excludedDir) + ctx.config.exclusionPatterns.none { pattern -> + pattern.containsMatchIn(file.absolutePath) } } diff --git a/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt b/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt index 08d9d8bdcf..6775df2aaf 100644 --- a/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt +++ b/cpg-language-python/src/test/kotlin/de/fraunhofer/aisec/cpg/frontends/python/PythonFrontendTest.kt @@ -1586,29 +1586,6 @@ class PythonFrontendTest : BaseTest() { } } - @Test - fun test() { - val directoryPath = Path.of("/home", "lshala", "repos", "nova") - val excludedDirs = listOf("tests", "accelerator") - val tr = - analyze(".py", directoryPath, usePasses = true) { - it.registerLanguage() - it.excludedDirs(excludedDirs) - } - - val problemsList = tr.components.flatMap { it.translationUnits }.flatMap { it.problems } - - val msg = - (problemsList) - .groupBy { it.problem } - .toList() - .sortedBy { it.second.size } - .reversed() - .map { "${it.second.size}: ${it.first}" } - - msg.forEach(System.out::println) - } - class PythonValueEvaluator : ValueEvaluator() { override fun computeBinaryOpEffect( lhsValue: Any?,