0

我想获取目标 c 方法开始执行的行号。

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

输出应该是:2,6

我怎样才能用libclang实现它。

我不想为此使用正则表达式,因为它就足够了。

4

1 回答 1

0
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);
}
于 2013-09-27T07:45:00.727 回答