Skip to content

Commit

Permalink
Add descriptor reporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipDolnik committed May 16, 2024
1 parent ebe197b commit 453d665
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import co.touchlab.skie.phases.SkiePhaseScheduler
import co.touchlab.skie.plugin.analytics.AnalyticsCollector
import co.touchlab.skie.util.CompilerShim
import co.touchlab.skie.util.directory.FrameworkLayout
import co.touchlab.skie.util.Reporter
import co.touchlab.skie.util.KirReporter
import co.touchlab.skie.util.directory.SkieBuildDirectory
import co.touchlab.skie.util.directory.SkieDirectories

Expand All @@ -28,7 +28,7 @@ interface CommonSkieContext {

val skiePerformanceAnalyticsProducer: SkiePerformanceAnalytics.Producer

val reporter: Reporter
val kirReporter: KirReporter

val skiePhaseScheduler: SkiePhaseScheduler

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package co.touchlab.skie.util

import co.touchlab.skie.kir.element.KirElement

class KirReporter : Reporter<KirElement>()
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
package co.touchlab.skie.util

import co.touchlab.skie.kir.element.KirElement
import java.util.Collections

class Reporter {
abstract class Reporter<T> {

private val mutableReports = Collections.synchronizedList(mutableListOf<Report>())
private val mutableReports = Collections.synchronizedList(mutableListOf<Report<T>>())

val reports: List<Report> by ::mutableReports
val reports: List<Report<T>> by ::mutableReports

fun report(severity: Severity, message: String, source: KirElement? = null) {
fun report(severity: Severity, message: String, source: T? = null) {
mutableReports.add(Report(message, severity, source))
}

fun error(message: String, source: KirElement? = null) {
fun error(message: String, source: T? = null) {
report(Severity.Error, message, source)
}

fun warning(message: String, source: KirElement? = null) {
fun warning(message: String, source: T? = null) {
report(Severity.Warning, message, source)
}

enum class Severity {
Error, Warning
}

data class Report(val message: String, val severity: Severity, val source: KirElement?)
data class Report<T>(val message: String, val severity: Severity, val source: T?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private fun reportCollision(
collisionReason: String,
source: KirElement,
) {
reporter.warning(
kirReporter.warning(
message = "'$originalName' was renamed to '$newName' because of a name collision with $collisionReason. " +
"Consider resolving the conflict either by changing the name in Kotlin, or via the @ObjCName annotation. " +
"You can also suppress this warning using the 'SuppressSkieWarning.NameCollision' configuration. " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package co.touchlab.skie.context
import co.touchlab.skie.configuration.provider.descriptor.DescriptorConfigurationProvider
import co.touchlab.skie.kir.descriptor.DescriptorProvider
import co.touchlab.skie.phases.ForegroundPhase
import co.touchlab.skie.util.DescriptorReporter
import org.jetbrains.kotlin.backend.konan.KonanConfig

interface ForegroundPhaseCompilerContext : ForegroundPhase.Context {
Expand All @@ -12,4 +13,6 @@ interface ForegroundPhaseCompilerContext : ForegroundPhase.Context {
val descriptorConfigurationProvider: DescriptorConfigurationProvider

val konanConfig: KonanConfig

val descriptorReporter: DescriptorReporter
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import co.touchlab.skie.plugin.analytics.AnalyticsCollector
import co.touchlab.skie.spi.SkiePluginRegistrar
import co.touchlab.skie.util.ActualCompilerShim
import co.touchlab.skie.util.CompilerShim
import co.touchlab.skie.util.DescriptorReporter
import co.touchlab.skie.util.directory.FrameworkLayout
import co.touchlab.skie.util.Reporter
import co.touchlab.skie.util.KirReporter
import co.touchlab.skie.util.directory.SkieDirectories
import org.jetbrains.kotlin.backend.konan.KonanConfigKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
Expand Down Expand Up @@ -52,7 +53,9 @@ class InitPhaseContext(

override val skiePerformanceAnalyticsProducer: SkiePerformanceAnalytics.Producer = SkiePerformanceAnalytics.Producer(rootConfiguration)

override val reporter: Reporter = Reporter()
override val kirReporter: KirReporter = KirReporter()

val descriptorReporter: DescriptorReporter = DescriptorReporter()

override val compilerShim: CompilerShim = ActualCompilerShim()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ import co.touchlab.skie.phases.BackgroundPhase
import co.touchlab.skie.phases.ScheduledPhase
import co.touchlab.skie.phases.configurables
import co.touchlab.skie.phases.util.StatefulScheduledPhase
import co.touchlab.skie.util.KotlinCompilerVersion
import co.touchlab.skie.util.DescriptorReporter
import co.touchlab.skie.util.TargetTriple
import co.touchlab.skie.util.current
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -60,6 +59,8 @@ class MainSkieContext internal constructor(

val declarationBuilder: DeclarationBuilderImpl = DeclarationBuilderImpl(mainModuleDescriptor, descriptorProvider)

override val descriptorReporter: DescriptorReporter = initPhaseContext.descriptorReporter

lateinit var kirProvider: KirProvider

lateinit var descriptorKirProvider: DescriptorKirProvider
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import co.touchlab.skie.context.ForegroundPhaseCompilerContext
import co.touchlab.skie.kir.descriptor.DescriptorProvider
import co.touchlab.skie.kir.descriptor.ExtraDescriptorBuiltins
import co.touchlab.skie.kir.descriptor.cache.CachedObjCExportMapper
import co.touchlab.skie.util.DescriptorReporter
import org.jetbrains.kotlin.backend.konan.KonanConfig
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.konan.target.AppleConfigurables
Expand All @@ -30,5 +31,8 @@ val ForegroundPhase.Context.extraDescriptorBuiltins: ExtraDescriptorBuiltins
val ForegroundPhase.Context.configurables: AppleConfigurables
get() = konanConfig.platform.configurables as AppleConfigurables

val ForegroundPhase.Context.descriptorReporter: DescriptorReporter
get() = typedContext.descriptorReporter

private val ForegroundPhase.Context.typedContext: ForegroundPhaseCompilerContext
get() = context as ForegroundPhaseCompilerContext
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package co.touchlab.skie.phases

import co.touchlab.skie.context.InitPhaseContext
import co.touchlab.skie.util.DescriptorReporter
import org.jetbrains.kotlin.config.CompilerConfiguration

val InitPhase.Context.compilerConfiguration: CompilerConfiguration
get() = typedContext.compilerConfiguration

val InitPhase.Context.descriptorReporter: DescriptorReporter
get() = typedContext.descriptorReporter

private val InitPhase.Context.typedContext: InitPhaseContext
get() = context as InitPhaseContext
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,42 @@ package co.touchlab.skie.phases.other

import co.touchlab.skie.phases.LinkPhase
import co.touchlab.skie.phases.descriptorKirProvider
import co.touchlab.skie.phases.descriptorReporter
import co.touchlab.skie.phases.konanConfig
import co.touchlab.skie.util.Reporter
import co.touchlab.skie.util.Reporter.Severity
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageUtil
import org.jetbrains.kotlin.cli.jvm.compiler.report
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.js.resolve.diagnostics.findPsi
import org.jetbrains.kotlin.renderer.DescriptorRenderer

object ProcessReportedMessagesPhase : LinkPhase {

context(LinkPhase.Context)
override suspend fun execute() {
reporter.reports.forEach {
report(it)
kirReporter.reportAll(descriptorKirProvider::findDeclarationDescriptor)

descriptorReporter.reportAll { it }
}

context(LinkPhase.Context)
private fun <T> Reporter<T>.reportAll(findDeclarationDescriptor: (T) -> DeclarationDescriptor?) {
this.reports.forEach {
report(it, findDeclarationDescriptor)
}
}

context(LinkPhase.Context)
private fun report(report: Reporter.Report) {
val declaration = report.source?.let { descriptorKirProvider.findDeclarationDescriptor(it) }
private fun <T> report(report: Reporter.Report<T>, findDeclarationDescriptor: (T) -> DeclarationDescriptor?) {
val declarationDescriptor = report.source?.let { findDeclarationDescriptor(it) }

report(report, declarationDescriptor)
}

context(LinkPhase.Context)
private fun <T> report(report: Reporter.Report<T>, declaration: DeclarationDescriptor?) {
val location = MessageUtil.psiElementToMessageLocation(declaration?.findPsi())?.let {
CompilerMessageLocation.create(it.path, it.line, it.column, it.lineContent)
}
Expand All @@ -36,8 +49,8 @@ object ProcessReportedMessagesPhase : LinkPhase {
}

when (report.severity) {
Severity.Error -> konanConfig.configuration.report(CompilerMessageSeverity.ERROR, message, location)
Severity.Warning -> konanConfig.configuration.report(CompilerMessageSeverity.WARNING, message, location)
Reporter.Severity.Error -> konanConfig.configuration.report(CompilerMessageSeverity.ERROR, message, location)
Reporter.Severity.Warning -> konanConfig.configuration.report(CompilerMessageSeverity.WARNING, message, location)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package co.touchlab.skie.util

import org.jetbrains.kotlin.descriptors.DeclarationDescriptor

class DescriptorReporter : Reporter<DeclarationDescriptor>()

0 comments on commit 453d665

Please sign in to comment.