blob: 4ef390413972f6c21a56d285405feb4f59182438 [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
Douglas Gregor4db64a42010-01-23 00:14:00 +000042void free_remapped_files(struct CXUnsavedFile *unsaved_files,
43 int num_unsaved_files) {
44 int i;
45 for (i = 0; i != num_unsaved_files; ++i) {
46 free((char *)unsaved_files[i].Filename);
47 free((char *)unsaved_files[i].Contents);
48 }
49}
50
51int parse_remapped_files(int argc, const char **argv, int start_arg,
52 struct CXUnsavedFile **unsaved_files,
53 int *num_unsaved_files) {
54 int i;
55 int arg;
56 int prefix_len = strlen("-remap-file=");
57 *unsaved_files = 0;
58 *num_unsaved_files = 0;
59
60 /* Count the number of remapped files. */
61 for (arg = start_arg; arg < argc; ++arg) {
62 if (strncmp(argv[arg], "-remap-file=", prefix_len))
63 break;
64
65 ++*num_unsaved_files;
66 }
67
68 if (*num_unsaved_files == 0)
69 return 0;
70
71 *unsaved_files
72 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
73 *num_unsaved_files);
74 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
75 struct CXUnsavedFile *unsaved = *unsaved_files + i;
76 const char *arg_string = argv[arg] + prefix_len;
77 int filename_len;
78 char *filename;
79 char *contents;
80 FILE *to_file;
81 const char *semi = strchr(arg_string, ';');
82 if (!semi) {
83 fprintf(stderr,
84 "error: -remap-file=from;to argument is missing semicolon\n");
85 free_remapped_files(*unsaved_files, i);
86 *unsaved_files = 0;
87 *num_unsaved_files = 0;
88 return -1;
89 }
90
91 /* Open the file that we're remapping to. */
92 to_file = fopen(semi + 1, "r");
93 if (!to_file) {
94 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
95 semi + 1);
96 free_remapped_files(*unsaved_files, i);
97 *unsaved_files = 0;
98 *num_unsaved_files = 0;
99 return -1;
100 }
101
102 /* Determine the length of the file we're remapping to. */
103 fseek(to_file, 0, SEEK_END);
104 unsaved->Length = ftell(to_file);
105 fseek(to_file, 0, SEEK_SET);
106
107 /* Read the contents of the file we're remapping to. */
108 contents = (char *)malloc(unsaved->Length + 1);
109 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
110 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
111 (feof(to_file) ? "EOF" : "error"), semi + 1);
112 fclose(to_file);
113 free_remapped_files(*unsaved_files, i);
114 *unsaved_files = 0;
115 *num_unsaved_files = 0;
116 return -1;
117 }
118 contents[unsaved->Length] = 0;
119 unsaved->Contents = contents;
120
121 /* Close the file. */
122 fclose(to_file);
123
124 /* Copy the file name that we're remapping from. */
125 filename_len = semi - arg_string;
126 filename = (char *)malloc(filename_len + 1);
127 memcpy(filename, arg_string, filename_len);
128 filename[filename_len] = 0;
129 unsaved->Filename = filename;
130 }
131
132 return 0;
133}
134
Ted Kremenek0d435192009-11-17 18:13:31 +0000135/******************************************************************************/
136/* Pretty-printing. */
137/******************************************************************************/
138
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000139static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000140 if (clang_isInvalid(Cursor.kind))
Ted Kremenek1c6da172009-11-17 19:37:36 +0000141 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +0000142 else {
Steve Naroffef0cef62009-11-09 17:45:52 +0000143 CXString string;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000144 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000145 unsigned line, column;
Steve Naroffef0cef62009-11-09 17:45:52 +0000146 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +0000147 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +0000148 clang_getCString(string));
149 clang_disposeString(string);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000150
151 Referenced = clang_getCursorReferenced(Cursor);
152 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
153 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000154 clang_getInstantiationLocation(Loc, 0, &line, &column);
155 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000156 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000157
158 if (clang_isCursorDefinition(Cursor))
159 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000160 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000161}
Steve Naroff89922f82009-08-31 00:59:03 +0000162
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000163static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000164 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
165 const char *source;
166 CXFile file;
167 clang_getInstantiationLocation(Loc, &file, 0, 0);
168 source = clang_getFileName(file);
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000169 if (!source)
170 return "<invalid loc>";
171 return basename(source);
172}
173
Ted Kremenek0d435192009-11-17 18:13:31 +0000174/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000175/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000176/******************************************************************************/
177
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000178static const char *FileCheckPrefix = "CHECK";
179
Douglas Gregora7bde202010-01-19 00:34:46 +0000180static void PrintCursorExtent(CXCursor C) {
181 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000182 CXFile begin_file, end_file;
183 unsigned begin_line, begin_column, end_line, end_column;
184
185 clang_getInstantiationLocation(clang_getRangeStart(extent),
186 &begin_file, &begin_line, &begin_column);
187 clang_getInstantiationLocation(clang_getRangeEnd(extent),
188 &end_file, &end_line, &end_column);
189 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000190 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000191
192 printf(" [Extent=%d:%d:%d:%d]", begin_line, begin_column,
193 end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000194}
195
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000196/* Data used by all of the visitors. */
197typedef struct {
198 CXTranslationUnit TU;
199 enum CXCursorKind *Filter;
200} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000201
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000202
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000203enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
204 CXCursor Parent,
205 CXClientData ClientData) {
206 VisitorData *Data = (VisitorData *)ClientData;
207 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000208 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000209 unsigned line, column;
210 clang_getInstantiationLocation(Loc, 0, &line, &column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000211 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000212 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000213 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000214 PrintCursorExtent(Cursor);
Ted Kremenek70ee5422010-01-16 01:44:12 +0000215 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000216 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000217 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000218
219 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000220}
Steve Naroff50398192009-08-28 15:28:48 +0000221
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000222static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
223 CXCursor Parent,
224 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000225 const char *startBuf, *endBuf;
226 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
227 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000228 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000229
Douglas Gregorb6998662010-01-19 19:34:47 +0000230 if (Cursor.kind != CXCursor_FunctionDecl ||
231 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000232 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000233
234 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
235 &startLine, &startColumn,
236 &endLine, &endColumn);
237 /* Probe the entire body, looking for both decls and refs. */
238 curLine = startLine;
239 curColumn = startColumn;
240
241 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000242 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000243 CXFile file;
Douglas Gregor98258af2010-01-18 22:46:11 +0000244 const char *source = 0;
245
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000246 if (*startBuf == '\n') {
247 startBuf++;
248 curLine++;
249 curColumn = 1;
250 } else if (*startBuf != '\t')
251 curColumn++;
252
Douglas Gregor98258af2010-01-18 22:46:11 +0000253 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000254 clang_getInstantiationLocation(Loc, &file, 0, 0);
255 source = clang_getFileName(file);
Douglas Gregor98258af2010-01-18 22:46:11 +0000256 if (source) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000257 CXSourceLocation RefLoc
258 = clang_getLocation(Data->TU, file, curLine, curColumn);
259 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000260 if (Ref.kind == CXCursor_NoDeclFound) {
261 /* Nothing found here; that's fine. */
262 } else if (Ref.kind != CXCursor_FunctionDecl) {
263 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
264 curLine, curColumn);
265 PrintCursor(Ref);
266 printf("\n");
267 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000268 }
269 startBuf++;
270 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000271
272 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000273}
274
Ted Kremenek7d405622010-01-12 23:34:26 +0000275/******************************************************************************/
276/* USR testing. */
277/******************************************************************************/
278
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000279enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
280 CXClientData ClientData) {
281 VisitorData *Data = (VisitorData *)ClientData;
282 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000283 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000284 if (!USR.Spelling) {
285 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000286 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000287 }
288 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregora7bde202010-01-19 00:34:46 +0000289 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000290 printf("\n");
291 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000292
293 return CXChildVisit_Recurse;
294 }
295
296 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000297}
298
299/******************************************************************************/
300/* Loading ASTs/source. */
301/******************************************************************************/
302
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000303static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000304 const char *filter, const char *prefix,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000305 CXCursorVisitor Visitor) {
Ted Kremenek0d435192009-11-17 18:13:31 +0000306 enum CXCursorKind K = CXCursor_NotImplemented;
307 enum CXCursorKind *ck = &K;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000308 VisitorData Data;
309
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000310 if (prefix)
311 FileCheckPrefix = prefix;
312
Ted Kremenek0d435192009-11-17 18:13:31 +0000313 /* Perform some simple filtering. */
314 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
315 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
316 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
317 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
318 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
319 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000320 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
Ted Kremenek0d435192009-11-17 18:13:31 +0000321 else {
322 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
323 return 1;
324 }
325
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000326 Data.TU = TU;
327 Data.Filter = ck;
Douglas Gregorb2cd4872010-01-20 23:57:43 +0000328 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000329 clang_disposeTranslationUnit(TU);
330 return 0;
331}
332
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000333int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000334 const char *prefix,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000335 CXCursorVisitor Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000336 CXIndex Idx;
337 CXTranslationUnit TU;
338 Idx = clang_createIndex(/* excludeDeclsFromPCH */
339 !strcmp(filter, "local") ? 1 : 0,
340 /* displayDiagnostics */ 1);
341
342 if (!CreateTranslationUnit(Idx, file, &TU))
343 return 1;
344
Ted Kremenek98271562010-01-12 18:53:15 +0000345 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000346}
347
Ted Kremenek98271562010-01-12 18:53:15 +0000348int perform_test_load_source(int argc, const char **argv, const char *filter,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000349 CXCursorVisitor Visitor) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000350 const char *UseExternalASTs =
351 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000352 CXIndex Idx;
353 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000354 struct CXUnsavedFile *unsaved_files = 0;
355 int num_unsaved_files = 0;
356 int result;
357
Daniel Dunbarada487d2009-12-01 02:03:10 +0000358 Idx = clang_createIndex(/* excludeDeclsFromPCH */
359 !strcmp(filter, "local") ? 1 : 0,
360 /* displayDiagnostics */ 1);
361
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000362 if (UseExternalASTs && strlen(UseExternalASTs))
363 clang_setUseExternalASTGeneration(Idx, 1);
364
Douglas Gregor4db64a42010-01-23 00:14:00 +0000365 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files))
366 return -1;
367
368 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
369 argc - num_unsaved_files,
370 argv + num_unsaved_files,
371 num_unsaved_files,
372 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000373 if (!TU) {
374 fprintf(stderr, "Unable to load translation unit!\n");
375 return 1;
376 }
377
Douglas Gregor4db64a42010-01-23 00:14:00 +0000378 result = perform_test_load(Idx, TU, filter, NULL, Visitor);
379 free_remapped_files(unsaved_files, num_unsaved_files);
380 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000381}
382
Ted Kremenek0d435192009-11-17 18:13:31 +0000383/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000384/* Logic for testing clang_getCursor(). */
385/******************************************************************************/
386
387static void print_cursor_file_scan(CXCursor cursor,
388 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000389 unsigned end_line, unsigned end_col,
390 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000391 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000392 if (prefix)
393 printf("-%s", prefix);
394 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
395 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000396 PrintCursor(cursor);
397 printf("\n");
398}
399
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000400static int perform_file_scan(const char *ast_file, const char *source_file,
401 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000402 CXIndex Idx;
403 CXTranslationUnit TU;
404 FILE *fp;
405 unsigned line;
406 CXCursor prevCursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000407 CXFile file;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000408 unsigned printed;
409 unsigned start_line, start_col, last_line, last_col;
410 size_t i;
411
412 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
413 /* displayDiagnostics */ 1))) {
414 fprintf(stderr, "Could not create Index\n");
415 return 1;
416 }
417
418 if (!CreateTranslationUnit(Idx, ast_file, &TU))
419 return 1;
420
421 if ((fp = fopen(source_file, "r")) == NULL) {
422 fprintf(stderr, "Could not open '%s'\n", source_file);
423 return 1;
424 }
425
426 line = 0;
427 prevCursor = clang_getNullCursor();
428 printed = 0;
429 start_line = last_line = 1;
430 start_col = last_col = 1;
431
Douglas Gregorb9790342010-01-22 21:44:22 +0000432 file = clang_getFile(TU, source_file);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000433 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000434 size_t len = 0;
435 int c;
436
437 while ((c = fgetc(fp)) != EOF) {
438 len++;
439 if (c == '\n')
440 break;
441 }
442
Ted Kremenek1c6da172009-11-17 19:37:36 +0000443 ++line;
444
445 for (i = 0; i < len ; ++i) {
446 CXCursor cursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000447 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, i+1));
Ted Kremenek1c6da172009-11-17 19:37:36 +0000448
449 if (!clang_equalCursors(cursor, prevCursor) &&
450 prevCursor.kind != CXCursor_InvalidFile) {
451 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000452 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000453 printed = 1;
454 start_line = line;
455 start_col = (unsigned) i+1;
456 }
457 else {
458 printed = 0;
459 }
460
461 prevCursor = cursor;
462 last_line = line;
463 last_col = (unsigned) i+1;
464 }
465 }
466
467 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
468 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000469 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000470 }
471
472 fclose(fp);
473 return 0;
474}
475
476/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000477/* Logic for testing clang_codeComplete(). */
478/******************************************************************************/
479
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000480/* Parse file:line:column from the input string. Returns 0 on success, non-zero
481 on failure. If successful, the pointer *filename will contain newly-allocated
482 memory (that will be owned by the caller) to store the file name. */
483int parse_file_line_column(const char *input, char **filename, unsigned *line,
484 unsigned *column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000485 /* Find the second colon. */
486 const char *second_colon = strrchr(input, ':'), *first_colon;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000487 char *endptr = 0;
Douglas Gregor88d23952009-11-09 18:19:57 +0000488 if (!second_colon || second_colon == input) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000489 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
490 return 1;
491 }
492
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000493 /* Parse the column number. */
Douglas Gregor88d23952009-11-09 18:19:57 +0000494 *column = strtol(second_colon + 1, &endptr, 10);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000495 if (*endptr != 0) {
496 fprintf(stderr, "could not parse column in '%s'\n", input);
Douglas Gregor88d23952009-11-09 18:19:57 +0000497 return 1;
498 }
499
500 /* Find the first colon. */
501 first_colon = second_colon - 1;
502 while (first_colon != input && *first_colon != ':')
503 --first_colon;
504 if (first_colon == input) {
505 fprintf(stderr, "could not parse line in '%s'\n", input);
506 return 1;
507 }
508
509 /* Parse the line number. */
510 *line = strtol(first_colon + 1, &endptr, 10);
511 if (*endptr != ':') {
512 fprintf(stderr, "could not parse line in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000513 return 1;
514 }
515
Douglas Gregor88d23952009-11-09 18:19:57 +0000516 /* Copy the file name. */
517 *filename = (char*)malloc(first_colon - input + 1);
518 memcpy(*filename, input, first_colon - input);
519 (*filename)[first_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000520 return 0;
521}
522
523const char *
524clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
525 switch (Kind) {
526 case CXCompletionChunk_Optional: return "Optional";
527 case CXCompletionChunk_TypedText: return "TypedText";
528 case CXCompletionChunk_Text: return "Text";
529 case CXCompletionChunk_Placeholder: return "Placeholder";
530 case CXCompletionChunk_Informative: return "Informative";
531 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
532 case CXCompletionChunk_LeftParen: return "LeftParen";
533 case CXCompletionChunk_RightParen: return "RightParen";
534 case CXCompletionChunk_LeftBracket: return "LeftBracket";
535 case CXCompletionChunk_RightBracket: return "RightBracket";
536 case CXCompletionChunk_LeftBrace: return "LeftBrace";
537 case CXCompletionChunk_RightBrace: return "RightBrace";
538 case CXCompletionChunk_LeftAngle: return "LeftAngle";
539 case CXCompletionChunk_RightAngle: return "RightAngle";
540 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000541 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000542 case CXCompletionChunk_Colon: return "Colon";
543 case CXCompletionChunk_SemiColon: return "SemiColon";
544 case CXCompletionChunk_Equal: return "Equal";
545 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
546 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000547 }
548
549 return "Unknown";
550}
551
Douglas Gregor3ac73852009-11-09 16:04:45 +0000552void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000553 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000554
555 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000556 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000557 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000558 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000559 = clang_getCompletionChunkKind(completion_string, I);
560
561 if (Kind == CXCompletionChunk_Optional) {
562 fprintf(file, "{Optional ");
563 print_completion_string(
564 clang_getCompletionChunkCompletionString(completion_string, I),
565 file);
566 fprintf(file, "}");
567 continue;
568 }
569
Douglas Gregord5a20892009-11-09 17:05:28 +0000570 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000571 fprintf(file, "{%s %s}",
572 clang_getCompletionChunkKindSpelling(Kind),
573 text? text : "");
574 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000575}
576
577void print_completion_result(CXCompletionResult *completion_result,
578 CXClientData client_data) {
579 FILE *file = (FILE *)client_data;
580 fprintf(file, "%s:",
581 clang_getCursorKindSpelling(completion_result->CursorKind));
582 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000583 fprintf(file, "\n");
584}
585
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000586int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000587 const char *input = argv[1];
588 char *filename = 0;
589 unsigned line;
590 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000591 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000592 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000593 struct CXUnsavedFile *unsaved_files = 0;
594 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000595 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000596
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000597 input += strlen("-code-completion-at=");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000598 if ((errorCode = parse_file_line_column(input, &filename, &line, &column)))
599 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000600
Douglas Gregor735df882009-12-02 09:21:34 +0000601 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
602 return -1;
603
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000604 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000605 results = clang_codeComplete(CIdx,
606 argv[argc - 1], argc - num_unsaved_files - 3,
607 argv + num_unsaved_files + 2,
608 num_unsaved_files, unsaved_files,
609 filename, line, column);
610 if (results) {
611 unsigned i, n = results->NumResults;
612 for (i = 0; i != n; ++i)
613 print_completion_result(results->Results + i, stdout);
614 clang_disposeCodeCompleteResults(results);
615 }
616
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000617 clang_disposeIndex(CIdx);
618 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000619
Douglas Gregor735df882009-12-02 09:21:34 +0000620 free_remapped_files(unsaved_files, num_unsaved_files);
621
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000622 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000623}
624
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000625typedef struct {
626 char *filename;
627 unsigned line;
628 unsigned column;
629} CursorSourceLocation;
630
631int inspect_cursor_at(int argc, const char **argv) {
632 CXIndex CIdx;
633 int errorCode;
634 struct CXUnsavedFile *unsaved_files = 0;
635 int num_unsaved_files = 0;
636 CXTranslationUnit TU;
637 CXCursor Cursor;
638 CursorSourceLocation *Locations = 0;
639 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000640
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000641 /* Count the number of locations. */
642 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
643 ++NumLocations;
644
645 /* Parse the locations. */
646 assert(NumLocations > 0 && "Unable to count locations?");
647 Locations = (CursorSourceLocation *)malloc(
648 NumLocations * sizeof(CursorSourceLocation));
649 for (Loc = 0; Loc < NumLocations; ++Loc) {
650 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
651 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
652 &Locations[Loc].line,
653 &Locations[Loc].column)))
654 return errorCode;
655 }
656
657 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
658 &num_unsaved_files))
659 return -1;
660
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000661 CIdx = clang_createIndex(0, 1);
662 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
663 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000664 argv + num_unsaved_files + 1 + NumLocations,
665 num_unsaved_files,
666 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000667 if (!TU) {
668 fprintf(stderr, "unable to parse input\n");
669 return -1;
670 }
671
672 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000673 CXFile file = clang_getFile(TU, Locations[Loc].filename);
674 if (!file)
675 continue;
676
677 Cursor = clang_getCursor(TU,
678 clang_getLocation(TU, file, Locations[Loc].line,
679 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000680 PrintCursor(Cursor);
681 printf("\n");
682 free(Locations[Loc].filename);
683 }
684
685 clang_disposeTranslationUnit(TU);
686 clang_disposeIndex(CIdx);
687 free(Locations);
688 free_remapped_files(unsaved_files, num_unsaved_files);
689 return 0;
690}
691
Ted Kremenek0d435192009-11-17 18:13:31 +0000692/******************************************************************************/
693/* Command line processing. */
694/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000695
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000696static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000697 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000698 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000699 if (strcmp(s, "-usrs") == 0)
700 return USRVisitor;
701 return NULL;
702}
703
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000704static void print_usage(void) {
705 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000706 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000707 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000708 " c-index-test -test-file-scan <AST file> <source file> "
709 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000710 " c-index-test -test-load-tu <AST file> <symbol filter> "
711 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000712 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
713 "[FileCheck prefix]\n"
714 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000715 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n\n");
716 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +0000717 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000718 " all - load all symbols, including those from PCH\n"
719 " local - load all symbols except those in PCH\n"
720 " category - only load ObjC categories (non-PCH)\n"
721 " interface - only load ObjC interfaces (non-PCH)\n"
722 " protocol - only load ObjC protocols (non-PCH)\n"
723 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000724 " typedef - only load typdefs (non-PCH)\n"
725 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000726}
727
728int main(int argc, const char **argv) {
729 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
730 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000731 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
732 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000733 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000734 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +0000735 if (I)
736 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I);
737 }
738 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000739 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +0000740 if (I)
741 return perform_test_load_source(argc - 3, argv + 3, argv[2], I);
742 }
743 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000744 return perform_file_scan(argv[2], argv[3],
745 argc >= 5 ? argv[4] : 0);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000746
747 print_usage();
748 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000749}