sighingnow / libclang

(Unofficial) Release libclang (clang.cindex) on pypi.
https://pypi.org/project/libclang
Other
81 stars 21 forks source link

Instance Variable Names Not Available For Objective-C Pointer Types (with Protocols/Generics) #45

Open devm18426 opened 1 year ago

devm18426 commented 1 year ago

I'm experiencing some strange behavior while parsing the following Objective-C code:


code = """@class NSObject, SomeProtocol;

@interface OtherClass : NSObject {
    id* testVar;
    id*<SomeProtocol> objWithSomeMethod;
    NSObject*<SomeProtocol> objWithSomeMethod2;
    NSObject<SomeProtocol> objWithSomeMethod3;
}
@end"""

translation_unit = i.parse("test.h", unsaved_files=(("test.h", code),), args=("-x", "objective-c"))

# Access instance variable declarations
instance_cursors = list(list(translation_unit.cursor.get_children())[3].get_children())

testvar_cursor, objwithsomemethod_cursor, objwithsomemethod2_cursor, objwithsomemethod3_cursor = instance_cursors

print(f"testVar: {testvar_cursor.kind=} {testvar_cursor.objc_type_encoding=} {testvar_cursor.type.kind=}  {testvar_cursor.displayname=}")

print(f"objWithSomeMethod: {objwithsomemethod_cursor.kind=} {objwithsomemethod_cursor.objc_type_encoding=} {objwithsomemethod_cursor.type.kind=}  {objwithsomemethod_cursor.displayname=}")

print(f"objWithSomeMethod2: {objwithsomemethod2_cursor.kind=} {objwithsomemethod2_cursor.objc_type_encoding=} {objwithsomemethod2_cursor.type.kind=}  {objwithsomemethod2_cursor.displayname=}")

print(f"objWithSomeMethod3: {objwithsomemethod3_cursor.kind=} {objwithsomemethod3_cursor.objc_type_encoding=} {objwithsomemethod3_cursor.type.kind=}  {objwithsomemethod3_cursor.displayname=}")

Result:

testVar: testvar_cursor.kind=CursorKind.OBJC_IVAR_DECL testvar_cursor.objc_type_encoding='^@' testvar_cursor.type.kind=TypeKind.POINTER  testvar_cursor.displayname='test'
objWithSomeMethod: objwithsomemethod_cursor.kind=CursorKind.OBJC_IVAR_DECL objwithsomemethod_cursor.objc_type_encoding='^@' objwithsomemethod_cursor.type.kind=TypeKind.POINTER  objwithsomemethod_cursor.displayname=''
objWithSomeMethod2: objwithsomemethod2_cursor.kind=CursorKind.OBJC_IVAR_DECL objwithsomemethod2_cursor.objc_type_encoding='@' objwithsomemethod2_cursor.type.kind=TypeKind.OBJCOBJECTPOINTER  objwithsomemethod2_cursor.displayname=''
objWithSomeMethod3: objwithsomemethod3_cursor.kind=CursorKind.OBJC_IVAR_DECL objwithsomemethod3_cursor.objc_type_encoding='{NSObject=}' objwithsomemethod3_cursor.type.kind=TypeKind.OBJCINTERFACE  objwithsomemethod3_cursor.displayname='objWithSomeMethod3'

It would appear that the cursor displayname (and spelling) properties are empty when the cursor type is POINTER or OBJCOBJECTPOINTER and the type contains Objective-C protocol information. The regular protocol-less pointer testVar seems to work fine.

Additionally, I see no way to access the protocol modifier on the type declaration of the variables (the angle brackets <SomeProtocol>). The variable types are reported as simple pointers without any protocol information that I can identify.

I've also included objWithSomeMethod3 which is not a pointer (OBJCINTERFACE) but has a protocol defined. The displayname works fine however protocol info is likewise unavailable.

Is this behavior intended or am I missing something?

Thanks.

sighingnow commented 1 year ago

I'm not familiar of Objective-C but I think it should be posted to LLVM discussion forums. This repo only provides pre-compiled Python packages to help the installation process and has nothing to do with the behavior of clang parser itself.