Skip to content

Commit 9ebe59d

Browse files
authored
Merge pull request github#11637 from igfoo/igfoo/kotlin-1.8
Kotlin: Add 1.8 support
2 parents 18a815c + 3367da8 commit 9ebe59d

File tree

21 files changed

+188
-107
lines changed

21 files changed

+188
-107
lines changed

docs/codeql/reusables/supported-versions-compilers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
Java,"Java 7 to 18 [4]_","javac (OpenJDK and Oracle JDK),
2121

2222
Eclipse compiler for Java (ECJ) [5]_",``.java``
23-
Kotlin [6]_,"Kotlin 1.5.0 to 1.7.21","kotlinc",``.kt``
23+
Kotlin [6]_,"Kotlin 1.5.0 to 1.8.0","kotlinc",``.kt``
2424
JavaScript,ECMAScript 2022 or lower,Not applicable,"``.js``, ``.jsx``, ``.mjs``, ``.es``, ``.es6``, ``.htm``, ``.html``, ``.xhtm``, ``.xhtml``, ``.vue``, ``.hbs``, ``.ejs``, ``.njk``, ``.json``, ``.yaml``, ``.yml``, ``.raml``, ``.xml`` [7]_"
2525
Python [8]_,"2.7, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10",Not applicable,``.py``
2626
Ruby [9]_,"up to 3.1",Not applicable,"``.rb``, ``.erb``, ``.gemspec``, ``Gemfile``"

java/kotlin-extractor/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ sourceSets {
4646

4747
"utils/versions/v_1_7_20-Beta/createImplicitParameterDeclarationWithWrappedDescriptor.kt",
4848
"utils/versions/v_1_7_20-Beta/allOverriddenIncludingSelf.kt",
49+
50+
"utils/versions/v_1_8_0/ExperimentalCompilerApi.kt",
51+
"utils/versions/v_1_8_0/FirIncompatiblePluginAPI.kt",
4952
]
5053
}
5154
}

java/kotlin-extractor/kotlin_plugin_versions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def version_string_to_tuple(version):
2222
return tuple([int(m.group(i)) for i in range(1, 4)] + [m.group(4)])
2323

2424
# Version number used by CI. It needs to be one of the versions in many_versions.
25-
ci_version = '1.7.20'
25+
ci_version = '1.8.0'
2626

2727
# Version numbers in the list need to be in semantically increasing order
28-
many_versions = [ '1.4.32', '1.5.0', '1.5.10', '1.5.20', '1.5.30', '1.6.0', '1.6.20', '1.7.0', '1.7.20' ]
28+
many_versions = [ '1.4.32', '1.5.0', '1.5.10', '1.5.20', '1.5.30', '1.6.0', '1.6.20', '1.7.0', '1.7.20', '1.8.0' ]
2929

3030
many_versions_tuples = [version_string_to_tuple(v) for v in many_versions]
3131

java/kotlin-extractor/src/main/kotlin/KotlinExtractorCommandLineProcessor.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package com.github.codeql
33
import org.jetbrains.kotlin.compiler.plugin.AbstractCliOption
44
import org.jetbrains.kotlin.compiler.plugin.CliOption
55
import org.jetbrains.kotlin.compiler.plugin.CommandLineProcessor
6+
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
67
import org.jetbrains.kotlin.config.CompilerConfiguration
78
import org.jetbrains.kotlin.config.CompilerConfigurationKey
89

10+
@OptIn(ExperimentalCompilerApi::class)
911
class KotlinExtractorCommandLineProcessor : CommandLineProcessor {
1012
override val pluginId = "kotlin-extractor"
1113

java/kotlin-extractor/src/main/kotlin/KotlinExtractorComponentRegistrar.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
// For ComponentRegistrar
2+
@file:Suppress("DEPRECATION")
3+
14
package com.github.codeql
25

36
import org.jetbrains.kotlin.backend.common.extensions.IrGenerationExtension
7+
import org.jetbrains.kotlin.compiler.plugin.ExperimentalCompilerApi
48
import com.intellij.mock.MockProject
59
import org.jetbrains.kotlin.compiler.plugin.ComponentRegistrar
610
import org.jetbrains.kotlin.config.CompilerConfiguration
711

12+
@OptIn(ExperimentalCompilerApi::class)
813
class KotlinExtractorComponentRegistrar : ComponentRegistrar {
914
override fun registerProjectComponents(
1015
project: MockProject,

java/kotlin-extractor/src/main/kotlin/KotlinFileExtractor.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ open class KotlinFileExtractor(
13871387

13881388
private fun getNullabilityAnnotation(t: IrType, declOrigin: IrDeclarationOrigin, existingAnnotations: List<IrConstructorCall>, javaAnnotations: Collection<JavaAnnotation>?) =
13891389
getNullabilityAnnotationName(t, declOrigin, existingAnnotations, javaAnnotations)?.let {
1390-
pluginContext.referenceClass(it)?.let { annotationClass ->
1390+
getClassByFqName(pluginContext, it)?.let { annotationClass ->
13911391
annotationClass.owner.declarations.firstIsInstanceOrNull<IrConstructor>()?.let { annotationConstructor ->
13921392
IrConstructorCallImpl.fromSymbolOwner(
13931393
UNDEFINED_OFFSET, UNDEFINED_OFFSET, annotationConstructor.returnType, annotationConstructor.symbol, 0
@@ -1709,6 +1709,10 @@ open class KotlinFileExtractor(
17091709
when (b.kind) {
17101710
IrSyntheticBodyKind.ENUM_VALUES -> tw.writeKtSyntheticBody(callable, 1)
17111711
IrSyntheticBodyKind.ENUM_VALUEOF -> tw.writeKtSyntheticBody(callable, 2)
1712+
else -> {
1713+
// TODO: Support IrSyntheticBodyKind.ENUM_ENTRIES
1714+
logger.errorElement("Unhandled synthetic body kind " + b.kind.javaClass, b)
1715+
}
17121716
}
17131717
}
17141718
}
@@ -2400,7 +2404,7 @@ open class KotlinFileExtractor(
24002404

24012405
private fun findTopLevelFunctionOrWarn(functionFilter: String, type: String, parameterTypes: Array<String>, warnAgainstElement: IrElement): IrFunction? {
24022406

2403-
val fn = pluginContext.referenceFunctions(FqName(functionFilter))
2407+
val fn = getFunctionsByFqName(pluginContext, functionFilter)
24042408
.firstOrNull { fnSymbol ->
24052409
fnSymbol.owner.parentClassOrNull?.fqNameWhenAvailable?.asString() == type &&
24062410
fnSymbol.owner.valueParameters.map { it.type.classFqName?.asString() }.toTypedArray() contentEquals parameterTypes
@@ -2419,7 +2423,7 @@ open class KotlinFileExtractor(
24192423

24202424
private fun findTopLevelPropertyOrWarn(propertyFilter: String, type: String, warnAgainstElement: IrElement): IrProperty? {
24212425

2422-
val prop = pluginContext.referenceProperties(FqName(propertyFilter))
2426+
val prop = getPropertiesByFqName(pluginContext, propertyFilter)
24232427
.firstOrNull { it.owner.parentClassOrNull?.fqNameWhenAvailable?.asString() == type }
24242428
?.owner
24252429

java/kotlin-extractor/src/main/kotlin/KotlinUsesExtractor.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ open class KotlinUsesExtractor(
4141
val globalExtensionState: KotlinExtractorGlobalState
4242
) {
4343
fun referenceExternalClass(name: String) =
44-
pluginContext.referenceClass(FqName(name))?.owner.also {
44+
getClassByFqName(pluginContext, FqName(name))?.owner.also {
4545
if (it == null)
4646
logger.warn("Unable to resolve external class $name")
4747
else
@@ -118,7 +118,7 @@ open class KotlinUsesExtractor(
118118
}
119119

120120
fun getJavaEquivalentClass(c: IrClass) =
121-
getJavaEquivalentClassId(c)?.let { pluginContext.referenceClass(it.asSingleFqName()) }?.owner
121+
getJavaEquivalentClassId(c)?.let { getClassByFqName(pluginContext, it.asSingleFqName()) }?.owner
122122

123123
/**
124124
* Gets a KotlinFileExtractor based on this one, except it attributes locations to the file that declares the given class.
@@ -328,7 +328,7 @@ open class KotlinUsesExtractor(
328328
return@getOrPut null
329329
}
330330

331-
val result = pluginContext.referenceClass(qualifiedName)?.owner
331+
val result = getClassByFqName(pluginContext, qualifiedName)?.owner
332332
if (result != null) {
333333
logger.info("Replaced synthetic class ${c.name} with its real equivalent")
334334
return@getOrPut result
@@ -337,7 +337,7 @@ open class KotlinUsesExtractor(
337337
// The above doesn't work for (some) generated nested classes, such as R$id, which should be R.id
338338
val fqn = qualifiedName.asString()
339339
if (fqn.indexOf('$') >= 0) {
340-
val nested = pluginContext.referenceClass(FqName(fqn.replace('$', '.')))?.owner
340+
val nested = getClassByFqName(pluginContext, fqn.replace('$', '.'))?.owner
341341
if (nested != null) {
342342
logger.info("Replaced synthetic nested class ${c.name} with its real equivalent")
343343
return@getOrPut nested
@@ -454,7 +454,7 @@ open class KotlinUsesExtractor(
454454
}
455455

456456
fun tryGetPair(arity: Int): Pair<IrClass, List<IrTypeArgument>?>? {
457-
val replaced = pluginContext.referenceClass(fqName)?.owner ?: return null
457+
val replaced = getClassByFqName(pluginContext, fqName)?.owner ?: return null
458458
return Pair(replaced, List(arity) { makeTypeProjection(pluginContext.irBuiltIns.anyNType, Variance.INVARIANT) })
459459
}
460460

java/kotlin-extractor/src/main/kotlin/PrimitiveTypeInfo.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import org.jetbrains.kotlin.ir.declarations.IrPackageFragment
77
import org.jetbrains.kotlin.ir.types.IrSimpleType
88
import org.jetbrains.kotlin.ir.types.classOrNull
99
import org.jetbrains.kotlin.name.FqName
10+
import com.github.codeql.utils.*
1011

1112
class PrimitiveTypeMapping(val logger: Logger, val pluginContext: IrPluginContext) {
1213
fun getPrimitiveInfo(s: IrSimpleType) =
@@ -25,7 +26,7 @@ class PrimitiveTypeMapping(val logger: Logger, val pluginContext: IrPluginContex
2526
)
2627

2728
private fun findClass(fqName: String, fallback: IrClass): IrClass {
28-
val symbol = pluginContext.referenceClass(FqName(fqName))
29+
val symbol = getClassByFqName(pluginContext, fqName)
2930
if(symbol == null) {
3031
logger.warn("Can't find $fqName")
3132
// Do the best we can
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.github.codeql.utils
2+
3+
import org.jetbrains.kotlin.backend.common.extensions.FirIncompatiblePluginAPI
4+
import org.jetbrains.kotlin.backend.common.extensions.IrPluginContext
5+
import org.jetbrains.kotlin.ir.symbols.*
6+
import org.jetbrains.kotlin.name.FqName
7+
8+
fun getClassByFqName(pluginContext: IrPluginContext, fqName: String): IrClassSymbol? {
9+
return getClassByFqName(pluginContext, FqName(fqName))
10+
}
11+
12+
fun getClassByFqName(pluginContext: IrPluginContext, fqName: FqName): IrClassSymbol? {
13+
@OptIn(FirIncompatiblePluginAPI::class)
14+
return pluginContext.referenceClass(fqName)
15+
}
16+
17+
fun getFunctionsByFqName(pluginContext: IrPluginContext, fqName: String): Collection<IrSimpleFunctionSymbol> {
18+
return getFunctionsByFqName(pluginContext, FqName(fqName))
19+
}
20+
21+
fun getFunctionsByFqName(pluginContext: IrPluginContext, fqName: FqName): Collection<IrSimpleFunctionSymbol> {
22+
@OptIn(FirIncompatiblePluginAPI::class)
23+
return pluginContext.referenceFunctions(fqName)
24+
}
25+
26+
fun getPropertiesByFqName(pluginContext: IrPluginContext, fqName: String): Collection<IrPropertySymbol> {
27+
return getPropertiesByFqName(pluginContext, FqName(fqName))
28+
}
29+
30+
fun getPropertiesByFqName(pluginContext: IrPluginContext, fqName: FqName): Collection<IrPropertySymbol> {
31+
@OptIn(FirIncompatiblePluginAPI::class)
32+
return pluginContext.referenceProperties(fqName)
33+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package org.jetbrains.kotlin.compiler.plugin
2+
3+
@RequiresOptIn("This API is experimental. There are no stability guarantees for it")
4+
annotation class ExperimentalCompilerApi

0 commit comments

Comments
 (0)