summaryrefslogtreecommitdiff
path: root/plugins/kotlin/jps/jps-plugin/src/org/jetbrains/kotlin/jps/build/MessageCollectorAdapter.kt
blob: 45d1e0c57e6ee8f560e0ef151f1cab81ab2253b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.

package org.jetbrains.kotlin.jps.build

import org.jetbrains.annotations.Nls
import org.jetbrains.jps.incremental.CompileContext
import org.jetbrains.jps.incremental.messages.BuildMessage
import org.jetbrains.jps.incremental.messages.CompilerMessage
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
import org.jetbrains.kotlin.config.CompilerRunnerConstants
import org.jetbrains.kotlin.jps.targets.KotlinModuleBuildTarget
import java.io.File

class MessageCollectorAdapter(
    private val context: CompileContext,
    private val kotlinTarget: KotlinModuleBuildTarget<*>?
) : MessageCollector {
    private var hasErrors = false

    override fun report(severity: CompilerMessageSeverity, @Nls message: String, location: CompilerMessageSourceLocation?) {
        hasErrors = hasErrors || severity.isError

        var prefix = ""
        if (severity == CompilerMessageSeverity.EXCEPTION) {
            prefix = CompilerRunnerConstants.INTERNAL_ERROR_PREFIX
        }

        val kind = kind(severity)
        if (kind != null) {
            // Report target when cross-compiling common files
            if (location != null && kotlinTarget != null && kotlinTarget.isFromIncludedSourceRoot(File(location.path))) {
                val moduleName = kotlinTarget.module.name
                prefix += "[$moduleName] "
            }

            context.processMessage(
                CompilerMessage(
                    CompilerRunnerConstants.KOTLIN_COMPILER_NAME,
                    kind,
                    prefix + message,
                    location?.path,
                    -1, -1, -1,
                    location?.line?.toLong() ?: -1,
                    location?.column?.toLong() ?: -1
                )
            )
        } else {
            val path = if (location != null) "${location.path}:${location.line}:${location.column}: " else ""
            KotlinBuilder.LOG.debug(path + message)
        }
    }

    override fun clear() {
        hasErrors = false
    }

    override fun hasErrors(): Boolean = hasErrors

    private fun kind(severity: CompilerMessageSeverity): BuildMessage.Kind? {
        return when (severity) {
            CompilerMessageSeverity.INFO -> BuildMessage.Kind.INFO
            CompilerMessageSeverity.ERROR, CompilerMessageSeverity.EXCEPTION -> BuildMessage.Kind.ERROR
            CompilerMessageSeverity.WARNING, CompilerMessageSeverity.STRONG_WARNING -> BuildMessage.Kind.WARNING
            CompilerMessageSeverity.LOGGING -> null
            else -> throw IllegalArgumentException("Unsupported severity: $severity")
        }
    }
}