Search code examples
objective-cparsinglibclang

How can I skip includes using libclang?


I am using libclang to parse a objective c source code file. The following code finds all Objective-C instance method declarations, but it also finds declarations in the includes:

 enum CXCursorKind curKind  = clang_getCursorKind(cursor);
 CXString curKindName  = clang_getCursorKindSpelling(curKind);

 const char *funcDecl="ObjCInstanceMethodDecl";

 if(strcmp(clang_getCString(curKindName),funcDecl)==0{


 }

How can I skip everything, which comes from header includes? I am only interested in my own Objective-C instance method declarations in the source file, not in any of the includes.

e.g. the following should not be included

...

Location: /System/Library/Frameworks/Foundation.framework/Headers/NSObject.h:15:9:315
Type: 
TypeKind: Invalid
CursorKind: ObjCInstanceMethodDecl

...

Solution

  • Answering this question because I couldn't believe that hard-coding paths comparisons was the only solution, and indeed, there is a clang_Location_isFromMainFile function that does exactly what you want, so that you can filter unwanted results in the visitor, like this :

    if (clang_Location_isFromMainFile (clang_getCursorLocation (cursor)) == 0) {
      return CXChildVisit_Continue;
    }