blob: f1f9372a1870eaf7a4d59d11f5643abbf31471e0 [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));
12 else
Steve Naroffff9e18c2009-09-24 20:03:06 +000013 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
14 clang_getCursorSpelling(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000015}
Steve Naroff89922f82009-08-31 00:59:03 +000016
Steve Naroffc857ea42009-09-02 13:28:54 +000017static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
18{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000019 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000020 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
21 clang_getCursorLine(Cursor),
22 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000023 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000024 printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl));
Daniel Dunbarbce6f622009-09-03 05:59:50 +000025 }
Steve Naroffc857ea42009-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 Naroffff9e18c2009-09-24 20:03:06 +000031 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
32 clang_getCursorLine(Cursor),
33 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000034 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000035 printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit)));
36
37 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +000038
Steve Naroff4ade6d62009-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 Narofff7469a32009-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 Naroffff9e18c2009-09-24 20:03:06 +000061 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
62 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000063 PrintCursor(Ref);
Steve Naroffff9e18c2009-09-24 20:03:06 +000064 printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl));
Steve Narofff7469a32009-09-23 20:00:53 +000065 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000066 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +000067 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000068 }
69 }
Steve Naroff2d4d6292009-08-31 14:26:51 +000070 }
Steve Naroff89922f82009-08-31 00:59:03 +000071}
Steve Naroff50398192009-08-28 15:28:48 +000072
73/*
74 * First sign of life:-)
75 */
76int main(int argc, char **argv) {
Steve Narofff7469a32009-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 Naroff50398192009-08-28 15:28:48 +000082 CXIndex Idx = clang_createIndex();
83 CXTranslationUnit TU = clang_createTranslationUnit(Idx, argv[1]);
Steve Narofff7469a32009-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 Naroff50398192009-08-28 15:28:48 +000098 return 1;
Steve Narofff7469a32009-09-23 20:00:53 +000099 }
Steve Naroff50398192009-08-28 15:28:48 +0000100}