Skip to content

Commit

Permalink
Refactor filter
Browse files Browse the repository at this point in the history
  • Loading branch information
lshala committed Nov 19, 2024
1 parent fcca597 commit 5b9f5a0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ private constructor(
matchCommentsToNodes: Boolean,
addIncludesToGraph: Boolean,
passConfigurations: Map<KClass<out Pass<*>>, PassConfiguration>,
/** This list contains the directories which should be excluded from the analysis. */
val excludedDirectories: List<Path>
/** A list of exclusion patterns used to filter files and directories. */
val exclusionPatterns: List<Regex>
) {
/** This list contains all languages which we want to translate. */
val languages: List<Language<*>>
Expand Down Expand Up @@ -259,7 +259,7 @@ private constructor(
private var useDefaultPasses = false
private var passConfigurations: MutableMap<KClass<out Pass<*>>, PassConfiguration> =
mutableMapOf()
private val excludedDirectories = mutableListOf<Path>()
private val exclusionPatterns = mutableListOf<Regex>()

fun symbols(symbols: Map<String, String>): Builder {
this.symbols = symbols
Expand Down Expand Up @@ -456,9 +456,17 @@ private constructor(
return this.configurePass(T::class, config)
}

/** Adds the directories to the [excludedDirectories] list. */
fun excludedDirs(dirs: List<String>): 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<String>): Builder {
exclusionPatterns.addAll(patterns.map { Regex(it) })
return this

Check warning on line 470 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationConfiguration.kt#L469-L470

Added lines #L469 - L470 were not covered by tests
}

Expand Down Expand Up @@ -657,7 +665,7 @@ private constructor(
matchCommentsToNodes,
addIncludesToGraph,
passConfigurations,
excludedDirectories
exclusionPatterns
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check warning on line 152 in cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt

View check run for this annotation

Codecov / codecov/patch

cpg-core/src/main/kotlin/de/fraunhofer/aisec/cpg/TranslationManager.kt#L152

Added line #L152 was not covered by tests
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<PythonLanguage>()
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?,
Expand Down

0 comments on commit 5b9f5a0

Please sign in to comment.