From 69334271d8bd2d0a332aefa3bda31f123cfe3e9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Doln=C3=ADk?= Date: Mon, 27 Nov 2023 10:39:57 +0100 Subject: [PATCH] Add configuration to suppress name collision warnings. --- SKIE/acceptance-tests | 2 +- .../annotations/SuppressSkieWarning.kt | 12 ++++++++++++ .../skie/configuration/SuppressSkieWarning.kt | 19 +++++++++++++++++++ .../util/SirDeclaration+resolveCollision.kt | 17 +++++++++++++++-- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 SKIE/common/configuration/annotations/src/commonMain/kotlin/co/touchlab/skie/configuration/annotations/SuppressSkieWarning.kt create mode 100644 SKIE/common/configuration/declaration/src/commonMain/kotlin/co/touchlab/skie/configuration/SuppressSkieWarning.kt diff --git a/SKIE/acceptance-tests b/SKIE/acceptance-tests index 649ca559..4aa51d1b 160000 --- a/SKIE/acceptance-tests +++ b/SKIE/acceptance-tests @@ -1 +1 @@ -Subproject commit 649ca55979892c6cb58b14563e28347f5d3b0025 +Subproject commit 4aa51d1b81e54f23a4882d12bb5138bb751985d1 diff --git a/SKIE/common/configuration/annotations/src/commonMain/kotlin/co/touchlab/skie/configuration/annotations/SuppressSkieWarning.kt b/SKIE/common/configuration/annotations/src/commonMain/kotlin/co/touchlab/skie/configuration/annotations/SuppressSkieWarning.kt new file mode 100644 index 00000000..bb305381 --- /dev/null +++ b/SKIE/common/configuration/annotations/src/commonMain/kotlin/co/touchlab/skie/configuration/annotations/SuppressSkieWarning.kt @@ -0,0 +1,12 @@ +package co.touchlab.skie.configuration.annotations + +@Target +annotation class SuppressSkieWarning { + + /** + * Suppresses a warning about SKIE renaming a declaration because of a name collision. + */ + @Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.CONSTRUCTOR) + @Retention(AnnotationRetention.BINARY) + annotation class NameCollision(val suppress: Boolean = true) +} diff --git a/SKIE/common/configuration/declaration/src/commonMain/kotlin/co/touchlab/skie/configuration/SuppressSkieWarning.kt b/SKIE/common/configuration/declaration/src/commonMain/kotlin/co/touchlab/skie/configuration/SuppressSkieWarning.kt new file mode 100644 index 00000000..f6e8780b --- /dev/null +++ b/SKIE/common/configuration/declaration/src/commonMain/kotlin/co/touchlab/skie/configuration/SuppressSkieWarning.kt @@ -0,0 +1,19 @@ +package co.touchlab.skie.configuration + +import co.touchlab.skie.configuration.annotations.SuppressSkieWarning + +object SuppressSkieWarning { + + /** + * If true, SKIE will not raise a warning that it renamed the given declaration because of a name collision. + */ + object NameCollision : ConfigurationKey.Boolean { + + override val defaultValue: Boolean = false + + override val skieRuntimeValue: Boolean = false + + override fun getAnnotationValue(configurationTarget: ConfigurationTarget): Boolean? = + configurationTarget.findAnnotation()?.suppress + } +} diff --git a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/util/SirDeclaration+resolveCollision.kt b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/util/SirDeclaration+resolveCollision.kt index 47321230..9dd20c00 100644 --- a/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/util/SirDeclaration+resolveCollision.kt +++ b/SKIE/compiler/kotlin-plugin/src/kgp_common/kotlin/co/touchlab/skie/util/SirDeclaration+resolveCollision.kt @@ -2,7 +2,10 @@ package co.touchlab.skie.util +import co.touchlab.skie.configuration.SuppressSkieWarning +import co.touchlab.skie.configuration.getConfiguration import co.touchlab.skie.kir.element.KirCallableDeclaration +import co.touchlab.skie.kir.element.KirClass import co.touchlab.skie.kir.element.KirElement import co.touchlab.skie.kir.element.KirEnumEntry import co.touchlab.skie.kir.element.classDescriptorOrNull @@ -114,7 +117,7 @@ private inline fun T.resolveCollisionWithWarning( val newName = getName() val kirElement = findKirElement() - if (kirElement != null) { + if (kirElement != null && kirElement.shouldReportCollision) { reportCollision( originalName = originalName, newName = newName, @@ -132,6 +135,15 @@ private inline fun T.resolveCollision(collisionReasonProvider: T.() -> Strin } while (collisionReasonProvider() != null) } +context(SirPhase.Context) +private val KirElement.shouldReportCollision: Boolean + get() = when (this) { + is KirCallableDeclaration<*> -> !getConfiguration(SuppressSkieWarning.NameCollision) + is KirClass -> !getConfiguration(SuppressSkieWarning.NameCollision) + is KirEnumEntry -> owner.shouldReportCollision + else -> true + } + context(SirPhase.Context) private fun reportCollision( originalName: String, @@ -142,7 +154,8 @@ private fun reportCollision( reporter.warning( message = "'$originalName' was renamed to '$newName' because of a name collision with $collisionReason. " + "Consider resolving the conflict either by changing the name in Kotlin, or via the @ObjCName annotation. " + - "Using renamed declarations from Swift is not recommended because their name will change if the conflict is resolved.", + "You can also suppress this warning using the 'SuppressSkieWarning.NameCollision' configuration. " + + "However using renamed declarations from Swift is not recommended because their name will change if the conflict is resolved.", declaration = declarationDescriptor, ) }