blob: dbf31bea5a91ff5dfb19ac7f3bb74e65c082c486 [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Steve Naroff89922f82009-08-31 00:59:03 +00004#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00005#include <string.h>
6
7static void PrintCursor(CXCursor Cursor) {
8 printf("%s => %s\n", clang_getCursorKindSpelling(Cursor.kind),
9 clang_getCursorSpelling(Cursor));
10}
Steve Naroff89922f82009-08-31 00:59:03 +000011
Steve Naroffc857ea42009-09-02 13:28:54 +000012static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
13{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000014 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +000015 PrintCursor(Cursor);
16 printf(" Context: %s\n", clang_getDeclSpelling(Dcl));
17 printf(" Source: %s (%d:%d)\n", clang_getCursorSource(Cursor),
18 clang_getCursorLine(Cursor),
19 clang_getCursorColumn(Cursor));
Daniel Dunbarbce6f622009-09-03 05:59:50 +000020 }
Steve Naroffc857ea42009-09-02 13:28:54 +000021}
22static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
23 CXClientData Filter)
24{
25 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +000026 PrintCursor(Cursor);
27 printf(" Context: %s\n", clang_getTranslationUnitSpelling(Unit));
28 printf(" Source: %s (%d:%d)\n", clang_getCursorSource(Cursor),
29 clang_getCursorLine(Cursor),
30 clang_getCursorColumn(Cursor));
Steve Naroffc857ea42009-09-02 13:28:54 +000031
Steve Naroffc857ea42009-09-02 13:28:54 +000032 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +000033 }
Steve Naroff89922f82009-08-31 00:59:03 +000034}
Steve Naroff50398192009-08-28 15:28:48 +000035
36/*
37 * First sign of life:-)
38 */
39int main(int argc, char **argv) {
40 CXIndex Idx = clang_createIndex();
41 CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
Steve Naroff2b8ee6c2009-09-01 15:55:40 +000042
Steve Naroffaf08ddc2009-09-03 15:49:00 +000043 if (argc == 2)
44 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
45 else if (argc == 3) {
46 enum CXCursorKind K = CXCursor_Invalid;
47
48 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
49 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
50 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
51 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
52 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
53
54 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
55 }
Steve Naroff50398192009-08-28 15:28:48 +000056 return 1;
57}