blob: a5b2f9166b26fcf43847cca3445fe93375d6fa29 [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) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000306
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000307 if (prefix)
308 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000309
310 if (Visitor) {
311 enum CXCursorKind K = CXCursor_NotImplemented;
312 enum CXCursorKind *ck = &K;
313 VisitorData Data;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000314
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000315 /* Perform some simple filtering. */
316 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
317 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
318 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
319 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
320 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
321 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
322 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
323 else {
324 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
325 return 1;
326 }
327
328 Data.TU = TU;
329 Data.Filter = ck;
330 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000331 }
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000332
Ted Kremenek0d435192009-11-17 18:13:31 +0000333 clang_disposeTranslationUnit(TU);
334 return 0;
335}
336
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000337int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenek98271562010-01-12 18:53:15 +0000338 const char *prefix,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000339 CXCursorVisitor Visitor) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000340 CXIndex Idx;
341 CXTranslationUnit TU;
342 Idx = clang_createIndex(/* excludeDeclsFromPCH */
343 !strcmp(filter, "local") ? 1 : 0,
344 /* displayDiagnostics */ 1);
345
346 if (!CreateTranslationUnit(Idx, file, &TU))
347 return 1;
348
Ted Kremenek98271562010-01-12 18:53:15 +0000349 return perform_test_load(Idx, TU, filter, prefix, Visitor);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000350}
351
Ted Kremenek98271562010-01-12 18:53:15 +0000352int perform_test_load_source(int argc, const char **argv, const char *filter,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000353 CXCursorVisitor Visitor) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000354 const char *UseExternalASTs =
355 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000356 CXIndex Idx;
357 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000358 struct CXUnsavedFile *unsaved_files = 0;
359 int num_unsaved_files = 0;
360 int result;
361
Daniel Dunbarada487d2009-12-01 02:03:10 +0000362 Idx = clang_createIndex(/* excludeDeclsFromPCH */
363 !strcmp(filter, "local") ? 1 : 0,
364 /* displayDiagnostics */ 1);
365
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000366 if (UseExternalASTs && strlen(UseExternalASTs))
367 clang_setUseExternalASTGeneration(Idx, 1);
368
Douglas Gregor4db64a42010-01-23 00:14:00 +0000369 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files))
370 return -1;
371
372 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
373 argc - num_unsaved_files,
374 argv + num_unsaved_files,
375 num_unsaved_files,
376 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000377 if (!TU) {
378 fprintf(stderr, "Unable to load translation unit!\n");
379 return 1;
380 }
381
Douglas Gregor4db64a42010-01-23 00:14:00 +0000382 result = perform_test_load(Idx, TU, filter, NULL, Visitor);
383 free_remapped_files(unsaved_files, num_unsaved_files);
384 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000385}
386
Ted Kremenek0d435192009-11-17 18:13:31 +0000387/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000388/* Logic for testing clang_getCursor(). */
389/******************************************************************************/
390
391static void print_cursor_file_scan(CXCursor cursor,
392 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000393 unsigned end_line, unsigned end_col,
394 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000395 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000396 if (prefix)
397 printf("-%s", prefix);
398 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
399 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000400 PrintCursor(cursor);
401 printf("\n");
402}
403
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000404static int perform_file_scan(const char *ast_file, const char *source_file,
405 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000406 CXIndex Idx;
407 CXTranslationUnit TU;
408 FILE *fp;
409 unsigned line;
410 CXCursor prevCursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000411 CXFile file;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000412 unsigned printed;
413 unsigned start_line, start_col, last_line, last_col;
414 size_t i;
415
416 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
417 /* displayDiagnostics */ 1))) {
418 fprintf(stderr, "Could not create Index\n");
419 return 1;
420 }
421
422 if (!CreateTranslationUnit(Idx, ast_file, &TU))
423 return 1;
424
425 if ((fp = fopen(source_file, "r")) == NULL) {
426 fprintf(stderr, "Could not open '%s'\n", source_file);
427 return 1;
428 }
429
430 line = 0;
431 prevCursor = clang_getNullCursor();
432 printed = 0;
433 start_line = last_line = 1;
434 start_col = last_col = 1;
435
Douglas Gregorb9790342010-01-22 21:44:22 +0000436 file = clang_getFile(TU, source_file);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000437 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000438 size_t len = 0;
439 int c;
440
441 while ((c = fgetc(fp)) != EOF) {
442 len++;
443 if (c == '\n')
444 break;
445 }
446
Ted Kremenek1c6da172009-11-17 19:37:36 +0000447 ++line;
448
449 for (i = 0; i < len ; ++i) {
450 CXCursor cursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000451 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, i+1));
Ted Kremenek1c6da172009-11-17 19:37:36 +0000452
453 if (!clang_equalCursors(cursor, prevCursor) &&
454 prevCursor.kind != CXCursor_InvalidFile) {
455 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000456 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000457 printed = 1;
458 start_line = line;
459 start_col = (unsigned) i+1;
460 }
461 else {
462 printed = 0;
463 }
464
465 prevCursor = cursor;
466 last_line = line;
467 last_col = (unsigned) i+1;
468 }
469 }
470
471 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
472 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000473 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000474 }
475
476 fclose(fp);
477 return 0;
478}
479
480/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000481/* Logic for testing clang_codeComplete(). */
482/******************************************************************************/
483
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000484/* Parse file:line:column from the input string. Returns 0 on success, non-zero
485 on failure. If successful, the pointer *filename will contain newly-allocated
486 memory (that will be owned by the caller) to store the file name. */
487int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000488 unsigned *column, unsigned *second_line,
489 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000490 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000491 const char *last_colon = strrchr(input, ':');
492 unsigned values[4], i;
493 unsigned num_values = (second_line && second_column)? 4 : 2;
494
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000495 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000496 if (!last_colon || last_colon == input) {
497 if (num_values == 4)
498 fprintf(stderr, "could not parse filename:line:column:line:column in "
499 "'%s'\n", input);
500 else
501 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000502 return 1;
503 }
504
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000505 for (i = 0; i != num_values; ++i) {
506 const char *prev_colon;
507
508 /* Parse the next line or column. */
509 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
510 if (*endptr != 0 && *endptr != ':') {
511 fprintf(stderr, "could not parse %s in '%s'\n",
512 (i % 2 ? "column" : "line"), input);
513 return 1;
514 }
515
516 if (i + 1 == num_values)
517 break;
518
519 /* Find the previous colon. */
520 prev_colon = last_colon - 1;
521 while (prev_colon != input && *prev_colon != ':')
522 --prev_colon;
523 if (prev_colon == input) {
524 fprintf(stderr, "could not parse %s in '%s'\n",
525 (i % 2 == 0? "column" : "line"), input);
526 return 1;
527 }
528
529 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000530 }
531
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000532 *line = values[0];
533 *column = values[1];
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000534
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000535 if (second_line && second_column) {
536 *second_line = values[2];
537 *second_column = values[3];
538 }
539
Douglas Gregor88d23952009-11-09 18:19:57 +0000540 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000541 *filename = (char*)malloc(last_colon - input + 1);
542 memcpy(*filename, input, last_colon - input);
543 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000544 return 0;
545}
546
547const char *
548clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
549 switch (Kind) {
550 case CXCompletionChunk_Optional: return "Optional";
551 case CXCompletionChunk_TypedText: return "TypedText";
552 case CXCompletionChunk_Text: return "Text";
553 case CXCompletionChunk_Placeholder: return "Placeholder";
554 case CXCompletionChunk_Informative: return "Informative";
555 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
556 case CXCompletionChunk_LeftParen: return "LeftParen";
557 case CXCompletionChunk_RightParen: return "RightParen";
558 case CXCompletionChunk_LeftBracket: return "LeftBracket";
559 case CXCompletionChunk_RightBracket: return "RightBracket";
560 case CXCompletionChunk_LeftBrace: return "LeftBrace";
561 case CXCompletionChunk_RightBrace: return "RightBrace";
562 case CXCompletionChunk_LeftAngle: return "LeftAngle";
563 case CXCompletionChunk_RightAngle: return "RightAngle";
564 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000565 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000566 case CXCompletionChunk_Colon: return "Colon";
567 case CXCompletionChunk_SemiColon: return "SemiColon";
568 case CXCompletionChunk_Equal: return "Equal";
569 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
570 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000571 }
572
573 return "Unknown";
574}
575
Douglas Gregor3ac73852009-11-09 16:04:45 +0000576void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000577 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000578
579 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000580 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000581 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000582 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000583 = clang_getCompletionChunkKind(completion_string, I);
584
585 if (Kind == CXCompletionChunk_Optional) {
586 fprintf(file, "{Optional ");
587 print_completion_string(
588 clang_getCompletionChunkCompletionString(completion_string, I),
589 file);
590 fprintf(file, "}");
591 continue;
592 }
593
Douglas Gregord5a20892009-11-09 17:05:28 +0000594 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000595 fprintf(file, "{%s %s}",
596 clang_getCompletionChunkKindSpelling(Kind),
597 text? text : "");
598 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000599}
600
601void print_completion_result(CXCompletionResult *completion_result,
602 CXClientData client_data) {
603 FILE *file = (FILE *)client_data;
604 fprintf(file, "%s:",
605 clang_getCursorKindSpelling(completion_result->CursorKind));
606 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000607 fprintf(file, "\n");
608}
609
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000610int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000611 const char *input = argv[1];
612 char *filename = 0;
613 unsigned line;
614 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000615 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000616 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000617 struct CXUnsavedFile *unsaved_files = 0;
618 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000619 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000620
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000621 input += strlen("-code-completion-at=");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000622 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
623 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000624 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000625
Douglas Gregor735df882009-12-02 09:21:34 +0000626 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
627 return -1;
628
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000629 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000630 results = clang_codeComplete(CIdx,
631 argv[argc - 1], argc - num_unsaved_files - 3,
632 argv + num_unsaved_files + 2,
633 num_unsaved_files, unsaved_files,
634 filename, line, column);
635 if (results) {
636 unsigned i, n = results->NumResults;
637 for (i = 0; i != n; ++i)
638 print_completion_result(results->Results + i, stdout);
639 clang_disposeCodeCompleteResults(results);
640 }
641
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000642 clang_disposeIndex(CIdx);
643 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000644
Douglas Gregor735df882009-12-02 09:21:34 +0000645 free_remapped_files(unsaved_files, num_unsaved_files);
646
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000647 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000648}
649
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000650typedef struct {
651 char *filename;
652 unsigned line;
653 unsigned column;
654} CursorSourceLocation;
655
656int inspect_cursor_at(int argc, const char **argv) {
657 CXIndex CIdx;
658 int errorCode;
659 struct CXUnsavedFile *unsaved_files = 0;
660 int num_unsaved_files = 0;
661 CXTranslationUnit TU;
662 CXCursor Cursor;
663 CursorSourceLocation *Locations = 0;
664 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000665
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000666 /* Count the number of locations. */
667 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
668 ++NumLocations;
669
670 /* Parse the locations. */
671 assert(NumLocations > 0 && "Unable to count locations?");
672 Locations = (CursorSourceLocation *)malloc(
673 NumLocations * sizeof(CursorSourceLocation));
674 for (Loc = 0; Loc < NumLocations; ++Loc) {
675 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
676 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
677 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000678 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000679 return errorCode;
680 }
681
682 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
683 &num_unsaved_files))
684 return -1;
685
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000686 CIdx = clang_createIndex(0, 1);
687 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
688 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000689 argv + num_unsaved_files + 1 + NumLocations,
690 num_unsaved_files,
691 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000692 if (!TU) {
693 fprintf(stderr, "unable to parse input\n");
694 return -1;
695 }
696
697 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000698 CXFile file = clang_getFile(TU, Locations[Loc].filename);
699 if (!file)
700 continue;
701
702 Cursor = clang_getCursor(TU,
703 clang_getLocation(TU, file, Locations[Loc].line,
704 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000705 PrintCursor(Cursor);
706 printf("\n");
707 free(Locations[Loc].filename);
708 }
709
710 clang_disposeTranslationUnit(TU);
711 clang_disposeIndex(CIdx);
712 free(Locations);
713 free_remapped_files(unsaved_files, num_unsaved_files);
714 return 0;
715}
716
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000717int perform_token_annotation(int argc, const char **argv) {
718 const char *input = argv[1];
719 char *filename = 0;
720 unsigned line, second_line;
721 unsigned column, second_column;
722 CXIndex CIdx;
723 CXTranslationUnit TU = 0;
724 int errorCode;
725 struct CXUnsavedFile *unsaved_files = 0;
726 int num_unsaved_files = 0;
727 CXToken *tokens;
728 unsigned num_tokens;
729 CXSourceRange range;
730 CXSourceLocation startLoc, endLoc;
731 CXFile file = 0;
732 CXCursor *cursors = 0;
733 unsigned i;
734
735 input += strlen("-test-annotate-tokens=");
736 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
737 &second_line, &second_column)))
738 return errorCode;
739
740 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
741 return -1;
742
743 CIdx = clang_createIndex(0, 0);
744 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
745 argc - num_unsaved_files - 3,
746 argv + num_unsaved_files + 2,
747 num_unsaved_files,
748 unsaved_files);
749 if (!TU) {
750 fprintf(stderr, "unable to parse input\n");
751 clang_disposeIndex(CIdx);
752 free(filename);
753 free_remapped_files(unsaved_files, num_unsaved_files);
754 return -1;
755 }
756 errorCode = 0;
757
758 file = clang_getFile(TU, filename);
759 if (!file) {
760 fprintf(stderr, "file %s is not in this translation unit\n", filename);
761 errorCode = -1;
762 goto teardown;
763 }
764
765 startLoc = clang_getLocation(TU, file, line, column);
766 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
767 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
768 column);
769 errorCode = -1;
770 goto teardown;
771 }
772
773 endLoc = clang_getLocation(TU, file, second_line, second_column);
774 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
775 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
776 second_line, second_column);
777 errorCode = -1;
778 goto teardown;
779 }
780
781 range = clang_getRange(startLoc, endLoc);
782 clang_tokenize(TU, range, &tokens, &num_tokens);
783 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
784 clang_annotateTokens(TU, tokens, num_tokens, cursors);
785 for (i = 0; i != num_tokens; ++i) {
786 const char *kind = "<unknown>";
787 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
788 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
789 unsigned start_line, start_column, end_line, end_column;
790
791 switch (clang_getTokenKind(tokens[i])) {
792 case CXToken_Punctuation: kind = "Punctuation"; break;
793 case CXToken_Keyword: kind = "Keyword"; break;
794 case CXToken_Identifier: kind = "Identifier"; break;
795 case CXToken_Literal: kind = "Literal"; break;
796 case CXToken_Comment: kind = "Comment"; break;
797 }
798 clang_getInstantiationLocation(clang_getRangeStart(extent),
799 0, &start_line, &start_column);
800 clang_getInstantiationLocation(clang_getRangeEnd(extent),
801 0, &end_line, &end_column);
802 printf("%s: \"%s\" [%d:%d - %d:%d]\n", kind, clang_getCString(spelling),
803 start_line, start_column, end_line, end_column);
804 }
805 free(cursors);
806
807 teardown:
808 clang_disposeTranslationUnit(TU);
809 clang_disposeIndex(CIdx);
810 free(filename);
811 free_remapped_files(unsaved_files, num_unsaved_files);
812 return errorCode;
813}
814
Ted Kremenek0d435192009-11-17 18:13:31 +0000815/******************************************************************************/
816/* Command line processing. */
817/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000818
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000819static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000820 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000821 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000822 if (strcmp(s, "-usrs") == 0)
823 return USRVisitor;
824 return NULL;
825}
826
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000827static void print_usage(void) {
828 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000829 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000830 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000831 " c-index-test -test-file-scan <AST file> <source file> "
832 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000833 " c-index-test -test-load-tu <AST file> <symbol filter> "
834 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000835 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
836 "[FileCheck prefix]\n"
837 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000838 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000839 fprintf(stderr,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000840 " c-index-test -test-annotate-tokens=<range> {<args>}* \n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000841 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000842 " all - load all symbols, including those from PCH\n"
843 " local - load all symbols except those in PCH\n"
844 " category - only load ObjC categories (non-PCH)\n"
845 " interface - only load ObjC interfaces (non-PCH)\n"
846 " protocol - only load ObjC protocols (non-PCH)\n"
847 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000848 " typedef - only load typdefs (non-PCH)\n"
849 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000850}
851
852int main(int argc, const char **argv) {
853 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
854 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000855 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
856 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000857 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000858 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +0000859 if (I)
860 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I);
861 }
862 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000863 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +0000864 if (I)
865 return perform_test_load_source(argc - 3, argv + 3, argv[2], I);
866 }
867 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000868 return perform_file_scan(argv[2], argv[3],
869 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000870 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
871 return perform_token_annotation(argc, argv);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000872 print_usage();
873 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000874}