[Native][tests] Keep reflection package name when renaming packages
The test engine renames packages in test files to allow group many tests into single compilation for performance reasons. As a result, reflection APIs return different package names. To deal with this, the test engine uses certain heuristics to detect tests using reflection APIs, and disables grouping (and thus package renaming) for them. This commit provides an alternative solution for the same problem -- now the test engine instructs the compiler to use original package names for reflection information, by means of the introduced `ReflectionPackageName` annotation.
This commit is contained in:
+21
-11
@@ -152,6 +152,7 @@ private class ExtTestDataFile(
|
||||
val args = mutableListOf<String>()
|
||||
testDataFileSettings.languageSettings.sorted().mapTo(args) { "-XXLanguage:$it" }
|
||||
testDataFileSettings.optInsForCompiler.sorted().mapTo(args) { "-opt-in=$it" }
|
||||
args += "-opt-in=kotlin.native.internal.InternalForKotlinNativeTests" // for ReflectionPackageName
|
||||
if (testDataFileSettings.expectActualLinker) args += "-Xexpect-actual-linker"
|
||||
return TestCompilerArgs(args)
|
||||
}
|
||||
@@ -281,6 +282,12 @@ private class ExtTestDataFile(
|
||||
file.addAfter(newPackageDirective, file.fileAnnotationList).ensureSurroundedByWhiteSpace()
|
||||
}
|
||||
|
||||
// Add @ReflectionPackageName annotation to make the compiler use original package name in the reflective information.
|
||||
val annotationText =
|
||||
"kotlin.native.internal.ReflectionPackageName(${oldPackageName.asString().quoteAsKotlinStringLiteral()})"
|
||||
val fileAnnotationList = handler.psiFactory.createFileAnnotationListWithAnnotation(annotationText)
|
||||
file.addAnnotations(fileAnnotationList)
|
||||
|
||||
visitKtElement(file, file.collectAccessibleDeclarationNames())
|
||||
}
|
||||
|
||||
@@ -468,23 +475,13 @@ private class ExtTestDataFile(
|
||||
filesToTransform.forEach { handler ->
|
||||
handler.accept(object : KtTreeVisitorVoid() {
|
||||
override fun visitKtFile(file: KtFile) {
|
||||
val oldFileAnnotationList = file.fileAnnotationList
|
||||
|
||||
val newFileAnnotationList = handler.psiFactory.createFile(buildString {
|
||||
testDataFileSettings.optInsForSourceCode.forEach {
|
||||
appendLine(getAnnotationText(it))
|
||||
}
|
||||
}).fileAnnotationList!!
|
||||
|
||||
if (oldFileAnnotationList != null) {
|
||||
// Add new annotations to the old ones.
|
||||
newFileAnnotationList.annotationEntries.forEach {
|
||||
oldFileAnnotationList.add(it).ensureSurroundedByWhiteSpace()
|
||||
}
|
||||
} else {
|
||||
// Insert the annotations list immediately before package directive.
|
||||
file.addBefore(newFileAnnotationList, file.packageDirective).ensureSurroundedByWhiteSpace()
|
||||
}
|
||||
file.addAnnotations(newFileAnnotationList)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -493,6 +490,19 @@ private class ExtTestDataFile(
|
||||
return allFileLevelAnnotationsSorted
|
||||
}
|
||||
|
||||
private fun KtFile.addAnnotations(fileAnnotationList: KtFileAnnotationList) {
|
||||
val oldFileAnnotationList = this.fileAnnotationList
|
||||
if (oldFileAnnotationList != null) {
|
||||
// Add new annotations to the old ones.
|
||||
fileAnnotationList.annotationEntries.forEach {
|
||||
oldFileAnnotationList.add(it).ensureSurroundedByWhiteSpace()
|
||||
}
|
||||
} else {
|
||||
// Insert the annotations list immediately before package directive.
|
||||
this.addBefore(fileAnnotationList, packageDirective).ensureSurroundedByWhiteSpace()
|
||||
}
|
||||
}
|
||||
|
||||
/** Finds the fully-qualified name of the entry point function (aka `fun box(): String`). */
|
||||
private fun findEntryPoint(): String = with(structure) {
|
||||
val result = mutableListOf<String>()
|
||||
|
||||
+25
@@ -31,3 +31,28 @@ internal const val LAUNCHER_MODULE_NAME = "__launcher__" // Used only in KLIB te
|
||||
internal const val STATIC_CACHE_DIR_NAME = "__static_cache__"
|
||||
|
||||
internal fun prettyHash(hash: Int): String = hash.toUInt().toString(16).padStart(8, '0')
|
||||
|
||||
/**
|
||||
* Returns the expression to be parsed by Kotlin as string literal with given contents,
|
||||
* i.e. transforms `foo$bar` to `"foo\$bar"`.
|
||||
*/
|
||||
internal fun String.quoteAsKotlinStringLiteral(): String = buildString {
|
||||
append('"')
|
||||
|
||||
this@quoteAsKotlinStringLiteral.forEach { c ->
|
||||
when (c) {
|
||||
in charactersAllowedInKotlinStringLiterals -> append(c)
|
||||
'$' -> append("\\$")
|
||||
else -> append("\\u" + "%04X".format(c.code))
|
||||
}
|
||||
}
|
||||
|
||||
append('"')
|
||||
}
|
||||
|
||||
private val charactersAllowedInKotlinStringLiterals: Set<Char> = mutableSetOf<Char>().apply {
|
||||
addAll('a' .. 'z')
|
||||
addAll('A' .. 'Z')
|
||||
addAll('0' .. '9')
|
||||
addAll(listOf('_', '@', ':', ';', '.', ',', '{', '}', '=', '[', ']', '^', '#', '*', ' ', '(', ')'))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user