blob: 8df2fc261dc28c8c27dd208cab1630c6f02538ad [file] [log] [blame]
Steve Naroffb836cb72009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff3ba83e92009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Steve Naroffc674c2d2009-08-31 00:59:03 +00004#include <stdio.h>
5
Steve Naroff6b0576b2009-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 Naroff51366e92009-09-02 13:28:54 +000014static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
15{
Steve Naroff6b0576b2009-09-03 00:32:06 +000016 printf("%s: ", clang_getDeclSpelling(Dcl));
17 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter))
18 PrintCursor(Cursor);
Steve Naroff51366e92009-09-02 13:28:54 +000019}
Steve Naroff6b0576b2009-09-03 00:32:06 +000020
Steve Naroff51366e92009-09-02 13:28:54 +000021static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
22 CXClientData Filter)
23{
Steve Naroff6b0576b2009-09-03 00:32:06 +000024 printf("%s: ", clang_getTranslationUnitSpelling(Unit));
Steve Naroff51366e92009-09-02 13:28:54 +000025 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroff6b0576b2009-09-03 00:32:06 +000026 PrintCursor(Cursor);
Steve Naroff51366e92009-09-02 13:28:54 +000027
Steve Naroff51366e92009-09-02 13:28:54 +000028 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff52833ba2009-08-31 14:26:51 +000029 }
Steve Naroffc674c2d2009-08-31 00:59:03 +000030}
Steve Naroff3ba83e92009-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 Naroffb836cb72009-09-01 15:55:40 +000038
Steve Naroff51366e92009-09-02 13:28:54 +000039 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
Steve Naroff3ba83e92009-08-28 15:28:48 +000040 return 1;
41}