JS DCE: disable logging by default
Based on Vladislav Saifulin's PR #4031
This commit is contained in:
@@ -38,7 +38,10 @@ import org.jetbrains.kotlin.js.util.TextOutputImpl
|
||||
import java.io.File
|
||||
import java.io.InputStreamReader
|
||||
|
||||
class DeadCodeElimination(private val logConsumer: (DCELogLevel, String) -> Unit) {
|
||||
class DeadCodeElimination(
|
||||
private val printReachabilityInfo: Boolean,
|
||||
private val logConsumer: (DCELogLevel, String) -> Unit
|
||||
) {
|
||||
val moduleMapping = mutableMapOf<JsBlock, String>()
|
||||
private val reachableNames = mutableSetOf<String>()
|
||||
|
||||
@@ -61,7 +64,7 @@ class DeadCodeElimination(private val logConsumer: (DCELogLevel, String) -> Unit
|
||||
analyzer.moduleMapping += moduleMapping
|
||||
root.accept(analyzer)
|
||||
|
||||
val usageFinder = ReachabilityTracker(context, analyzer.analysisResult, logConsumer)
|
||||
val usageFinder = ReachabilityTracker(context, analyzer.analysisResult, logConsumer.takeIf { printReachabilityInfo })
|
||||
root.accept(usageFinder)
|
||||
|
||||
for (reachableName in reachableNames) {
|
||||
@@ -78,10 +81,11 @@ class DeadCodeElimination(private val logConsumer: (DCELogLevel, String) -> Unit
|
||||
fun run(
|
||||
inputFiles: Collection<InputFile>,
|
||||
rootReachableNames: Set<String>,
|
||||
printReachabilityInfo: Boolean,
|
||||
logConsumer: (DCELogLevel, String) -> Unit
|
||||
): DeadCodeEliminationResult {
|
||||
val program = JsProgram()
|
||||
val dce = DeadCodeElimination(logConsumer)
|
||||
val dce = DeadCodeElimination(printReachabilityInfo, logConsumer)
|
||||
|
||||
var hasErrors = false
|
||||
val blocks = inputFiles.map { file ->
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.js.inline.util.collectLocalVariables
|
||||
class ReachabilityTracker(
|
||||
private val context: Context,
|
||||
private val analysisResult: AnalysisResult,
|
||||
private val logConsumer: (DCELogLevel, String) -> Unit
|
||||
private val logConsumer: ((DCELogLevel, String) -> Unit)?
|
||||
) : RecursiveJsVisitor() {
|
||||
companion object {
|
||||
private val CALL_FUNCTIONS = setOf("call", "apply")
|
||||
@@ -237,14 +237,15 @@ class ReachabilityTracker(
|
||||
currentNodeWithLocation = old
|
||||
}
|
||||
|
||||
private fun report(message: String) {
|
||||
logConsumer(DCELogLevel.INFO, " ".repeat(depth) + message)
|
||||
}
|
||||
|
||||
private fun reportAndNest(message: String, dueTo: JsNode?, action: () -> Unit) {
|
||||
val location = dueTo?.extractLocation()
|
||||
val fullMessage = if (location != null) "$message (due to ${location.asString()})" else message
|
||||
report(fullMessage)
|
||||
if (logConsumer != null) {
|
||||
val indent = " ".repeat(depth)
|
||||
val fullMessage = when (val location = dueTo?.extractLocation()) {
|
||||
null -> "$indent$message"
|
||||
else -> "$indent$message (due to ${location.asString()})"
|
||||
}
|
||||
logConsumer.invoke(DCELogLevel.INFO, fullMessage)
|
||||
}
|
||||
nested(action)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user