blob: 9e94058e0d3c18748058f8ce7da383f2867ec05e [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 Naroff4ade6d62009-09-23 17:52:52 +000035 if (Cursor.kind == CXCursor_FunctionDefn) {
36 const char *startBuf, *endBuf;
37 unsigned startLine, startColumn, endLine, endColumn;
38 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
39 &startLine, &startColumn,
40 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000041 {
42 /* Probe the entire body, looking for both decls and refs. */
43 unsigned curLine = startLine, curColumn = startColumn;
44 CXCursor Ref;
45
46 while (startBuf <= endBuf) {
47 if (*startBuf == '\n') {
48 startBuf++;
49 curLine++;
50 curColumn = 1;
51 } else if (*startBuf != '\t')
52 curColumn++;
53
54 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
55 curLine, curColumn);
56 if (Ref.kind != CXCursor_FunctionDecl) {
57 PrintCursor(Ref);
58 printf("(Context: %s", clang_getDeclSpelling(Ref.decl));
59 printf(" Source: %s (%d:%d))\n", clang_getCursorSource(Ref),
60 curLine, curColumn);
61 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000062 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +000063 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000064 }
65 }
Steve Naroffc857ea42009-09-02 13:28:54 +000066 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +000067 }
Steve Naroff89922f82009-08-31 00:59:03 +000068}
Steve Naroff50398192009-08-28 15:28:48 +000069
70/*
71 * First sign of life:-)
72 */
73int main(int argc, char **argv) {
Steve Narofff7469a32009-09-23 20:00:53 +000074 if (argc != 3) {
75 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
76 return 0;
77 }
78 {
Steve Naroff50398192009-08-28 15:28:48 +000079 CXIndex Idx = clang_createIndex();
80 CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
Steve Narofff7469a32009-09-23 20:00:53 +000081 enum CXCursorKind K = CXCursor_NotImplemented;
82
83 if (!strcmp(argv[2], "all")) {
84 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
85 return 1;
86 }
87 /* Perform some simple filtering. */
88 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
89 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
90 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
91 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
92 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
93
94 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroff50398192009-08-28 15:28:48 +000095 return 1;
Steve Narofff7469a32009-09-23 20:00:53 +000096 }
Steve Naroff50398192009-08-28 15:28:48 +000097}