微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – libclang从游标中获取类名

我试图将一个参数的类名提取到objective-C中的方法调用.我正在解析的代码是:
- (void)testAddConcreteDataModel:(DFDemoDataModelOne*)helpmeh {
    [self.dataModels addobject:helpmeh];
}

我需要的结果是helpmeh类的类型,即“DFDemoDataModelOne”.

到目前为止,我有以下代码,输出:“[(DFDataModelContainer).dataModels addobject:helpmeh]”

if (cursor.kind == CXCursor_ObjcmessageExpr) {
        __block Nsstring* memberName = nil;
        __block Nsstring* ownerClassName = nil;
        __block Nsstring* methodName = [Nsstring stringWithUTF8String:clang_getCString(clang_getCursordisplayName(cursor))];

        clang_visitChildrenWithBlock(cursor,^enum CXChildVisitResult(CXCursor cursor,CXCursor parent) {
            if (cursor.kind == CXCursor_MemberRefExpr) {
                memberName = [Nsstring stringWithUTF8String:clang_getCString(clang_getCursordisplayName(cursor))];
                ownerClassName = [Nsstring stringWithUTF8String:clang_getCString(clang_getCursordisplayName(clang_getCursorSemanticParent(clang_getCursorReferenced(cursor))))];
            } else {
                if (memberName) {
                    Nsstring* param = [Nsstring stringWithUTF8String:clang_getCString(clang_getCursordisplayName(cursor))];
                    NSLog(@"[(%@).%@ %@%@]",ownerClassName,memberName,methodName,param);
                    clang_visitChildrenWithBlock(cursor,CXCursor parent) {

                         // test
                         if ([param isEqualToString:@"helpmeh"] && cursor.kind == CXCursor_DeclRefExpr) {
                            // found the interesting part.. what Now?
                         }
                         return CXChildVisit_Recurse;
                     }
                }
            }
            return CXChildVisit_Continue;
        }
    }

关于如何从游标中提取信息,我只是有点迷失 – 当我转储我的类时,我可以看到我需要的信息就在那里(参见最后一行):

|-ObjcmethodDecl 0x112790f90 <line:32:1,line:34:1> - testAddConcreteDataModel: 'void'
  | |-ImplicitParamDecl 0x112791960 <<invalid sloc>> self 'DFDataModelContainer *const __strong'
  | |-ImplicitParamDecl 0x1127919c0 <<invalid sloc>> _cmd 'SEL':'SEL *'
  | |-ParmVarDecl 0x112791040 <line:32:35,col:55> helpmeh 'DFDemoDataModelOne *__strong'
  | `-CompoundStmt 0x112791bf0 <col:63,line:34:1>
  |   `-ExprWithCleanups 0x112791bd8 <line:33:5,col:39> 'void'
  |     `-ObjcmessageExpr 0x112791ba0 <col:5,col:39> 'void' selector=addobject:
  |       |-PseudoObjectExpr 0x112791b48 <col:6,col:11> 'NSMutableArray *'
  |       | |-ObjCPropertyRefExpr 0x112791ad0 <col:6,col:11> '<pseudo-object type>' lvalue objcproperty Kind=PropertyRef Property="dataModels" Messaging=Getter
  |       | | `-OpaqueValueExpr 0x112791ab0 <col:6> 'DFDataModelContainer *'
  |       | |   `-ImplicitCastExpr 0x112791a40 <col:6> 'DFDataModelContainer *' <LValuetoRValue>
  |       | |     `-DeclRefExpr 0x112791a18 <col:6> 'DFDataModelContainer *const __strong' lvalue ImplicitParam 0x112791960 'self' 'DFDataModelContainer *const __strong'
  |       | |-OpaqueValueExpr 0x112791ab0 <col:6> 'DFDataModelContainer *'
  |       | | `-ImplicitCastExpr 0x112791a40 <col:6> 'DFDataModelContainer *' <LValuetoRValue>
  |       | |   `-DeclRefExpr 0x112791a18 <col:6> 'DFDataModelContainer *const __strong' lvalue ImplicitParam 0x112791960 'self' 'DFDataModelContainer *const __strong'
  |       | `-ImplicitCastExpr 0x112791b30 <col:11> 'NSMutableArray *' <ARCReclaimReturnedobject>
  |       |   `-ObjcmessageExpr 0x112791b00 <col:11> 'NSMutableArray *' selector=dataModels
  |       |     `-OpaqueValueExpr 0x112791ab0 <col:6> 'DFDataModelContainer *'
  |       |       `-ImplicitCastExpr 0x112791a40 <col:6> 'DFDataModelContainer *' <LValuetoRValue>
  |       |         `-DeclRefExpr 0x112791a18 <col:6> 'DFDataModelContainer *const __strong' lvalue ImplicitParam 0x112791960 'self' 'DFDataModelContainer *const __strong'
  |       `-ImplicitCastExpr 0x112791b88 <col:32> 'id':'id' <BitCast>
  |         `-ImplicitCastExpr 0x112791b70 <col:32> 'DFDemoDataModelOne *' <LValuetoRValue>
  |           `-DeclRefExpr 0x112791a88 <col:32> 'DFDemoDataModelOne *__strong' lvalue ParmVar 0x112791040 'helpmeh' 'DFDemoDataModelOne *__strong'

任何指针非常感谢!

解决方法

你试过看 clang_getCursorSemanticParent功能吗?

我无法帮助你使用ObjC代码,但一般的想法应该是这样的:

>调用clang_getCursorSemanticParent来获取类声明的游标>在类声明游标上调用clang_getCursorSpelling或clang_getCursordisplayName以获取类名

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐