blob: f1f9372a1870eaf7a4d59d11f5643abbf31471e0 [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>
Steve Naroff38c1a7b2009-09-03 15:49:00 +00005#include <string.h>
6
Steve Naroffa7753c42009-09-24 20:03:06 +00007extern char *basename(const char *);
8
Steve Naroff38c1a7b2009-09-03 15:49:00 +00009static void PrintCursor(CXCursor Cursor) {
Steve Naroff54f22fb2009-09-15 20:25:34 +000010 if (clang_isInvalid(Cursor.kind))
11 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
12 else
Steve Naroffa7753c42009-09-24 20:03:06 +000013 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
14 clang_getCursorSpelling(Cursor));
Steve Naroff38c1a7b2009-09-03 15:49:00 +000015}
Steve Naroff1054e602009-08-31 00:59:03 +000016
Steve Naroff3645f5a2009-09-02 13:28:54 +000017static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
18{
Daniel Dunbar3a0637b2009-09-03 05:59:50 +000019 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffa7753c42009-09-24 20:03:06 +000020 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
21 clang_getCursorLine(Cursor),
22 clang_getCursorColumn(Cursor));
Steve Naroff38c1a7b2009-09-03 15:49:00 +000023 PrintCursor(Cursor);
Steve Naroffa7753c42009-09-24 20:03:06 +000024 printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl));
Daniel Dunbar3a0637b2009-09-03 05:59:50 +000025 }
Steve Naroff3645f5a2009-09-02 13:28:54 +000026}
27static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
28 CXClientData Filter)
29{
30 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffa7753c42009-09-24 20:03:06 +000031 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
32 clang_getCursorLine(Cursor),
33 clang_getCursorColumn(Cursor));
Steve Naroff38c1a7b2009-09-03 15:49:00 +000034 PrintCursor(Cursor);
Steve Naroffa7753c42009-09-24 20:03:06 +000035 printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit)));
36
37 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff3645f5a2009-09-02 13:28:54 +000038
Steve Naroff76b8f132009-09-23 17:52:52 +000039 if (Cursor.kind == CXCursor_FunctionDefn) {
40 const char *startBuf, *endBuf;
41 unsigned startLine, startColumn, endLine, endColumn;
42 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
43 &startLine, &startColumn,
44 &endLine, &endColumn);
Steve Narofff99203ab2009-09-23 20:00:53 +000045 {
46 /* Probe the entire body, looking for both decls and refs. */
47 unsigned curLine = startLine, curColumn = startColumn;
48 CXCursor Ref;
49
50 while (startBuf <= endBuf) {
51 if (*startBuf == '\n') {
52 startBuf++;
53 curLine++;
54 curColumn = 1;
55 } else if (*startBuf != '\t')
56 curColumn++;
57
58 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
59 curLine, curColumn);
60 if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffa7753c42009-09-24 20:03:06 +000061 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
62 curLine, curColumn);
Steve Narofff99203ab2009-09-23 20:00:53 +000063 PrintCursor(Ref);
Steve Naroffa7753c42009-09-24 20:03:06 +000064 printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl));
Steve Narofff99203ab2009-09-23 20:00:53 +000065 }
Steve Naroff76b8f132009-09-23 17:52:52 +000066 startBuf++;
Steve Naroff76b8f132009-09-23 17:52:52 +000067 }
Steve Naroff76b8f132009-09-23 17:52:52 +000068 }
69 }
Steve Naroff772c1a42009-08-31 14:26:51 +000070 }
Steve Naroff1054e602009-08-31 00:59:03 +000071}
Steve Naroffa1c72842009-08-28 15:28:48 +000072
73/*
74 * First sign of life:-)
75 */
76int main(int argc, char **argv) {
Steve Narofff99203ab2009-09-23 20:00:53 +000077 if (argc != 3) {
78 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
79 return 0;
80 }
81 {
Steve Naroffa1c72842009-08-28 15:28:48 +000082 CXIndex Idx = clang_createIndex();
83 CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
Steve Narofff99203ab2009-09-23 20:00:53 +000084 enum CXCursorKind K = CXCursor_NotImplemented;
85
86 if (!strcmp(argv[2], "all")) {
87 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
88 return 1;
89 }
90 /* Perform some simple filtering. */
91 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
92 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
93 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
94 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
95 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
96
97 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroffa1c72842009-08-28 15:28:48 +000098 return 1;
Steve Narofff99203ab2009-09-23 20:00:53 +000099 }
Steve Naroffa1c72842009-08-28 15:28:48 +0000100}