blob: 2ad9da12bb65ee124995445b65f78e8eaeb5fce9 [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 Gregor46766dc2010-01-26 19:19:08 +0000154 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000155 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;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000167 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000168 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/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000175/* Callbacks. */
176/******************************************************************************/
177
178typedef void (*PostVisitTU)(CXTranslationUnit);
179
180/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000181/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000182/******************************************************************************/
183
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000184static const char *FileCheckPrefix = "CHECK";
185
Douglas Gregora7bde202010-01-19 00:34:46 +0000186static void PrintCursorExtent(CXCursor C) {
187 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000188 CXFile begin_file, end_file;
189 unsigned begin_line, begin_column, end_line, end_column;
190
191 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000192 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000193 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000194 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000195 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000196 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000197
198 printf(" [Extent=%d:%d:%d:%d]", begin_line, begin_column,
199 end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000200}
201
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000202/* Data used by all of the visitors. */
203typedef struct {
204 CXTranslationUnit TU;
205 enum CXCursorKind *Filter;
206} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000207
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000208
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000209enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
210 CXCursor Parent,
211 CXClientData ClientData) {
212 VisitorData *Data = (VisitorData *)ClientData;
213 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000214 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000215 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000216 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000217 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000218 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000219 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000220 PrintCursorExtent(Cursor);
Ted Kremenek70ee5422010-01-16 01:44:12 +0000221 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000222 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000223 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000224
225 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000226}
Steve Naroff50398192009-08-28 15:28:48 +0000227
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000228static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
229 CXCursor Parent,
230 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000231 const char *startBuf, *endBuf;
232 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
233 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000234 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000235
Douglas Gregorb6998662010-01-19 19:34:47 +0000236 if (Cursor.kind != CXCursor_FunctionDecl ||
237 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000238 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000239
240 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
241 &startLine, &startColumn,
242 &endLine, &endColumn);
243 /* Probe the entire body, looking for both decls and refs. */
244 curLine = startLine;
245 curColumn = startColumn;
246
247 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000248 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000249 CXFile file;
Douglas Gregor98258af2010-01-18 22:46:11 +0000250 const char *source = 0;
251
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000252 if (*startBuf == '\n') {
253 startBuf++;
254 curLine++;
255 curColumn = 1;
256 } else if (*startBuf != '\t')
257 curColumn++;
258
Douglas Gregor98258af2010-01-18 22:46:11 +0000259 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000260 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000261 source = clang_getFileName(file);
Douglas Gregor98258af2010-01-18 22:46:11 +0000262 if (source) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000263 CXSourceLocation RefLoc
264 = clang_getLocation(Data->TU, file, curLine, curColumn);
265 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000266 if (Ref.kind == CXCursor_NoDeclFound) {
267 /* Nothing found here; that's fine. */
268 } else if (Ref.kind != CXCursor_FunctionDecl) {
269 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
270 curLine, curColumn);
271 PrintCursor(Ref);
272 printf("\n");
273 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000274 }
275 startBuf++;
276 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000277
278 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000279}
280
Ted Kremenek7d405622010-01-12 23:34:26 +0000281/******************************************************************************/
282/* USR testing. */
283/******************************************************************************/
284
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000285enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
286 CXClientData ClientData) {
287 VisitorData *Data = (VisitorData *)ClientData;
288 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000289 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000290 if (!USR.Spelling) {
291 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000292 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000293 }
294 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregora7bde202010-01-19 00:34:46 +0000295 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000296 printf("\n");
297 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000298
299 return CXChildVisit_Recurse;
300 }
301
302 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000303}
304
305/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000306/* Inclusion stack testing. */
307/******************************************************************************/
308
309void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
310 unsigned includeStackLen, CXClientData data) {
311
312 unsigned i;
313 printf("file: %s\nincluded by:\n", clang_getFileName(includedFile));
314 for (i = 0; i < includeStackLen; ++i) {
315 CXFile includingFile;
316 unsigned line, column;
317 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
318 &column, 0);
319 printf(" %s:%d:%d\n", clang_getFileName(includingFile), line, column);
320 }
321 printf("\n");
322}
323
324void PrintInclusionStack(CXTranslationUnit TU) {
325 clang_getInclusions(TU, InclusionVisitor, NULL);
326}
327
328/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000329/* Loading ASTs/source. */
330/******************************************************************************/
331
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000332static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000333 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000334 CXCursorVisitor Visitor,
335 PostVisitTU PV) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000336
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000337 if (prefix)
338 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000339
340 if (Visitor) {
341 enum CXCursorKind K = CXCursor_NotImplemented;
342 enum CXCursorKind *ck = &K;
343 VisitorData Data;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000344
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000345 /* Perform some simple filtering. */
346 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
347 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
348 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
349 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
350 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
351 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
352 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
353 else {
354 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
355 return 1;
356 }
357
358 Data.TU = TU;
359 Data.Filter = ck;
360 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000361 }
Ted Kremenekce2ae882010-01-26 17:59:48 +0000362
363 if (PV)
364 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000365
Ted Kremenek0d435192009-11-17 18:13:31 +0000366 clang_disposeTranslationUnit(TU);
367 return 0;
368}
369
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000370int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000371 const char *prefix, CXCursorVisitor Visitor,
372 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000373 CXIndex Idx;
374 CXTranslationUnit TU;
375 Idx = clang_createIndex(/* excludeDeclsFromPCH */
376 !strcmp(filter, "local") ? 1 : 0,
377 /* displayDiagnostics */ 1);
378
379 if (!CreateTranslationUnit(Idx, file, &TU))
380 return 1;
381
Ted Kremenekce2ae882010-01-26 17:59:48 +0000382 return perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000383}
384
Ted Kremenekce2ae882010-01-26 17:59:48 +0000385int perform_test_load_source(int argc, const char **argv,
386 const char *filter, CXCursorVisitor Visitor,
387 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000388 const char *UseExternalASTs =
389 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000390 CXIndex Idx;
391 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000392 struct CXUnsavedFile *unsaved_files = 0;
393 int num_unsaved_files = 0;
394 int result;
395
Daniel Dunbarada487d2009-12-01 02:03:10 +0000396 Idx = clang_createIndex(/* excludeDeclsFromPCH */
397 !strcmp(filter, "local") ? 1 : 0,
398 /* displayDiagnostics */ 1);
399
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000400 if (UseExternalASTs && strlen(UseExternalASTs))
401 clang_setUseExternalASTGeneration(Idx, 1);
402
Douglas Gregor4db64a42010-01-23 00:14:00 +0000403 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files))
404 return -1;
405
406 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
407 argc - num_unsaved_files,
408 argv + num_unsaved_files,
409 num_unsaved_files,
410 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000411 if (!TU) {
412 fprintf(stderr, "Unable to load translation unit!\n");
413 return 1;
414 }
415
Ted Kremenekce2ae882010-01-26 17:59:48 +0000416 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000417 free_remapped_files(unsaved_files, num_unsaved_files);
418 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000419}
420
Ted Kremenek0d435192009-11-17 18:13:31 +0000421/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000422/* Logic for testing clang_getCursor(). */
423/******************************************************************************/
424
425static void print_cursor_file_scan(CXCursor cursor,
426 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000427 unsigned end_line, unsigned end_col,
428 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000429 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000430 if (prefix)
431 printf("-%s", prefix);
432 printf("{start_line=%d start_col=%d end_line=%d end_col=%d} ",
433 start_line, start_col, end_line, end_col);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000434 PrintCursor(cursor);
435 printf("\n");
436}
437
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000438static int perform_file_scan(const char *ast_file, const char *source_file,
439 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000440 CXIndex Idx;
441 CXTranslationUnit TU;
442 FILE *fp;
443 unsigned line;
444 CXCursor prevCursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000445 CXFile file;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000446 unsigned printed;
447 unsigned start_line, start_col, last_line, last_col;
448 size_t i;
449
450 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
451 /* displayDiagnostics */ 1))) {
452 fprintf(stderr, "Could not create Index\n");
453 return 1;
454 }
455
456 if (!CreateTranslationUnit(Idx, ast_file, &TU))
457 return 1;
458
459 if ((fp = fopen(source_file, "r")) == NULL) {
460 fprintf(stderr, "Could not open '%s'\n", source_file);
461 return 1;
462 }
463
464 line = 0;
465 prevCursor = clang_getNullCursor();
466 printed = 0;
467 start_line = last_line = 1;
468 start_col = last_col = 1;
469
Douglas Gregorb9790342010-01-22 21:44:22 +0000470 file = clang_getFile(TU, source_file);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000471 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000472 size_t len = 0;
473 int c;
474
475 while ((c = fgetc(fp)) != EOF) {
476 len++;
477 if (c == '\n')
478 break;
479 }
480
Ted Kremenek1c6da172009-11-17 19:37:36 +0000481 ++line;
482
483 for (i = 0; i < len ; ++i) {
484 CXCursor cursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000485 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, i+1));
Ted Kremenek1c6da172009-11-17 19:37:36 +0000486
487 if (!clang_equalCursors(cursor, prevCursor) &&
488 prevCursor.kind != CXCursor_InvalidFile) {
489 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000490 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000491 printed = 1;
492 start_line = line;
493 start_col = (unsigned) i+1;
494 }
495 else {
496 printed = 0;
497 }
498
499 prevCursor = cursor;
500 last_line = line;
501 last_col = (unsigned) i+1;
502 }
503 }
504
505 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
506 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000507 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000508 }
509
510 fclose(fp);
511 return 0;
512}
513
514/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000515/* Logic for testing clang_codeComplete(). */
516/******************************************************************************/
517
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000518/* Parse file:line:column from the input string. Returns 0 on success, non-zero
519 on failure. If successful, the pointer *filename will contain newly-allocated
520 memory (that will be owned by the caller) to store the file name. */
521int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000522 unsigned *column, unsigned *second_line,
523 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000524 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000525 const char *last_colon = strrchr(input, ':');
526 unsigned values[4], i;
527 unsigned num_values = (second_line && second_column)? 4 : 2;
528
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000529 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000530 if (!last_colon || last_colon == input) {
531 if (num_values == 4)
532 fprintf(stderr, "could not parse filename:line:column:line:column in "
533 "'%s'\n", input);
534 else
535 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000536 return 1;
537 }
538
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000539 for (i = 0; i != num_values; ++i) {
540 const char *prev_colon;
541
542 /* Parse the next line or column. */
543 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
544 if (*endptr != 0 && *endptr != ':') {
545 fprintf(stderr, "could not parse %s in '%s'\n",
546 (i % 2 ? "column" : "line"), input);
547 return 1;
548 }
549
550 if (i + 1 == num_values)
551 break;
552
553 /* Find the previous colon. */
554 prev_colon = last_colon - 1;
555 while (prev_colon != input && *prev_colon != ':')
556 --prev_colon;
557 if (prev_colon == input) {
558 fprintf(stderr, "could not parse %s in '%s'\n",
559 (i % 2 == 0? "column" : "line"), input);
560 return 1;
561 }
562
563 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000564 }
565
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000566 *line = values[0];
567 *column = values[1];
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000568
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000569 if (second_line && second_column) {
570 *second_line = values[2];
571 *second_column = values[3];
572 }
573
Douglas Gregor88d23952009-11-09 18:19:57 +0000574 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000575 *filename = (char*)malloc(last_colon - input + 1);
576 memcpy(*filename, input, last_colon - input);
577 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000578 return 0;
579}
580
581const char *
582clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
583 switch (Kind) {
584 case CXCompletionChunk_Optional: return "Optional";
585 case CXCompletionChunk_TypedText: return "TypedText";
586 case CXCompletionChunk_Text: return "Text";
587 case CXCompletionChunk_Placeholder: return "Placeholder";
588 case CXCompletionChunk_Informative: return "Informative";
589 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
590 case CXCompletionChunk_LeftParen: return "LeftParen";
591 case CXCompletionChunk_RightParen: return "RightParen";
592 case CXCompletionChunk_LeftBracket: return "LeftBracket";
593 case CXCompletionChunk_RightBracket: return "RightBracket";
594 case CXCompletionChunk_LeftBrace: return "LeftBrace";
595 case CXCompletionChunk_RightBrace: return "RightBrace";
596 case CXCompletionChunk_LeftAngle: return "LeftAngle";
597 case CXCompletionChunk_RightAngle: return "RightAngle";
598 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000599 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000600 case CXCompletionChunk_Colon: return "Colon";
601 case CXCompletionChunk_SemiColon: return "SemiColon";
602 case CXCompletionChunk_Equal: return "Equal";
603 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
604 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000605 }
606
607 return "Unknown";
608}
609
Douglas Gregor3ac73852009-11-09 16:04:45 +0000610void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000611 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000612
613 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000614 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000615 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000616 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000617 = clang_getCompletionChunkKind(completion_string, I);
618
619 if (Kind == CXCompletionChunk_Optional) {
620 fprintf(file, "{Optional ");
621 print_completion_string(
622 clang_getCompletionChunkCompletionString(completion_string, I),
623 file);
624 fprintf(file, "}");
625 continue;
626 }
627
Douglas Gregord5a20892009-11-09 17:05:28 +0000628 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000629 fprintf(file, "{%s %s}",
630 clang_getCompletionChunkKindSpelling(Kind),
631 text? text : "");
632 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000633}
634
635void print_completion_result(CXCompletionResult *completion_result,
636 CXClientData client_data) {
637 FILE *file = (FILE *)client_data;
638 fprintf(file, "%s:",
639 clang_getCursorKindSpelling(completion_result->CursorKind));
640 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000641 fprintf(file, "\n");
642}
643
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000644int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000645 const char *input = argv[1];
646 char *filename = 0;
647 unsigned line;
648 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000649 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000650 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000651 struct CXUnsavedFile *unsaved_files = 0;
652 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000653 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000654
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000655 input += strlen("-code-completion-at=");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000656 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
657 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000658 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000659
Douglas Gregor735df882009-12-02 09:21:34 +0000660 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
661 return -1;
662
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000663 CIdx = clang_createIndex(0, 0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000664 results = clang_codeComplete(CIdx,
665 argv[argc - 1], argc - num_unsaved_files - 3,
666 argv + num_unsaved_files + 2,
667 num_unsaved_files, unsaved_files,
668 filename, line, column);
669 if (results) {
670 unsigned i, n = results->NumResults;
671 for (i = 0; i != n; ++i)
672 print_completion_result(results->Results + i, stdout);
673 clang_disposeCodeCompleteResults(results);
674 }
675
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000676 clang_disposeIndex(CIdx);
677 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000678
Douglas Gregor735df882009-12-02 09:21:34 +0000679 free_remapped_files(unsaved_files, num_unsaved_files);
680
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000681 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000682}
683
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000684typedef struct {
685 char *filename;
686 unsigned line;
687 unsigned column;
688} CursorSourceLocation;
689
690int inspect_cursor_at(int argc, const char **argv) {
691 CXIndex CIdx;
692 int errorCode;
693 struct CXUnsavedFile *unsaved_files = 0;
694 int num_unsaved_files = 0;
695 CXTranslationUnit TU;
696 CXCursor Cursor;
697 CursorSourceLocation *Locations = 0;
698 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000699
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000700 /* Count the number of locations. */
701 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
702 ++NumLocations;
703
704 /* Parse the locations. */
705 assert(NumLocations > 0 && "Unable to count locations?");
706 Locations = (CursorSourceLocation *)malloc(
707 NumLocations * sizeof(CursorSourceLocation));
708 for (Loc = 0; Loc < NumLocations; ++Loc) {
709 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
710 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
711 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000712 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000713 return errorCode;
714 }
715
716 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
717 &num_unsaved_files))
718 return -1;
719
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000720 CIdx = clang_createIndex(0, 1);
721 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
722 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000723 argv + num_unsaved_files + 1 + NumLocations,
724 num_unsaved_files,
725 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000726 if (!TU) {
727 fprintf(stderr, "unable to parse input\n");
728 return -1;
729 }
730
731 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000732 CXFile file = clang_getFile(TU, Locations[Loc].filename);
733 if (!file)
734 continue;
735
736 Cursor = clang_getCursor(TU,
737 clang_getLocation(TU, file, Locations[Loc].line,
738 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000739 PrintCursor(Cursor);
740 printf("\n");
741 free(Locations[Loc].filename);
742 }
743
744 clang_disposeTranslationUnit(TU);
745 clang_disposeIndex(CIdx);
746 free(Locations);
747 free_remapped_files(unsaved_files, num_unsaved_files);
748 return 0;
749}
750
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000751int perform_token_annotation(int argc, const char **argv) {
752 const char *input = argv[1];
753 char *filename = 0;
754 unsigned line, second_line;
755 unsigned column, second_column;
756 CXIndex CIdx;
757 CXTranslationUnit TU = 0;
758 int errorCode;
759 struct CXUnsavedFile *unsaved_files = 0;
760 int num_unsaved_files = 0;
761 CXToken *tokens;
762 unsigned num_tokens;
763 CXSourceRange range;
764 CXSourceLocation startLoc, endLoc;
765 CXFile file = 0;
766 CXCursor *cursors = 0;
767 unsigned i;
768
769 input += strlen("-test-annotate-tokens=");
770 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
771 &second_line, &second_column)))
772 return errorCode;
773
774 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
775 return -1;
776
777 CIdx = clang_createIndex(0, 0);
778 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
779 argc - num_unsaved_files - 3,
780 argv + num_unsaved_files + 2,
781 num_unsaved_files,
782 unsaved_files);
783 if (!TU) {
784 fprintf(stderr, "unable to parse input\n");
785 clang_disposeIndex(CIdx);
786 free(filename);
787 free_remapped_files(unsaved_files, num_unsaved_files);
788 return -1;
789 }
790 errorCode = 0;
791
792 file = clang_getFile(TU, filename);
793 if (!file) {
794 fprintf(stderr, "file %s is not in this translation unit\n", filename);
795 errorCode = -1;
796 goto teardown;
797 }
798
799 startLoc = clang_getLocation(TU, file, line, column);
800 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
801 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
802 column);
803 errorCode = -1;
804 goto teardown;
805 }
806
807 endLoc = clang_getLocation(TU, file, second_line, second_column);
808 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
809 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
810 second_line, second_column);
811 errorCode = -1;
812 goto teardown;
813 }
814
815 range = clang_getRange(startLoc, endLoc);
816 clang_tokenize(TU, range, &tokens, &num_tokens);
817 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
818 clang_annotateTokens(TU, tokens, num_tokens, cursors);
819 for (i = 0; i != num_tokens; ++i) {
820 const char *kind = "<unknown>";
821 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
822 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
823 unsigned start_line, start_column, end_line, end_column;
824
825 switch (clang_getTokenKind(tokens[i])) {
826 case CXToken_Punctuation: kind = "Punctuation"; break;
827 case CXToken_Keyword: kind = "Keyword"; break;
828 case CXToken_Identifier: kind = "Identifier"; break;
829 case CXToken_Literal: kind = "Literal"; break;
830 case CXToken_Comment: kind = "Comment"; break;
831 }
832 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000833 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000834 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000835 0, &end_line, &end_column, 0);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000836 printf("%s: \"%s\" [%d:%d - %d:%d]", kind, clang_getCString(spelling),
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000837 start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000838 if (!clang_isInvalid(cursors[i].kind)) {
839 printf(" ");
840 PrintCursor(cursors[i]);
841 }
842 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000843 }
844 free(cursors);
845
846 teardown:
847 clang_disposeTranslationUnit(TU);
848 clang_disposeIndex(CIdx);
849 free(filename);
850 free_remapped_files(unsaved_files, num_unsaved_files);
851 return errorCode;
852}
853
Ted Kremenek0d435192009-11-17 18:13:31 +0000854/******************************************************************************/
855/* Command line processing. */
856/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000857
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000858static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000859 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000860 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000861 if (strcmp(s, "-usrs") == 0)
862 return USRVisitor;
863 return NULL;
864}
865
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000866static void print_usage(void) {
867 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000868 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000869 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000870 " c-index-test -test-file-scan <AST file> <source file> "
871 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000872 " c-index-test -test-load-tu <AST file> <symbol filter> "
873 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000874 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
875 "[FileCheck prefix]\n"
876 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000877 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000878 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +0000879 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
880 " c-index-test -test-inclusion-stack-source {<args>}*\n"
881 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000882 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000883 " all - load all symbols, including those from PCH\n"
884 " local - load all symbols except those in PCH\n"
885 " category - only load ObjC categories (non-PCH)\n"
886 " interface - only load ObjC interfaces (non-PCH)\n"
887 " protocol - only load ObjC protocols (non-PCH)\n"
888 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000889 " typedef - only load typdefs (non-PCH)\n"
890 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000891}
892
893int main(int argc, const char **argv) {
894 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
895 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000896 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
897 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000898 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000899 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +0000900 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +0000901 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
902 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +0000903 }
904 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000905 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +0000906 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +0000907 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +0000908 }
909 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000910 return perform_file_scan(argv[2], argv[3],
911 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000912 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
913 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000914 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
915 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
916 PrintInclusionStack);
917 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
918 return perform_test_load_tu(argv[2], "all", NULL, NULL,
919 PrintInclusionStack);
920
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000921 print_usage();
922 return 1;
Steve Naroff50398192009-08-28 15:28:48 +0000923}