blob: f5ddfa68da9e9c9a9861d0c46127de0d4bb18e86 [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>
7
Ted Kremenek0d435192009-11-17 18:13:31 +00008/******************************************************************************/
9/* Utility functions. */
10/******************************************************************************/
11
John Thompson2e06fc82009-10-27 13:42:56 +000012#ifdef _MSC_VER
13char *basename(const char* path)
14{
15 char* base1 = (char*)strrchr(path, '/');
16 char* base2 = (char*)strrchr(path, '\\');
17 if (base1 && base2)
18 return((base1 > base2) ? base1 + 1 : base2 + 1);
19 else if (base1)
20 return(base1 + 1);
21 else if (base2)
22 return(base2 + 1);
23
24 return((char*)path);
25}
26#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000027extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000028#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000029
Ted Kremenek1c6da172009-11-17 19:37:36 +000030static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
31 CXTranslationUnit *TU) {
32
33 *TU = clang_createTranslationUnit(Idx, file);
34 if (!TU) {
35 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
36 return 0;
37 }
38 return 1;
39}
40
41
Ted Kremenek0d435192009-11-17 18:13:31 +000042/******************************************************************************/
43/* Pretty-printing. */
44/******************************************************************************/
45
Steve Naroffaf08ddc2009-09-03 15:49:00 +000046static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +000047 if (clang_isInvalid(Cursor.kind))
Ted Kremenek1c6da172009-11-17 19:37:36 +000048 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +000049 else {
Eric Christopherf393c3b2009-10-05 21:33:42 +000050 CXDecl DeclReferenced;
Steve Naroffef0cef62009-11-09 17:45:52 +000051 CXString string;
52 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000053 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000054 clang_getCString(string));
55 clang_disposeString(string);
Eric Christopherf393c3b2009-10-05 21:33:42 +000056 DeclReferenced = clang_getCursorDecl(Cursor);
Steve Naroff85e2db72009-10-01 00:31:07 +000057 if (DeclReferenced)
58 printf(":%d:%d", clang_getDeclLine(DeclReferenced),
59 clang_getDeclColumn(DeclReferenced));
Steve Naroff699a07d2009-09-25 21:32:34 +000060 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000061}
Steve Naroff89922f82009-08-31 00:59:03 +000062
Ted Kremenek9298cfc2009-11-17 05:31:58 +000063static const char* GetCursorSource(CXCursor Cursor) {
64 const char *source = clang_getCursorSource(Cursor);
65 if (!source)
66 return "<invalid loc>";
67 return basename(source);
68}
69
Ted Kremenek0d435192009-11-17 18:13:31 +000070/******************************************************************************/
71/* Logic for testing clang_loadTranslationUnit(). */
72/******************************************************************************/
73
Eric Christopherf393c3b2009-10-05 21:33:42 +000074static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000075{
Daniel Dunbarbce6f622009-09-03 05:59:50 +000076 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000077 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000078 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000079 clang_getCursorLine(Cursor),
80 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000081 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000082 string = clang_getDeclSpelling(Dcl);
83 printf(" [Context=%s]\n", clang_getCString(string));
84 clang_disposeString(string);
Daniel Dunbarbce6f622009-09-03 05:59:50 +000085 }
Steve Naroffc857ea42009-09-02 13:28:54 +000086}
87static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Eric Christopherf393c3b2009-10-05 21:33:42 +000088 CXClientData Filter)
Steve Naroffc857ea42009-09-02 13:28:54 +000089{
90 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Steve Naroffef0cef62009-11-09 17:45:52 +000091 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +000092 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Cursor),
Steve Naroffff9e18c2009-09-24 20:03:06 +000093 clang_getCursorLine(Cursor),
94 clang_getCursorColumn(Cursor));
Steve Naroffaf08ddc2009-09-03 15:49:00 +000095 PrintCursor(Cursor);
Steve Naroffef0cef62009-11-09 17:45:52 +000096 string = clang_getTranslationUnitSpelling(Unit);
97 printf(" [Context=%s]\n",
98 basename(clang_getCString(string)));
99 clang_disposeString(string);
Steve Naroffff9e18c2009-09-24 20:03:06 +0000100
101 clang_loadDeclaration(Cursor.decl, DeclVisitor, 0);
Steve Naroffc857ea42009-09-02 13:28:54 +0000102
Steve Naroff4ade6d62009-09-23 17:52:52 +0000103 if (Cursor.kind == CXCursor_FunctionDefn) {
104 const char *startBuf, *endBuf;
105 unsigned startLine, startColumn, endLine, endColumn;
106 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
107 &startLine, &startColumn,
108 &endLine, &endColumn);
Steve Narofff7469a32009-09-23 20:00:53 +0000109 {
110 /* Probe the entire body, looking for both decls and refs. */
111 unsigned curLine = startLine, curColumn = startColumn;
112 CXCursor Ref;
Eric Christopherf393c3b2009-10-05 21:33:42 +0000113
Steve Naroff6a6de8b2009-10-21 13:56:23 +0000114 while (startBuf < endBuf) {
Steve Narofff7469a32009-09-23 20:00:53 +0000115 if (*startBuf == '\n') {
116 startBuf++;
117 curLine++;
118 curColumn = 1;
119 } else if (*startBuf != '\t')
120 curColumn++;
Ted Kremenekfbcb2b72009-10-22 17:22:53 +0000121
Steve Narofff96b5242009-10-28 20:44:47 +0000122 Ref = clang_getCursor(Unit, clang_getCursorSource(Cursor),
123 curLine, curColumn);
Douglas Gregor02465752009-10-16 21:24:31 +0000124 if (Ref.kind == CXCursor_NoDeclFound) {
Ted Kremenek8ce88a42009-10-17 06:37:16 +0000125 /* Nothing found here; that's fine. */
Douglas Gregor02465752009-10-16 21:24:31 +0000126 } else if (Ref.kind != CXCursor_FunctionDecl) {
Steve Naroffef0cef62009-11-09 17:45:52 +0000127 CXString string;
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000128 printf("// CHECK: %s:%d:%d: ", GetCursorSource(Ref),
129 curLine, curColumn);
Steve Narofff7469a32009-09-23 20:00:53 +0000130 PrintCursor(Ref);
Steve Naroffef0cef62009-11-09 17:45:52 +0000131 string = clang_getDeclSpelling(Ref.decl);
132 printf(" [Context:%s]\n", clang_getCString(string));
133 clang_disposeString(string);
Steve Narofff7469a32009-09-23 20:00:53 +0000134 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000135 startBuf++;
Steve Naroff4ade6d62009-09-23 17:52:52 +0000136 }
Steve Naroff4ade6d62009-09-23 17:52:52 +0000137 }
138 }
Steve Naroff2d4d6292009-08-31 14:26:51 +0000139 }
Steve Naroff89922f82009-08-31 00:59:03 +0000140}
Steve Naroff50398192009-08-28 15:28:48 +0000141
Ted Kremenek0d435192009-11-17 18:13:31 +0000142int perform_test_load_tu(const char *file, const char *filter) {
143 CXIndex Idx;
144 CXTranslationUnit TU;
145 enum CXCursorKind K = CXCursor_NotImplemented;
146 enum CXCursorKind *ck = &K;
147 Idx = clang_createIndex(/* excludeDeclsFromPCH */
148 !strcmp(filter, "local") ? 1 : 0,
149 /* displayDiagnostics */ 1);
150
Ted Kremenek1c6da172009-11-17 19:37:36 +0000151 if (!CreateTranslationUnit(Idx, file, &TU))
Ted Kremenek0d435192009-11-17 18:13:31 +0000152 return 1;
Ted Kremenek0d435192009-11-17 18:13:31 +0000153
154 /* Perform some simple filtering. */
155 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
156 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
157 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
158 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
159 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
160 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
161 else {
162 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
163 return 1;
164 }
165
166 clang_loadTranslationUnit(TU, TranslationUnitVisitor, ck);
167 clang_disposeTranslationUnit(TU);
168 return 0;
169}
170
171/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000172/* Logic for testing clang_getCursor(). */
173/******************************************************************************/
174
175static void print_cursor_file_scan(CXCursor cursor,
176 unsigned start_line, unsigned start_col,
177 unsigned end_line, unsigned end_col) {
178 printf("CHECK: {start_line=%d start_col=%d end_line=%d end_col=%d} ",
179 start_line, start_col, end_line, end_col);
180 PrintCursor(cursor);
181 printf("\n");
182}
183
184static int perform_file_scan(const char *ast_file, const char *source_file) {
185 CXIndex Idx;
186 CXTranslationUnit TU;
187 FILE *fp;
188 unsigned line;
189 CXCursor prevCursor;
190 unsigned printed;
191 unsigned start_line, start_col, last_line, last_col;
192 size_t i;
193
194 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
195 /* displayDiagnostics */ 1))) {
196 fprintf(stderr, "Could not create Index\n");
197 return 1;
198 }
199
200 if (!CreateTranslationUnit(Idx, ast_file, &TU))
201 return 1;
202
203 if ((fp = fopen(source_file, "r")) == NULL) {
204 fprintf(stderr, "Could not open '%s'\n", source_file);
205 return 1;
206 }
207
208 line = 0;
209 prevCursor = clang_getNullCursor();
210 printed = 0;
211 start_line = last_line = 1;
212 start_col = last_col = 1;
213
214 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000215 size_t len = 0;
216 int c;
217
218 while ((c = fgetc(fp)) != EOF) {
219 len++;
220 if (c == '\n')
221 break;
222 }
223
Ted Kremenek1c6da172009-11-17 19:37:36 +0000224 ++line;
225
226 for (i = 0; i < len ; ++i) {
227 CXCursor cursor;
228 cursor = clang_getCursor(TU, source_file, line, i+1);
229
230 if (!clang_equalCursors(cursor, prevCursor) &&
231 prevCursor.kind != CXCursor_InvalidFile) {
232 print_cursor_file_scan(prevCursor, start_line, start_col,
233 last_line, last_col);
234 printed = 1;
235 start_line = line;
236 start_col = (unsigned) i+1;
237 }
238 else {
239 printed = 0;
240 }
241
242 prevCursor = cursor;
243 last_line = line;
244 last_col = (unsigned) i+1;
245 }
246 }
247
248 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
249 print_cursor_file_scan(prevCursor, start_line, start_col,
250 last_line, last_col);
251 }
252
253 fclose(fp);
254 return 0;
255}
256
257/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000258/* Logic for testing clang_codeComplete(). */
259/******************************************************************************/
260
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000261/* Parse file:line:column from the input string. Returns 0 on success, non-zero
262 on failure. If successful, the pointer *filename will contain newly-allocated
263 memory (that will be owned by the caller) to store the file name. */
264int parse_file_line_column(const char *input, char **filename, unsigned *line,
265 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000266 /* Find the second colon. */
267 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000268 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000269 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000270 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
271 return 1;
272 }
273
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000274 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000275 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000276 if (*endptr != 0) {
277 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000278 return 1;
279 }
280
281 /* Find the first colon. */
282 first_colon = second_colon - 1;
283 while (first_colon != input && *first_colon != ':')
284 --first_colon;
285 if (first_colon == input) {
286 fprintf(stderr, "could not parse line in '%s'\n", input);
287 return 1;
288 }
289
290 /* Parse the line number. */
291 *line = strtol(first_colon + 1, &endptr, 10);
292 if (*endptr != ':') {
293 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000294 return 1;
295 }
296
Douglas Gregor88d23952009-11-09 18:19:57 +0000297 /* Copy the file name. */
298 *filename = (char*)malloc(first_colon - input + 1);
299 memcpy(*filename, input, first_colon - input);
300 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000301 return 0;
302}
303
304const char *
305clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
306 switch (Kind) {
307 case CXCompletionChunk_Optional: return "Optional";
308 case CXCompletionChunk_TypedText: return "TypedText";
309 case CXCompletionChunk_Text: return "Text";
310 case CXCompletionChunk_Placeholder: return "Placeholder";
311 case CXCompletionChunk_Informative: return "Informative";
312 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
313 case CXCompletionChunk_LeftParen: return "LeftParen";
314 case CXCompletionChunk_RightParen: return "RightParen";
315 case CXCompletionChunk_LeftBracket: return "LeftBracket";
316 case CXCompletionChunk_RightBracket: return "RightBracket";
317 case CXCompletionChunk_LeftBrace: return "LeftBrace";
318 case CXCompletionChunk_RightBrace: return "RightBrace";
319 case CXCompletionChunk_LeftAngle: return "LeftAngle";
320 case CXCompletionChunk_RightAngle: return "RightAngle";
321 case CXCompletionChunk_Comma: return "Comma";
322 }
323
324 return "Unknown";
325}
326
Douglas Gregor3ac73852009-11-09 16:04:45 +0000327void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000328 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000329
330 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000331 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000332 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000333 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000334 = clang_getCompletionChunkKind(completion_string, I);
335
336 if (Kind == CXCompletionChunk_Optional) {
337 fprintf(file, "{Optional ");
338 print_completion_string(
339 clang_getCompletionChunkCompletionString(completion_string, I),
340 file);
341 fprintf(file, "}");
342 continue;
343 }
344
Douglas Gregord5a20892009-11-09 17:05:28 +0000345 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000346 fprintf(file, "{%s %s}",
347 clang_getCompletionChunkKindSpelling(Kind),
348 text? text : "");
349 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000350}
351
352void print_completion_result(CXCompletionResult *completion_result,
353 CXClientData client_data) {
354 FILE *file = (FILE *)client_data;
355 fprintf(file, "%s:",
356 clang_getCursorKindSpelling(completion_result->CursorKind));
357 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000358 fprintf(file, "\n");
359}
360
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000361int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000362 const char *input = argv[1];
363 char *filename = 0;
364 unsigned line;
365 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000366 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000367 int errorCode;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000368
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000369 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000370 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
371 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000372
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000373 CIdx = clang_createIndex(0, 0);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000374 clang_codeComplete(CIdx, argv[argc - 1], argc - 3, argv + 2,
375 filename, line, column, &print_completion_result, stdout);
376 clang_disposeIndex(CIdx);
377 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000378
379 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000380}
381
Ted Kremenek0d435192009-11-17 18:13:31 +0000382/******************************************************************************/
383/* Command line processing. */
384/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000385
386static void print_usage(void) {
387 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000388 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Ted Kremenek1c6da172009-11-17 19:37:36 +0000389 " c-index-test -test-file-scan <AST file> <source file>\n"
Ted Kremenek0d435192009-11-17 18:13:31 +0000390 " c-index-test -test-load-tu <AST file> <symbol filter>\n\n"
391 " <symbol filter> options for -test-load-tu:\n%s",
392 " all - load all symbols, including those from PCH\n"
393 " local - load all symbols except those in PCH\n"
394 " category - only load ObjC categories (non-PCH)\n"
395 " interface - only load ObjC interfaces (non-PCH)\n"
396 " protocol - only load ObjC protocols (non-PCH)\n"
397 " function - only load functions (non-PCH)\n"
398 " typedef - only load typdefs (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000399}
400
401int main(int argc, const char **argv) {
402 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
403 return perform_code_completion(argc, argv);
404 if (argc == 4 && strcmp(argv[1], "-test-load-tu") == 0)
405 return perform_test_load_tu(argv[2], argv[3]);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000406 if (argc == 4 && strcmp(argv[1], "-test-file-scan") == 0)
407 return perform_file_scan(argv[2], argv[3]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000408
409 print_usage();
410 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000411}