forked from tschuchortdev/kotlin-compile-testing
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Capture diagnostics with a severity level #260
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
core/src/main/kotlin/com/tschuchort/compiletesting/DiagnosticMessage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2020 The Android Open Source Project | ||
* | ||
* 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 com.facebook.buck.jvm.java.javax.com.tschuchort.compiletesting | ||
|
||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity | ||
|
||
public enum class DiagnosticSeverity { | ||
ERROR, | ||
WARNING, | ||
INFO, | ||
LOGGING, | ||
} | ||
|
||
/** | ||
* Holder for diagnostics messages | ||
*/ | ||
public data class DiagnosticMessage( | ||
val severity: DiagnosticSeverity, | ||
val message: String, | ||
) | ||
|
||
internal fun CompilerMessageSeverity.toSeverity() = when (this) { | ||
CompilerMessageSeverity.EXCEPTION, | ||
CompilerMessageSeverity.ERROR -> DiagnosticSeverity.ERROR | ||
CompilerMessageSeverity.STRONG_WARNING, | ||
CompilerMessageSeverity.WARNING -> DiagnosticSeverity.WARNING | ||
CompilerMessageSeverity.INFO -> DiagnosticSeverity.INFO | ||
CompilerMessageSeverity.LOGGING, | ||
CompilerMessageSeverity.OUTPUT -> DiagnosticSeverity.LOGGING | ||
} |
145 changes: 145 additions & 0 deletions
145
core/src/main/kotlin/com/tschuchort/compiletesting/DiagnosticsMessageCollector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright 2021 The Android Open Source Project | ||
* | ||
* 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 com.facebook.buck.jvm.java.javax.com.tschuchort.compiletesting | ||
|
||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity | ||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation | ||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector | ||
import javax.tools.Diagnostic | ||
|
||
/** | ||
* Custom message collector for Kotlin compilation that collects messages into | ||
* [DiagnosticMessage] objects. | ||
*/ | ||
internal class DiagnosticsMessageCollector( | ||
private val stepName: String, | ||
private val verbose: Boolean, | ||
private val diagnostics: MutableList<DiagnosticMessage>, | ||
) : MessageCollector { | ||
|
||
override fun clear() { | ||
diagnostics.clear() | ||
} | ||
|
||
/** | ||
* Returns `true` if this collector has any warning messages. | ||
*/ | ||
fun hasWarnings() = diagnostics.any { | ||
it.severity == DiagnosticSeverity.WARNING | ||
} | ||
|
||
override fun hasErrors(): Boolean { | ||
return diagnostics.any { | ||
it.severity == DiagnosticSeverity.ERROR | ||
} | ||
} | ||
|
||
override fun report( | ||
severity: CompilerMessageSeverity, | ||
message: String, | ||
location: CompilerMessageSourceLocation? | ||
) { | ||
if (!verbose && CompilerMessageSeverity.VERBOSE.contains(severity)) return | ||
|
||
val severity = | ||
if (stepName == "kapt" && getJavaVersion() >= 17) { | ||
// Workaround for KT-54030 | ||
message.getSeverityFromPrefix() ?: severity.toSeverity() | ||
} else { | ||
severity.toSeverity() | ||
} | ||
doReport(severity, message) | ||
} | ||
|
||
private fun doReport( | ||
severity: DiagnosticSeverity, | ||
message: String, | ||
) { | ||
if (message == KSP_ADDITIONAL_ERROR_MESSAGE) { | ||
// ignore this as it will impact error counts. | ||
return | ||
} | ||
// Strip kapt/ksp prefixes | ||
val strippedMessage = message.stripPrefixes() | ||
diagnostics.add( | ||
DiagnosticMessage( | ||
severity = severity, | ||
message = strippedMessage, | ||
) | ||
) | ||
} | ||
|
||
/** | ||
* Removes prefixes added by kapt / ksp from the message | ||
*/ | ||
private fun String.stripPrefixes(): String { | ||
return stripKind().stripKspPrefix() | ||
} | ||
|
||
/** | ||
* KAPT prepends the message kind to the message, we'll remove it here. | ||
*/ | ||
private fun String.stripKind(): String { | ||
val firstLine = lineSequence().firstOrNull() ?: return this | ||
val match = KIND_REGEX.find(firstLine) ?: return this | ||
return substring(match.range.last + 1) | ||
} | ||
|
||
/** | ||
* KSP prepends ksp to each message, we'll strip it here. | ||
*/ | ||
private fun String.stripKspPrefix(): String { | ||
val firstLine = lineSequence().firstOrNull() ?: return this | ||
val match = KSP_PREFIX_REGEX.find(firstLine) ?: return this | ||
return substring(match.range.last + 1) | ||
} | ||
|
||
private fun String.getSeverityFromPrefix(): DiagnosticSeverity? { | ||
val kindMatch = | ||
// The (\w+) for the kind prefix is is the 4th capture group | ||
KAPT_LOCATION_AND_KIND_REGEX.find(this)?.groupValues?.getOrNull(4) | ||
// The (\w+) is the 1st capture group | ||
?: KIND_REGEX.find(this)?.groupValues?.getOrNull(1) | ||
?: return null | ||
return if (kindMatch.equals("error", ignoreCase = true)) { | ||
DiagnosticSeverity.ERROR | ||
} else if (kindMatch.equals("warning", ignoreCase = true)) { | ||
DiagnosticSeverity.WARNING | ||
} else if (kindMatch.equals("note", ignoreCase = true)) { | ||
DiagnosticSeverity.INFO | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
private fun getJavaVersion(): Int = | ||
System.getProperty("java.specification.version")?.substringAfter('.')?.toIntOrNull() ?: 6 | ||
companion object { | ||
// example: foo/bar/Subject.kt:2: warning: the real message | ||
private val KAPT_LOCATION_AND_KIND_REGEX = """^(.*\.(kt|java)):(\d+): (\w+): """.toRegex() | ||
// detect things like "Note: " to be stripped from the message. | ||
// We could limit this to known diagnostic kinds (instead of matching \w:) but it is always | ||
// added so not really necessary until we hit a parser bug :) | ||
// example: "error: the real message" | ||
private val KIND_REGEX = """^(\w+): """.toRegex() | ||
// example: "[ksp] the real message" | ||
private val KSP_PREFIX_REGEX = """^\[ksp] """.toRegex() | ||
// KSP always prints an additional error if any other error occurred. | ||
// We drop that additional message to provide a more consistent error count with KAPT/javac. | ||
private const val KSP_ADDITIONAL_ERROR_MESSAGE = | ||
"Error occurred in KSP, check log for detail" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
core/src/main/kotlin/com/tschuchort/compiletesting/MutliMessageCollector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.facebook.buck.jvm.java.javax.com.tschuchort.compiletesting | ||
|
||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity | ||
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSourceLocation | ||
import org.jetbrains.kotlin.cli.common.messages.MessageCollector | ||
|
||
internal class MultiMessageCollector( | ||
private vararg val collectors: MessageCollector | ||
) : MessageCollector { | ||
|
||
override fun clear() { | ||
collectors.forEach { it.clear() } | ||
} | ||
|
||
override fun hasErrors(): Boolean { | ||
return collectors.any { it.hasErrors() } | ||
} | ||
|
||
override fun report(severity: CompilerMessageSeverity, message: String, location: CompilerMessageSourceLocation?) { | ||
collectors.forEach { it.report(severity, message, location) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missed these weird packages, fixing them separately before releasing