blob: b33b0f4aa775f729232be492b4df67d93ec8962c [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
Douglas Gregor5352ac02010-01-28 00:27:43 +000031static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
32 CXClientData ClientData);
33
Ted Kremenek1c6da172009-11-17 19:37:36 +000034static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
35 CXTranslationUnit *TU) {
36
Douglas Gregor5352ac02010-01-28 00:27:43 +000037 *TU = clang_createTranslationUnit(Idx, file, PrintDiagnosticCallback, 0);
Ted Kremenek1c6da172009-11-17 19:37:36 +000038 if (!TU) {
39 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
40 return 0;
41 }
42 return 1;
43}
44
Douglas Gregor4db64a42010-01-23 00:14:00 +000045void free_remapped_files(struct CXUnsavedFile *unsaved_files,
46 int num_unsaved_files) {
47 int i;
48 for (i = 0; i != num_unsaved_files; ++i) {
49 free((char *)unsaved_files[i].Filename);
50 free((char *)unsaved_files[i].Contents);
51 }
52}
53
54int parse_remapped_files(int argc, const char **argv, int start_arg,
55 struct CXUnsavedFile **unsaved_files,
56 int *num_unsaved_files) {
57 int i;
58 int arg;
59 int prefix_len = strlen("-remap-file=");
60 *unsaved_files = 0;
61 *num_unsaved_files = 0;
62
63 /* Count the number of remapped files. */
64 for (arg = start_arg; arg < argc; ++arg) {
65 if (strncmp(argv[arg], "-remap-file=", prefix_len))
66 break;
67
68 ++*num_unsaved_files;
69 }
70
71 if (*num_unsaved_files == 0)
72 return 0;
73
74 *unsaved_files
75 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
76 *num_unsaved_files);
77 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
78 struct CXUnsavedFile *unsaved = *unsaved_files + i;
79 const char *arg_string = argv[arg] + prefix_len;
80 int filename_len;
81 char *filename;
82 char *contents;
83 FILE *to_file;
84 const char *semi = strchr(arg_string, ';');
85 if (!semi) {
86 fprintf(stderr,
87 "error: -remap-file=from;to argument is missing semicolon\n");
88 free_remapped_files(*unsaved_files, i);
89 *unsaved_files = 0;
90 *num_unsaved_files = 0;
91 return -1;
92 }
93
94 /* Open the file that we're remapping to. */
95 to_file = fopen(semi + 1, "r");
96 if (!to_file) {
97 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
98 semi + 1);
99 free_remapped_files(*unsaved_files, i);
100 *unsaved_files = 0;
101 *num_unsaved_files = 0;
102 return -1;
103 }
104
105 /* Determine the length of the file we're remapping to. */
106 fseek(to_file, 0, SEEK_END);
107 unsaved->Length = ftell(to_file);
108 fseek(to_file, 0, SEEK_SET);
109
110 /* Read the contents of the file we're remapping to. */
111 contents = (char *)malloc(unsaved->Length + 1);
112 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
113 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
114 (feof(to_file) ? "EOF" : "error"), semi + 1);
115 fclose(to_file);
116 free_remapped_files(*unsaved_files, i);
117 *unsaved_files = 0;
118 *num_unsaved_files = 0;
119 return -1;
120 }
121 contents[unsaved->Length] = 0;
122 unsaved->Contents = contents;
123
124 /* Close the file. */
125 fclose(to_file);
126
127 /* Copy the file name that we're remapping from. */
128 filename_len = semi - arg_string;
129 filename = (char *)malloc(filename_len + 1);
130 memcpy(filename, arg_string, filename_len);
131 filename[filename_len] = 0;
132 unsaved->Filename = filename;
133 }
134
135 return 0;
136}
137
Ted Kremenek0d435192009-11-17 18:13:31 +0000138/******************************************************************************/
139/* Pretty-printing. */
140/******************************************************************************/
141
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000142static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000143 if (clang_isInvalid(Cursor.kind))
Ted Kremenek1c6da172009-11-17 19:37:36 +0000144 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +0000145 else {
Steve Naroffef0cef62009-11-09 17:45:52 +0000146 CXString string;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000147 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000148 unsigned line, column;
Steve Naroffef0cef62009-11-09 17:45:52 +0000149 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +0000150 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +0000151 clang_getCString(string));
152 clang_disposeString(string);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000153
154 Referenced = clang_getCursorReferenced(Cursor);
155 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
156 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000157 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000158 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000159 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000160
161 if (clang_isCursorDefinition(Cursor))
162 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000163 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000164}
Steve Naroff89922f82009-08-31 00:59:03 +0000165
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000166static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000167 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
168 const char *source;
169 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000170 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000171 source = clang_getFileName(file);
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000172 if (!source)
173 return "<invalid loc>";
174 return basename(source);
175}
176
Ted Kremenek0d435192009-11-17 18:13:31 +0000177/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000178/* Callbacks. */
179/******************************************************************************/
180
181typedef void (*PostVisitTU)(CXTranslationUnit);
182
Douglas Gregor5352ac02010-01-28 00:27:43 +0000183static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
184 CXClientData ClientData) {
185 FILE *out = (FILE *)ClientData;
186 CXFile file;
187 unsigned line, column;
188 CXString text;
189 enum CXDiagnosticSeverity severity = clang_getDiagnosticSeverity(Diagnostic);
190
191 /* Ignore diagnostics that should be ignored. */
192 if (severity == CXDiagnostic_Ignored)
193 return;
194
195 /* Print file:line:column. */
196 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
197 &file, &line, &column, 0);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000198 if (file) {
Douglas Gregora3890ba2010-02-08 23:11:56 +0000199 unsigned i, n;
Douglas Gregor51c6d382010-01-29 00:41:11 +0000200 unsigned printed_any_ranges = 0;
201
202 fprintf(out, "%s:%d:%d:", clang_getFileName(file), line, column);
203
Douglas Gregora3890ba2010-02-08 23:11:56 +0000204 n = clang_getDiagnosticNumRanges(Diagnostic);
205 for (i = 0; i != n; ++i) {
Douglas Gregor51c6d382010-01-29 00:41:11 +0000206 CXFile start_file, end_file;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000207 CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i);
208
Douglas Gregor51c6d382010-01-29 00:41:11 +0000209 unsigned start_line, start_column, end_line, end_column;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000210 clang_getInstantiationLocation(clang_getRangeStart(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000211 &start_file, &start_line, &start_column,0);
Douglas Gregora3890ba2010-02-08 23:11:56 +0000212 clang_getInstantiationLocation(clang_getRangeEnd(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000213 &end_file, &end_line, &end_column, 0);
214
215 if (start_file != end_file || start_file != file)
216 continue;
217
218 fprintf(out, "{%d:%d-%d:%d}", start_line, start_column, end_line,
219 end_column+1);
220 printed_any_ranges = 1;
221 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000222 if (printed_any_ranges)
223 fprintf(out, ":");
224
225 fprintf(out, " ");
226 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000227
228 /* Print warning/error/etc. */
229 switch (severity) {
230 case CXDiagnostic_Ignored: assert(0 && "impossible"); break;
231 case CXDiagnostic_Note: fprintf(out, "note: "); break;
232 case CXDiagnostic_Warning: fprintf(out, "warning: "); break;
233 case CXDiagnostic_Error: fprintf(out, "error: "); break;
234 case CXDiagnostic_Fatal: fprintf(out, "fatal error: "); break;
235 }
236
237 text = clang_getDiagnosticSpelling(Diagnostic);
238 if (clang_getCString(text))
239 fprintf(out, "%s\n", clang_getCString(text));
240 else
241 fprintf(out, "<no diagnostic text>\n");
242 clang_disposeString(text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000243
244 if (file) {
245 unsigned i, num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
246 for (i = 0; i != num_fixits; ++i) {
247 switch (clang_getDiagnosticFixItKind(Diagnostic, i)) {
248 case CXFixIt_Insertion: {
249 CXSourceLocation insertion_loc;
250 CXFile insertion_file;
251 unsigned insertion_line, insertion_column;
252 text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc);
253 clang_getInstantiationLocation(insertion_loc, &insertion_file,
254 &insertion_line, &insertion_column, 0);
255 if (insertion_file == file)
256 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
257 clang_getCString(text), insertion_line, insertion_column);
258 clang_disposeString(text);
259 break;
260 }
261
262 case CXFixIt_Removal: {
263 CXFile start_file, end_file;
264 unsigned start_line, start_column, end_line, end_column;
265 CXSourceRange remove_range
266 = clang_getDiagnosticFixItRemoval(Diagnostic, i);
267 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
268 &start_file, &start_line, &start_column,
269 0);
270 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
271 &end_file, &end_line, &end_column, 0);
272 if (start_file == file && end_file == file)
273 fprintf(out, "FIX-IT: Remove %d:%d-%d:%d\n",
274 start_line, start_column, end_line, end_column+1);
275 break;
276 }
277
278 case CXFixIt_Replacement: {
279 CXFile start_file, end_file;
280 unsigned start_line, start_column, end_line, end_column;
281 CXSourceRange remove_range;
282 text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range);
283 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
284 &start_file, &start_line, &start_column,
285 0);
286 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
287 &end_file, &end_line, &end_column, 0);
288 if (start_file == end_file)
289 fprintf(out, "FIX-IT: Replace %d:%d-%d:%d with \"%s\"\n",
290 start_line, start_column, end_line, end_column+1,
291 clang_getCString(text));
292 clang_disposeString(text);
293 break;
294 }
295 }
296 }
297 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000298}
299
Ted Kremenekce2ae882010-01-26 17:59:48 +0000300/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000301/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000302/******************************************************************************/
303
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000304static const char *FileCheckPrefix = "CHECK";
305
Douglas Gregora7bde202010-01-19 00:34:46 +0000306static void PrintCursorExtent(CXCursor C) {
307 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000308 CXFile begin_file, end_file;
309 unsigned begin_line, begin_column, end_line, end_column;
310
311 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000312 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000313 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000314 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000315 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000316 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000317
318 printf(" [Extent=%d:%d:%d:%d]", begin_line, begin_column,
319 end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000320}
321
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000322/* Data used by all of the visitors. */
323typedef struct {
324 CXTranslationUnit TU;
325 enum CXCursorKind *Filter;
326} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000327
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000328
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000329enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
330 CXCursor Parent,
331 CXClientData ClientData) {
332 VisitorData *Data = (VisitorData *)ClientData;
333 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000334 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000335 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000336 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000337 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000338 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000339 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000340 PrintCursorExtent(Cursor);
Ted Kremenek70ee5422010-01-16 01:44:12 +0000341 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000342 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000343 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000344
345 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000346}
Steve Naroff50398192009-08-28 15:28:48 +0000347
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000348static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
349 CXCursor Parent,
350 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000351 const char *startBuf, *endBuf;
352 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
353 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000354 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000355
Douglas Gregorb6998662010-01-19 19:34:47 +0000356 if (Cursor.kind != CXCursor_FunctionDecl ||
357 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000358 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000359
360 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
361 &startLine, &startColumn,
362 &endLine, &endColumn);
363 /* Probe the entire body, looking for both decls and refs. */
364 curLine = startLine;
365 curColumn = startColumn;
366
367 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000368 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000369 CXFile file;
Douglas Gregor98258af2010-01-18 22:46:11 +0000370 const char *source = 0;
371
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000372 if (*startBuf == '\n') {
373 startBuf++;
374 curLine++;
375 curColumn = 1;
376 } else if (*startBuf != '\t')
377 curColumn++;
378
Douglas Gregor98258af2010-01-18 22:46:11 +0000379 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000380 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000381 source = clang_getFileName(file);
Douglas Gregor98258af2010-01-18 22:46:11 +0000382 if (source) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000383 CXSourceLocation RefLoc
384 = clang_getLocation(Data->TU, file, curLine, curColumn);
385 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000386 if (Ref.kind == CXCursor_NoDeclFound) {
387 /* Nothing found here; that's fine. */
388 } else if (Ref.kind != CXCursor_FunctionDecl) {
389 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
390 curLine, curColumn);
391 PrintCursor(Ref);
392 printf("\n");
393 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000394 }
395 startBuf++;
396 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000397
398 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000399}
400
Ted Kremenek7d405622010-01-12 23:34:26 +0000401/******************************************************************************/
402/* USR testing. */
403/******************************************************************************/
404
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000405enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
406 CXClientData ClientData) {
407 VisitorData *Data = (VisitorData *)ClientData;
408 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000409 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000410 if (!USR.Spelling) {
411 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000412 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000413 }
414 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregora7bde202010-01-19 00:34:46 +0000415 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000416 printf("\n");
417 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000418
419 return CXChildVisit_Recurse;
420 }
421
422 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000423}
424
425/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000426/* Inclusion stack testing. */
427/******************************************************************************/
428
429void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
430 unsigned includeStackLen, CXClientData data) {
431
432 unsigned i;
433 printf("file: %s\nincluded by:\n", clang_getFileName(includedFile));
434 for (i = 0; i < includeStackLen; ++i) {
435 CXFile includingFile;
436 unsigned line, column;
437 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
438 &column, 0);
439 printf(" %s:%d:%d\n", clang_getFileName(includingFile), line, column);
440 }
441 printf("\n");
442}
443
444void PrintInclusionStack(CXTranslationUnit TU) {
445 clang_getInclusions(TU, InclusionVisitor, NULL);
446}
447
448/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000449/* Loading ASTs/source. */
450/******************************************************************************/
451
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000452static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000453 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000454 CXCursorVisitor Visitor,
455 PostVisitTU PV) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000456
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000457 if (prefix)
458 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000459
460 if (Visitor) {
461 enum CXCursorKind K = CXCursor_NotImplemented;
462 enum CXCursorKind *ck = &K;
463 VisitorData Data;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000464
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000465 /* Perform some simple filtering. */
466 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000467 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000468 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
469 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
470 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
471 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
472 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
473 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
474 else {
475 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
476 return 1;
477 }
478
479 Data.TU = TU;
480 Data.Filter = ck;
481 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000482 }
Ted Kremenekce2ae882010-01-26 17:59:48 +0000483
484 if (PV)
485 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000486
Ted Kremenek0d435192009-11-17 18:13:31 +0000487 clang_disposeTranslationUnit(TU);
488 return 0;
489}
490
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000491int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000492 const char *prefix, CXCursorVisitor Visitor,
493 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000494 CXIndex Idx;
495 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000496 int result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000497 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000498 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000499
Ted Kremenek020a0952010-02-11 07:41:25 +0000500 if (!CreateTranslationUnit(Idx, file, &TU)) {
501 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000502 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000503 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000504
Ted Kremenek020a0952010-02-11 07:41:25 +0000505 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
506 clang_disposeIndex(Idx);
507 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000508}
509
Ted Kremenekce2ae882010-01-26 17:59:48 +0000510int perform_test_load_source(int argc, const char **argv,
511 const char *filter, CXCursorVisitor Visitor,
512 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000513 const char *UseExternalASTs =
514 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000515 CXIndex Idx;
516 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000517 struct CXUnsavedFile *unsaved_files = 0;
518 int num_unsaved_files = 0;
519 int result;
520
Daniel Dunbarada487d2009-12-01 02:03:10 +0000521 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000522 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000523
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000524 if (UseExternalASTs && strlen(UseExternalASTs))
525 clang_setUseExternalASTGeneration(Idx, 1);
526
Ted Kremenek020a0952010-02-11 07:41:25 +0000527 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
528 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000529 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000530 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000531
532 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
533 argc - num_unsaved_files,
534 argv + num_unsaved_files,
535 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000536 unsaved_files,
537 PrintDiagnosticCallback,
538 stderr);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000539 if (!TU) {
540 fprintf(stderr, "Unable to load translation unit!\n");
Ted Kremenek020a0952010-02-11 07:41:25 +0000541 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000542 return 1;
543 }
544
Ted Kremenekce2ae882010-01-26 17:59:48 +0000545 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000546 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000547 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000548 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000549}
550
Ted Kremenek0d435192009-11-17 18:13:31 +0000551/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000552/* Logic for testing clang_getCursor(). */
553/******************************************************************************/
554
555static void print_cursor_file_scan(CXCursor cursor,
556 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000557 unsigned end_line, unsigned end_col,
558 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000559 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000560 if (prefix)
561 printf("-%s", prefix);
562 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
563 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000564 PrintCursor(cursor);
565 printf("\n");
566}
567
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000568static int perform_file_scan(const char *ast_file, const char *source_file,
569 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000570 CXIndex Idx;
571 CXTranslationUnit TU;
572 FILE *fp;
573 unsigned line;
574 CXCursor prevCursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000575 CXFile file;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000576 unsigned printed;
577 unsigned start_line, start_col, last_line, last_col;
578 size_t i;
579
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000580 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000581 fprintf(stderr, "Could not create Index\n");
582 return 1;
583 }
584
585 if (!CreateTranslationUnit(Idx, ast_file, &TU))
586 return 1;
587
588 if ((fp = fopen(source_file, "r")) == NULL) {
589 fprintf(stderr, "Could not open '%s'\n", source_file);
590 return 1;
591 }
592
593 line = 0;
594 prevCursor = clang_getNullCursor();
595 printed = 0;
596 start_line = last_line = 1;
597 start_col = last_col = 1;
598
Douglas Gregorb9790342010-01-22 21:44:22 +0000599 file = clang_getFile(TU, source_file);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000600 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000601 size_t len = 0;
602 int c;
603
604 while ((c = fgetc(fp)) != EOF) {
605 len++;
606 if (c == '\n')
607 break;
608 }
609
Ted Kremenek1c6da172009-11-17 19:37:36 +0000610 ++line;
611
612 for (i = 0; i < len ; ++i) {
613 CXCursor cursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000614 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, i+1));
Ted Kremenek1c6da172009-11-17 19:37:36 +0000615
616 if (!clang_equalCursors(cursor, prevCursor) &&
617 prevCursor.kind != CXCursor_InvalidFile) {
618 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000619 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000620 printed = 1;
621 start_line = line;
622 start_col = (unsigned) i+1;
623 }
624 else {
625 printed = 0;
626 }
627
628 prevCursor = cursor;
629 last_line = line;
630 last_col = (unsigned) i+1;
631 }
632 }
633
634 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
635 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000636 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000637 }
638
639 fclose(fp);
640 return 0;
641}
642
643/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000644/* Logic for testing clang_codeComplete(). */
645/******************************************************************************/
646
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000647/* Parse file:line:column from the input string. Returns 0 on success, non-zero
648 on failure. If successful, the pointer *filename will contain newly-allocated
649 memory (that will be owned by the caller) to store the file name. */
650int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000651 unsigned *column, unsigned *second_line,
652 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000653 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000654 const char *last_colon = strrchr(input, ':');
655 unsigned values[4], i;
656 unsigned num_values = (second_line && second_column)? 4 : 2;
657
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000658 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000659 if (!last_colon || last_colon == input) {
660 if (num_values == 4)
661 fprintf(stderr, "could not parse filename:line:column:line:column in "
662 "'%s'\n", input);
663 else
664 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000665 return 1;
666 }
667
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000668 for (i = 0; i != num_values; ++i) {
669 const char *prev_colon;
670
671 /* Parse the next line or column. */
672 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
673 if (*endptr != 0 && *endptr != ':') {
674 fprintf(stderr, "could not parse %s in '%s'\n",
675 (i % 2 ? "column" : "line"), input);
676 return 1;
677 }
678
679 if (i + 1 == num_values)
680 break;
681
682 /* Find the previous colon. */
683 prev_colon = last_colon - 1;
684 while (prev_colon != input && *prev_colon != ':')
685 --prev_colon;
686 if (prev_colon == input) {
687 fprintf(stderr, "could not parse %s in '%s'\n",
688 (i % 2 == 0? "column" : "line"), input);
689 return 1;
690 }
691
692 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000693 }
694
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000695 *line = values[0];
696 *column = values[1];
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000697
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000698 if (second_line && second_column) {
699 *second_line = values[2];
700 *second_column = values[3];
701 }
702
Douglas Gregor88d23952009-11-09 18:19:57 +0000703 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000704 *filename = (char*)malloc(last_colon - input + 1);
705 memcpy(*filename, input, last_colon - input);
706 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000707 return 0;
708}
709
710const char *
711clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
712 switch (Kind) {
713 case CXCompletionChunk_Optional: return "Optional";
714 case CXCompletionChunk_TypedText: return "TypedText";
715 case CXCompletionChunk_Text: return "Text";
716 case CXCompletionChunk_Placeholder: return "Placeholder";
717 case CXCompletionChunk_Informative: return "Informative";
718 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
719 case CXCompletionChunk_LeftParen: return "LeftParen";
720 case CXCompletionChunk_RightParen: return "RightParen";
721 case CXCompletionChunk_LeftBracket: return "LeftBracket";
722 case CXCompletionChunk_RightBracket: return "RightBracket";
723 case CXCompletionChunk_LeftBrace: return "LeftBrace";
724 case CXCompletionChunk_RightBrace: return "RightBrace";
725 case CXCompletionChunk_LeftAngle: return "LeftAngle";
726 case CXCompletionChunk_RightAngle: return "RightAngle";
727 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000728 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000729 case CXCompletionChunk_Colon: return "Colon";
730 case CXCompletionChunk_SemiColon: return "SemiColon";
731 case CXCompletionChunk_Equal: return "Equal";
732 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
733 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000734 }
735
736 return "Unknown";
737}
738
Douglas Gregor3ac73852009-11-09 16:04:45 +0000739void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000740 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000741
742 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000743 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000744 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000745 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000746 = clang_getCompletionChunkKind(completion_string, I);
747
748 if (Kind == CXCompletionChunk_Optional) {
749 fprintf(file, "{Optional ");
750 print_completion_string(
751 clang_getCompletionChunkCompletionString(completion_string, I),
752 file);
753 fprintf(file, "}");
754 continue;
755 }
756
Douglas Gregord5a20892009-11-09 17:05:28 +0000757 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000758 fprintf(file, "{%s %s}",
759 clang_getCompletionChunkKindSpelling(Kind),
760 text? text : "");
761 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000762}
763
764void print_completion_result(CXCompletionResult *completion_result,
765 CXClientData client_data) {
766 FILE *file = (FILE *)client_data;
767 fprintf(file, "%s:",
768 clang_getCursorKindSpelling(completion_result->CursorKind));
769 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000770 fprintf(file, "\n");
771}
772
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000773int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000774 const char *input = argv[1];
775 char *filename = 0;
776 unsigned line;
777 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000778 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000779 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000780 struct CXUnsavedFile *unsaved_files = 0;
781 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000782 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000783
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000784 input += strlen("-code-completion-at=");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000785 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
786 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000787 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000788
Douglas Gregor735df882009-12-02 09:21:34 +0000789 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
790 return -1;
791
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000792 CIdx = clang_createIndex(0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000793 results = clang_codeComplete(CIdx,
794 argv[argc - 1], argc - num_unsaved_files - 3,
795 argv + num_unsaved_files + 2,
796 num_unsaved_files, unsaved_files,
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000797 filename, line, column,
798 PrintDiagnosticCallback, stderr);
799
Douglas Gregorec6762c2009-12-18 16:20:58 +0000800 if (results) {
801 unsigned i, n = results->NumResults;
802 for (i = 0; i != n; ++i)
803 print_completion_result(results->Results + i, stdout);
804 clang_disposeCodeCompleteResults(results);
805 }
806
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000807 clang_disposeIndex(CIdx);
808 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000809
Douglas Gregor735df882009-12-02 09:21:34 +0000810 free_remapped_files(unsaved_files, num_unsaved_files);
811
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000812 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000813}
814
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000815typedef struct {
816 char *filename;
817 unsigned line;
818 unsigned column;
819} CursorSourceLocation;
820
821int inspect_cursor_at(int argc, const char **argv) {
822 CXIndex CIdx;
823 int errorCode;
824 struct CXUnsavedFile *unsaved_files = 0;
825 int num_unsaved_files = 0;
826 CXTranslationUnit TU;
827 CXCursor Cursor;
828 CursorSourceLocation *Locations = 0;
829 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000830
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000831 /* Count the number of locations. */
832 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
833 ++NumLocations;
834
835 /* Parse the locations. */
836 assert(NumLocations > 0 && "Unable to count locations?");
837 Locations = (CursorSourceLocation *)malloc(
838 NumLocations * sizeof(CursorSourceLocation));
839 for (Loc = 0; Loc < NumLocations; ++Loc) {
840 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
841 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
842 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000843 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000844 return errorCode;
845 }
846
847 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
848 &num_unsaved_files))
849 return -1;
850
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000851 CIdx = clang_createIndex(0);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000852 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
853 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000854 argv + num_unsaved_files + 1 + NumLocations,
855 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000856 unsaved_files,
857 PrintDiagnosticCallback,
858 stderr);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000859 if (!TU) {
860 fprintf(stderr, "unable to parse input\n");
861 return -1;
862 }
863
864 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000865 CXFile file = clang_getFile(TU, Locations[Loc].filename);
866 if (!file)
867 continue;
868
869 Cursor = clang_getCursor(TU,
870 clang_getLocation(TU, file, Locations[Loc].line,
871 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000872 PrintCursor(Cursor);
873 printf("\n");
874 free(Locations[Loc].filename);
875 }
876
877 clang_disposeTranslationUnit(TU);
878 clang_disposeIndex(CIdx);
879 free(Locations);
880 free_remapped_files(unsaved_files, num_unsaved_files);
881 return 0;
882}
883
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000884int perform_token_annotation(int argc, const char **argv) {
885 const char *input = argv[1];
886 char *filename = 0;
887 unsigned line, second_line;
888 unsigned column, second_column;
889 CXIndex CIdx;
890 CXTranslationUnit TU = 0;
891 int errorCode;
892 struct CXUnsavedFile *unsaved_files = 0;
893 int num_unsaved_files = 0;
894 CXToken *tokens;
895 unsigned num_tokens;
896 CXSourceRange range;
897 CXSourceLocation startLoc, endLoc;
898 CXFile file = 0;
899 CXCursor *cursors = 0;
900 unsigned i;
901
902 input += strlen("-test-annotate-tokens=");
903 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
904 &second_line, &second_column)))
905 return errorCode;
906
907 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
908 return -1;
909
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000910 CIdx = clang_createIndex(0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000911 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
912 argc - num_unsaved_files - 3,
913 argv + num_unsaved_files + 2,
914 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000915 unsaved_files,
916 PrintDiagnosticCallback,
917 stderr);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000918 if (!TU) {
919 fprintf(stderr, "unable to parse input\n");
920 clang_disposeIndex(CIdx);
921 free(filename);
922 free_remapped_files(unsaved_files, num_unsaved_files);
923 return -1;
924 }
925 errorCode = 0;
926
927 file = clang_getFile(TU, filename);
928 if (!file) {
929 fprintf(stderr, "file %s is not in this translation unit\n", filename);
930 errorCode = -1;
931 goto teardown;
932 }
933
934 startLoc = clang_getLocation(TU, file, line, column);
935 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
936 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
937 column);
938 errorCode = -1;
939 goto teardown;
940 }
941
942 endLoc = clang_getLocation(TU, file, second_line, second_column);
943 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
944 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
945 second_line, second_column);
946 errorCode = -1;
947 goto teardown;
948 }
949
950 range = clang_getRange(startLoc, endLoc);
951 clang_tokenize(TU, range, &tokens, &num_tokens);
952 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
953 clang_annotateTokens(TU, tokens, num_tokens, cursors);
954 for (i = 0; i != num_tokens; ++i) {
955 const char *kind = "<unknown>";
956 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
957 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
958 unsigned start_line, start_column, end_line, end_column;
959
960 switch (clang_getTokenKind(tokens[i])) {
961 case CXToken_Punctuation: kind = "Punctuation"; break;
962 case CXToken_Keyword: kind = "Keyword"; break;
963 case CXToken_Identifier: kind = "Identifier"; break;
964 case CXToken_Literal: kind = "Literal"; break;
965 case CXToken_Comment: kind = "Comment"; break;
966 }
967 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000968 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000969 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000970 0, &end_line, &end_column, 0);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000971 printf("%s: \"%s\" [%d:%d - %d:%d]", kind, clang_getCString(spelling),
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000972 start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000973 if (!clang_isInvalid(cursors[i].kind)) {
974 printf(" ");
975 PrintCursor(cursors[i]);
976 }
977 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000978 }
979 free(cursors);
980
981 teardown:
982 clang_disposeTranslationUnit(TU);
983 clang_disposeIndex(CIdx);
984 free(filename);
985 free_remapped_files(unsaved_files, num_unsaved_files);
986 return errorCode;
987}
988
Ted Kremenek0d435192009-11-17 18:13:31 +0000989/******************************************************************************/
990/* Command line processing. */
991/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000992
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000993static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000994 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000995 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000996 if (strcmp(s, "-usrs") == 0)
997 return USRVisitor;
998 return NULL;
999}
1000
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001001static void print_usage(void) {
1002 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001003 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001004 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001005 " c-index-test -test-file-scan <AST file> <source file> "
1006 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001007 " c-index-test -test-load-tu <AST file> <symbol filter> "
1008 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001009 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1010 "[FileCheck prefix]\n"
1011 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001012 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001013 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +00001014 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1015 " c-index-test -test-inclusion-stack-source {<args>}*\n"
1016 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001017 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001018 " all - load all symbols, including those from PCH\n"
1019 " local - load all symbols except those in PCH\n"
1020 " category - only load ObjC categories (non-PCH)\n"
1021 " interface - only load ObjC interfaces (non-PCH)\n"
1022 " protocol - only load ObjC protocols (non-PCH)\n"
1023 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001024 " typedef - only load typdefs (non-PCH)\n"
1025 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001026}
1027
1028int main(int argc, const char **argv) {
1029 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1030 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001031 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1032 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001033 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001034 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001035 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001036 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1037 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001038 }
1039 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001040 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001041 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001042 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001043 }
1044 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001045 return perform_file_scan(argv[2], argv[3],
1046 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001047 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1048 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001049 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1050 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1051 PrintInclusionStack);
1052 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1053 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1054 PrintInclusionStack);
1055
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001056 print_usage();
1057 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001058}