aboutsummaryrefslogtreecommitdiff
path: root/tools/c-index-test/c-index-test.c
blob: dbf31bea5a91ff5dfb19ac7f3bb74e65c082c486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* c-index-test.c */

#include "clang-c/Index.h"
#include <stdio.h>
#include <string.h>

static void PrintCursor(CXCursor Cursor) {
  printf("%s => %s\n", clang_getCursorKindSpelling(Cursor.kind),
                       clang_getCursorSpelling(Cursor));
}

static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) 
{
  if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
    PrintCursor(Cursor);
    printf("  Context: %s\n", clang_getDeclSpelling(Dcl));
    printf("  Source:  %s (%d:%d)\n", clang_getCursorSource(Cursor),
                                      clang_getCursorLine(Cursor),
                                      clang_getCursorColumn(Cursor));
  }
}
static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
                                   CXClientData Filter) 
{
  if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
    PrintCursor(Cursor);
    printf("  Context: %s\n", clang_getTranslationUnitSpelling(Unit));
    printf("  Source:  %s (%d:%d)\n", clang_getCursorSource(Cursor),
                                      clang_getCursorLine(Cursor),
                                      clang_getCursorColumn(Cursor));

    clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
  }
}

/*
 * First sign of life:-)
 */
int main(int argc, char **argv) {
  CXIndex Idx = clang_createIndex();
  CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
  
  if (argc == 2)
    clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
  else if (argc == 3) {
    enum CXCursorKind K = CXCursor_Invalid;
    
    if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
    else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
    else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
    else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
    else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
    
    clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
  }
  return 1;
}