Kapt: Fix javac error reporting in Kotlin daemon, also fix parsing error reporting (KT-15524)
This commit is contained in:
committed by
Yan Zhulanow
parent
cde7cb1076
commit
6ded5939eb
@@ -42,7 +42,7 @@ class KaptContext(
|
|||||||
val options: Options
|
val options: Options
|
||||||
|
|
||||||
init {
|
init {
|
||||||
KaptJavaLog.preRegister(context)
|
KaptJavaLog.preRegister(context, logger.messageCollector)
|
||||||
JavacFileManager.preRegister(context)
|
JavacFileManager.preRegister(context)
|
||||||
KaptTreeMaker.preRegister(context)
|
KaptTreeMaker.preRegister(context)
|
||||||
KaptJavaCompiler.preRegister(context)
|
KaptJavaCompiler.preRegister(context)
|
||||||
|
|||||||
@@ -16,7 +16,6 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.kapt3
|
package org.jetbrains.kotlin.kapt3
|
||||||
|
|
||||||
import com.sun.tools.javac.comp.CompileStates
|
|
||||||
import com.sun.tools.javac.file.JavacFileManager
|
import com.sun.tools.javac.file.JavacFileManager
|
||||||
import com.sun.tools.javac.main.Option
|
import com.sun.tools.javac.main.Option
|
||||||
import com.sun.tools.javac.processing.AnnotationProcessingError
|
import com.sun.tools.javac.processing.AnnotationProcessingError
|
||||||
@@ -64,17 +63,10 @@ fun KaptContext.doAnnotationProcessing(
|
|||||||
val parsedJavaFiles = compiler.parseFiles(javaFileObjects)
|
val parsedJavaFiles = compiler.parseFiles(javaFileObjects)
|
||||||
|
|
||||||
val log = Log.instance(context)
|
val log = Log.instance(context)
|
||||||
if (compiler.shouldStop(CompileStates.CompileState.PARSE) || log.nerrors > 0) {
|
|
||||||
log.flush()
|
|
||||||
throw KaptError(KaptError.Kind.JAVA_FILE_PARSING_ERROR)
|
|
||||||
}
|
|
||||||
|
|
||||||
val warningsBeforeAp: Int
|
val warningsBeforeAp: Int
|
||||||
try {
|
try {
|
||||||
val analyzedFiles = compiler.enterTrees(parsedJavaFiles + additionalSources)
|
val analyzedFiles = compiler.enterTrees(parsedJavaFiles + additionalSources)
|
||||||
if (log.nerrors > 0) {
|
|
||||||
throw KaptError(KaptError.Kind.ERROR_WHILE_ANALYSIS)
|
|
||||||
}
|
|
||||||
|
|
||||||
warningsBeforeAp = log.nwarnings
|
warningsBeforeAp = log.nwarnings
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,18 @@ package org.jetbrains.kotlin.kapt3.javac
|
|||||||
import com.sun.tools.javac.util.Context
|
import com.sun.tools.javac.util.Context
|
||||||
import com.sun.tools.javac.util.JCDiagnostic
|
import com.sun.tools.javac.util.JCDiagnostic
|
||||||
import com.sun.tools.javac.util.Log
|
import com.sun.tools.javac.util.Log
|
||||||
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||||
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.*
|
||||||
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||||
|
import org.jetbrains.kotlin.kapt3.util.MessageCollectorBackedWriter
|
||||||
|
import java.io.PrintWriter
|
||||||
|
|
||||||
class KaptJavaLog(context: Context?) : Log(context) {
|
class KaptJavaLog(
|
||||||
|
context: Context?,
|
||||||
|
errWriter: PrintWriter,
|
||||||
|
warnWriter: PrintWriter,
|
||||||
|
noticeWriter: PrintWriter
|
||||||
|
) : Log(context, errWriter, warnWriter, noticeWriter) {
|
||||||
override fun report(diagnostic: JCDiagnostic) {
|
override fun report(diagnostic: JCDiagnostic) {
|
||||||
if (diagnostic.type == JCDiagnostic.DiagnosticType.ERROR && diagnostic.code in IGNORED_DIAGNOSTICS) {
|
if (diagnostic.type == JCDiagnostic.DiagnosticType.ERROR && diagnostic.code in IGNORED_DIAGNOSTICS) {
|
||||||
return
|
return
|
||||||
@@ -37,8 +47,14 @@ class KaptJavaLog(context: Context?) : Log(context) {
|
|||||||
"compiler.err.name.clash.same.erasure.no.hide",
|
"compiler.err.name.clash.same.erasure.no.hide",
|
||||||
"compiler.err.already.defined")
|
"compiler.err.already.defined")
|
||||||
|
|
||||||
internal fun preRegister(context: Context) {
|
internal fun preRegister(context: Context, messageCollector: MessageCollector) {
|
||||||
context.put(Log.logKey, Context.Factory<Log>(::KaptJavaLog))
|
context.put(Log.logKey, Context.Factory<Log> {
|
||||||
|
fun makeWriter(severity: CompilerMessageSeverity) = PrintWriter(MessageCollectorBackedWriter(messageCollector, severity))
|
||||||
|
val errWriter = makeWriter(ERROR)
|
||||||
|
val warnWriter = makeWriter(STRONG_WARNING)
|
||||||
|
val noticeWriter = makeWriter(INFO)
|
||||||
|
KaptJavaLog(it, errWriter, warnWriter, noticeWriter)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2010-2017 JetBrains s.r.o.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jetbrains.kotlin.kapt3.util
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation
|
||||||
|
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
|
||||||
|
import org.jetbrains.kotlin.cli.common.messages.GroupingMessageCollector
|
||||||
|
import org.jetbrains.kotlin.cli.common.messages.MessageCollector
|
||||||
|
import java.io.Writer
|
||||||
|
|
||||||
|
class MessageCollectorBackedWriter(val messageCollector: MessageCollector, val severity: CompilerMessageSeverity) : Writer() {
|
||||||
|
override fun write(buffer: CharArray, offset: Int, length: Int) {
|
||||||
|
messageCollector.report(severity, String(buffer, offset, length), CompilerMessageLocation.NO_LOCATION)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun flush() {
|
||||||
|
if (messageCollector is GroupingMessageCollector) {
|
||||||
|
messageCollector.flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun close() {
|
||||||
|
flush()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user