blob: e7043c14820f46e57ff03024930445365ed9b31c [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) {
Steve Naroff77128dd2009-09-15 20:25:34 +00008 if (clang_isInvalid(Cursor.kind))
9 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
10 else
11 printf("%s => %s ", clang_getCursorKindSpelling(Cursor.kind),
12 clang_getCursorSpelling(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000013}
Steve Naroff89922f82009-08-31 00:59:03 +000014
Steve Naroffc857ea42009-09-02 13:28:54 +000015static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
16{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000017 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +000018 PrintCursor(Cursor);
Steve Naroff8b26cbd2009-09-11 00:12:01 +000019 printf("(Context: %s", clang_getDeclSpelling(Dcl));
20 printf(" Source: %s (%d:%d))\n", clang_getCursorSource(Cursor),
Steve Naroffaf08ddc2009-09-03 15:49:00 +000021 clang_getCursorLine(Cursor),
22 clang_getCursorColumn(Cursor));
Daniel Dunbarbce6f622009-09-03 05:59:50 +000023 }
Steve Naroffc857ea42009-09-02 13:28:54 +000024}
25static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
26 CXClientData Filter)
27{
28 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffaf08ddc2009-09-03 15:49:00 +000029 PrintCursor(Cursor);
Steve Naroff8b26cbd2009-09-11 00:12:01 +000030 printf("(Context: %s", clang_getTranslationUnitSpelling(Unit));
31 printf(" Source: %s (%d:%d))\n", clang_getCursorSource(Cursor),
Steve Naroffaf08ddc2009-09-03 15:49:00 +000032 clang_getCursorLine(Cursor),
33 clang_getCursorColumn(Cursor));
Steve Naroffc857ea42009-09-02 13:28:54 +000034
Steve Naroffc857ea42009-09-02 13:28:54 +000035 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +000036 }
Steve Naroff89922f82009-08-31 00:59:03 +000037}
Steve Naroff50398192009-08-28 15:28:48 +000038
39/*
40 * First sign of life:-)
41 */
42int main(int argc, char **argv) {
43 CXIndex Idx = clang_createIndex();
44 CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
Steve Naroff9efa7672009-09-04 15:44:05 +000045
46 if (argc == 2) {
47 /* malloc - returns a cursor of type CXCursor_FunctionDecl */
48 CXCursor C = clang_getCursor(TU, "/usr/include/stdlib.h", 169, 7);
49 PrintCursor(C);
50 /* methodSignature - returns a cursor of type ObjCInstanceMethodDecl */
51 C = clang_getCursor(TU, "/System/Library/Frameworks/Foundation.framework/Headers/NSInvocation.h", 22, 1);
52 PrintCursor(C);
Steve Naroff77128dd2009-09-15 20:25:34 +000053 C = clang_getCursor(TU, "Large.m", 5, 18);
54 PrintCursor(C);
Steve Naroff9efa7672009-09-04 15:44:05 +000055 } else if (argc == 3) {
Steve Naroff77128dd2009-09-15 20:25:34 +000056 enum CXCursorKind K = CXCursor_NotImplemented;
Steve Naroffaf08ddc2009-09-03 15:49:00 +000057
Steve Naroff9efa7672009-09-04 15:44:05 +000058 if (!strcmp(argv[2], "all")) {
59 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
60 return 1;
61 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000062 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
63 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
64 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
65 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
66 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
67
68 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
69 }
Steve Naroff50398192009-08-28 15:28:48 +000070 return 1;
71}