blob: 8df2fc261dc28c8c27dd208cab1630c6f02538ad [file] [log] [blame]
Steve Naroff69b10fd2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroffa1c72842009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Steve Naroff1054e602009-08-31 00:59:03 +00004#include <stdio.h>
5
Steve Naroffef3cf2a2009-09-03 00:32:06 +00006static void PrintCursor(CXCursor Cursor) {
7 printf("%s => %s", clang_getCursorKindSpelling(Cursor.kind),
8 clang_getCursorSpelling(Cursor));
9 printf(" (%s,%d:%d)\n", clang_getCursorSource(Cursor),
10 clang_getCursorLine(Cursor),
11 clang_getCursorColumn(Cursor));
12}
13
Steve Naroff3645f5a2009-09-02 13:28:54 +000014static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
15{
Steve Naroffef3cf2a2009-09-03 00:32:06 +000016 printf("%s: ", clang_getDeclSpelling(Dcl));
17 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter))
18 PrintCursor(Cursor);
Steve Naroff3645f5a2009-09-02 13:28:54 +000019}
Steve Naroffef3cf2a2009-09-03 00:32:06 +000020
Steve Naroff3645f5a2009-09-02 13:28:54 +000021static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
22 CXClientData Filter)
23{
Steve Naroffef3cf2a2009-09-03 00:32:06 +000024 printf("%s: ", clang_getTranslationUnitSpelling(Unit));
Steve Naroff3645f5a2009-09-02 13:28:54 +000025 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef3cf2a2009-09-03 00:32:06 +000026 PrintCursor(Cursor);
Steve Naroff3645f5a2009-09-02 13:28:54 +000027
Steve Naroff3645f5a2009-09-02 13:28:54 +000028 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff772c1a42009-08-31 14:26:51 +000029 }
Steve Naroff1054e602009-08-31 00:59:03 +000030}
Steve Naroffa1c72842009-08-28 15:28:48 +000031
32/*
33 * First sign of life:-)
34 */
35int main(int argc, char **argv) {
36 CXIndex Idx = clang_createIndex();
37 CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
Steve Naroff69b10fd2009-09-01 15:55:40 +000038
Steve Naroff3645f5a2009-09-02 13:28:54 +000039 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
Steve Naroffa1c72842009-08-28 15:28:48 +000040 return 1;
41}