From bc13173ea99e8c996bda8c70a68e251c137bbe77 Mon Sep 17 00:00:00 2001 From: Johan Bay Date: Wed, 12 Oct 2022 16:54:00 +0000 Subject: [PATCH] [K/N] cinterop: Do not expand non-constant macros Expanding macros such as __FILE__ or __TIME__ exposes arbitrary generated filenames and timestamps from the compiler pipeline which are not useful for interop and makes the klib generation non-deterministic. This patch instead redefines the macros to just map to their name in the properties available from Kotlin. Co-authored-by: Johan Bay --- .../kotlin/native/interop/indexer/MacroConstants.kt | 9 ++++++++- kotlin-native/backend.native/tests/build.gradle | 10 ++++++++++ .../backend.native/tests/interop/kt54284/kt54284.def | 7 +++++++ .../backend.native/tests/interop/kt54284/main.kt | 12 ++++++++++++ 4 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 kotlin-native/backend.native/tests/interop/kt54284/kt54284.def create mode 100644 kotlin-native/backend.native/tests/interop/kt54284/main.kt diff --git a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt index eed66ccf8b3..ef75406761a 100644 --- a/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt +++ b/kotlin-native/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/MacroConstants.kt @@ -60,6 +60,13 @@ private fun expandMacros( // or function-like construction (e.g. #define FOO throw()) but such a function is undeclared. compilerArgs += "-Werror=implicit-function-declaration" + // Some predefined macros expand to contextual values that won't make sense to expose in Kotlin properties. + // We instead redefined them to string values of the macro name. + compilerArgs += "-Wno-builtin-macro-redefined" + val predefinedMacros = listOf("__DATE__", "__TIME__", "__TIMESTAMP__", "__FILE__", "__FILE_NAME__", "__BASE_FILE__", "__LINE__") + predefinedMacros.forEach { + compilerArgs += "-D${it}=\"${it}\"" + } // Ensure libclang reports all errors: compilerArgs += "-ferror-limit=0" @@ -319,4 +326,4 @@ private fun canMacroBeConstant(cursor: CValue): Boolean { // Requires updating to 3.9.1 due to https://bugs.llvm.org//show_bug.cgi?id=9069 return true -} \ No newline at end of file +} diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index 3498abfe6fb..fa724c9d231 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -4285,6 +4285,10 @@ createInterop("kt51925") { it.defFile 'interop/kt51925/kt51925.def' } +createInterop("kt54284") { + it.defFile 'interop/kt54284/kt54284.def' +} + createInterop("kt43502") { it.defFile 'interop/kt43502/kt43502.def' it.headers "$projectDir/interop/kt43502/kt43502.h" @@ -4759,6 +4763,12 @@ interopTest("interop_kt44283") { source = "interop/kt44283/main.kt" } +interopTest("interop_kt54284") { + disabled = (project.testTarget == 'wasm32') // No interop for wasm yet. + interop = 'kt54284' + source = "interop/kt54284/main.kt" +} + // TODO: This test should be run with caches on. interopTest("interop_kt51925") { disabled = (project.testTarget == 'wasm32') // No interop for wasm yet. diff --git a/kotlin-native/backend.native/tests/interop/kt54284/kt54284.def b/kotlin-native/backend.native/tests/interop/kt54284/kt54284.def new file mode 100644 index 00000000000..f98693f82bb --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/kt54284/kt54284.def @@ -0,0 +1,7 @@ +--- +#define KFILE "FILE:" __FILE__ +#define KLINE "LINE:" __LINE__ +#define KTIME "TIME:" __TIME__ +#define KDATE "DATE:" __DATE__ +#define KFILENAME "NAME:" __FILE_NAME__ +#define KBASEFILE "BASE:" __BASE_FILE__ diff --git a/kotlin-native/backend.native/tests/interop/kt54284/main.kt b/kotlin-native/backend.native/tests/interop/kt54284/main.kt new file mode 100644 index 00000000000..b6ba650860b --- /dev/null +++ b/kotlin-native/backend.native/tests/interop/kt54284/main.kt @@ -0,0 +1,12 @@ +import kotlinx.cinterop.* +import kt54284.* +import kotlin.test.* + +fun main() { + assertEquals(KFILE, "FILE:__FILE__") + assertEquals(KLINE, "LINE:__LINE__") + assertEquals(KTIME, "TIME:__TIME__") + assertEquals(KDATE, "DATE:__DATE__") + assertEquals(KFILENAME, "NAME:__FILE_NAME__") + assertEquals(KBASEFILE, "BASE:__BASE_FILE__") +}