From 838cccf275b121878003d9c8c2f7600d78b10989 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Fri, 18 Oct 2019 20:17:54 +0300 Subject: [PATCH] Workaround clang crash in cinterop tool (KT-34467) --- .../kotlin/native/interop/indexer/Indexer.kt | 2 +- .../kotlin/native/interop/indexer/Utils.kt | 7 ++++++- backend.native/tests/build.gradle | 14 ++++++++++++++ backend.native/tests/interop/objc/kt34467/foo.h | 1 + backend.native/tests/interop/objc/kt34467/foo.kt | 6 ++++++ .../tests/interop/objc/kt34467/module_library.def | 2 ++ .../interop/objc/kt34467/module_library.modulemap | 6 ++++++ .../interop/objc/kt34467/module_library_umbrella.h | 1 + 8 files changed, 37 insertions(+), 2 deletions(-) create mode 100644 backend.native/tests/interop/objc/kt34467/foo.h create mode 100644 backend.native/tests/interop/objc/kt34467/foo.kt create mode 100644 backend.native/tests/interop/objc/kt34467/module_library.def create mode 100644 backend.native/tests/interop/objc/kt34467/module_library.modulemap create mode 100644 backend.native/tests/interop/objc/kt34467/module_library_umbrella.h diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt index bd4f5cfeef0..4771832fbb1 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Indexer.kt @@ -958,7 +958,7 @@ fun buildNativeIndexImpl(library: NativeLibrary, verbose: Boolean): IndexerResul private fun indexDeclarations(nativeIndex: NativeIndexImpl): CompilationWithPCH { withIndex { index -> - val translationUnit = nativeIndex.library.parse( + val translationUnit = nativeIndex.library.copyWithArgsForPCH().parse( index, options = CXTranslationUnit_DetailedPreprocessingRecord or CXTranslationUnit_ForSerialization ) diff --git a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt index 3fb0dd97ec0..7772c6e567f 100644 --- a/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt +++ b/Interop/Indexer/src/main/kotlin/org/jetbrains/kotlin/native/interop/indexer/Utils.kt @@ -257,6 +257,11 @@ fun Compilation.copy( language = language ) +// Clang-8 crashes when consuming a precompiled header built with -fmodule-map-file argument (see KT-34467). +// We ignore this argument when building a pch to workaround this crash. +fun Compilation.copyWithArgsForPCH(): Compilation = + copy(compilerArgs = compilerArgs.filterNot { it.startsWith("-fmodule-map-file") }) + data class CompilationImpl( override val includes: List, override val additionalPreambleLines: List, @@ -271,7 +276,7 @@ data class CompilationImpl( */ fun Compilation.precompileHeaders(): CompilationWithPCH = withIndex { index -> val options = CXTranslationUnit_ForSerialization - val translationUnit = this.parse(index, options) + val translationUnit = copyWithArgsForPCH().parse(index, options) try { translationUnit.ensureNoCompileErrors() withPrecompiledHeader(translationUnit) diff --git a/backend.native/tests/build.gradle b/backend.native/tests/build.gradle index 2007f041629..6e9affcb5e7 100644 --- a/backend.native/tests/build.gradle +++ b/backend.native/tests/build.gradle @@ -3318,6 +3318,13 @@ if (PlatformInfo.isAppleTarget(project)) { it.headers "$projectDir/interop/objc/msg_send/messaging.h" it.linkerOpts "-L$buildDir", "-lobjcmessaging" } + + createInterop("objcKt34467") { + it.defFile 'interop/objc/kt34467/module_library.def' + def moduleMap = file("interop/objc/kt34467/module_library.modulemap").absolutePath + def includePath = file("interop/objc/kt34467").absolutePath + it.compilerOpts "-fmodule-map-file=$moduleMap", "-I$includePath" + } } createInterop("withSpaces") { @@ -3565,6 +3572,13 @@ interopTest("interop_objc_msg_send") { } } +interopTest("interop_objc_kt34467") { + disabled = !isAppleTarget(project) + source = "interop/objc/kt34467/foo.kt" + interop = 'objcKt34467' + goldValue = "OK\n42\n" +} + standaloneTest("jsinterop_math") { doBefore { def jsinteropScript = isWindows() ? "jsinterop.bat" : "jsinterop" diff --git a/backend.native/tests/interop/objc/kt34467/foo.h b/backend.native/tests/interop/objc/kt34467/foo.h new file mode 100644 index 00000000000..6f57fb0a9b4 --- /dev/null +++ b/backend.native/tests/interop/objc/kt34467/foo.h @@ -0,0 +1 @@ +#define ANSWER 42 \ No newline at end of file diff --git a/backend.native/tests/interop/objc/kt34467/foo.kt b/backend.native/tests/interop/objc/kt34467/foo.kt new file mode 100644 index 00000000000..34dfbdfc5ce --- /dev/null +++ b/backend.native/tests/interop/objc/kt34467/foo.kt @@ -0,0 +1,6 @@ +import module_library.* + +fun main() { + println("OK") + println(ANSWER) +} \ No newline at end of file diff --git a/backend.native/tests/interop/objc/kt34467/module_library.def b/backend.native/tests/interop/objc/kt34467/module_library.def new file mode 100644 index 00000000000..3038e078a4f --- /dev/null +++ b/backend.native/tests/interop/objc/kt34467/module_library.def @@ -0,0 +1,2 @@ +language = Objective-C +modules = module_library \ No newline at end of file diff --git a/backend.native/tests/interop/objc/kt34467/module_library.modulemap b/backend.native/tests/interop/objc/kt34467/module_library.modulemap new file mode 100644 index 00000000000..b9fbc34f98d --- /dev/null +++ b/backend.native/tests/interop/objc/kt34467/module_library.modulemap @@ -0,0 +1,6 @@ +module module_library { + umbrella header "module_library_umbrella.h" + + export * + module * { export * } +} diff --git a/backend.native/tests/interop/objc/kt34467/module_library_umbrella.h b/backend.native/tests/interop/objc/kt34467/module_library_umbrella.h new file mode 100644 index 00000000000..e3262d6e539 --- /dev/null +++ b/backend.native/tests/interop/objc/kt34467/module_library_umbrella.h @@ -0,0 +1 @@ +#import