blob: 1139f626b62a33c4a1cdccbdd7ebe3de78f3a9eb [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 Naroff85e2db72009-10-01 00:31:07 +000015 CXDecl DeclReferenced = clang_getCursorDecl(Cursor);
16 if (DeclReferenced)
17 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
18 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000019 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000020}
Steve Naroff89922f82009-08-31 00:59:03 +000021
Steve Naroffc857ea42009-09-02 13:28:54 +000022static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
23{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000024 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000025 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
26 clang_getCursorLine(Cursor),
27 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000028 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000029 printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl));
Daniel Dunbarbce6f622009-09-03 05:59:50 +000030 }
Steve Naroffc857ea42009-09-02 13:28:54 +000031}
32static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
33 CXClientData Filter)
34{
35 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000036 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
37 clang_getCursorLine(Cursor),
38 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000039 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000040 printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit)));
41
42 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +000043
Steve Naroff4ade6d62009-09-23 17:52:52 +000044 if (Cursor.kind == CXCursor_FunctionDefn) {
45 const char *startBuf, *endBuf;
46 unsigned startLine, startColumn, endLine, endColumn;
47 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
48 &startLine, &startColumn,
49 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000050 {
51 /* Probe the entire body, looking for both decls and refs. */
52 unsigned curLine = startLine, curColumn = startColumn;
53 CXCursor Ref;
54
55 while (startBuf <= endBuf) {
56 if (*startBuf == '\n') {
57 startBuf++;
58 curLine++;
59 curColumn = 1;
60 } else if (*startBuf != '\t')
61 curColumn++;
62
63 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
64 curLine, curColumn);
65 if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000066 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
67 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000068 PrintCursor(Ref);
Steve Naroffff9e18c2009-09-24 20:03:06 +000069 printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl));
Steve Narofff7469a32009-09-23 20:00:53 +000070 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000071 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +000072 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000073 }
74 }
Steve Naroff2d4d6292009-08-31 14:26:51 +000075 }
Steve Naroff89922f82009-08-31 00:59:03 +000076}
Steve Naroff50398192009-08-28 15:28:48 +000077
78/*
79 * First sign of life:-)
80 */
81int main(int argc, char **argv) {
Steve Narofff7469a32009-09-23 20:00:53 +000082 if (argc != 3) {
83 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
84 return 0;
85 }
86 {
Steve Naroff50398192009-08-28 15:28:48 +000087 CXIndex Idx = clang_createIndex();
88 CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
Steve Narofff7469a32009-09-23 20:00:53 +000089 enum CXCursorKind K = CXCursor_NotImplemented;
90
91 if (!strcmp(argv[2], "all")) {
92 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
93 return 1;
94 }
95 /* Perform some simple filtering. */
96 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
97 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
98 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
99 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
100 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
101
102 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroff50398192009-08-28 15:28:48 +0000103 return 1;
Steve Narofff7469a32009-09-23 20:00:53 +0000104 }
Steve Naroff50398192009-08-28 15:28:48 +0000105}