Rearrange fir diagnostic for further reuse

This commit is contained in:
Ilya Chernikov
2021-09-17 22:13:23 +02:00
parent a5bf8f5f91
commit 31c58767cf
61 changed files with 1972 additions and 1944 deletions
@@ -14,10 +14,10 @@ import org.jetbrains.kotlin.cli.common.messages.MessageUtil
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils
import org.jetbrains.kotlin.diagnostics.Severity
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDefaultErrorMessages
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnostic
import org.jetbrains.kotlin.fir.analysis.diagnostics.KtDiagnostic
object FirDiagnosticsCompilerResultsReporter {
fun reportDiagnostics(diagnostics: Collection<FirDiagnostic>, reporter: MessageCollector): Boolean {
fun reportDiagnostics(diagnostics: Collection<KtDiagnostic>, reporter: MessageCollector): Boolean {
var hasErrors = false
for (diagnostic in diagnostics.sortedWith(DiagnosticComparator)) {
hasErrors = reportDiagnostic(diagnostic, reporter) || hasErrors
@@ -27,7 +27,7 @@ object FirDiagnosticsCompilerResultsReporter {
}
@Suppress("UNUSED_PARAMETER")
private fun reportSpecialErrors(diagnostics: Collection<FirDiagnostic>) {
private fun reportSpecialErrors(diagnostics: Collection<KtDiagnostic>) {
/*
* TODO: handle next diagnostics when they will be supported in FIR:
* - INCOMPATIBLE_CLASS
@@ -37,7 +37,7 @@ object FirDiagnosticsCompilerResultsReporter {
*/
}
private fun reportDiagnostic(diagnostic: FirDiagnostic, reporter: MessageCollector): Boolean {
private fun reportDiagnostic(diagnostic: KtDiagnostic, reporter: MessageCollector): Boolean {
if (!diagnostic.isValid) return false
diagnostic.location()?.let { location ->
val severity = AnalyzerWithCompilerReport.convertSeverity(diagnostic.severity)
@@ -48,24 +48,24 @@ object FirDiagnosticsCompilerResultsReporter {
return diagnostic.severity == Severity.ERROR
}
private fun FirDiagnostic.location(): CompilerMessageSourceLocation? = when (val element = element) {
private fun KtDiagnostic.location(): CompilerMessageSourceLocation? = when (val element = element) {
is KtPsiSourceElement -> element.location(this)
is KtLightSourceElement -> element.location(this)
}
private fun KtPsiSourceElement.location(diagnostic: FirDiagnostic): CompilerMessageSourceLocation? {
private fun KtPsiSourceElement.location(diagnostic: KtDiagnostic): CompilerMessageSourceLocation? {
val file = psi.containingFile
return MessageUtil.psiFileToMessageLocation(file, file.name, DiagnosticUtils.getLineAndColumnRange(file, diagnostic.textRanges))
}
@Suppress("UNUSED_PARAMETER")
private fun KtLightSourceElement.location(diagnostic: FirDiagnostic): CompilerMessageSourceLocation? {
private fun KtLightSourceElement.location(diagnostic: KtDiagnostic): CompilerMessageSourceLocation? {
// TODO: support light tree
return null
}
private object DiagnosticComparator : Comparator<FirDiagnostic> {
override fun compare(o1: FirDiagnostic, o2: FirDiagnostic): Int {
private object DiagnosticComparator : Comparator<KtDiagnostic> {
override fun compare(o1: KtDiagnostic, o2: KtDiagnostic): Int {
val element1 = o1.element
val element2 = o1.element
// TODO: support light tree
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.codegen.CodegenFactory
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.config.*
import org.jetbrains.kotlin.fir.*
import org.jetbrains.kotlin.fir.analysis.diagnostics.FirDiagnostic
import org.jetbrains.kotlin.fir.analysis.diagnostics.KtDiagnostic
import org.jetbrains.kotlin.fir.backend.Fir2IrResult
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmBackendClassResolver
import org.jetbrains.kotlin.fir.backend.jvm.FirJvmBackendExtension
@@ -244,16 +244,16 @@ object FirKotlinToJvmBytecodeCompiler {
val commonRawFir = commonSession?.buildFirFromKtFiles(commonKtFiles)
val rawFir = session.buildFirFromKtFiles(ktFiles)
val allFirDiagnostics = mutableListOf<FirDiagnostic>()
val allKtDiagnostics = mutableListOf<KtDiagnostic>()
commonSession?.apply {
val (commonScopeSession, commonFir) = runResolution(commonRawFir!!)
runCheckers(commonScopeSession, commonFir).values.flattenTo(allFirDiagnostics)
runCheckers(commonScopeSession, commonFir).values.flattenTo(allKtDiagnostics)
}
val (scopeSession, fir) = session.runResolution(rawFir)
session.runCheckers(scopeSession, fir).values.flattenTo(allFirDiagnostics)
session.runCheckers(scopeSession, fir).values.flattenTo(allKtDiagnostics)
val hasErrors = FirDiagnosticsCompilerResultsReporter.reportDiagnostics(allFirDiagnostics, messageCollector)
val hasErrors = FirDiagnosticsCompilerResultsReporter.reportDiagnostics(allKtDiagnostics, messageCollector)
return if (syntaxErrors || hasErrors) null else FirResult(session, scopeSession, fir)
}