Native: fix cinterop for Swift-produced Objective-C categories

For some reason, libclang's indexer doesn't index categories with
__attribute__((external_source_symbol(language="Swift",...))),
so we have to additionally enumerate them explicitly.

^KT-49455 Fixed
This commit is contained in:
Svyatoslav Scherbina
2022-02-10 17:36:19 +03:00
committed by Space
parent f1623347d8
commit d8b1992fde
4 changed files with 48 additions and 0 deletions
@@ -988,6 +988,13 @@ public open class NativeIndexImpl(val library: NativeLibrary, val verbose: Boole
getObjCProtocolAt(cursor)
}
}
fun indexObjCCategory(cursor: CValue<CXCursor>) {
if (isAvailable(cursor)) {
getObjCCategoryAt(cursor)
}
}
protected open fun String.isUnknownTemplate() = false
private fun getParentName(cursor: CValue<CXCursor>, pkg: List<String> = emptyList()) : String? {
@@ -1225,6 +1232,13 @@ private fun indexDeclarations(nativeIndex: NativeIndexImpl): CompilationWithPCH
when (cursor.kind) {
CXCursorKind.CXCursor_ObjCInterfaceDecl -> nativeIndex.indexObjCClass(cursor)
CXCursorKind.CXCursor_ObjCProtocolDecl -> nativeIndex.indexObjCProtocol(cursor)
CXCursorKind.CXCursor_ObjCCategoryDecl -> {
// This fixes https://youtrack.jetbrains.com/issue/KT-49455, which effectively seems to be a bug in libclang:
// the libclang indexer doesn't properly index categories with
// `__attribute__((external_source_symbol(language="Swift",...)))`.
// As a workaround, additionally enumerate all the categories explicitly.
nativeIndex.indexObjCCategory(cursor)
}
else -> {}
}
}
@@ -0,0 +1,17 @@
#import <Foundation/NSObject.h>
// https://youtrack.jetbrains.com/issue/KT-49455
@interface KT49455 : NSObject
@end;
__attribute__((external_source_symbol(language="Swift", defined_in="sample",generated_declaration)))
@interface KT49455 (KT49455Ext)
- (int)extensionFunction;
@end
// Just to ensure that unavailable categories don't break anything.
__attribute__((unavailable("unavailableExtensionFunction is unavailable")))
@interface KT49455 (KT49455UnavailableExt)
- (int)unavailableExtensionFunction;
@end
@@ -0,0 +1,7 @@
import kotlin.test.*
import objcTests.*
@Test
fun testKT49455() {
assertEquals(111, KT49455().extensionFunction())
}
@@ -0,0 +1,10 @@
#import "kt49455.h"
@implementation KT49455
@end;
@implementation KT49455 (KT49455Ext)
- (int)extensionFunction {
return 111;
}
@end