Search code examples
objective-cparsingmethodslibclang

How to find lines of objective c method implementations using libclang


I want to get the line numbers where the implementation of objective c method start.

1 #include "foobar.h"
2 -(void)Foo{
3 ...
4 }
5
6 +(NSInteger *)bar{
7 ...
8 }

The output should be: 2,6

How can i achieve it with libclang.

I do not want to use a regex for that, because it will be sufficient.


Solution

  • Solution:
    
    CXSourceRange range = clang_getCursorExtent(cursor);
    CXSourceLocation startLocation = clang_getRangeStart(range);
    CXSourceLocation endLocation = clang_getRangeEnd(range);
    
    CXFile file;
    unsigned int line, column, offset;
    clang_getInstantiationLocation(startLocation, &file, &line, &column, &offset);
    
    enum CXCursorKind curKind  = clang_getCursorKind(cursor);
    CXString curKindName  = clang_getCursorKindSpelling(curKind);
    const char *funcDecl="ObjCInstanceMethodDecl";
    
    if(strcmp(clang_getCString(curKindName),funcDecl)==0){
      printf("%u",line);
    }