Skip to content

Commit

Permalink
Extracted configuration in a more generic pass-specific config
Browse files Browse the repository at this point in the history
  • Loading branch information
oxisto committed Nov 16, 2023
1 parent 374d213 commit a2411da
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ private constructor(
compilationDatabase: CompilationDatabase?,
matchCommentsToNodes: Boolean,
addIncludesToGraph: Boolean,
maxComplexityForDFG: Int?,
passConfigurations: Map<KClass<out Pass<*>>, PassConfiguration>,
) {
/** This list contains all languages which we want to translate. */
val languages: List<Language<*>>
Expand Down Expand Up @@ -166,7 +166,7 @@ private constructor(
/** This sub configuration object holds all information about inference and smart-guessing. */
val inferenceConfiguration: InferenceConfiguration

val maxComplexityForDFG: Int?
val passConfigurations: Map<KClass<out Pass<*>>, PassConfiguration>

init {
registeredPasses = passes
Expand All @@ -181,7 +181,7 @@ private constructor(
this.compilationDatabase = compilationDatabase
this.matchCommentsToNodes = matchCommentsToNodes
this.addIncludesToGraph = addIncludesToGraph
this.maxComplexityForDFG = maxComplexityForDFG
this.passConfigurations = passConfigurations
}

/** Returns a list of all analyzed files. */
Expand Down Expand Up @@ -232,7 +232,8 @@ private constructor(
private var matchCommentsToNodes = false
private var addIncludesToGraph = true
private var useDefaultPasses = false
private var maxComplexityForDFG: Int? = null
private var passConfigurations: MutableMap<KClass<out Pass<*>>, PassConfiguration> =
mutableMapOf()

fun symbols(symbols: Map<String, String>): Builder {
this.symbols = symbols
Expand Down Expand Up @@ -414,11 +415,15 @@ private constructor(
return this
}

fun maxComplexityForDFG(max: Int): Builder {
this.maxComplexityForDFG = max
fun <T : Pass<*>> configurePass(clazz: KClass<T>, config: PassConfiguration): Builder {
this.passConfigurations[clazz] = config
return this
}

inline fun <reified T : Pass<*>> configurePass(config: PassConfiguration): Builder {
return this.configurePass(T::class, config)
}

/**
* Loads and registers an additional [Language] based on a fully qualified class name (FQN).
*/
Expand Down Expand Up @@ -605,7 +610,7 @@ private constructor(
compilationDatabase,
matchCommentsToNodes,
addIncludesToGraph,
maxComplexityForDFG
passConfigurations
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ import kotlin.contracts.contract
@DependsOn(DFGPass::class)
open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : TranslationUnitPass(ctx) {

class Configuration(var maxComplexity: Int? = null) : PassConfiguration() {}

override fun cleanup() {
// Nothing to do
}
Expand All @@ -61,7 +63,7 @@ open class ControlFlowSensitiveDFGPass(ctx: TranslationContext) : TranslationUni
* @param node every node in the TranslationResult
*/
protected fun handle(node: Node) {
val max = config.maxComplexityForDFG
val max = passConfig<Configuration>()?.maxComplexity

if (node is FunctionDeclaration) {
// Skip empty functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ abstract class TranslationUnitPass(ctx: TranslationContext) : Pass<TranslationUn
*/
interface PassTarget

open class PassConfiguration {}

/**
* Represents an abstract class that enhances the graph before it is persisted. Passes can exist at
* three different levels:
Expand Down Expand Up @@ -109,6 +111,10 @@ sealed class Pass<T : PassTarget>(final override val ctx: TranslationContext) :

val log: Logger = LoggerFactory.getLogger(Pass::class.java)
}

fun <T : PassConfiguration> passConfig(): T? {
return this.config.passConfigurations[this::class] as? T
}
}

/**
Expand Down

0 comments on commit a2411da

Please sign in to comment.