blob: 8791ee20a5831ab47350a2fc2eaf6f79a3b76161 [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
John Thompsonde258b52009-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 Naroffa7753c42009-09-24 20:03:06 +000022extern char *basename(const char *);
John Thompsonde258b52009-10-27 13:42:56 +000023#endif
Steve Naroffa7753c42009-09-24 20:03:06 +000024
Steve Naroff38c1a7b2009-09-03 15:49:00 +000025static void PrintCursor(CXCursor Cursor) {
Steve Naroff54f22fb2009-09-15 20:25:34 +000026 if (clang_isInvalid(Cursor.kind))
27 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff63f475a2009-09-25 21:32:34 +000028 else {
Eric Christopherc2ac4f02009-10-05 21:33:42 +000029 CXDecl DeclReferenced;
Steve Naroffa7753c42009-09-24 20:03:06 +000030 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
31 clang_getCursorSpelling(Cursor));
Eric Christopherc2ac4f02009-10-05 21:33:42 +000032 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroffa6c56bb2009-10-01 00:31:07 +000033 if (DeclReferenced)
34 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
35 clang_getDeclColumn(DeclReferenced));
Steve Naroff63f475a2009-09-25 21:32:34 +000036 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +000037}
Steve Naroff1054e602009-08-31 00:59:03 +000038
Eric Christopherc2ac4f02009-10-05 21:33:42 +000039static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
Steve Naroff3645f5a2009-09-02 13:28:54 +000040{
Daniel Dunbar3a0637b2009-09-03 05:59:50 +000041 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffa7753c42009-09-24 20:03:06 +000042 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
43 clang_getCursorLine(Cursor),
44 clang_getCursorColumn(Cursor));
Steve Naroff38c1a7b2009-09-03 15:49:00 +000045 PrintCursor(Cursor);
Steve Naroffa7753c42009-09-24 20:03:06 +000046 printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl));
Daniel Dunbar3a0637b2009-09-03 05:59:50 +000047 }
Steve Naroff3645f5a2009-09-02 13:28:54 +000048}
49static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Eric Christopherc2ac4f02009-10-05 21:33:42 +000050 CXClientData Filter)
Steve Naroff3645f5a2009-09-02 13:28:54 +000051{
52 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffa7753c42009-09-24 20:03:06 +000053 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
54 clang_getCursorLine(Cursor),
55 clang_getCursorColumn(Cursor));
Steve Naroff38c1a7b2009-09-03 15:49:00 +000056 PrintCursor(Cursor);
Steve Naroffa7753c42009-09-24 20:03:06 +000057 printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit)));
58
59 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroff3645f5a2009-09-02 13:28:54 +000060
Steve Naroff76b8f132009-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 Narofff99203ab2009-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 Christopherc2ac4f02009-10-05 21:33:42 +000071
Steve Naroff20bad0b2009-10-21 13:56:23 +000072 while (startBuf < endBuf) {
Steve Narofff99203ab2009-09-23 20:00:53 +000073 if (*startBuf == '\n') {
74 startBuf++;
75 curLine++;
76 curColumn = 1;
77 } else if (*startBuf != '\t')
78 curColumn++;
Ted Kremeneka96b72a2009-10-22 17:22:53 +000079
Steve Naroff58bd62d2009-10-28 20:44:47 +000080 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
81 curLine, curColumn);
Douglas Gregord3d923a2009-10-16 21:24:31 +000082 if (Ref.kind == CXCursor_NoDeclFound) {
Ted Kremenekbbf7fa32009-10-17 06:37:16 +000083 /* Nothing found here; that's fine. */
Douglas Gregord3d923a2009-10-16 21:24:31 +000084 } else if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffa7753c42009-09-24 20:03:06 +000085 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
86 curLine, curColumn);
Steve Narofff99203ab2009-09-23 20:00:53 +000087 PrintCursor(Ref);
Steve Naroffa7753c42009-09-24 20:03:06 +000088 printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl));
Steve Narofff99203ab2009-09-23 20:00:53 +000089 }
Steve Naroff76b8f132009-09-23 17:52:52 +000090 startBuf++;
Steve Naroff76b8f132009-09-23 17:52:52 +000091 }
Steve Naroff76b8f132009-09-23 17:52:52 +000092 }
93 }
Steve Naroff772c1a42009-08-31 14:26:51 +000094 }
Steve Naroff1054e602009-08-31 00:59:03 +000095}
Steve Naroffa1c72842009-08-28 15:28:48 +000096
97/*
98 * First sign of life:-)
99 */
100int main(int argc, char **argv) {
Steve Narofff99203ab2009-09-23 20:00:53 +0000101 if (argc != 3) {
102 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
103 return 0;
104 }
105 {
Ted Kremenek5ccfc982009-10-17 06:42:15 +0000106 CXIndex Idx;
107 CXTranslationUnit TU;
108 enum CXCursorKind K = CXCursor_NotImplemented;
109
Steve Naroff531e2842009-10-20 14:46:24 +0000110 Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(argv[2], "local") ? 1 : 0,
111 /* displayDiagnostics */ 1);
Ted Kremenek5ccfc982009-10-17 06:42:15 +0000112
Steve Naroff531e2842009-10-20 14:46:24 +0000113 TU = clang_createTranslationUnit(Idx, argv[1]);
Ted Kremenek5ccfc982009-10-17 06:42:15 +0000114
Douglas Gregor16bef852009-10-16 20:01:17 +0000115 if (!TU) {
116 fprintf(stderr, "Unable to load translation unit!\n");
117 return 1;
118 }
Eric Christopherc2ac4f02009-10-05 21:33:42 +0000119
Douglas Gregor16bef852009-10-16 20:01:17 +0000120 if (!strcmp(argv[2], "all") || !strcmp(argv[2], "local")) {
Steve Narofff99203ab2009-09-23 20:00:53 +0000121 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
Steve Naroff44cd60e2009-10-15 22:23:48 +0000122 clang_disposeTranslationUnit(TU);
Steve Narofff99203ab2009-09-23 20:00:53 +0000123 return 1;
Eric Christopherc2ac4f02009-10-05 21:33:42 +0000124 }
Steve Narofff99203ab2009-09-23 20:00:53 +0000125 /* Perform some simple filtering. */
126 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
127 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
128 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
129 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
130 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
Eric Christopherc2ac4f02009-10-05 21:33:42 +0000131
Steve Narofff99203ab2009-09-23 20:00:53 +0000132 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroff44cd60e2009-10-15 22:23:48 +0000133 clang_disposeTranslationUnit(TU);
Steve Naroffa1c72842009-08-28 15:28:48 +0000134 return 1;
Steve Narofff99203ab2009-09-23 20:00:53 +0000135 }
Steve Naroffa1c72842009-08-28 15:28:48 +0000136}