Decompose and rewrite CheckerTestUtil to Kotlin

This commit is contained in:
victor.petukhov
2019-02-07 17:29:54 +03:00
parent 9e06e1eec0
commit f92232f015
21 changed files with 1225 additions and 1206 deletions
@@ -0,0 +1,65 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers
import org.jetbrains.kotlin.checkers.diagnostics.ActualDiagnostic
import org.jetbrains.kotlin.checkers.diagnostics.DebugInfoDiagnostic
import org.jetbrains.kotlin.checkers.diagnostics.factories.DebugInfoDiagnosticFactory0
import org.jetbrains.kotlin.checkers.utils.DebugInfoUtil
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtReferenceExpression
class CheckerDebugInfoReporter(
private val dynamicCallDescriptors: MutableList<DeclarationDescriptor>,
private val markDynamicCalls: Boolean,
private val debugAnnotations: MutableList<ActualDiagnostic>,
private val withNewInference: Boolean,
private val platform: String?
) : DebugInfoUtil.DebugInfoReporter() {
override fun reportElementWithErrorType(expression: KtReferenceExpression) {
newDiagnostic(
expression,
DebugInfoDiagnosticFactory0.ELEMENT_WITH_ERROR_TYPE
)
}
override fun reportMissingUnresolved(expression: KtReferenceExpression) {
newDiagnostic(
expression,
DebugInfoDiagnosticFactory0.MISSING_UNRESOLVED
)
}
override fun reportUnresolvedWithTarget(
expression: KtReferenceExpression,
target: String
) {
newDiagnostic(expression, DebugInfoDiagnosticFactory0.UNRESOLVED_WITH_TARGET)
}
override fun reportDynamicCall(
element: KtElement,
declarationDescriptor: DeclarationDescriptor
) {
dynamicCallDescriptors.add(declarationDescriptor)
if (markDynamicCalls) {
newDiagnostic(element, DebugInfoDiagnosticFactory0.DYNAMIC)
}
}
private fun newDiagnostic(
element: KtElement,
factory: DebugInfoDiagnosticFactory0
) {
debugAnnotations.add(
ActualDiagnostic(
DebugInfoDiagnostic(element, factory), platform, withNewInference
)
)
}
}
@@ -0,0 +1,22 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers
import com.intellij.util.containers.ContainerUtil
import org.jetbrains.kotlin.checkers.diagnostics.TextDiagnostic
class DiagnosedRange constructor(val start: Int) {
var end: Int = 0
private val diagnostics = ContainerUtil.newSmartList<TextDiagnostic>()
fun getDiagnostics(): List<TextDiagnostic> {
return diagnostics
}
fun addDiagnostic(diagnostic: String) {
diagnostics.add(TextDiagnostic.parseDiagnostic(diagnostic))
}
}
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers
import com.google.common.collect.Maps
import com.intellij.openapi.util.TextRange
import org.jetbrains.kotlin.checkers.diagnostics.AbstractTestDiagnostic
import org.jetbrains.kotlin.checkers.diagnostics.PositionalTextDiagnostic
import org.jetbrains.kotlin.checkers.diagnostics.TextDiagnostic
abstract class AbstractDiagnosticDescriptor internal constructor(val start: Int, val end: Int) {
val textRange: TextRange
get() = TextRange(start, end)
}
class ActualDiagnosticDescriptor internal constructor(start: Int, end: Int, val diagnostics: List<AbstractTestDiagnostic>) :
AbstractDiagnosticDescriptor(start, end) {
val textDiagnosticsMap: MutableMap<AbstractTestDiagnostic, TextDiagnostic>
get() {
val diagnosticMap = Maps.newLinkedHashMap<AbstractTestDiagnostic, TextDiagnostic>()
for (diagnostic in diagnostics) {
diagnosticMap[diagnostic] = TextDiagnostic.asTextDiagnostic(diagnostic)
}
return diagnosticMap
}
}
class TextDiagnosticDescriptor internal constructor(private val positionalTextDiagnostic: PositionalTextDiagnostic) :
AbstractDiagnosticDescriptor(positionalTextDiagnostic.start, positionalTextDiagnostic.end) {
val textDiagnostic: TextDiagnostic
get() = positionalTextDiagnostic.diagnostic
}
@@ -0,0 +1,16 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers
import org.jetbrains.kotlin.checkers.diagnostics.TextDiagnostic
interface DiagnosticDiffCallbacks {
fun missingDiagnostic(diagnostic: TextDiagnostic, expectedStart: Int, expectedEnd: Int)
fun wrongParametersDiagnostic(expectedDiagnostic: TextDiagnostic, actualDiagnostic: TextDiagnostic, start: Int, end: Int)
fun unexpectedDiagnostic(diagnostic: TextDiagnostic, actualStart: Int, actualEnd: Int)
}
@@ -0,0 +1,18 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers.diagnostics
interface AbstractTestDiagnostic : Comparable<AbstractTestDiagnostic> {
val name: String
val platform: String?
val inferenceCompatibility: TextDiagnostic.InferenceCompatibility
fun enhanceInferenceCompatibility(inferenceCompatibility: TextDiagnostic.InferenceCompatibility)
override fun compareTo(other: AbstractTestDiagnostic): Int
}
@@ -0,0 +1,63 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers.diagnostics
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticWithParameters1
class ActualDiagnostic constructor(val diagnostic: Diagnostic, override val platform: String?, withNewInference: Boolean) :
AbstractTestDiagnostic {
override var inferenceCompatibility = if (withNewInference)
TextDiagnostic.InferenceCompatibility.NEW
else
TextDiagnostic.InferenceCompatibility.OLD
override val name: String
get() = diagnostic.factory.name
val file: PsiFile
get() = diagnostic.psiFile
override fun compareTo(other: AbstractTestDiagnostic): Int {
return if (this.diagnostic is DiagnosticWithParameters1<*, *> && other is ActualDiagnostic && other.diagnostic is DiagnosticWithParameters1<*, *>) {
(name + this.diagnostic.a).compareTo(other.name + other.diagnostic.a)
} else if (this.diagnostic is DiagnosticWithParameters1<*, *>) {
(name + this.diagnostic.a).compareTo(other.name)
} else if (other is ActualDiagnostic && other.diagnostic is DiagnosticWithParameters1<*, *>) {
name.compareTo(other.name + other.diagnostic.a)
} else {
name.compareTo(other.name)
}
}
override fun enhanceInferenceCompatibility(inferenceCompatibility: TextDiagnostic.InferenceCompatibility) {
this.inferenceCompatibility = inferenceCompatibility
}
override fun equals(other: Any?): Boolean {
if (other !is ActualDiagnostic) return false
// '==' on diagnostics is intentional here
return other.diagnostic === diagnostic &&
(if (other.platform == null) platform == null else other.platform == platform) &&
other.inferenceCompatibility == inferenceCompatibility
}
override fun hashCode(): Int {
var result = System.identityHashCode(diagnostic)
result = 31 * result + (platform?.hashCode() ?: 0)
result = 31 * result + inferenceCompatibility.hashCode()
return result
}
override fun toString(): String {
val inferenceAbbreviation = inferenceCompatibility.abbreviation
return (if (inferenceAbbreviation != null) inferenceAbbreviation + ";" else "") +
(if (platform != null) "$platform:" else "") +
diagnostic.toString()
}
}
@@ -0,0 +1,47 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers.diagnostics
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiErrorElement
import com.intellij.psi.PsiFile
import org.jetbrains.kotlin.checkers.diagnostics.factories.SyntaxErrorDiagnosticFactory
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory
import org.jetbrains.kotlin.diagnostics.Severity
import org.jetbrains.kotlin.psi.KtElement
class DebugInfoDiagnostic(element: KtElement, factory: DiagnosticFactory<*>) : AbstractDiagnosticForTests(element, factory)
class SyntaxErrorDiagnostic(errorElement: PsiErrorElement) : AbstractDiagnosticForTests(errorElement,
SyntaxErrorDiagnosticFactory.INSTANCE
)
open class AbstractDiagnosticForTests(private val element: PsiElement, private val factory: DiagnosticFactory<*>) : Diagnostic {
override fun getFactory(): DiagnosticFactory<*> {
return factory
}
override fun getSeverity(): Severity {
return Severity.ERROR
}
override fun getPsiElement(): PsiElement {
return element
}
override fun getTextRanges(): List<TextRange> {
return listOf(element.textRange)
}
override fun getPsiFile(): PsiFile {
return element.containingFile
}
override fun isValid(): Boolean {
return true
}
}
@@ -1,19 +1,8 @@
/*
* 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.
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers.diagnostics
data class PositionalTextDiagnostic(val diagnostic: CheckerTestUtil.TextDiagnostic, val start: Int, val end: Int)
data class PositionalTextDiagnostic(val diagnostic: TextDiagnostic, val start: Int, val end: Int)
@@ -0,0 +1,165 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers.diagnostics
import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.SmartList
import com.intellij.util.containers.ContainerUtil
import org.jetbrains.kotlin.checkers.utils.CheckerTestUtil
import org.jetbrains.kotlin.checkers.utils.CheckerTestUtil.INDIVIDUAL_PARAMETER_PATTERN
import org.jetbrains.kotlin.checkers.utils.CheckerTestUtil.SHOULD_BE_ESCAPED
import org.jetbrains.kotlin.diagnostics.rendering.AbstractDiagnosticWithParametersRenderer
import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages
import java.util.regex.Pattern
class TextDiagnostic(
override val name: String,
override val platform: String?,
val parameters: List<String>?,
inference: InferenceCompatibility?
) : AbstractTestDiagnostic {
override var inferenceCompatibility = inference ?: InferenceCompatibility.ALL
val description: String
get() = (if (platform != null) "$platform:" else "") + name
enum class InferenceCompatibility constructor(internal var abbreviation: String?) {
NEW(CheckerTestUtil.NEW_INFERENCE_PREFIX), OLD(CheckerTestUtil.OLD_INFERENCE_PREFIX), ALL(null);
fun isCompatible(other: InferenceCompatibility): Boolean {
return this == other || this == ALL || other == ALL
}
}
override fun compareTo(other: AbstractTestDiagnostic): Int {
return name.compareTo(other.name)
}
override fun enhanceInferenceCompatibility(inferenceCompatibility: InferenceCompatibility) {
this.inferenceCompatibility = inferenceCompatibility
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other == null || javaClass != other.javaClass) return false
val that = other as TextDiagnostic?
if (name != that!!.name) return false
if (if (platform != null) platform != that.platform else that.platform != null) return false
if (if (parameters != null) parameters != that.parameters else that.parameters != null) return false
return inferenceCompatibility == that.inferenceCompatibility
}
override fun hashCode(): Int {
var result = name.hashCode()
result = 31 * result + (platform?.hashCode() ?: 0)
result = 31 * result + (parameters?.hashCode() ?: 0)
result = 31 * result + inferenceCompatibility.hashCode()
return result
}
fun asString(): String {
val result = StringBuilder()
if (inferenceCompatibility.abbreviation != null) {
result.append(inferenceCompatibility.abbreviation)
result.append(";")
}
if (platform != null) {
result.append(platform)
result.append(":")
}
result.append(name)
if (parameters != null) {
result.append("(")
result.append(StringUtil.join(parameters, { escape(it) }, "; "))
result.append(")")
}
return result.toString()
}
override fun toString(): String {
return asString()
}
companion object {
private fun escape(s: String): String {
return s.replace("([$SHOULD_BE_ESCAPED])".toRegex(), "\\\\$1")
}
private fun unescape(s: String): String {
return s.replace("\\\\([$SHOULD_BE_ESCAPED])".toRegex(), "$1")
}
fun parseDiagnostic(text: String): TextDiagnostic {
val matcher = CheckerTestUtil.individualDiagnosticPattern.matcher(text)
if (!matcher.find())
throw IllegalArgumentException("Could not parse diagnostic: $text")
val inference = computeInferenceCompatibility(
extractDataBefore(
matcher.group(1),
";"
)
)
val platform =
extractDataBefore(matcher.group(2), ":")
val name = matcher.group(3)
val parameters = matcher.group(4) ?: return TextDiagnostic(
name,
platform,
null,
inference
)
val parsedParameters = SmartList<String>()
val parametersMatcher = INDIVIDUAL_PARAMETER_PATTERN.matcher(parameters)
while (parametersMatcher.find())
parsedParameters.add(unescape(parametersMatcher.group().trim({ it <= ' ' })))
return TextDiagnostic(name, platform, parsedParameters, inference)
}
private fun computeInferenceCompatibility(abbreviation: String?): InferenceCompatibility {
return if (abbreviation == null) InferenceCompatibility.ALL else InferenceCompatibility.values().single { inference -> abbreviation == inference.abbreviation }
}
private fun extractDataBefore(prefix: String?, anchor: String): String? {
assert(prefix == null || prefix.endsWith(anchor)) { prefix ?: "" }
return prefix?.substringBeforeLast(anchor, prefix)
}
fun asTextDiagnostic(abstractTestDiagnostic: AbstractTestDiagnostic): TextDiagnostic {
return if (abstractTestDiagnostic is ActualDiagnostic) {
asTextDiagnostic(abstractTestDiagnostic)
} else abstractTestDiagnostic as TextDiagnostic
}
private fun asTextDiagnostic(actualDiagnostic: ActualDiagnostic): TextDiagnostic {
val diagnostic = actualDiagnostic.diagnostic
val renderer = DefaultErrorMessages.getRendererForDiagnostic(diagnostic)
val diagnosticName = actualDiagnostic.name
if (renderer is AbstractDiagnosticWithParametersRenderer) {
val renderParameters = renderer.renderParameters(diagnostic)
val parameters = ContainerUtil.map(renderParameters, { it.toString() })
return TextDiagnostic(
diagnosticName,
actualDiagnostic.platform,
parameters,
actualDiagnostic.inferenceCompatibility
)
}
return TextDiagnostic(
diagnosticName,
actualDiagnostic.platform,
null,
actualDiagnostic.inferenceCompatibility
)
}
}
}
@@ -0,0 +1,19 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers.diagnostics.factories
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.resolve.BindingContext
interface DebugInfoDiagnosticFactory {
val withExplicitDefinitionOnly: Boolean
fun createDiagnostic(
expression: KtExpression,
bindingContext: BindingContext
): Diagnostic
}
@@ -0,0 +1,59 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers.diagnostics.factories
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.checkers.diagnostics.DebugInfoDiagnostic
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory0
import org.jetbrains.kotlin.diagnostics.PositioningStrategies
import org.jetbrains.kotlin.diagnostics.Severity
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.resolve.BindingContext
class DebugInfoDiagnosticFactory0 : DiagnosticFactory0<PsiElement>,
DebugInfoDiagnosticFactory {
private val name: String
override val withExplicitDefinitionOnly: Boolean
override fun createDiagnostic(
expression: KtExpression,
bindingContext: BindingContext
): Diagnostic {
return DebugInfoDiagnostic(expression, this)
}
private constructor(name: String, severity: Severity = Severity.ERROR) : super(severity, PositioningStrategies.DEFAULT) {
this.name = name
this.withExplicitDefinitionOnly = false
}
private constructor(name: String, severity: Severity, withExplicitDefinitionOnly: Boolean) : super(
severity,
PositioningStrategies.DEFAULT
) {
this.name = name
this.withExplicitDefinitionOnly = withExplicitDefinitionOnly
}
override fun getName(): String {
return "DEBUG_INFO_$name"
}
companion object {
val SMARTCAST = DebugInfoDiagnosticFactory0("SMARTCAST", Severity.INFO)
val IMPLICIT_RECEIVER_SMARTCAST =
DebugInfoDiagnosticFactory0("IMPLICIT_RECEIVER_SMARTCAST", Severity.INFO)
val CONSTANT = DebugInfoDiagnosticFactory0("CONSTANT", Severity.INFO)
val LEAKING_THIS = DebugInfoDiagnosticFactory0("LEAKING_THIS")
val IMPLICIT_EXHAUSTIVE =
DebugInfoDiagnosticFactory0("IMPLICIT_EXHAUSTIVE", Severity.INFO)
val ELEMENT_WITH_ERROR_TYPE = DebugInfoDiagnosticFactory0("ELEMENT_WITH_ERROR_TYPE")
val UNRESOLVED_WITH_TARGET = DebugInfoDiagnosticFactory0("UNRESOLVED_WITH_TARGET")
val MISSING_UNRESOLVED = DebugInfoDiagnosticFactory0("MISSING_UNRESOLVED")
val DYNAMIC = DebugInfoDiagnosticFactory0("DYNAMIC", Severity.INFO)
}
}
@@ -0,0 +1,20 @@
/*
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers.diagnostics.factories
import org.jetbrains.kotlin.checkers.diagnostics.SyntaxErrorDiagnostic
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory
import org.jetbrains.kotlin.diagnostics.Severity
class SyntaxErrorDiagnosticFactory private constructor() : DiagnosticFactory<SyntaxErrorDiagnostic>(Severity.ERROR) {
override fun getName(): String {
return "SYNTAX"
}
companion object {
val INSTANCE = SyntaxErrorDiagnosticFactory()
}
}
File diff suppressed because it is too large Load Diff
@@ -1,20 +1,9 @@
/*
* Copyright 2010-2015 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.
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.checkers;
package org.jetbrains.kotlin.checkers.utils;
import com.intellij.psi.PsiElement;
import com.intellij.psi.tree.IElementType;