blob: 2d4ebf440614b0889d9b9c1182f489e07d826f85 [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>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00007#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00008
Ted Kremenek0d435192009-11-17 18:13:31 +00009/******************************************************************************/
10/* Utility functions. */
11/******************************************************************************/
12
John Thompson2e06fc82009-10-27 13:42:56 +000013#ifdef _MSC_VER
14char *basename(const char* path)
15{
16 char* base1 = (char*)strrchr(path, '/');
17 char* base2 = (char*)strrchr(path, '\\');
18 if (base1 && base2)
19 return((base1 > base2) ? base1 + 1 : base2 + 1);
20 else if (base1)
21 return(base1 + 1);
22 else if (base2)
23 return(base2 + 1);
24
25 return((char*)path);
26}
27#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000028extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000029#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000030
Ted Kremenek1c6da172009-11-17 19:37:36 +000031static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
32 CXTranslationUnit *TU) {
33
34 *TU = clang_createTranslationUnit(Idx, file);
35 if (!TU) {
36 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
37 return 0;
38 }
39 return 1;
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 {
Steve Naroffef0cef62009-11-09 17:45:52 +000050 CXString string;
Douglas Gregorc5d1e932010-01-19 01:20:04 +000051 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +000052 unsigned line, column;
Steve Naroffef0cef62009-11-09 17:45:52 +000053 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +000054 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +000055 clang_getCString(string));
56 clang_disposeString(string);
Douglas Gregorc5d1e932010-01-19 01:20:04 +000057
58 Referenced = clang_getCursorReferenced(Cursor);
59 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
60 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor1db19de2010-01-19 21:36:55 +000061 clang_getInstantiationLocation(Loc, 0, &line, &column);
62 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +000063 }
Douglas Gregorb6998662010-01-19 19:34:47 +000064
65 if (clang_isCursorDefinition(Cursor))
66 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +000067 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +000068}
Steve Naroff89922f82009-08-31 00:59:03 +000069
Ted Kremenek9298cfc2009-11-17 05:31:58 +000070static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +000071 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
72 const char *source;
73 CXFile file;
74 clang_getInstantiationLocation(Loc, &file, 0, 0);
75 source = clang_getFileName(file);
Ted Kremenek9298cfc2009-11-17 05:31:58 +000076 if (!source)
77 return "<invalid loc>";
78 return basename(source);
79}
80
Ted Kremenek0d435192009-11-17 18:13:31 +000081/******************************************************************************/
82/* Logic for testing clang_loadTranslationUnit(). */
83/******************************************************************************/
84
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +000085static const char *FileCheckPrefix = "CHECK";
86
Douglas Gregora7bde202010-01-19 00:34:46 +000087static void PrintCursorExtent(CXCursor C) {
88 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +000089 CXFile begin_file, end_file;
90 unsigned begin_line, begin_column, end_line, end_column;
91
92 clang_getInstantiationLocation(clang_getRangeStart(extent),
93 &begin_file, &begin_line, &begin_column);
94 clang_getInstantiationLocation(clang_getRangeEnd(extent),
95 &end_file, &end_line, &end_column);
96 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +000097 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +000098
99 printf(" [Extent=%d:%d:%d:%d]", begin_line, begin_column,
100 end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000101}
102
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000103static void DeclVisitor(CXDecl Dcl, CXCursor Cursor, CXClientData Filter) {
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000104 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000105 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000106 CXFile file;
107 unsigned line, column;
108 const char *source;
109 clang_getInstantiationLocation(Loc, &file, &line, &column);
110 source = clang_getFileName(file);
Douglas Gregor98258af2010-01-18 22:46:11 +0000111 if (!source)
112 source = "<invalid loc>";
Douglas Gregor1db19de2010-01-19 21:36:55 +0000113 printf("// %s: %s:%d:%d: ", FileCheckPrefix, source, line, column);
Douglas Gregora7bde202010-01-19 00:34:46 +0000114 PrintCursor(Cursor);
115 PrintCursorExtent(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000116
117 printf("\n");
Daniel Dunbarbce6f622009-09-03 05:59:50 +0000118 }
Steve Naroffc857ea42009-09-02 13:28:54 +0000119}
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000120
Steve Naroffc857ea42009-09-02 13:28:54 +0000121static void TranslationUnitVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000122 CXClientData Filter) {
Steve Naroffc857ea42009-09-02 13:28:54 +0000123 if (!Filter || (Cursor.kind == *(enum CXCursorKind *)Filter)) {
Ted Kremenek70ee5422010-01-16 01:44:12 +0000124 CXDecl D;
Douglas Gregor98258af2010-01-18 22:46:11 +0000125 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000126 unsigned line, column;
127 clang_getInstantiationLocation(Loc, 0, &line, &column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000128 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000129 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000130 PrintCursor(Cursor);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000131
Ted Kremenek70ee5422010-01-16 01:44:12 +0000132 D = clang_getCursorDecl(Cursor);
133 if (!D) {
134 printf("\n");
135 return;
136 }
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000137
Douglas Gregora7bde202010-01-19 00:34:46 +0000138 PrintCursorExtent(Cursor);
Ted Kremenek70ee5422010-01-16 01:44:12 +0000139 printf("\n");
140 clang_loadDeclaration(D, DeclVisitor, 0);
Steve Naroff2d4d6292009-08-31 14:26:51 +0000141 }
Steve Naroff89922f82009-08-31 00:59:03 +0000142}
Steve Naroff50398192009-08-28 15:28:48 +0000143
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000144static void FunctionScanVisitor(CXTranslationUnit Unit, CXCursor Cursor,
145 CXClientData Filter) {
146 const char *startBuf, *endBuf;
147 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
148 CXCursor Ref;
149
Douglas Gregorb6998662010-01-19 19:34:47 +0000150 if (Cursor.kind != CXCursor_FunctionDecl ||
151 !clang_isCursorDefinition(Cursor))
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000152 return;
153
154 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
155 &startLine, &startColumn,
156 &endLine, &endColumn);
157 /* Probe the entire body, looking for both decls and refs. */
158 curLine = startLine;
159 curColumn = startColumn;
160
161 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000162 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000163 CXFile file;
Douglas Gregor98258af2010-01-18 22:46:11 +0000164 const char *source = 0;
165
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000166 if (*startBuf == '\n') {
167 startBuf++;
168 curLine++;
169 curColumn = 1;
170 } else if (*startBuf != '\t')
171 curColumn++;
172
Douglas Gregor98258af2010-01-18 22:46:11 +0000173 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000174 clang_getInstantiationLocation(Loc, &file, 0, 0);
175 source = clang_getFileName(file);
Douglas Gregor98258af2010-01-18 22:46:11 +0000176 if (source) {
177 Ref = clang_getCursor(Unit, source, curLine, curColumn);
178 if (Ref.kind == CXCursor_NoDeclFound) {
179 /* Nothing found here; that's fine. */
180 } else if (Ref.kind != CXCursor_FunctionDecl) {
181 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
182 curLine, curColumn);
183 PrintCursor(Ref);
184 printf("\n");
185 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000186 }
187 startBuf++;
188 }
189}
190
Ted Kremenek7d405622010-01-12 23:34:26 +0000191/******************************************************************************/
192/* USR testing. */
193/******************************************************************************/
194
195static void USRDeclVisitor(CXDecl D, CXCursor C, CXClientData Filter) {
196 if (!Filter || (C.kind == *(enum CXCursorKind *)Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000197 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000198 if (!USR.Spelling) {
199 clang_disposeString(USR);
200 return;
201 }
202 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregora7bde202010-01-19 00:34:46 +0000203 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000204 printf("\n");
205 clang_disposeString(USR);
206 }
207}
208
209static void USRVisitor(CXTranslationUnit Unit, CXCursor Cursor,
Ted Kremenek70ee5422010-01-16 01:44:12 +0000210 CXClientData Filter) {
211 CXDecl D = clang_getCursorDecl(Cursor);
212 if (D) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000213 /* USRDeclVisitor(Unit, Cursor.decl, Cursor, Filter);*/
Ted Kremenek70ee5422010-01-16 01:44:12 +0000214 clang_loadDeclaration(D, USRDeclVisitor, 0);
Ted Kremenek7d405622010-01-12 23:34:26 +0000215 }
216}
217
218/******************************************************************************/
219/* Loading ASTs/source. */
220/******************************************************************************/
221
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000222static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000223 const char *filter, const char *prefix,
224 CXTranslationUnitIterator Visitor) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000225 enum CXCursorKind K = CXCursor_NotImplemented;
226 enum CXCursorKind *ck = &K;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000227
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000228 if (prefix)
229 FileCheckPrefix = prefix;
230
Ted Kremenek0d435192009-11-17 18:13:31 +0000231 /* Perform some simple filtering. */
232 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
233 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
234 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
235 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
236 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
237 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000238 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000239 else {
240 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
241 return 1;
242 }
243
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000244 clang_loadTranslationUnit(TU, Visitor, ck);
Ted Kremenek0d435192009-11-17 18:13:31 +0000245 clang_disposeTranslationUnit(TU);
246 return 0;
247}
248
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000249int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000250 const char *prefix,
251 CXTranslationUnitIterator Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000252 CXIndex Idx;
253 CXTranslationUnit TU;
254 Idx = clang_createIndex(/* excludeDeclsFromPCH */
255 !strcmp(filter, "local") ? 1 : 0,
256 /* displayDiagnostics */ 1);
257
258 if (!CreateTranslationUnit(Idx, file, &TU))
259 return 1;
260
Ted Kremenek98271562010-01-12 18:53:15 +0000261 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000262}
263
Ted Kremenek98271562010-01-12 18:53:15 +0000264int perform_test_load_source(int argc, const char **argv, const char *filter,
265 CXTranslationUnitIterator Visitor) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000266 const char *UseExternalASTs =
267 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000268 CXIndex Idx;
269 CXTranslationUnit TU;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000270 Idx = clang_createIndex(/* excludeDeclsFromPCH */
271 !strcmp(filter, "local") ? 1 : 0,
272 /* displayDiagnostics */ 1);
273
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000274 if (UseExternalASTs && strlen(UseExternalASTs))
275 clang_setUseExternalASTGeneration(Idx, 1);
276
Daniel Dunbarada487d2009-12-01 02:03:10 +0000277 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, argc, argv);
278 if (!TU) {
279 fprintf(stderr, "Unable to load translation unit!\n");
280 return 1;
281 }
282
Ted Kremenek98271562010-01-12 18:53:15 +0000283 return perform_test_load(Idx, TU, filter, NULL, Visitor);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000284}
285
Ted Kremenek0d435192009-11-17 18:13:31 +0000286/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000287/* Logic for testing clang_getCursor(). */
288/******************************************************************************/
289
290static void print_cursor_file_scan(CXCursor cursor,
291 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000292 unsigned end_line, unsigned end_col,
293 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000294 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000295 if (prefix)
296 printf("-%s", prefix);
297 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
298 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000299 PrintCursor(cursor);
300 printf("\n");
301}
302
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000303static int perform_file_scan(const char *ast_file, const char *source_file,
304 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000305 CXIndex Idx;
306 CXTranslationUnit TU;
307 FILE *fp;
308 unsigned line;
309 CXCursor prevCursor;
310 unsigned printed;
311 unsigned start_line, start_col, last_line, last_col;
312 size_t i;
313
314 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
315 /* displayDiagnostics */ 1))) {
316 fprintf(stderr, "Could not create Index\n");
317 return 1;
318 }
319
320 if (!CreateTranslationUnit(Idx, ast_file, &TU))
321 return 1;
322
323 if ((fp = fopen(source_file, "r")) == NULL) {
324 fprintf(stderr, "Could not open '%s'\n", source_file);
325 return 1;
326 }
327
328 line = 0;
329 prevCursor = clang_getNullCursor();
330 printed = 0;
331 start_line = last_line = 1;
332 start_col = last_col = 1;
333
334 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000335 size_t len = 0;
336 int c;
337
338 while ((c = fgetc(fp)) != EOF) {
339 len++;
340 if (c == '\n')
341 break;
342 }
343
Ted Kremenek1c6da172009-11-17 19:37:36 +0000344 ++line;
345
346 for (i = 0; i < len ; ++i) {
347 CXCursor cursor;
348 cursor = clang_getCursor(TU, source_file, line, i+1);
349
350 if (!clang_equalCursors(cursor, prevCursor) &&
351 prevCursor.kind != CXCursor_InvalidFile) {
352 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000353 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000354 printed = 1;
355 start_line = line;
356 start_col = (unsigned) i+1;
357 }
358 else {
359 printed = 0;
360 }
361
362 prevCursor = cursor;
363 last_line = line;
364 last_col = (unsigned) i+1;
365 }
366 }
367
368 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
369 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000370 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000371 }
372
373 fclose(fp);
374 return 0;
375}
376
377/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000378/* Logic for testing clang_codeComplete(). */
379/******************************************************************************/
380
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000381/* Parse file:line:column from the input string. Returns 0 on success, non-zero
382 on failure. If successful, the pointer *filename will contain newly-allocated
383 memory (that will be owned by the caller) to store the file name. */
384int parse_file_line_column(const char *input, char **filename, unsigned *line,
385 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000386 /* Find the second colon. */
387 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000388 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000389 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000390 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
391 return 1;
392 }
393
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000394 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000395 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000396 if (*endptr != 0) {
397 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000398 return 1;
399 }
400
401 /* Find the first colon. */
402 first_colon = second_colon - 1;
403 while (first_colon != input && *first_colon != ':')
404 --first_colon;
405 if (first_colon == input) {
406 fprintf(stderr, "could not parse line in '%s'\n", input);
407 return 1;
408 }
409
410 /* Parse the line number. */
411 *line = strtol(first_colon + 1, &endptr, 10);
412 if (*endptr != ':') {
413 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000414 return 1;
415 }
416
Douglas Gregor88d23952009-11-09 18:19:57 +0000417 /* Copy the file name. */
418 *filename = (char*)malloc(first_colon - input + 1);
419 memcpy(*filename, input, first_colon - input);
420 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000421 return 0;
422}
423
424const char *
425clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
426 switch (Kind) {
427 case CXCompletionChunk_Optional: return "Optional";
428 case CXCompletionChunk_TypedText: return "TypedText";
429 case CXCompletionChunk_Text: return "Text";
430 case CXCompletionChunk_Placeholder: return "Placeholder";
431 case CXCompletionChunk_Informative: return "Informative";
432 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
433 case CXCompletionChunk_LeftParen: return "LeftParen";
434 case CXCompletionChunk_RightParen: return "RightParen";
435 case CXCompletionChunk_LeftBracket: return "LeftBracket";
436 case CXCompletionChunk_RightBracket: return "RightBracket";
437 case CXCompletionChunk_LeftBrace: return "LeftBrace";
438 case CXCompletionChunk_RightBrace: return "RightBrace";
439 case CXCompletionChunk_LeftAngle: return "LeftAngle";
440 case CXCompletionChunk_RightAngle: return "RightAngle";
441 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000442 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000443 case CXCompletionChunk_Colon: return "Colon";
444 case CXCompletionChunk_SemiColon: return "SemiColon";
445 case CXCompletionChunk_Equal: return "Equal";
446 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
447 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000448 }
449
450 return "Unknown";
451}
452
Douglas Gregor3ac73852009-11-09 16:04:45 +0000453void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000454 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000455
456 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000457 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000458 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000459 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000460 = clang_getCompletionChunkKind(completion_string, I);
461
462 if (Kind == CXCompletionChunk_Optional) {
463 fprintf(file, "{Optional ");
464 print_completion_string(
465 clang_getCompletionChunkCompletionString(completion_string, I),
466 file);
467 fprintf(file, "}");
468 continue;
469 }
470
Douglas Gregord5a20892009-11-09 17:05:28 +0000471 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000472 fprintf(file, "{%s %s}",
473 clang_getCompletionChunkKindSpelling(Kind),
474 text? text : "");
475 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000476}
477
478void print_completion_result(CXCompletionResult *completion_result,
479 CXClientData client_data) {
480 FILE *file = (FILE *)client_data;
481 fprintf(file, "%s:",
482 clang_getCursorKindSpelling(completion_result->CursorKind));
483 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000484 fprintf(file, "\n");
485}
486
Douglas Gregor735df882009-12-02 09:21:34 +0000487void free_remapped_files(struct CXUnsavedFile *unsaved_files,
488 int num_unsaved_files) {
489 int i;
490 for (i = 0; i != num_unsaved_files; ++i) {
491 free((char *)unsaved_files[i].Filename);
492 free((char *)unsaved_files[i].Contents);
493 }
494}
495
496int parse_remapped_files(int argc, const char **argv, int start_arg,
497 struct CXUnsavedFile **unsaved_files,
498 int *num_unsaved_files) {
499 int i;
500 int arg;
501 int prefix_len = strlen("-remap-file=");
502 *unsaved_files = 0;
503 *num_unsaved_files = 0;
504
505 /* Count the number of remapped files. */
506 for (arg = start_arg; arg < argc; ++arg) {
507 if (strncmp(argv[arg], "-remap-file=", prefix_len))
508 break;
509
510 ++*num_unsaved_files;
511 }
512
513 if (*num_unsaved_files == 0)
514 return 0;
515
516 *unsaved_files
517 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
518 *num_unsaved_files);
519 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
520 struct CXUnsavedFile *unsaved = *unsaved_files + i;
521 const char *arg_string = argv[arg] + prefix_len;
522 int filename_len;
523 char *filename;
524 char *contents;
525 FILE *to_file;
526 const char *semi = strchr(arg_string, ';');
527 if (!semi) {
528 fprintf(stderr,
529 "error: -remap-file=from;to argument is missing semicolon\n");
530 free_remapped_files(*unsaved_files, i);
531 *unsaved_files = 0;
532 *num_unsaved_files = 0;
533 return -1;
534 }
535
536 /* Open the file that we're remapping to. */
537 to_file = fopen(semi + 1, "r");
538 if (!to_file) {
539 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
540 semi + 1);
541 free_remapped_files(*unsaved_files, i);
542 *unsaved_files = 0;
543 *num_unsaved_files = 0;
544 return -1;
545 }
546
547 /* Determine the length of the file we're remapping to. */
548 fseek(to_file, 0, SEEK_END);
549 unsaved->Length = ftell(to_file);
550 fseek(to_file, 0, SEEK_SET);
551
552 /* Read the contents of the file we're remapping to. */
553 contents = (char *)malloc(unsaved->Length + 1);
Chandler Carruth4da689a2009-12-17 09:18:43 +0000554 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
555 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
556 (feof(to_file) ? "EOF" : "error"), semi + 1);
557 fclose(to_file);
558 free_remapped_files(*unsaved_files, i);
559 *unsaved_files = 0;
560 *num_unsaved_files = 0;
561 return -1;
562 }
Douglas Gregor735df882009-12-02 09:21:34 +0000563 contents[unsaved->Length] = 0;
564 unsaved->Contents = contents;
565
566 /* Close the file. */
567 fclose(to_file);
568
569 /* Copy the file name that we're remapping from. */
570 filename_len = semi - arg_string;
571 filename = (char *)malloc(filename_len + 1);
572 memcpy(filename, arg_string, filename_len);
573 filename[filename_len] = 0;
574 unsaved->Filename = filename;
575 }
576
577 return 0;
578}
579
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000580int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000581 const char *input = argv[1];
582 char *filename = 0;
583 unsigned line;
584 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000585 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000586 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000587 struct CXUnsavedFile *unsaved_files = 0;
588 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000589 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000590
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000591 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000592 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
593 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000594
Douglas Gregor735df882009-12-02 09:21:34 +0000595 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
596 return -1;
597
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000598 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000599 results = clang_codeComplete(CIdx,
600 argv[argc - 1], argc - num_unsaved_files - 3,
601 argv + num_unsaved_files + 2,
602 num_unsaved_files, unsaved_files,
603 filename, line, column);
604 if (results) {
605 unsigned i, n = results->NumResults;
606 for (i = 0; i != n; ++i)
607 print_completion_result(results->Results + i, stdout);
608 clang_disposeCodeCompleteResults(results);
609 }
610
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000611 clang_disposeIndex(CIdx);
612 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000613
Douglas Gregor735df882009-12-02 09:21:34 +0000614 free_remapped_files(unsaved_files, num_unsaved_files);
615
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000616 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000617}
618
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000619typedef struct {
620 char *filename;
621 unsigned line;
622 unsigned column;
623} CursorSourceLocation;
624
625int inspect_cursor_at(int argc, const char **argv) {
626 CXIndex CIdx;
627 int errorCode;
628 struct CXUnsavedFile *unsaved_files = 0;
629 int num_unsaved_files = 0;
630 CXTranslationUnit TU;
631 CXCursor Cursor;
632 CursorSourceLocation *Locations = 0;
633 unsigned NumLocations = 0, Loc;
634
635 /* Count the number of locations. */
636 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
637 ++NumLocations;
638
639 /* Parse the locations. */
640 assert(NumLocations > 0 && "Unable to count locations?");
641 Locations = (CursorSourceLocation *)malloc(
642 NumLocations * sizeof(CursorSourceLocation));
643 for (Loc = 0; Loc < NumLocations; ++Loc) {
644 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
645 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
646 &Locations[Loc].line,
647 &Locations[Loc].column)))
648 return errorCode;
649 }
650
651 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
652 &num_unsaved_files))
653 return -1;
654
655 if (num_unsaved_files > 0) {
656 fprintf(stderr, "cannot remap files when looking for a cursor\n");
657 return -1;
658 }
659
660 CIdx = clang_createIndex(0, 1);
661 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
662 argc - num_unsaved_files - 2 - NumLocations,
663 argv + num_unsaved_files + 1 + NumLocations);
664 if (!TU) {
665 fprintf(stderr, "unable to parse input\n");
666 return -1;
667 }
668
669 for (Loc = 0; Loc < NumLocations; ++Loc) {
670 Cursor = clang_getCursor(TU, Locations[Loc].filename,
671 Locations[Loc].line, Locations[Loc].column);
672 PrintCursor(Cursor);
673 printf("\n");
674 free(Locations[Loc].filename);
675 }
676
677 clang_disposeTranslationUnit(TU);
678 clang_disposeIndex(CIdx);
679 free(Locations);
680 free_remapped_files(unsaved_files, num_unsaved_files);
681 return 0;
682}
683
Ted Kremenek0d435192009-11-17 18:13:31 +0000684/******************************************************************************/
685/* Command line processing. */
686/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000687
Ted Kremenek7d405622010-01-12 23:34:26 +0000688static CXTranslationUnitIterator GetVisitor(const char *s) {
689 if (s[0] == '\0')
690 return TranslationUnitVisitor;
691 if (strcmp(s, "-usrs") == 0)
692 return USRVisitor;
693 return NULL;
694}
695
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000696static void print_usage(void) {
697 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000698 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000699 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000700 " c-index-test -test-file-scan <AST file> <source file> "
701 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000702 " c-index-test -test-load-tu <AST file> <symbol filter> "
703 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000704 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
705 "[FileCheck prefix]\n"
706 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000707 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n");
708 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +0000709 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000710 " all - load all symbols, including those from PCH\n"
711 " local - load all symbols except those in PCH\n"
712 " category - only load ObjC categories (non-PCH)\n"
713 " interface - only load ObjC interfaces (non-PCH)\n"
714 " protocol - only load ObjC protocols (non-PCH)\n"
715 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000716 " typedef - only load typdefs (non-PCH)\n"
717 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000718}
719
720int main(int argc, const char **argv) {
721 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
722 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000723 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
724 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000725 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
726 CXTranslationUnitIterator I = GetVisitor(argv[1] + 13);
727 if (I)
728 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I);
729 }
730 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
731 CXTranslationUnitIterator I = GetVisitor(argv[1] + 17);
732 if (I)
733 return perform_test_load_source(argc - 3, argv + 3, argv[2], I);
734 }
735 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000736 return perform_file_scan(argv[2], argv[3],
737 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000738
739 print_usage();
740 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000741}