escape invalid keyword chars in include file paths and package name (#1384)

This commit is contained in:
youta ogino
2018-03-29 16:37:19 +09:00
committed by Nikolay Igotti
parent cca9280093
commit d7c49bfd46
2 changed files with 19 additions and 4 deletions
@@ -107,7 +107,7 @@ class SimpleBridgeGeneratorImpl(
"JNIEXPORT $cReturnType JNICALL $functionName ($joinedCParameters)"
}
KotlinPlatform.NATIVE -> {
val functionName = pkgName.replace('.', '_') + "_$kotlinFunctionName"
val functionName = pkgName.replace(INVALID_CLANG_IDENTIFIER_REGEX, "_") + "_$kotlinFunctionName"
kotlinLines.add("@SymbolName(${functionName.quoteAsKotlinLiteral()})")
"$cReturnType $functionName ($joinedCParameters)"
}
@@ -167,7 +167,7 @@ class SimpleBridgeGeneratorImpl(
val joinedCParameters = cFunctionParameters.joinToString { (name, type) -> "$type $name" }
val cReturnType = returnType.nativeType
val symbolName = pkgName.replace('.', '_') + "_$kotlinFunctionName"
val symbolName = pkgName.replace(INVALID_CLANG_IDENTIFIER_REGEX, "_") + "_$kotlinFunctionName"
kotlinLines.add("@konan.internal.ExportForCppRuntime(${symbolName.quoteAsKotlinLiteral()})")
val cFunctionHeader = "$cReturnType $symbolName($joinedCParameters)"
@@ -237,4 +237,8 @@ class SimpleBridgeGeneratorImpl(
nativeBacked !in excludedClients
}
}
}
companion object {
private val INVALID_CLANG_IDENTIFIER_REGEX = "[^a-zA-Z1-9_]".toRegex()
}
}
@@ -891,7 +891,14 @@ class StubGenerator(
out("@file:Suppress(${suppress.joinToString { it.quoteAsKotlinLiteral() }})")
if (pkgName != "") {
out("package $pkgName")
val packageName = pkgName.split(".").joinToString("."){
if(it.matches(VALID_PACKAGE_NAME_REGEX)){
it
}else{
"`$it`"
}
}
out("package $packageName")
out("")
}
if (platform == KotlinPlatform.NATIVE) {
@@ -1012,4 +1019,8 @@ class StubGenerator(
val mappingBridgeGenerator: MappingBridgeGenerator =
MappingBridgeGeneratorImpl(declarationMapper, simpleBridgeGenerator)
companion object {
private val VALID_PACKAGE_NAME_REGEX = "[a-zA-Z1-9_.]".toRegex()
}
}