blob: 39c3446f046b66ba9875079c3d8e7a64509935c3 [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);
198 if (file)
199 fprintf(out, "%s:%d:%d: ", clang_getFileName(file), line, column);
200
201 /* Print warning/error/etc. */
202 switch (severity) {
203 case CXDiagnostic_Ignored: assert(0 && "impossible"); break;
204 case CXDiagnostic_Note: fprintf(out, "note: "); break;
205 case CXDiagnostic_Warning: fprintf(out, "warning: "); break;
206 case CXDiagnostic_Error: fprintf(out, "error: "); break;
207 case CXDiagnostic_Fatal: fprintf(out, "fatal error: "); break;
208 }
209
210 text = clang_getDiagnosticSpelling(Diagnostic);
211 if (clang_getCString(text))
212 fprintf(out, "%s\n", clang_getCString(text));
213 else
214 fprintf(out, "<no diagnostic text>\n");
215 clang_disposeString(text);
216}
217
Ted Kremenekce2ae882010-01-26 17:59:48 +0000218/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000219/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000220/******************************************************************************/
221
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000222static const char *FileCheckPrefix = "CHECK";
223
Douglas Gregora7bde202010-01-19 00:34:46 +0000224static void PrintCursorExtent(CXCursor C) {
225 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000226 CXFile begin_file, end_file;
227 unsigned begin_line, begin_column, end_line, end_column;
228
229 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000230 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000231 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000232 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000233 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000234 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000235
236 printf(" [Extent=%d:%d:%d:%d]", begin_line, begin_column,
237 end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000238}
239
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000240/* Data used by all of the visitors. */
241typedef struct {
242 CXTranslationUnit TU;
243 enum CXCursorKind *Filter;
244} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000245
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000246
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000247enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
248 CXCursor Parent,
249 CXClientData ClientData) {
250 VisitorData *Data = (VisitorData *)ClientData;
251 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000252 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000253 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000254 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000255 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000256 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000257 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000258 PrintCursorExtent(Cursor);
Ted Kremenek70ee5422010-01-16 01:44:12 +0000259 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000260 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000261 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000262
263 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000264}
Steve Naroff50398192009-08-28 15:28:48 +0000265
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000266static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
267 CXCursor Parent,
268 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000269 const char *startBuf, *endBuf;
270 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
271 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000272 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000273
Douglas Gregorb6998662010-01-19 19:34:47 +0000274 if (Cursor.kind != CXCursor_FunctionDecl ||
275 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000276 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000277
278 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
279 &startLine, &startColumn,
280 &endLine, &endColumn);
281 /* Probe the entire body, looking for both decls and refs. */
282 curLine = startLine;
283 curColumn = startColumn;
284
285 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000286 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000287 CXFile file;
Douglas Gregor98258af2010-01-18 22:46:11 +0000288 const char *source = 0;
289
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000290 if (*startBuf == '\n') {
291 startBuf++;
292 curLine++;
293 curColumn = 1;
294 } else if (*startBuf != '\t')
295 curColumn++;
296
Douglas Gregor98258af2010-01-18 22:46:11 +0000297 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000298 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000299 source = clang_getFileName(file);
Douglas Gregor98258af2010-01-18 22:46:11 +0000300 if (source) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000301 CXSourceLocation RefLoc
302 = clang_getLocation(Data->TU, file, curLine, curColumn);
303 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000304 if (Ref.kind == CXCursor_NoDeclFound) {
305 /* Nothing found here; that's fine. */
306 } else if (Ref.kind != CXCursor_FunctionDecl) {
307 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
308 curLine, curColumn);
309 PrintCursor(Ref);
310 printf("\n");
311 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000312 }
313 startBuf++;
314 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000315
316 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000317}
318
Ted Kremenek7d405622010-01-12 23:34:26 +0000319/******************************************************************************/
320/* USR testing. */
321/******************************************************************************/
322
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000323enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
324 CXClientData ClientData) {
325 VisitorData *Data = (VisitorData *)ClientData;
326 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000327 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000328 if (!USR.Spelling) {
329 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000330 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000331 }
332 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregora7bde202010-01-19 00:34:46 +0000333 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000334 printf("\n");
335 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000336
337 return CXChildVisit_Recurse;
338 }
339
340 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000341}
342
343/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000344/* Inclusion stack testing. */
345/******************************************************************************/
346
347void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
348 unsigned includeStackLen, CXClientData data) {
349
350 unsigned i;
351 printf("file: %s\nincluded by:\n", clang_getFileName(includedFile));
352 for (i = 0; i < includeStackLen; ++i) {
353 CXFile includingFile;
354 unsigned line, column;
355 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
356 &column, 0);
357 printf(" %s:%d:%d\n", clang_getFileName(includingFile), line, column);
358 }
359 printf("\n");
360}
361
362void PrintInclusionStack(CXTranslationUnit TU) {
363 clang_getInclusions(TU, InclusionVisitor, NULL);
364}
365
366/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000367/* Loading ASTs/source. */
368/******************************************************************************/
369
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000370static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000371 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000372 CXCursorVisitor Visitor,
373 PostVisitTU PV) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000374
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000375 if (prefix)
376 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000377
378 if (Visitor) {
379 enum CXCursorKind K = CXCursor_NotImplemented;
380 enum CXCursorKind *ck = &K;
381 VisitorData Data;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000382
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000383 /* Perform some simple filtering. */
384 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
385 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
386 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
387 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
388 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
389 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
390 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
391 else {
392 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
393 return 1;
394 }
395
396 Data.TU = TU;
397 Data.Filter = ck;
398 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000399 }
Ted Kremenekce2ae882010-01-26 17:59:48 +0000400
401 if (PV)
402 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000403
Ted Kremenek0d435192009-11-17 18:13:31 +0000404 clang_disposeTranslationUnit(TU);
405 return 0;
406}
407
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000408int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000409 const char *prefix, CXCursorVisitor Visitor,
410 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000411 CXIndex Idx;
412 CXTranslationUnit TU;
413 Idx = clang_createIndex(/* excludeDeclsFromPCH */
414 !strcmp(filter, "local") ? 1 : 0,
415 /* displayDiagnostics */ 1);
416
417 if (!CreateTranslationUnit(Idx, file, &TU))
418 return 1;
419
Ted Kremenekce2ae882010-01-26 17:59:48 +0000420 return perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000421}
422
Ted Kremenekce2ae882010-01-26 17:59:48 +0000423int perform_test_load_source(int argc, const char **argv,
424 const char *filter, CXCursorVisitor Visitor,
425 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000426 const char *UseExternalASTs =
427 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000428 CXIndex Idx;
429 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000430 struct CXUnsavedFile *unsaved_files = 0;
431 int num_unsaved_files = 0;
432 int result;
433
Daniel Dunbarada487d2009-12-01 02:03:10 +0000434 Idx = clang_createIndex(/* excludeDeclsFromPCH */
435 !strcmp(filter, "local") ? 1 : 0,
436 /* displayDiagnostics */ 1);
437
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000438 if (UseExternalASTs && strlen(UseExternalASTs))
439 clang_setUseExternalASTGeneration(Idx, 1);
440
Douglas Gregor4db64a42010-01-23 00:14:00 +0000441 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files))
442 return -1;
443
444 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
445 argc - num_unsaved_files,
446 argv + num_unsaved_files,
447 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000448 unsaved_files,
449 PrintDiagnosticCallback,
450 stderr);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000451 if (!TU) {
452 fprintf(stderr, "Unable to load translation unit!\n");
453 return 1;
454 }
455
Ted Kremenekce2ae882010-01-26 17:59:48 +0000456 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000457 free_remapped_files(unsaved_files, num_unsaved_files);
458 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000459}
460
Ted Kremenek0d435192009-11-17 18:13:31 +0000461/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000462/* Logic for testing clang_getCursor(). */
463/******************************************************************************/
464
465static void print_cursor_file_scan(CXCursor cursor,
466 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000467 unsigned end_line, unsigned end_col,
468 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000469 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000470 if (prefix)
471 printf("-%s", prefix);
472 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
473 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000474 PrintCursor(cursor);
475 printf("\n");
476}
477
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000478static int perform_file_scan(const char *ast_file, const char *source_file,
479 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000480 CXIndex Idx;
481 CXTranslationUnit TU;
482 FILE *fp;
483 unsigned line;
484 CXCursor prevCursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000485 CXFile file;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000486 unsigned printed;
487 unsigned start_line, start_col, last_line, last_col;
488 size_t i;
489
490 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
491 /* displayDiagnostics */ 1))) {
492 fprintf(stderr, "Could not create Index\n");
493 return 1;
494 }
495
496 if (!CreateTranslationUnit(Idx, ast_file, &TU))
497 return 1;
498
499 if ((fp = fopen(source_file, "r")) == NULL) {
500 fprintf(stderr, "Could not open '%s'\n", source_file);
501 return 1;
502 }
503
504 line = 0;
505 prevCursor = clang_getNullCursor();
506 printed = 0;
507 start_line = last_line = 1;
508 start_col = last_col = 1;
509
Douglas Gregorb9790342010-01-22 21:44:22 +0000510 file = clang_getFile(TU, source_file);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000511 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000512 size_t len = 0;
513 int c;
514
515 while ((c = fgetc(fp)) != EOF) {
516 len++;
517 if (c == '\n')
518 break;
519 }
520
Ted Kremenek1c6da172009-11-17 19:37:36 +0000521 ++line;
522
523 for (i = 0; i < len ; ++i) {
524 CXCursor cursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000525 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, i+1));
Ted Kremenek1c6da172009-11-17 19:37:36 +0000526
527 if (!clang_equalCursors(cursor, prevCursor) &&
528 prevCursor.kind != CXCursor_InvalidFile) {
529 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000530 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000531 printed = 1;
532 start_line = line;
533 start_col = (unsigned) i+1;
534 }
535 else {
536 printed = 0;
537 }
538
539 prevCursor = cursor;
540 last_line = line;
541 last_col = (unsigned) i+1;
542 }
543 }
544
545 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
546 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000547 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000548 }
549
550 fclose(fp);
551 return 0;
552}
553
554/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000555/* Logic for testing clang_codeComplete(). */
556/******************************************************************************/
557
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000558/* Parse file:line:column from the input string. Returns 0 on success, non-zero
559 on failure. If successful, the pointer *filename will contain newly-allocated
560 memory (that will be owned by the caller) to store the file name. */
561int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000562 unsigned *column, unsigned *second_line,
563 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000564 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000565 const char *last_colon = strrchr(input, ':');
566 unsigned values[4], i;
567 unsigned num_values = (second_line && second_column)? 4 : 2;
568
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000569 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000570 if (!last_colon || last_colon == input) {
571 if (num_values == 4)
572 fprintf(stderr, "could not parse filename:line:column:line:column in "
573 "'%s'\n", input);
574 else
575 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000576 return 1;
577 }
578
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000579 for (i = 0; i != num_values; ++i) {
580 const char *prev_colon;
581
582 /* Parse the next line or column. */
583 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
584 if (*endptr != 0 && *endptr != ':') {
585 fprintf(stderr, "could not parse %s in '%s'\n",
586 (i % 2 ? "column" : "line"), input);
587 return 1;
588 }
589
590 if (i + 1 == num_values)
591 break;
592
593 /* Find the previous colon. */
594 prev_colon = last_colon - 1;
595 while (prev_colon != input && *prev_colon != ':')
596 --prev_colon;
597 if (prev_colon == input) {
598 fprintf(stderr, "could not parse %s in '%s'\n",
599 (i % 2 == 0? "column" : "line"), input);
600 return 1;
601 }
602
603 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000604 }
605
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000606 *line = values[0];
607 *column = values[1];
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000608
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000609 if (second_line && second_column) {
610 *second_line = values[2];
611 *second_column = values[3];
612 }
613
Douglas Gregor88d23952009-11-09 18:19:57 +0000614 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000615 *filename = (char*)malloc(last_colon - input + 1);
616 memcpy(*filename, input, last_colon - input);
617 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000618 return 0;
619}
620
621const char *
622clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
623 switch (Kind) {
624 case CXCompletionChunk_Optional: return "Optional";
625 case CXCompletionChunk_TypedText: return "TypedText";
626 case CXCompletionChunk_Text: return "Text";
627 case CXCompletionChunk_Placeholder: return "Placeholder";
628 case CXCompletionChunk_Informative: return "Informative";
629 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
630 case CXCompletionChunk_LeftParen: return "LeftParen";
631 case CXCompletionChunk_RightParen: return "RightParen";
632 case CXCompletionChunk_LeftBracket: return "LeftBracket";
633 case CXCompletionChunk_RightBracket: return "RightBracket";
634 case CXCompletionChunk_LeftBrace: return "LeftBrace";
635 case CXCompletionChunk_RightBrace: return "RightBrace";
636 case CXCompletionChunk_LeftAngle: return "LeftAngle";
637 case CXCompletionChunk_RightAngle: return "RightAngle";
638 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000639 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000640 case CXCompletionChunk_Colon: return "Colon";
641 case CXCompletionChunk_SemiColon: return "SemiColon";
642 case CXCompletionChunk_Equal: return "Equal";
643 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
644 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000645 }
646
647 return "Unknown";
648}
649
Douglas Gregor3ac73852009-11-09 16:04:45 +0000650void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000651 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000652
653 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000654 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000655 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000656 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000657 = clang_getCompletionChunkKind(completion_string, I);
658
659 if (Kind == CXCompletionChunk_Optional) {
660 fprintf(file, "{Optional ");
661 print_completion_string(
662 clang_getCompletionChunkCompletionString(completion_string, I),
663 file);
664 fprintf(file, "}");
665 continue;
666 }
667
Douglas Gregord5a20892009-11-09 17:05:28 +0000668 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000669 fprintf(file, "{%s %s}",
670 clang_getCompletionChunkKindSpelling(Kind),
671 text? text : "");
672 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000673}
674
675void print_completion_result(CXCompletionResult *completion_result,
676 CXClientData client_data) {
677 FILE *file = (FILE *)client_data;
678 fprintf(file, "%s:",
679 clang_getCursorKindSpelling(completion_result->CursorKind));
680 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000681 fprintf(file, "\n");
682}
683
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000684int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000685 const char *input = argv[1];
686 char *filename = 0;
687 unsigned line;
688 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000689 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000690 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000691 struct CXUnsavedFile *unsaved_files = 0;
692 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000693 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000694
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000695 input += strlen("-code-completion-at=");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000696 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
697 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000698 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000699
Douglas Gregor735df882009-12-02 09:21:34 +0000700 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
701 return -1;
702
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000703 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000704 results = clang_codeComplete(CIdx,
705 argv[argc - 1], argc - num_unsaved_files - 3,
706 argv + num_unsaved_files + 2,
707 num_unsaved_files, unsaved_files,
708 filename, line, column);
709 if (results) {
710 unsigned i, n = results->NumResults;
711 for (i = 0; i != n; ++i)
712 print_completion_result(results->Results + i, stdout);
713 clang_disposeCodeCompleteResults(results);
714 }
715
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000716 clang_disposeIndex(CIdx);
717 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000718
Douglas Gregor735df882009-12-02 09:21:34 +0000719 free_remapped_files(unsaved_files, num_unsaved_files);
720
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000721 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000722}
723
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000724typedef struct {
725 char *filename;
726 unsigned line;
727 unsigned column;
728} CursorSourceLocation;
729
730int inspect_cursor_at(int argc, const char **argv) {
731 CXIndex CIdx;
732 int errorCode;
733 struct CXUnsavedFile *unsaved_files = 0;
734 int num_unsaved_files = 0;
735 CXTranslationUnit TU;
736 CXCursor Cursor;
737 CursorSourceLocation *Locations = 0;
738 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000739
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000740 /* Count the number of locations. */
741 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
742 ++NumLocations;
743
744 /* Parse the locations. */
745 assert(NumLocations > 0 && "Unable to count locations?");
746 Locations = (CursorSourceLocation *)malloc(
747 NumLocations * sizeof(CursorSourceLocation));
748 for (Loc = 0; Loc < NumLocations; ++Loc) {
749 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
750 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
751 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000752 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000753 return errorCode;
754 }
755
756 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
757 &num_unsaved_files))
758 return -1;
759
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000760 CIdx = clang_createIndex(0, 1);
761 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
762 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000763 argv + num_unsaved_files + 1 + NumLocations,
764 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000765 unsaved_files,
766 PrintDiagnosticCallback,
767 stderr);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000768 if (!TU) {
769 fprintf(stderr, "unable to parse input\n");
770 return -1;
771 }
772
773 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000774 CXFile file = clang_getFile(TU, Locations[Loc].filename);
775 if (!file)
776 continue;
777
778 Cursor = clang_getCursor(TU,
779 clang_getLocation(TU, file, Locations[Loc].line,
780 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000781 PrintCursor(Cursor);
782 printf("\n");
783 free(Locations[Loc].filename);
784 }
785
786 clang_disposeTranslationUnit(TU);
787 clang_disposeIndex(CIdx);
788 free(Locations);
789 free_remapped_files(unsaved_files, num_unsaved_files);
790 return 0;
791}
792
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000793int perform_token_annotation(int argc, const char **argv) {
794 const char *input = argv[1];
795 char *filename = 0;
796 unsigned line, second_line;
797 unsigned column, second_column;
798 CXIndex CIdx;
799 CXTranslationUnit TU = 0;
800 int errorCode;
801 struct CXUnsavedFile *unsaved_files = 0;
802 int num_unsaved_files = 0;
803 CXToken *tokens;
804 unsigned num_tokens;
805 CXSourceRange range;
806 CXSourceLocation startLoc, endLoc;
807 CXFile file = 0;
808 CXCursor *cursors = 0;
809 unsigned i;
810
811 input += strlen("-test-annotate-tokens=");
812 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
813 &second_line, &second_column)))
814 return errorCode;
815
816 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
817 return -1;
818
819 CIdx = clang_createIndex(0, 0);
820 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
821 argc - num_unsaved_files - 3,
822 argv + num_unsaved_files + 2,
823 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000824 unsaved_files,
825 PrintDiagnosticCallback,
826 stderr);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000827 if (!TU) {
828 fprintf(stderr, "unable to parse input\n");
829 clang_disposeIndex(CIdx);
830 free(filename);
831 free_remapped_files(unsaved_files, num_unsaved_files);
832 return -1;
833 }
834 errorCode = 0;
835
836 file = clang_getFile(TU, filename);
837 if (!file) {
838 fprintf(stderr, "file %s is not in this translation unit\n", filename);
839 errorCode = -1;
840 goto teardown;
841 }
842
843 startLoc = clang_getLocation(TU, file, line, column);
844 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
845 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
846 column);
847 errorCode = -1;
848 goto teardown;
849 }
850
851 endLoc = clang_getLocation(TU, file, second_line, second_column);
852 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
853 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
854 second_line, second_column);
855 errorCode = -1;
856 goto teardown;
857 }
858
859 range = clang_getRange(startLoc, endLoc);
860 clang_tokenize(TU, range, &tokens, &num_tokens);
861 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
862 clang_annotateTokens(TU, tokens, num_tokens, cursors);
863 for (i = 0; i != num_tokens; ++i) {
864 const char *kind = "<unknown>";
865 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
866 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
867 unsigned start_line, start_column, end_line, end_column;
868
869 switch (clang_getTokenKind(tokens[i])) {
870 case CXToken_Punctuation: kind = "Punctuation"; break;
871 case CXToken_Keyword: kind = "Keyword"; break;
872 case CXToken_Identifier: kind = "Identifier"; break;
873 case CXToken_Literal: kind = "Literal"; break;
874 case CXToken_Comment: kind = "Comment"; break;
875 }
876 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000877 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000878 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000879 0, &end_line, &end_column, 0);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000880 printf("%s: \"%s\" [%d:%d - %d:%d]", kind, clang_getCString(spelling),
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000881 start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000882 if (!clang_isInvalid(cursors[i].kind)) {
883 printf(" ");
884 PrintCursor(cursors[i]);
885 }
886 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000887 }
888 free(cursors);
889
890 teardown:
891 clang_disposeTranslationUnit(TU);
892 clang_disposeIndex(CIdx);
893 free(filename);
894 free_remapped_files(unsaved_files, num_unsaved_files);
895 return errorCode;
896}
897
Ted Kremenek0d435192009-11-17 18:13:31 +0000898/******************************************************************************/
899/* Command line processing. */
900/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000901
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000902static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000903 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000904 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000905 if (strcmp(s, "-usrs") == 0)
906 return USRVisitor;
907 return NULL;
908}
909
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000910static void print_usage(void) {
911 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000912 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000913 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000914 " c-index-test -test-file-scan <AST file> <source file> "
915 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000916 " c-index-test -test-load-tu <AST file> <symbol filter> "
917 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000918 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
919 "[FileCheck prefix]\n"
920 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000921 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000922 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +0000923 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
924 " c-index-test -test-inclusion-stack-source {<args>}*\n"
925 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000926 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000927 " all - load all symbols, including those from PCH\n"
928 " local - load all symbols except those in PCH\n"
929 " category - only load ObjC categories (non-PCH)\n"
930 " interface - only load ObjC interfaces (non-PCH)\n"
931 " protocol - only load ObjC protocols (non-PCH)\n"
932 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000933 " typedef - only load typdefs (non-PCH)\n"
934 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000935}
936
937int main(int argc, const char **argv) {
938 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
939 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000940 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
941 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000942 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000943 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +0000944 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +0000945 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
946 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +0000947 }
948 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000949 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +0000950 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +0000951 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +0000952 }
953 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000954 return perform_file_scan(argv[2], argv[3],
955 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000956 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
957 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000958 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
959 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
960 PrintInclusionStack);
961 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
962 return perform_test_load_tu(argv[2], "all", NULL, NULL,
963 PrintInclusionStack);
964
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000965 print_usage();
966 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000967}