blob: 56b29ac136fbe740c8dac91adb76a458183afbb2 [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
John Thompson2e06fc82009-10-27 13:42:56 +00007#ifdef _MSC_VER
8char *basename(const char* path)
9{
10 char* base1 = (char*)strrchr(path, '/');
11 char* base2 = (char*)strrchr(path, '\\');
12 if (base1 && base2)
13 return((base1 > base2) ? base1 + 1 : base2 + 1);
14 else if (base1)
15 return(base1 + 1);
16 else if (base2)
17 return(base2 + 1);
18
19 return((char*)path);
20}
21#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000022extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000023#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000024
Steve Naroffaf08ddc2009-09-03 15:49:00 +000025static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000026 if (clang_isInvalid(Cursor.kind))
27 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000028 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000029 CXDecl DeclReferenced;
Steve Naroffff9e18c2009-09-24 20:03:06 +000030 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
31 clang_getCursorSpelling(Cursor));
Eric Christopherf393c3b2009-10-05 21:33:42 +000032 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000033 if (DeclReferenced)
34 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
35 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000036 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000037}
Steve Naroff89922f82009-08-31 00:59:03 +000038
Eric Christopherf393c3b2009-10-05 21:33:42 +000039static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000040{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000041 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000042 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
43 clang_getCursorLine(Cursor),
44 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000045 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000046 printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl));
Daniel Dunbarbce6f622009-09-03 05:59:50 +000047 }
Steve Naroffc857ea42009-09-02 13:28:54 +000048}
49static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Eric Christopherf393c3b2009-10-05 21:33:42 +000050 CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000051{
52 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000053 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
54 clang_getCursorLine(Cursor),
55 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000056 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000057 printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit)));
58
59 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +000060
Steve Naroff4ade6d62009-09-23 17:52:52 +000061 if (Cursor.kind == CXCursor_FunctionDefn) {
62 const char *startBuf, *endBuf;
63 unsigned startLine, startColumn, endLine, endColumn;
64 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
65 &startLine, &startColumn,
66 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000067 {
68 /* Probe the entire body, looking for both decls and refs. */
69 unsigned curLine = startLine, curColumn = startColumn;
70 CXCursor Ref;
Eric Christopherf393c3b2009-10-05 21:33:42 +000071
Steve Naroff6a6de8b2009-10-21 13:56:23 +000072 while (startBuf < endBuf) {
Fariborz Jahaniancab98822009-10-22 22:49:47 +000073 CXLookupHint hint;
Steve Narofff7469a32009-09-23 20:00:53 +000074 if (*startBuf == '\n') {
75 startBuf++;
76 curLine++;
77 curColumn = 1;
78 } else if (*startBuf != '\t')
79 curColumn++;
Ted Kremenekfbcb2b72009-10-22 17:22:53 +000080
Ted Kremenekfbcb2b72009-10-22 17:22:53 +000081 clang_initCXLookupHint(&hint);
82 hint.decl = Cursor.decl;
Eric Christopherf393c3b2009-10-05 21:33:42 +000083
Ted Kremenekfbcb2b72009-10-22 17:22:53 +000084 Ref = clang_getCursorWithHint(Unit, clang_getCursorSource(Cursor),
85 curLine, curColumn, &hint);
Douglas Gregor02465752009-10-16 21:24:31 +000086 if (Ref.kind == CXCursor_NoDeclFound) {
Ted Kremenek8ce88a42009-10-17 06:37:16 +000087 /* Nothing found here; that's fine. */
Douglas Gregor02465752009-10-16 21:24:31 +000088 } else if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000089 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
90 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000091 PrintCursor(Ref);
Steve Naroffff9e18c2009-09-24 20:03:06 +000092 printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl));
Steve Narofff7469a32009-09-23 20:00:53 +000093 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000094 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +000095 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000096 }
97 }
Steve Naroff2d4d6292009-08-31 14:26:51 +000098 }
Steve Naroff89922f82009-08-31 00:59:03 +000099}
Steve Naroff50398192009-08-28 15:28:48 +0000100
101/*
102 * First sign of life:-)
103 */
104int main(int argc, char **argv) {
Steve Narofff7469a32009-09-23 20:00:53 +0000105 if (argc != 3) {
106 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
107 return 0;
108 }
109 {
Ted Kremenek85f54682009-10-17 06:42:15 +0000110 CXIndex Idx;
111 CXTranslationUnit TU;
112 enum CXCursorKind K = CXCursor_NotImplemented;
113
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000114 Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(argv[2], "local") ? 1 : 0,
115 /* displayDiagnostics */ 1);
Ted Kremenek85f54682009-10-17 06:42:15 +0000116
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000117 TU = clang_createTranslationUnit(Idx, argv[1]);
Ted Kremenek85f54682009-10-17 06:42:15 +0000118
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000119 if (!TU) {
120 fprintf(stderr, "Unable to load translation unit!\n");
121 return 1;
122 }
Eric Christopherf393c3b2009-10-05 21:33:42 +0000123
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000124 if (!strcmp(argv[2], "all") || !strcmp(argv[2], "local")) {
Steve Narofff7469a32009-09-23 20:00:53 +0000125 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
Steve Naroffe19944c2009-10-15 22:23:48 +0000126 clang_disposeTranslationUnit(TU);
Steve Narofff7469a32009-09-23 20:00:53 +0000127 return 1;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000128 }
Steve Narofff7469a32009-09-23 20:00:53 +0000129 /* Perform some simple filtering. */
130 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
131 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
132 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
133 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
134 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000135
Steve Narofff7469a32009-09-23 20:00:53 +0000136 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroffe19944c2009-10-15 22:23:48 +0000137 clang_disposeTranslationUnit(TU);
Steve Naroff50398192009-08-28 15:28:48 +0000138 return 1;
Steve Narofff7469a32009-09-23 20:00:53 +0000139 }
Steve Naroff50398192009-08-28 15:28:48 +0000140}