Refactor IR reporting and infrastructure
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.diagnostics
|
||||
|
||||
import org.jetbrains.kotlin.AbstractKtSourceElement
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
|
||||
interface DiagnosticContext {
|
||||
@@ -27,3 +28,44 @@ abstract class MutableDiagnosticContext : DiagnosticContext {
|
||||
abstract class DiagnosticReporter {
|
||||
abstract fun report(diagnostic: KtDiagnostic?, context: DiagnosticContext)
|
||||
}
|
||||
|
||||
open class KtDiagnosticReporterWithContext(
|
||||
val diagnosticReporter: DiagnosticReporter,
|
||||
val languageVersionSettings: LanguageVersionSettings
|
||||
) : DiagnosticReporter() {
|
||||
override fun report(diagnostic: KtDiagnostic?, context: DiagnosticContext) = diagnosticReporter.report(diagnostic, context)
|
||||
|
||||
fun at(sourceElement: AbstractKtSourceElement?, containingFilePath: String): DiagnosticContextImpl =
|
||||
DiagnosticContextImpl(sourceElement, containingFilePath)
|
||||
|
||||
open inner class DiagnosticContextImpl(
|
||||
val sourceElement: AbstractKtSourceElement?,
|
||||
override val containingFilePath: String
|
||||
) : DiagnosticContext {
|
||||
|
||||
override fun isDiagnosticSuppressed(diagnostic: KtDiagnostic): Boolean {
|
||||
return false
|
||||
// TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
override val languageVersionSettings: LanguageVersionSettings
|
||||
get() = this@KtDiagnosticReporterWithContext.languageVersionSettings
|
||||
|
||||
@OptIn(InternalDiagnosticFactoryMethod::class)
|
||||
fun report(
|
||||
factory: KtDiagnosticFactory0,
|
||||
positioningStrategy: AbstractSourceElementPositioningStrategy? = null
|
||||
) {
|
||||
sourceElement?.let { report(factory.on(it, positioningStrategy), this) }
|
||||
}
|
||||
|
||||
@OptIn(InternalDiagnosticFactoryMethod::class)
|
||||
fun <A : Any> report(
|
||||
factory: KtDiagnosticFactory1<A>,
|
||||
a: A,
|
||||
positioningStrategy: AbstractSourceElementPositioningStrategy? = null
|
||||
) {
|
||||
sourceElement?.let { report(factory.on(it, a, positioningStrategy), this) }
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -11,4 +11,5 @@ import org.jetbrains.kotlin.diagnostics.KtDiagnostic
|
||||
abstract class BaseDiagnosticReporter : DiagnosticReporter() {
|
||||
abstract val diagnostics: List<KtDiagnostic>
|
||||
abstract val diagnosticsByFilePath: Map<String?, List<KtDiagnostic>>
|
||||
abstract val hasErrors: Boolean
|
||||
}
|
||||
|
||||
+11
-3
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.diagnostics.impl
|
||||
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticContext
|
||||
import org.jetbrains.kotlin.diagnostics.KtDiagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Severity
|
||||
|
||||
class DiagnosticReporterWithSuppress : BaseDiagnosticReporter() {
|
||||
private val _diagnosticsByFilePath: MutableMap<String?, MutableList<KtDiagnostic>> = mutableMapOf()
|
||||
@@ -15,10 +16,17 @@ class DiagnosticReporterWithSuppress : BaseDiagnosticReporter() {
|
||||
override val diagnosticsByFilePath: Map<String?, List<KtDiagnostic>>
|
||||
get() = _diagnosticsByFilePath
|
||||
|
||||
override var hasErrors = false
|
||||
private set
|
||||
|
||||
override fun report(diagnostic: KtDiagnostic?, context: DiagnosticContext) {
|
||||
if (diagnostic == null) return
|
||||
if (!context.isDiagnosticSuppressed(diagnostic)) {
|
||||
_diagnosticsByFilePath.getOrPut(context.containingFilePath) { mutableListOf() }.add(diagnostic)
|
||||
if (diagnostic != null && !context.isDiagnosticSuppressed(diagnostic)) {
|
||||
_diagnosticsByFilePath.getOrPut(context.containingFilePath) { mutableListOf() }.run {
|
||||
add(diagnostic)
|
||||
if (!hasErrors && diagnostic.severity == Severity.ERROR) {
|
||||
hasErrors = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+12
-2
@@ -7,6 +7,7 @@ package org.jetbrains.kotlin.diagnostics.impl
|
||||
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticContext
|
||||
import org.jetbrains.kotlin.diagnostics.KtDiagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.Severity
|
||||
|
||||
class SimpleDiagnosticReporter : BaseDiagnosticReporter() {
|
||||
private val _diagnosticsByFilePath: MutableMap<String?, MutableList<KtDiagnostic>> = mutableMapOf()
|
||||
@@ -15,8 +16,17 @@ class SimpleDiagnosticReporter : BaseDiagnosticReporter() {
|
||||
override val diagnosticsByFilePath: Map<String?, List<KtDiagnostic>>
|
||||
get() = _diagnosticsByFilePath
|
||||
|
||||
override var hasErrors = false
|
||||
private set
|
||||
|
||||
override fun report(diagnostic: KtDiagnostic?, context: DiagnosticContext) {
|
||||
if (diagnostic == null) return
|
||||
_diagnosticsByFilePath.getOrPut(context.containingFilePath) { mutableListOf() }.add(diagnostic)
|
||||
if (diagnostic != null) {
|
||||
_diagnosticsByFilePath.getOrPut(context.containingFilePath) { mutableListOf() }.run {
|
||||
add(diagnostic)
|
||||
if (!hasErrors && diagnostic.severity == Severity.ERROR) {
|
||||
hasErrors = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user