Reduce allocations in KotlinTarget and modifier checkers
This commit is contained in:
committed by
TeamCityServer
parent
2c1c24c042
commit
f2ffead881
+8
-6
@@ -6,7 +6,6 @@
|
|||||||
package org.jetbrains.kotlin.fir.analysis.checkers.declaration
|
package org.jetbrains.kotlin.fir.analysis.checkers.declaration
|
||||||
|
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget.*
|
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget.Companion.classActualTargets
|
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget.Companion.classActualTargets
|
||||||
import org.jetbrains.kotlin.fir.FirFakeSourceElementKind
|
import org.jetbrains.kotlin.fir.FirFakeSourceElementKind
|
||||||
import org.jetbrains.kotlin.fir.FirSourceElement
|
import org.jetbrains.kotlin.fir.FirSourceElement
|
||||||
@@ -53,9 +52,12 @@ object FirModifierChecker : FirBasicDeclarationChecker() {
|
|||||||
context: CheckerContext,
|
context: CheckerContext,
|
||||||
reporter: DiagnosticReporter
|
reporter: DiagnosticReporter
|
||||||
) {
|
) {
|
||||||
|
if (list.modifiers.isEmpty()) return
|
||||||
|
|
||||||
// general strategy: report no more than one error and any number of warnings
|
// general strategy: report no more than one error and any number of warnings
|
||||||
// therefore, a track of nodes with already reported errors should be kept
|
// therefore, a track of nodes with already reported errors should be kept
|
||||||
val reportedNodes = hashSetOf<FirModifier<*>>()
|
val reportedNodes = hashSetOf<FirModifier<*>>()
|
||||||
|
|
||||||
val actualTargets = getActualTargetList(owner).defaultTargets
|
val actualTargets = getActualTargetList(owner).defaultTargets
|
||||||
|
|
||||||
val parent = context.findClosest<FirDeclaration> {
|
val parent = context.findClosest<FirDeclaration> {
|
||||||
@@ -65,17 +67,17 @@ object FirModifierChecker : FirBasicDeclarationChecker() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
val actualParents = when (parent) {
|
val actualParents = when (parent) {
|
||||||
is FirAnonymousObject -> listOf(LOCAL_CLASS, CLASS)
|
is FirAnonymousObject -> KotlinTarget.LOCAL_CLASS_LIST
|
||||||
is FirClass -> classActualTargets(
|
is FirClass -> classActualTargets(
|
||||||
parent.classKind,
|
parent.classKind,
|
||||||
isInnerClass = (parent as? FirMemberDeclaration)?.isInner ?: false,
|
isInnerClass = (parent as? FirMemberDeclaration)?.isInner ?: false,
|
||||||
isCompanionObject = (parent as? FirRegularClass)?.isCompanion ?: false,
|
isCompanionObject = (parent as? FirRegularClass)?.isCompanion ?: false,
|
||||||
isLocalClass = parent.isLocal
|
isLocalClass = parent.isLocal
|
||||||
)
|
)
|
||||||
is FirPropertyAccessor -> listOf(if (parent.isSetter) PROPERTY_SETTER else PROPERTY_GETTER)
|
is FirPropertyAccessor -> if (parent.isSetter) KotlinTarget.PROPERTY_SETTER_LIST else KotlinTarget.PROPERTY_GETTER_LIST
|
||||||
is FirFunction -> listOf(FUNCTION)
|
is FirFunction -> KotlinTarget.FUNCTION_LIST
|
||||||
is FirEnumEntry -> listOf(ENUM_ENTRY, PROPERTY, FIELD)
|
is FirEnumEntry -> KotlinTarget.ENUM_ENTRY_LIST
|
||||||
else -> listOf(FILE)
|
else -> KotlinTarget.FILE_LIST
|
||||||
}
|
}
|
||||||
|
|
||||||
val modifiers = list.modifiers
|
val modifiers = list.modifiers
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ import org.jetbrains.kotlin.config.LanguageFeature
|
|||||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget
|
||||||
import org.jetbrains.kotlin.descriptors.annotations.KotlinTarget.*
|
|
||||||
import org.jetbrains.kotlin.diagnostics.Errors
|
import org.jetbrains.kotlin.diagnostics.Errors
|
||||||
import org.jetbrains.kotlin.lexer.KtKeywordToken
|
import org.jetbrains.kotlin.lexer.KtKeywordToken
|
||||||
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
import org.jetbrains.kotlin.lexer.KtModifierKeywordToken
|
||||||
@@ -206,10 +205,10 @@ object ModifierCheckerCore {
|
|||||||
isCompanionObject = parentDescriptor.isCompanionObject,
|
isCompanionObject = parentDescriptor.isCompanionObject,
|
||||||
isLocalClass = DescriptorUtils.isLocal(parentDescriptor)
|
isLocalClass = DescriptorUtils.isLocal(parentDescriptor)
|
||||||
)
|
)
|
||||||
is PropertySetterDescriptor -> listOf(PROPERTY_SETTER)
|
is PropertySetterDescriptor -> KotlinTarget.PROPERTY_SETTER_LIST
|
||||||
is PropertyGetterDescriptor -> listOf(PROPERTY_GETTER)
|
is PropertyGetterDescriptor -> KotlinTarget.PROPERTY_GETTER_LIST
|
||||||
is FunctionDescriptor -> listOf(FUNCTION)
|
is FunctionDescriptor -> KotlinTarget.FUNCTION_LIST
|
||||||
else -> listOf(FILE)
|
else -> KotlinTarget.FILE_LIST
|
||||||
}
|
}
|
||||||
val deprecatedParents = deprecatedParentTargetMap[modifierType]
|
val deprecatedParents = deprecatedParentTargetMap[modifierType]
|
||||||
if (deprecatedParents != null && actualParents.any { it in deprecatedParents }) {
|
if (deprecatedParents != null && actualParents.any { it in deprecatedParents }) {
|
||||||
|
|||||||
+22
-10
@@ -76,37 +76,49 @@ enum class KotlinTarget(val description: String, val isDefault: Boolean = true)
|
|||||||
fun valueOrNull(name: String): KotlinTarget? = map[name]
|
fun valueOrNull(name: String): KotlinTarget? = map[name]
|
||||||
|
|
||||||
val DEFAULT_TARGET_SET: Set<KotlinTarget> = values().filter { it.isDefault }.toSet()
|
val DEFAULT_TARGET_SET: Set<KotlinTarget> = values().filter { it.isDefault }.toSet()
|
||||||
|
|
||||||
val ALL_TARGET_SET: Set<KotlinTarget> = values().toSet()
|
val ALL_TARGET_SET: Set<KotlinTarget> = values().toSet()
|
||||||
|
|
||||||
|
val ANNOTATION_CLASS_LIST = listOf(ANNOTATION_CLASS, CLASS)
|
||||||
|
val LOCAL_CLASS_LIST = listOf(LOCAL_CLASS, CLASS)
|
||||||
|
val CLASS_LIST = listOf(CLASS_ONLY, CLASS)
|
||||||
|
val COMPANION_OBJECT_LIST = listOf(COMPANION_OBJECT, OBJECT, CLASS)
|
||||||
|
val OBJECT_LIST = listOf(OBJECT, CLASS)
|
||||||
|
val INTERFACE_LIST = listOf(INTERFACE, CLASS)
|
||||||
|
val ENUM_LIST = listOf(ENUM_CLASS, CLASS)
|
||||||
|
val ENUM_ENTRY_LIST = listOf(ENUM_ENTRY, PROPERTY, FIELD)
|
||||||
|
val PROPERTY_SETTER_LIST = listOf(PROPERTY_SETTER)
|
||||||
|
val PROPERTY_GETTER_LIST = listOf(PROPERTY_GETTER)
|
||||||
|
val FUNCTION_LIST = listOf(FUNCTION)
|
||||||
|
val FILE_LIST = listOf(FILE)
|
||||||
|
|
||||||
fun classActualTargets(
|
fun classActualTargets(
|
||||||
kind: ClassKind,
|
kind: ClassKind,
|
||||||
isInnerClass: Boolean,
|
isInnerClass: Boolean,
|
||||||
isCompanionObject: Boolean,
|
isCompanionObject: Boolean,
|
||||||
isLocalClass: Boolean
|
isLocalClass: Boolean
|
||||||
): List<KotlinTarget> = when (kind) {
|
): List<KotlinTarget> = when (kind) {
|
||||||
ClassKind.ANNOTATION_CLASS -> listOf(ANNOTATION_CLASS, CLASS)
|
ClassKind.ANNOTATION_CLASS -> ANNOTATION_CLASS_LIST
|
||||||
ClassKind.CLASS ->
|
ClassKind.CLASS ->
|
||||||
// inner local classes should be CLASS_ONLY, not LOCAL_CLASS
|
// inner local classes should be CLASS_ONLY, not LOCAL_CLASS
|
||||||
if (!isInnerClass && isLocalClass) {
|
if (!isInnerClass && isLocalClass) {
|
||||||
listOf(LOCAL_CLASS, CLASS)
|
LOCAL_CLASS_LIST
|
||||||
} else {
|
} else {
|
||||||
listOf(CLASS_ONLY, CLASS)
|
CLASS_LIST
|
||||||
}
|
}
|
||||||
ClassKind.OBJECT ->
|
ClassKind.OBJECT ->
|
||||||
if (isCompanionObject) {
|
if (isCompanionObject) {
|
||||||
listOf(COMPANION_OBJECT, OBJECT, CLASS)
|
COMPANION_OBJECT_LIST
|
||||||
} else {
|
} else {
|
||||||
listOf(OBJECT, CLASS)
|
OBJECT_LIST
|
||||||
}
|
}
|
||||||
ClassKind.INTERFACE -> listOf(INTERFACE, CLASS)
|
ClassKind.INTERFACE -> INTERFACE_LIST
|
||||||
ClassKind.ENUM_CLASS ->
|
ClassKind.ENUM_CLASS ->
|
||||||
if (isLocalClass) {
|
if (isLocalClass) {
|
||||||
listOf(LOCAL_CLASS, CLASS)
|
LOCAL_CLASS_LIST
|
||||||
} else {
|
} else {
|
||||||
listOf(ENUM_CLASS, CLASS)
|
ENUM_LIST
|
||||||
}
|
}
|
||||||
ClassKind.ENUM_ENTRY -> listOf(ENUM_ENTRY, PROPERTY, FIELD)
|
ClassKind.ENUM_ENTRY -> ENUM_ENTRY_LIST
|
||||||
}
|
}
|
||||||
|
|
||||||
val USE_SITE_MAPPING: Map<AnnotationUseSiteTarget, KotlinTarget> = mapOf(
|
val USE_SITE_MAPPING: Map<AnnotationUseSiteTarget, KotlinTarget> = mapOf(
|
||||||
|
|||||||
Reference in New Issue
Block a user