blob: cc4080e388b5e775c1d64d22c71109b0ef262c00 [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"
Douglas Gregor0c8296d2009-11-07 00:00:49 +00004#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00005#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00006#include <string.h>
Daniel Dunbar4dc99f82009-11-08 04:11:32 +00007#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00008
John Thompson2e06fc82009-10-27 13:42:56 +00009#ifdef _MSC_VER
10char *basename(const char* path)
11{
12 char* base1 = (char*)strrchr(path, '/');
13 char* base2 = (char*)strrchr(path, '\\');
14 if (base1 && base2)
15 return((base1 > base2) ? base1 + 1 : base2 + 1);
16 else if (base1)
17 return(base1 + 1);
18 else if (base2)
19 return(base2 + 1);
20
21 return((char*)path);
22}
23#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000024extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000025#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000026
Steve Naroffaf08ddc2009-09-03 15:49:00 +000027static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000028 if (clang_isInvalid(Cursor.kind))
29 printf("Invalid Cursor => %s\n", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000030 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000031 CXDecl DeclReferenced;
Steve Naroffff9e18c2009-09-24 20:03:06 +000032 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
33 clang_getCursorSpelling(Cursor));
Eric Christopherf393c3b2009-10-05 21:33:42 +000034 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000035 if (DeclReferenced)
36 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
37 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000038 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000039}
Steve Naroff89922f82009-08-31 00:59:03 +000040
Eric Christopherf393c3b2009-10-05 21:33:42 +000041static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000042{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000043 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000044 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
45 clang_getCursorLine(Cursor),
46 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000047 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000048 printf(" [Context=%s]\n", clang_getDeclSpelling(Dcl));
Daniel Dunbarbce6f622009-09-03 05:59:50 +000049 }
Steve Naroffc857ea42009-09-02 13:28:54 +000050}
51static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Eric Christopherf393c3b2009-10-05 21:33:42 +000052 CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000053{
54 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000055 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Cursor)),
56 clang_getCursorLine(Cursor),
57 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000058 PrintCursor(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000059 printf(" [Context=%s]\n", basename(clang_getTranslationUnitSpelling(Unit)));
60
61 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +000062
Steve Naroff4ade6d62009-09-23 17:52:52 +000063 if (Cursor.kind == CXCursor_FunctionDefn) {
64 const char *startBuf, *endBuf;
65 unsigned startLine, startColumn, endLine, endColumn;
66 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
67 &startLine, &startColumn,
68 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000069 {
70 /* Probe the entire body, looking for both decls and refs. */
71 unsigned curLine = startLine, curColumn = startColumn;
72 CXCursor Ref;
Eric Christopherf393c3b2009-10-05 21:33:42 +000073
Steve Naroff6a6de8b2009-10-21 13:56:23 +000074 while (startBuf < endBuf) {
Steve Narofff7469a32009-09-23 20:00:53 +000075 if (*startBuf == '\n') {
76 startBuf++;
77 curLine++;
78 curColumn = 1;
79 } else if (*startBuf != '\t')
80 curColumn++;
Ted Kremenekfbcb2b72009-10-22 17:22:53 +000081
Steve Narofff96b5242009-10-28 20:44:47 +000082 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
83 curLine, curColumn);
Douglas Gregor02465752009-10-16 21:24:31 +000084 if (Ref.kind == CXCursor_NoDeclFound) {
Ted Kremenek8ce88a42009-10-17 06:37:16 +000085 /* Nothing found here; that's fine. */
Douglas Gregor02465752009-10-16 21:24:31 +000086 } else if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffff9e18c2009-09-24 20:03:06 +000087 printf("// CHECK: %s:%d:%d: ", basename(clang_getCursorSource(Ref)),
88 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +000089 PrintCursor(Ref);
Daniel Dunbar4dc99f82009-11-08 04:11:32 +000090 printf(" [Context:%s]\n", clang_getDeclSpelling(Ref.decl));
Steve Narofff7469a32009-09-23 20:00:53 +000091 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000092 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +000093 }
Steve Naroff4ade6d62009-09-23 17:52:52 +000094 }
95 }
Steve Naroff2d4d6292009-08-31 14:26:51 +000096 }
Steve Naroff89922f82009-08-31 00:59:03 +000097}
Steve Naroff50398192009-08-28 15:28:48 +000098
Douglas Gregor0c8296d2009-11-07 00:00:49 +000099/* Parse file:line:column from the input string. Returns 0 on success, non-zero
100 on failure. If successful, the pointer *filename will contain newly-allocated
101 memory (that will be owned by the caller) to store the file name. */
102int parse_file_line_column(const char *input, char **filename, unsigned *line,
103 unsigned *column) {
104 const char *colon = strchr(input, ':');
105 char *endptr = 0;
106 if (!colon) {
107 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
108 return 1;
109 }
110
111 /* Copy the file name. */
112 *filename = (char*)malloc(colon - input);
113 strncpy(*filename, input, colon - input);
114 (*filename)[colon - input] = 0;
115 input = colon + 1;
116
117 /* Parse the line number. */
118 *line = strtol(input, &endptr, 10);
119 if (*endptr != ':') {
120 fprintf(stderr, "could not parse line:column in '%s'\n", input);
121 free(filename);
122 *filename = 0;
123 return 1;
124 }
125 input = endptr + 1;
126
127 /* Parse the column number. */
128 *column = strtol(input, &endptr, 10);
129 if (*endptr != 0) {
130 fprintf(stderr, "could not parse column in '%s'\n", input);
131 free(filename);
132 *filename = 0;
133 return 1;
134 }
135
136 return 0;
137}
138
139const char *
140clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
141 switch (Kind) {
142 case CXCompletionChunk_Optional: return "Optional";
143 case CXCompletionChunk_TypedText: return "TypedText";
144 case CXCompletionChunk_Text: return "Text";
145 case CXCompletionChunk_Placeholder: return "Placeholder";
146 case CXCompletionChunk_Informative: return "Informative";
147 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
148 case CXCompletionChunk_LeftParen: return "LeftParen";
149 case CXCompletionChunk_RightParen: return "RightParen";
150 case CXCompletionChunk_LeftBracket: return "LeftBracket";
151 case CXCompletionChunk_RightBracket: return "RightBracket";
152 case CXCompletionChunk_LeftBrace: return "LeftBrace";
153 case CXCompletionChunk_RightBrace: return "RightBrace";
154 case CXCompletionChunk_LeftAngle: return "LeftAngle";
155 case CXCompletionChunk_RightAngle: return "RightAngle";
156 case CXCompletionChunk_Comma: return "Comma";
157 }
158
159 return "Unknown";
160}
161
162void print_completion_result(CXCompletionResult *completion_result,
163 CXClientData client_data) {
164 FILE *file = (FILE *)client_data;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000165 int I, N;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000166
167 fprintf(file, "%s:",
168 clang_getCursorKindSpelling(completion_result->CursorKind));
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000169 N = clang_getNumCompletionChunks(completion_result->CompletionString);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000170 for (I = 0; I != N; ++I) {
171 const char *text
172 = clang_getCompletionChunkText(completion_result->CompletionString, I);
173
174 enum CXCompletionChunkKind Kind
175 = clang_getCompletionChunkKind(completion_result->CompletionString, I);
176 fprintf(file, "{%s %s}",
177 clang_getCompletionChunkKindSpelling(Kind),
178 text? text : "");
179 }
180 fprintf(file, "\n");
181}
182
183void perform_code_completion(int argc, const char **argv) {
184 const char *input = argv[1];
185 char *filename = 0;
186 unsigned line;
187 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000188 CXIndex CIdx;
189
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000190 input += strlen("-code-completion-at=");
191 if (parse_file_line_column(input, &filename, &line, &column))
192 return;
193
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000194 CIdx = clang_createIndex(0, 0);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000195 clang_codeComplete(CIdx, argv[argc - 1], argc - 3, argv + 2,
196 filename, line, column, &print_completion_result, stdout);
197 clang_disposeIndex(CIdx);
198 free(filename);
199}
200
Steve Naroff50398192009-08-28 15:28:48 +0000201/*
202 * First sign of life:-)
203 */
204int main(int argc, char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000205 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) {
206 perform_code_completion(argc, (const char **)argv);
207 return 0;
208 }
209
210
Steve Narofff7469a32009-09-23 20:00:53 +0000211 if (argc != 3) {
212 printf("Incorrect usage of c-index-test (requires 3 arguments)\n");
213 return 0;
214 }
215 {
Ted Kremenek85f54682009-10-17 06:42:15 +0000216 CXIndex Idx;
217 CXTranslationUnit TU;
218 enum CXCursorKind K = CXCursor_NotImplemented;
219
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000220 Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(argv[2], "local") ? 1 : 0,
221 /* displayDiagnostics */ 1);
Ted Kremenek85f54682009-10-17 06:42:15 +0000222
Steve Naroffe56b4ba2009-10-20 14:46:24 +0000223 TU = clang_createTranslationUnit(Idx, argv[1]);
Ted Kremenek85f54682009-10-17 06:42:15 +0000224
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000225 if (!TU) {
226 fprintf(stderr, "Unable to load translation unit!\n");
227 return 1;
228 }
Eric Christopherf393c3b2009-10-05 21:33:42 +0000229
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000230 if (!strcmp(argv[2], "all") || !strcmp(argv[2], "local")) {
Steve Narofff7469a32009-09-23 20:00:53 +0000231 clang_loadTranslationUnit(TU, TranslationUnitVisitor, 0);
Steve Naroffe19944c2009-10-15 22:23:48 +0000232 clang_disposeTranslationUnit(TU);
Steve Narofff7469a32009-09-23 20:00:53 +0000233 return 1;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000234 }
Steve Narofff7469a32009-09-23 20:00:53 +0000235 /* Perform some simple filtering. */
236 if (!strcmp(argv[2], "category")) K = CXCursor_ObjCCategoryDecl;
237 else if (!strcmp(argv[2], "interface")) K = CXCursor_ObjCInterfaceDecl;
238 else if (!strcmp(argv[2], "protocol")) K = CXCursor_ObjCProtocolDecl;
239 else if (!strcmp(argv[2], "function")) K = CXCursor_FunctionDecl;
240 else if (!strcmp(argv[2], "typedef")) K = CXCursor_TypedefDecl;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000241
Steve Narofff7469a32009-09-23 20:00:53 +0000242 clang_loadTranslationUnit(TU, TranslationUnitVisitor, &K);
Steve Naroffe19944c2009-10-15 22:23:48 +0000243 clang_disposeTranslationUnit(TU);
Steve Naroff50398192009-08-28 15:28:48 +0000244 return 1;
Steve Narofff7469a32009-09-23 20:00:53 +0000245 }
Steve Naroff50398192009-08-28 15:28:48 +0000246}