blob: aa93a7ed4f224c19a72c92bca38e3ade93279168 [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
Steve Naroffff9e18c2009-09-24 20:03:06 +00007extern char *basename(const char *);
8
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000010 if (clang_isInvalid(Cursor.kind))
11 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000012 else {
Steve Naroffff9e18c2009-09-24 20:03:06 +000013 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
14 clang_getCursorSpelling(Cursor));
Steve Naroff699a07d2009-09-25 21:32:34 +000015 if (Cursor.stmt) {
16 CXDecl DeclReferenced = clang_getCursorDecl(Cursor);
17 if (DeclReferenced)
18 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
19 clang_getDeclColumn(DeclReferenced));
20 }
21 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000022}
Steve Naroff89922f82009-08-31 00:59:03 +000023
Steve Naroffc857ea42009-09-02 13:28:54 +000024static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
25{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000026 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000027 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
28 clang_getCursorLine(Cursor),
29 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000030 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000031 printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl));
Daniel Dunbarbce6f622009-09-03 05:59:50 +000032 }
Steve Naroffc857ea42009-09-02 13:28:54 +000033}
34static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
35 CXClientData Filter)
36{
37 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000038 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
39 clang_getCursorLine(Cursor),
40 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000041 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000042 printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit)));
43
44 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +000045
Steve Naroff4ade6d62009-09-23 17:52:52 +000046 if (Cursor.kind == CXCursor_FunctionDefn) {
47 const char *startBuf, *endBuf;
48 unsigned startLine, startColumn, endLine, endColumn;
49 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
50 &startLine, &startColumn,
51 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000052 {
53 /* Probe the entire body, looking for both decls and refs. */
54 unsigned curLine = startLine, curColumn = startColumn;
55 CXCursor Ref;
56
57 while (startBuf <= endBuf) {
58 if (*startBuf == '\n') {
59 startBuf++;
60 curLine++;
61 curColumn = 1;
62 } else if (*startBuf != '\t')
63 curColumn++;
64
65 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
66 curLine, curColumn);
67 if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000068 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
69 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000070 PrintCursor(Ref);
Steve Naroffff9e18c2009-09-24 20:03:06 +000071 printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl));
Steve Narofff7469a32009-09-23 20:00:53 +000072 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000073 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +000074 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000075 }
76 }
Steve Naroff2d4d6292009-08-31 14:26:51 +000077 }
Steve Naroff89922f82009-08-31 00:59:03 +000078}
Steve Naroff50398192009-08-28 15:28:48 +000079
80/*
81 * First sign of life:-)
82 */
83int main(int argc, char **argv) {
Steve Narofff7469a32009-09-23 20:00:53 +000084 if (argc != 3) {
85 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
86 return 0;
87 }
88 {
Steve Naroff50398192009-08-28 15:28:48 +000089 CXIndex Idx = clang_createIndex();
90 CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
Steve Narofff7469a32009-09-23 20:00:53 +000091 enum CXCursorKind K = CXCursor_NotImplemented;
92
93 if (!strcmp(argv[2], "all")) {
94 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
95 return 1;
96 }
97 /* Perform some simple filtering. */
98 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
99 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
100 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
101 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
102 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
103
104 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroff50398192009-08-28 15:28:48 +0000105 return 1;
Steve Narofff7469a32009-09-23 20:00:53 +0000106 }
Steve Naroff50398192009-08-28 15:28:48 +0000107}