[Heikki Kultala] This patch contains the ABI changes for the TCE target.
[clang.git] / tools / c-index-test / c-index-test.c
blobd4e567d9e26ae0d4cbd9f07a5b55796518c062c0
1 /* c-index-test.c */
3 #include "clang-c/Index.h"
4 #include <ctype.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <assert.h>
10 /******************************************************************************/
11 /* Utility functions. */
12 /******************************************************************************/
14 #ifdef _MSC_VER
15 char *basename(const char* path)
17 char* base1 = (char*)strrchr(path, '/');
18 char* base2 = (char*)strrchr(path, '\\');
19 if (base1 && base2)
20 return((base1 > base2) ? base1 + 1 : base2 + 1);
21 else if (base1)
22 return(base1 + 1);
23 else if (base2)
24 return(base2 + 1);
26 return((char*)path);
28 #else
29 extern char *basename(const char *);
30 #endif
32 /** \brief Return the default parsing options. */
33 static unsigned getDefaultParsingOptions() {
34 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
36 if (getenv("CINDEXTEST_EDITING"))
37 options |= clang_defaultEditingTranslationUnitOptions();
38 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
39 options |= CXTranslationUnit_CacheCompletionResults;
41 return options;
44 static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
45 unsigned end_line, unsigned end_column) {
46 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
47 end_line, end_column);
50 static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
51 CXTranslationUnit *TU) {
53 *TU = clang_createTranslationUnit(Idx, file);
54 if (!*TU) {
55 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
56 return 0;
58 return 1;
61 void free_remapped_files(struct CXUnsavedFile *unsaved_files,
62 int num_unsaved_files) {
63 int i;
64 for (i = 0; i != num_unsaved_files; ++i) {
65 free((char *)unsaved_files[i].Filename);
66 free((char *)unsaved_files[i].Contents);
68 free(unsaved_files);
71 int parse_remapped_files(int argc, const char **argv, int start_arg,
72 struct CXUnsavedFile **unsaved_files,
73 int *num_unsaved_files) {
74 int i;
75 int arg;
76 int prefix_len = strlen("-remap-file=");
77 *unsaved_files = 0;
78 *num_unsaved_files = 0;
80 /* Count the number of remapped files. */
81 for (arg = start_arg; arg < argc; ++arg) {
82 if (strncmp(argv[arg], "-remap-file=", prefix_len))
83 break;
85 ++*num_unsaved_files;
88 if (*num_unsaved_files == 0)
89 return 0;
91 *unsaved_files
92 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
93 *num_unsaved_files);
94 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
95 struct CXUnsavedFile *unsaved = *unsaved_files + i;
96 const char *arg_string = argv[arg] + prefix_len;
97 int filename_len;
98 char *filename;
99 char *contents;
100 FILE *to_file;
101 const char *semi = strchr(arg_string, ';');
102 if (!semi) {
103 fprintf(stderr,
104 "error: -remap-file=from;to argument is missing semicolon\n");
105 free_remapped_files(*unsaved_files, i);
106 *unsaved_files = 0;
107 *num_unsaved_files = 0;
108 return -1;
111 /* Open the file that we're remapping to. */
112 to_file = fopen(semi + 1, "rb");
113 if (!to_file) {
114 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
115 semi + 1);
116 free_remapped_files(*unsaved_files, i);
117 *unsaved_files = 0;
118 *num_unsaved_files = 0;
119 return -1;
122 /* Determine the length of the file we're remapping to. */
123 fseek(to_file, 0, SEEK_END);
124 unsaved->Length = ftell(to_file);
125 fseek(to_file, 0, SEEK_SET);
127 /* Read the contents of the file we're remapping to. */
128 contents = (char *)malloc(unsaved->Length + 1);
129 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
130 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
131 (feof(to_file) ? "EOF" : "error"), semi + 1);
132 fclose(to_file);
133 free_remapped_files(*unsaved_files, i);
134 *unsaved_files = 0;
135 *num_unsaved_files = 0;
136 return -1;
138 contents[unsaved->Length] = 0;
139 unsaved->Contents = contents;
141 /* Close the file. */
142 fclose(to_file);
144 /* Copy the file name that we're remapping from. */
145 filename_len = semi - arg_string;
146 filename = (char *)malloc(filename_len + 1);
147 memcpy(filename, arg_string, filename_len);
148 filename[filename_len] = 0;
149 unsaved->Filename = filename;
152 return 0;
155 /******************************************************************************/
156 /* Pretty-printing. */
157 /******************************************************************************/
159 int want_display_name = 0;
161 static void PrintCursor(CXCursor Cursor) {
162 if (clang_isInvalid(Cursor.kind)) {
163 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
164 printf("Invalid Cursor => %s", clang_getCString(ks));
165 clang_disposeString(ks);
167 else {
168 CXString string, ks;
169 CXCursor Referenced;
170 unsigned line, column;
171 CXCursor SpecializationOf;
172 CXCursor *overridden;
173 unsigned num_overridden;
175 ks = clang_getCursorKindSpelling(Cursor.kind);
176 string = want_display_name? clang_getCursorDisplayName(Cursor)
177 : clang_getCursorSpelling(Cursor);
178 printf("%s=%s", clang_getCString(ks),
179 clang_getCString(string));
180 clang_disposeString(ks);
181 clang_disposeString(string);
183 Referenced = clang_getCursorReferenced(Cursor);
184 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
185 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
186 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
187 printf("[");
188 for (I = 0; I != N; ++I) {
189 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
190 CXSourceLocation Loc;
191 if (I)
192 printf(", ");
194 Loc = clang_getCursorLocation(Ovl);
195 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
196 printf("%d:%d", line, column);
198 printf("]");
199 } else {
200 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
201 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
202 printf(":%d:%d", line, column);
206 if (clang_isCursorDefinition(Cursor))
207 printf(" (Definition)");
209 switch (clang_getCursorAvailability(Cursor)) {
210 case CXAvailability_Available:
211 break;
213 case CXAvailability_Deprecated:
214 printf(" (deprecated)");
215 break;
217 case CXAvailability_NotAvailable:
218 printf(" (unavailable)");
219 break;
222 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
223 CXType T =
224 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
225 CXString S = clang_getTypeKindSpelling(T.kind);
226 printf(" [IBOutletCollection=%s]", clang_getCString(S));
227 clang_disposeString(S);
230 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
231 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
232 unsigned isVirtual = clang_isVirtualBase(Cursor);
233 const char *accessStr = 0;
235 switch (access) {
236 case CX_CXXInvalidAccessSpecifier:
237 accessStr = "invalid"; break;
238 case CX_CXXPublic:
239 accessStr = "public"; break;
240 case CX_CXXProtected:
241 accessStr = "protected"; break;
242 case CX_CXXPrivate:
243 accessStr = "private"; break;
246 printf(" [access=%s isVirtual=%s]", accessStr,
247 isVirtual ? "true" : "false");
250 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
251 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
252 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
253 CXString Name = clang_getCursorSpelling(SpecializationOf);
254 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
255 printf(" [Specialization of %s:%d:%d]",
256 clang_getCString(Name), line, column);
257 clang_disposeString(Name);
260 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
261 if (num_overridden) {
262 unsigned I;
263 printf(" [Overrides ");
264 for (I = 0; I != num_overridden; ++I) {
265 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
266 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
267 if (I)
268 printf(", ");
269 printf("@%d:%d", line, column);
271 printf("]");
272 clang_disposeOverriddenCursors(overridden);
275 if (Cursor.kind == CXCursor_InclusionDirective) {
276 CXFile File = clang_getIncludedFile(Cursor);
277 CXString Included = clang_getFileName(File);
278 printf(" (%s)", clang_getCString(Included));
279 clang_disposeString(Included);
284 static const char* GetCursorSource(CXCursor Cursor) {
285 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
286 CXString source;
287 CXFile file;
288 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
289 source = clang_getFileName(file);
290 if (!clang_getCString(source)) {
291 clang_disposeString(source);
292 return "<invalid loc>";
294 else {
295 const char *b = basename(clang_getCString(source));
296 clang_disposeString(source);
297 return b;
301 /******************************************************************************/
302 /* Callbacks. */
303 /******************************************************************************/
305 typedef void (*PostVisitTU)(CXTranslationUnit);
307 void PrintDiagnostic(CXDiagnostic Diagnostic) {
308 FILE *out = stderr;
309 CXFile file;
310 CXString Msg;
311 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
312 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
313 | CXDiagnostic_DisplayOption;
314 unsigned i, num_fixits;
316 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
317 return;
319 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
320 fprintf(stderr, "%s\n", clang_getCString(Msg));
321 clang_disposeString(Msg);
323 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
324 &file, 0, 0, 0);
325 if (!file)
326 return;
328 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
329 for (i = 0; i != num_fixits; ++i) {
330 CXSourceRange range;
331 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
332 CXSourceLocation start = clang_getRangeStart(range);
333 CXSourceLocation end = clang_getRangeEnd(range);
334 unsigned start_line, start_column, end_line, end_column;
335 CXFile start_file, end_file;
336 clang_getSpellingLocation(start, &start_file, &start_line,
337 &start_column, 0);
338 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
339 if (clang_equalLocations(start, end)) {
340 /* Insertion. */
341 if (start_file == file)
342 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
343 clang_getCString(insertion_text), start_line, start_column);
344 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
345 /* Removal. */
346 if (start_file == file && end_file == file) {
347 fprintf(out, "FIX-IT: Remove ");
348 PrintExtent(out, start_line, start_column, end_line, end_column);
349 fprintf(out, "\n");
351 } else {
352 /* Replacement. */
353 if (start_file == end_file) {
354 fprintf(out, "FIX-IT: Replace ");
355 PrintExtent(out, start_line, start_column, end_line, end_column);
356 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
358 break;
360 clang_disposeString(insertion_text);
364 void PrintDiagnostics(CXTranslationUnit TU) {
365 int i, n = clang_getNumDiagnostics(TU);
366 for (i = 0; i != n; ++i) {
367 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
368 PrintDiagnostic(Diag);
369 clang_disposeDiagnostic(Diag);
373 /******************************************************************************/
374 /* Logic for testing traversal. */
375 /******************************************************************************/
377 static const char *FileCheckPrefix = "CHECK";
379 static void PrintCursorExtent(CXCursor C) {
380 CXSourceRange extent = clang_getCursorExtent(C);
381 CXFile begin_file, end_file;
382 unsigned begin_line, begin_column, end_line, end_column;
384 clang_getSpellingLocation(clang_getRangeStart(extent),
385 &begin_file, &begin_line, &begin_column, 0);
386 clang_getSpellingLocation(clang_getRangeEnd(extent),
387 &end_file, &end_line, &end_column, 0);
388 if (!begin_file || !end_file)
389 return;
391 printf(" Extent=");
392 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
395 /* Data used by all of the visitors. */
396 typedef struct {
397 CXTranslationUnit TU;
398 enum CXCursorKind *Filter;
399 } VisitorData;
402 enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
403 CXCursor Parent,
404 CXClientData ClientData) {
405 VisitorData *Data = (VisitorData *)ClientData;
406 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
407 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
408 unsigned line, column;
409 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
410 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
411 GetCursorSource(Cursor), line, column);
412 PrintCursor(Cursor);
413 PrintCursorExtent(Cursor);
414 printf("\n");
415 return CXChildVisit_Recurse;
418 return CXChildVisit_Continue;
421 static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
422 CXCursor Parent,
423 CXClientData ClientData) {
424 const char *startBuf, *endBuf;
425 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
426 CXCursor Ref;
427 VisitorData *Data = (VisitorData *)ClientData;
429 if (Cursor.kind != CXCursor_FunctionDecl ||
430 !clang_isCursorDefinition(Cursor))
431 return CXChildVisit_Continue;
433 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
434 &startLine, &startColumn,
435 &endLine, &endColumn);
436 /* Probe the entire body, looking for both decls and refs. */
437 curLine = startLine;
438 curColumn = startColumn;
440 while (startBuf < endBuf) {
441 CXSourceLocation Loc;
442 CXFile file;
443 CXString source;
445 if (*startBuf == '\n') {
446 startBuf++;
447 curLine++;
448 curColumn = 1;
449 } else if (*startBuf != '\t')
450 curColumn++;
452 Loc = clang_getCursorLocation(Cursor);
453 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
455 source = clang_getFileName(file);
456 if (clang_getCString(source)) {
457 CXSourceLocation RefLoc
458 = clang_getLocation(Data->TU, file, curLine, curColumn);
459 Ref = clang_getCursor(Data->TU, RefLoc);
460 if (Ref.kind == CXCursor_NoDeclFound) {
461 /* Nothing found here; that's fine. */
462 } else if (Ref.kind != CXCursor_FunctionDecl) {
463 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
464 curLine, curColumn);
465 PrintCursor(Ref);
466 printf("\n");
469 clang_disposeString(source);
470 startBuf++;
473 return CXChildVisit_Continue;
476 /******************************************************************************/
477 /* USR testing. */
478 /******************************************************************************/
480 enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
481 CXClientData ClientData) {
482 VisitorData *Data = (VisitorData *)ClientData;
483 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
484 CXString USR = clang_getCursorUSR(C);
485 const char *cstr = clang_getCString(USR);
486 if (!cstr || cstr[0] == '\0') {
487 clang_disposeString(USR);
488 return CXChildVisit_Recurse;
490 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
492 PrintCursorExtent(C);
493 printf("\n");
494 clang_disposeString(USR);
496 return CXChildVisit_Recurse;
499 return CXChildVisit_Continue;
502 /******************************************************************************/
503 /* Inclusion stack testing. */
504 /******************************************************************************/
506 void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
507 unsigned includeStackLen, CXClientData data) {
509 unsigned i;
510 CXString fname;
512 fname = clang_getFileName(includedFile);
513 printf("file: %s\nincluded by:\n", clang_getCString(fname));
514 clang_disposeString(fname);
516 for (i = 0; i < includeStackLen; ++i) {
517 CXFile includingFile;
518 unsigned line, column;
519 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
520 &column, 0);
521 fname = clang_getFileName(includingFile);
522 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
523 clang_disposeString(fname);
525 printf("\n");
528 void PrintInclusionStack(CXTranslationUnit TU) {
529 clang_getInclusions(TU, InclusionVisitor, NULL);
532 /******************************************************************************/
533 /* Linkage testing. */
534 /******************************************************************************/
536 static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
537 CXClientData d) {
538 const char *linkage = 0;
540 if (clang_isInvalid(clang_getCursorKind(cursor)))
541 return CXChildVisit_Recurse;
543 switch (clang_getCursorLinkage(cursor)) {
544 case CXLinkage_Invalid: break;
545 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
546 case CXLinkage_Internal: linkage = "Internal"; break;
547 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
548 case CXLinkage_External: linkage = "External"; break;
551 if (linkage) {
552 PrintCursor(cursor);
553 printf("linkage=%s\n", linkage);
556 return CXChildVisit_Recurse;
559 /******************************************************************************/
560 /* Typekind testing. */
561 /******************************************************************************/
563 static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
564 CXClientData d) {
566 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
567 CXType T = clang_getCursorType(cursor);
568 CXString S = clang_getTypeKindSpelling(T.kind);
569 PrintCursor(cursor);
570 printf(" typekind=%s", clang_getCString(S));
571 if (clang_isConstQualifiedType(T))
572 printf(" const");
573 if (clang_isVolatileQualifiedType(T))
574 printf(" volatile");
575 if (clang_isRestrictQualifiedType(T))
576 printf(" restrict");
577 clang_disposeString(S);
578 /* Print the canonical type if it is different. */
580 CXType CT = clang_getCanonicalType(T);
581 if (!clang_equalTypes(T, CT)) {
582 CXString CS = clang_getTypeKindSpelling(CT.kind);
583 printf(" [canonical=%s]", clang_getCString(CS));
584 clang_disposeString(CS);
587 /* Print the return type if it exists. */
589 CXType RT = clang_getCursorResultType(cursor);
590 if (RT.kind != CXType_Invalid) {
591 CXString RS = clang_getTypeKindSpelling(RT.kind);
592 printf(" [result=%s]", clang_getCString(RS));
593 clang_disposeString(RS);
596 /* Print if this is a non-POD type. */
597 printf(" [isPOD=%d]", clang_isPODType(T));
599 printf("\n");
601 return CXChildVisit_Recurse;
605 /******************************************************************************/
606 /* Loading ASTs/source. */
607 /******************************************************************************/
609 static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
610 const char *filter, const char *prefix,
611 CXCursorVisitor Visitor,
612 PostVisitTU PV) {
614 if (prefix)
615 FileCheckPrefix = prefix;
617 if (Visitor) {
618 enum CXCursorKind K = CXCursor_NotImplemented;
619 enum CXCursorKind *ck = &K;
620 VisitorData Data;
622 /* Perform some simple filtering. */
623 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
624 else if (!strcmp(filter, "all-display") ||
625 !strcmp(filter, "local-display")) {
626 ck = NULL;
627 want_display_name = 1;
629 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
630 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
631 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
632 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
633 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
634 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
635 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
636 else {
637 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
638 return 1;
641 Data.TU = TU;
642 Data.Filter = ck;
643 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
646 if (PV)
647 PV(TU);
649 PrintDiagnostics(TU);
650 clang_disposeTranslationUnit(TU);
651 return 0;
654 int perform_test_load_tu(const char *file, const char *filter,
655 const char *prefix, CXCursorVisitor Visitor,
656 PostVisitTU PV) {
657 CXIndex Idx;
658 CXTranslationUnit TU;
659 int result;
660 Idx = clang_createIndex(/* excludeDeclsFromPCH */
661 !strcmp(filter, "local") ? 1 : 0,
662 /* displayDiagnosics=*/1);
664 if (!CreateTranslationUnit(Idx, file, &TU)) {
665 clang_disposeIndex(Idx);
666 return 1;
669 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
670 clang_disposeIndex(Idx);
671 return result;
674 int perform_test_load_source(int argc, const char **argv,
675 const char *filter, CXCursorVisitor Visitor,
676 PostVisitTU PV) {
677 CXIndex Idx;
678 CXTranslationUnit TU;
679 struct CXUnsavedFile *unsaved_files = 0;
680 int num_unsaved_files = 0;
681 int result;
683 Idx = clang_createIndex(/* excludeDeclsFromPCH */
684 (!strcmp(filter, "local") ||
685 !strcmp(filter, "local-display"))? 1 : 0,
686 /* displayDiagnosics=*/0);
688 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
689 clang_disposeIndex(Idx);
690 return -1;
693 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
694 argc - num_unsaved_files,
695 argv + num_unsaved_files,
696 num_unsaved_files,
697 unsaved_files);
698 if (!TU) {
699 fprintf(stderr, "Unable to load translation unit!\n");
700 free_remapped_files(unsaved_files, num_unsaved_files);
701 clang_disposeIndex(Idx);
702 return 1;
705 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
706 free_remapped_files(unsaved_files, num_unsaved_files);
707 clang_disposeIndex(Idx);
708 return result;
711 int perform_test_reparse_source(int argc, const char **argv, int trials,
712 const char *filter, CXCursorVisitor Visitor,
713 PostVisitTU PV) {
714 CXIndex Idx;
715 CXTranslationUnit TU;
716 struct CXUnsavedFile *unsaved_files = 0;
717 int num_unsaved_files = 0;
718 int result;
719 int trial;
721 Idx = clang_createIndex(/* excludeDeclsFromPCH */
722 !strcmp(filter, "local") ? 1 : 0,
723 /* displayDiagnosics=*/0);
725 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
726 clang_disposeIndex(Idx);
727 return -1;
730 /* Load the initial translation unit -- we do this without honoring remapped
731 * files, so that we have a way to test results after changing the source. */
732 TU = clang_parseTranslationUnit(Idx, 0,
733 argv + num_unsaved_files,
734 argc - num_unsaved_files,
735 0, 0, getDefaultParsingOptions());
736 if (!TU) {
737 fprintf(stderr, "Unable to load translation unit!\n");
738 free_remapped_files(unsaved_files, num_unsaved_files);
739 clang_disposeIndex(Idx);
740 return 1;
743 for (trial = 0; trial < trials; ++trial) {
744 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
745 clang_defaultReparseOptions(TU))) {
746 fprintf(stderr, "Unable to reparse translation unit!\n");
747 clang_disposeTranslationUnit(TU);
748 free_remapped_files(unsaved_files, num_unsaved_files);
749 clang_disposeIndex(Idx);
750 return -1;
754 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
755 free_remapped_files(unsaved_files, num_unsaved_files);
756 clang_disposeIndex(Idx);
757 return result;
760 /******************************************************************************/
761 /* Logic for testing clang_getCursor(). */
762 /******************************************************************************/
764 static void print_cursor_file_scan(CXCursor cursor,
765 unsigned start_line, unsigned start_col,
766 unsigned end_line, unsigned end_col,
767 const char *prefix) {
768 printf("// %s: ", FileCheckPrefix);
769 if (prefix)
770 printf("-%s", prefix);
771 PrintExtent(stdout, start_line, start_col, end_line, end_col);
772 printf(" ");
773 PrintCursor(cursor);
774 printf("\n");
777 static int perform_file_scan(const char *ast_file, const char *source_file,
778 const char *prefix) {
779 CXIndex Idx;
780 CXTranslationUnit TU;
781 FILE *fp;
782 CXCursor prevCursor = clang_getNullCursor();
783 CXFile file;
784 unsigned line = 1, col = 1;
785 unsigned start_line = 1, start_col = 1;
787 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
788 /* displayDiagnosics=*/1))) {
789 fprintf(stderr, "Could not create Index\n");
790 return 1;
793 if (!CreateTranslationUnit(Idx, ast_file, &TU))
794 return 1;
796 if ((fp = fopen(source_file, "r")) == NULL) {
797 fprintf(stderr, "Could not open '%s'\n", source_file);
798 return 1;
801 file = clang_getFile(TU, source_file);
802 for (;;) {
803 CXCursor cursor;
804 int c = fgetc(fp);
806 if (c == '\n') {
807 ++line;
808 col = 1;
809 } else
810 ++col;
812 /* Check the cursor at this position, and dump the previous one if we have
813 * found something new.
815 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
816 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
817 prevCursor.kind != CXCursor_InvalidFile) {
818 print_cursor_file_scan(prevCursor, start_line, start_col,
819 line, col, prefix);
820 start_line = line;
821 start_col = col;
823 if (c == EOF)
824 break;
826 prevCursor = cursor;
829 fclose(fp);
830 clang_disposeTranslationUnit(TU);
831 clang_disposeIndex(Idx);
832 return 0;
835 /******************************************************************************/
836 /* Logic for testing clang code completion. */
837 /******************************************************************************/
839 /* Parse file:line:column from the input string. Returns 0 on success, non-zero
840 on failure. If successful, the pointer *filename will contain newly-allocated
841 memory (that will be owned by the caller) to store the file name. */
842 int parse_file_line_column(const char *input, char **filename, unsigned *line,
843 unsigned *column, unsigned *second_line,
844 unsigned *second_column) {
845 /* Find the second colon. */
846 const char *last_colon = strrchr(input, ':');
847 unsigned values[4], i;
848 unsigned num_values = (second_line && second_column)? 4 : 2;
850 char *endptr = 0;
851 if (!last_colon || last_colon == input) {
852 if (num_values == 4)
853 fprintf(stderr, "could not parse filename:line:column:line:column in "
854 "'%s'\n", input);
855 else
856 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
857 return 1;
860 for (i = 0; i != num_values; ++i) {
861 const char *prev_colon;
863 /* Parse the next line or column. */
864 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
865 if (*endptr != 0 && *endptr != ':') {
866 fprintf(stderr, "could not parse %s in '%s'\n",
867 (i % 2 ? "column" : "line"), input);
868 return 1;
871 if (i + 1 == num_values)
872 break;
874 /* Find the previous colon. */
875 prev_colon = last_colon - 1;
876 while (prev_colon != input && *prev_colon != ':')
877 --prev_colon;
878 if (prev_colon == input) {
879 fprintf(stderr, "could not parse %s in '%s'\n",
880 (i % 2 == 0? "column" : "line"), input);
881 return 1;
884 last_colon = prev_colon;
887 *line = values[0];
888 *column = values[1];
890 if (second_line && second_column) {
891 *second_line = values[2];
892 *second_column = values[3];
895 /* Copy the file name. */
896 *filename = (char*)malloc(last_colon - input + 1);
897 memcpy(*filename, input, last_colon - input);
898 (*filename)[last_colon - input] = 0;
899 return 0;
902 const char *
903 clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
904 switch (Kind) {
905 case CXCompletionChunk_Optional: return "Optional";
906 case CXCompletionChunk_TypedText: return "TypedText";
907 case CXCompletionChunk_Text: return "Text";
908 case CXCompletionChunk_Placeholder: return "Placeholder";
909 case CXCompletionChunk_Informative: return "Informative";
910 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
911 case CXCompletionChunk_LeftParen: return "LeftParen";
912 case CXCompletionChunk_RightParen: return "RightParen";
913 case CXCompletionChunk_LeftBracket: return "LeftBracket";
914 case CXCompletionChunk_RightBracket: return "RightBracket";
915 case CXCompletionChunk_LeftBrace: return "LeftBrace";
916 case CXCompletionChunk_RightBrace: return "RightBrace";
917 case CXCompletionChunk_LeftAngle: return "LeftAngle";
918 case CXCompletionChunk_RightAngle: return "RightAngle";
919 case CXCompletionChunk_Comma: return "Comma";
920 case CXCompletionChunk_ResultType: return "ResultType";
921 case CXCompletionChunk_Colon: return "Colon";
922 case CXCompletionChunk_SemiColon: return "SemiColon";
923 case CXCompletionChunk_Equal: return "Equal";
924 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
925 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
928 return "Unknown";
931 void print_completion_string(CXCompletionString completion_string, FILE *file) {
932 int I, N;
934 N = clang_getNumCompletionChunks(completion_string);
935 for (I = 0; I != N; ++I) {
936 CXString text;
937 const char *cstr;
938 enum CXCompletionChunkKind Kind
939 = clang_getCompletionChunkKind(completion_string, I);
941 if (Kind == CXCompletionChunk_Optional) {
942 fprintf(file, "{Optional ");
943 print_completion_string(
944 clang_getCompletionChunkCompletionString(completion_string, I),
945 file);
946 fprintf(file, "}");
947 continue;
950 if (Kind == CXCompletionChunk_VerticalSpace) {
951 fprintf(file, "{VerticalSpace }");
952 continue;
955 text = clang_getCompletionChunkText(completion_string, I);
956 cstr = clang_getCString(text);
957 fprintf(file, "{%s %s}",
958 clang_getCompletionChunkKindSpelling(Kind),
959 cstr ? cstr : "");
960 clang_disposeString(text);
965 void print_completion_result(CXCompletionResult *completion_result,
966 CXClientData client_data) {
967 FILE *file = (FILE *)client_data;
968 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
970 fprintf(file, "%s:", clang_getCString(ks));
971 clang_disposeString(ks);
973 print_completion_string(completion_result->CompletionString, file);
974 fprintf(file, " (%u)",
975 clang_getCompletionPriority(completion_result->CompletionString));
976 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
977 case CXAvailability_Available:
978 break;
980 case CXAvailability_Deprecated:
981 fprintf(file, " (deprecated)");
982 break;
984 case CXAvailability_NotAvailable:
985 fprintf(file, " (unavailable)");
986 break;
988 fprintf(file, "\n");
991 int my_stricmp(const char *s1, const char *s2) {
992 while (*s1 && *s2) {
993 int c1 = tolower(*s1), c2 = tolower(*s2);
994 if (c1 < c2)
995 return -1;
996 else if (c1 > c2)
997 return 1;
999 ++s1;
1000 ++s2;
1003 if (*s1)
1004 return 1;
1005 else if (*s2)
1006 return -1;
1007 return 0;
1010 int perform_code_completion(int argc, const char **argv, int timing_only) {
1011 const char *input = argv[1];
1012 char *filename = 0;
1013 unsigned line;
1014 unsigned column;
1015 CXIndex CIdx;
1016 int errorCode;
1017 struct CXUnsavedFile *unsaved_files = 0;
1018 int num_unsaved_files = 0;
1019 CXCodeCompleteResults *results = 0;
1020 CXTranslationUnit TU = 0;
1021 unsigned I, Repeats = 1;
1022 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1024 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1025 completionOptions |= CXCodeComplete_IncludeCodePatterns;
1027 if (timing_only)
1028 input += strlen("-code-completion-timing=");
1029 else
1030 input += strlen("-code-completion-at=");
1032 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1033 0, 0)))
1034 return errorCode;
1036 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1037 return -1;
1039 CIdx = clang_createIndex(0, 0);
1041 if (getenv("CINDEXTEST_EDITING"))
1042 Repeats = 5;
1044 TU = clang_parseTranslationUnit(CIdx, 0,
1045 argv + num_unsaved_files + 2,
1046 argc - num_unsaved_files - 2,
1047 0, 0, getDefaultParsingOptions());
1048 if (!TU) {
1049 fprintf(stderr, "Unable to load translation unit!\n");
1050 return 1;
1053 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1054 fprintf(stderr, "Unable to reparse translation init!\n");
1055 return 1;
1058 for (I = 0; I != Repeats; ++I) {
1059 results = clang_codeCompleteAt(TU, filename, line, column,
1060 unsaved_files, num_unsaved_files,
1061 completionOptions);
1062 if (!results) {
1063 fprintf(stderr, "Unable to perform code completion!\n");
1064 return 1;
1066 if (I != Repeats-1)
1067 clang_disposeCodeCompleteResults(results);
1070 if (results) {
1071 unsigned i, n = results->NumResults;
1072 if (!timing_only) {
1073 /* Sort the code-completion results based on the typed text. */
1074 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1076 for (i = 0; i != n; ++i)
1077 print_completion_result(results->Results + i, stdout);
1079 n = clang_codeCompleteGetNumDiagnostics(results);
1080 for (i = 0; i != n; ++i) {
1081 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1082 PrintDiagnostic(diag);
1083 clang_disposeDiagnostic(diag);
1085 clang_disposeCodeCompleteResults(results);
1087 clang_disposeTranslationUnit(TU);
1088 clang_disposeIndex(CIdx);
1089 free(filename);
1091 free_remapped_files(unsaved_files, num_unsaved_files);
1093 return 0;
1096 typedef struct {
1097 char *filename;
1098 unsigned line;
1099 unsigned column;
1100 } CursorSourceLocation;
1102 int inspect_cursor_at(int argc, const char **argv) {
1103 CXIndex CIdx;
1104 int errorCode;
1105 struct CXUnsavedFile *unsaved_files = 0;
1106 int num_unsaved_files = 0;
1107 CXTranslationUnit TU;
1108 CXCursor Cursor;
1109 CursorSourceLocation *Locations = 0;
1110 unsigned NumLocations = 0, Loc;
1111 unsigned Repeats = 1;
1112 unsigned I;
1114 /* Count the number of locations. */
1115 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1116 ++NumLocations;
1118 /* Parse the locations. */
1119 assert(NumLocations > 0 && "Unable to count locations?");
1120 Locations = (CursorSourceLocation *)malloc(
1121 NumLocations * sizeof(CursorSourceLocation));
1122 for (Loc = 0; Loc < NumLocations; ++Loc) {
1123 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
1124 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1125 &Locations[Loc].line,
1126 &Locations[Loc].column, 0, 0)))
1127 return errorCode;
1130 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1131 &num_unsaved_files))
1132 return -1;
1134 if (getenv("CINDEXTEST_EDITING"))
1135 Repeats = 5;
1137 /* Parse the translation unit. When we're testing clang_getCursor() after
1138 reparsing, don't remap unsaved files until the second parse. */
1139 CIdx = clang_createIndex(1, 1);
1140 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1141 argv + num_unsaved_files + 1 + NumLocations,
1142 argc - num_unsaved_files - 2 - NumLocations,
1143 unsaved_files,
1144 Repeats > 1? 0 : num_unsaved_files,
1145 getDefaultParsingOptions());
1147 if (!TU) {
1148 fprintf(stderr, "unable to parse input\n");
1149 return -1;
1152 for (I = 0; I != Repeats; ++I) {
1153 if (Repeats > 1 &&
1154 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1155 clang_defaultReparseOptions(TU))) {
1156 clang_disposeTranslationUnit(TU);
1157 return 1;
1160 for (Loc = 0; Loc < NumLocations; ++Loc) {
1161 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1162 if (!file)
1163 continue;
1165 Cursor = clang_getCursor(TU,
1166 clang_getLocation(TU, file, Locations[Loc].line,
1167 Locations[Loc].column));
1168 if (I + 1 == Repeats) {
1169 PrintCursor(Cursor);
1170 printf("\n");
1171 free(Locations[Loc].filename);
1176 PrintDiagnostics(TU);
1177 clang_disposeTranslationUnit(TU);
1178 clang_disposeIndex(CIdx);
1179 free(Locations);
1180 free_remapped_files(unsaved_files, num_unsaved_files);
1181 return 0;
1184 int perform_token_annotation(int argc, const char **argv) {
1185 const char *input = argv[1];
1186 char *filename = 0;
1187 unsigned line, second_line;
1188 unsigned column, second_column;
1189 CXIndex CIdx;
1190 CXTranslationUnit TU = 0;
1191 int errorCode;
1192 struct CXUnsavedFile *unsaved_files = 0;
1193 int num_unsaved_files = 0;
1194 CXToken *tokens;
1195 unsigned num_tokens;
1196 CXSourceRange range;
1197 CXSourceLocation startLoc, endLoc;
1198 CXFile file = 0;
1199 CXCursor *cursors = 0;
1200 unsigned i;
1202 input += strlen("-test-annotate-tokens=");
1203 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1204 &second_line, &second_column)))
1205 return errorCode;
1207 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1208 return -1;
1210 CIdx = clang_createIndex(0, 1);
1211 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1212 argc - num_unsaved_files - 3,
1213 argv + num_unsaved_files + 2,
1214 num_unsaved_files,
1215 unsaved_files);
1216 if (!TU) {
1217 fprintf(stderr, "unable to parse input\n");
1218 clang_disposeIndex(CIdx);
1219 free(filename);
1220 free_remapped_files(unsaved_files, num_unsaved_files);
1221 return -1;
1223 errorCode = 0;
1225 file = clang_getFile(TU, filename);
1226 if (!file) {
1227 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1228 errorCode = -1;
1229 goto teardown;
1232 startLoc = clang_getLocation(TU, file, line, column);
1233 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
1234 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
1235 column);
1236 errorCode = -1;
1237 goto teardown;
1240 endLoc = clang_getLocation(TU, file, second_line, second_column);
1241 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
1242 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
1243 second_line, second_column);
1244 errorCode = -1;
1245 goto teardown;
1248 range = clang_getRange(startLoc, endLoc);
1249 clang_tokenize(TU, range, &tokens, &num_tokens);
1250 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1251 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1252 for (i = 0; i != num_tokens; ++i) {
1253 const char *kind = "<unknown>";
1254 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1255 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1256 unsigned start_line, start_column, end_line, end_column;
1258 switch (clang_getTokenKind(tokens[i])) {
1259 case CXToken_Punctuation: kind = "Punctuation"; break;
1260 case CXToken_Keyword: kind = "Keyword"; break;
1261 case CXToken_Identifier: kind = "Identifier"; break;
1262 case CXToken_Literal: kind = "Literal"; break;
1263 case CXToken_Comment: kind = "Comment"; break;
1265 clang_getSpellingLocation(clang_getRangeStart(extent),
1266 0, &start_line, &start_column, 0);
1267 clang_getSpellingLocation(clang_getRangeEnd(extent),
1268 0, &end_line, &end_column, 0);
1269 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1270 PrintExtent(stdout, start_line, start_column, end_line, end_column);
1271 if (!clang_isInvalid(cursors[i].kind)) {
1272 printf(" ");
1273 PrintCursor(cursors[i]);
1275 printf("\n");
1277 free(cursors);
1278 clang_disposeTokens(TU, tokens, num_tokens);
1280 teardown:
1281 PrintDiagnostics(TU);
1282 clang_disposeTranslationUnit(TU);
1283 clang_disposeIndex(CIdx);
1284 free(filename);
1285 free_remapped_files(unsaved_files, num_unsaved_files);
1286 return errorCode;
1289 /******************************************************************************/
1290 /* USR printing. */
1291 /******************************************************************************/
1293 static int insufficient_usr(const char *kind, const char *usage) {
1294 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1295 return 1;
1298 static unsigned isUSR(const char *s) {
1299 return s[0] == 'c' && s[1] == ':';
1302 static int not_usr(const char *s, const char *arg) {
1303 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1304 return 1;
1307 static void print_usr(CXString usr) {
1308 const char *s = clang_getCString(usr);
1309 printf("%s\n", s);
1310 clang_disposeString(usr);
1313 static void display_usrs() {
1314 fprintf(stderr, "-print-usrs options:\n"
1315 " ObjCCategory <class name> <category name>\n"
1316 " ObjCClass <class name>\n"
1317 " ObjCIvar <ivar name> <class USR>\n"
1318 " ObjCMethod <selector> [0=class method|1=instance method] "
1319 "<class USR>\n"
1320 " ObjCProperty <property name> <class USR>\n"
1321 " ObjCProtocol <protocol name>\n");
1324 int print_usrs(const char **I, const char **E) {
1325 while (I != E) {
1326 const char *kind = *I;
1327 unsigned len = strlen(kind);
1328 switch (len) {
1329 case 8:
1330 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1331 if (I + 2 >= E)
1332 return insufficient_usr(kind, "<ivar name> <class USR>");
1333 if (!isUSR(I[2]))
1334 return not_usr("<class USR>", I[2]);
1335 else {
1336 CXString x;
1337 x.data = (void*) I[2];
1338 x.private_flags = 0;
1339 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1342 I += 3;
1343 continue;
1345 break;
1346 case 9:
1347 if (memcmp(kind, "ObjCClass", 9) == 0) {
1348 if (I + 1 >= E)
1349 return insufficient_usr(kind, "<class name>");
1350 print_usr(clang_constructUSR_ObjCClass(I[1]));
1351 I += 2;
1352 continue;
1354 break;
1355 case 10:
1356 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1357 if (I + 3 >= E)
1358 return insufficient_usr(kind, "<method selector> "
1359 "[0=class method|1=instance method] <class USR>");
1360 if (!isUSR(I[3]))
1361 return not_usr("<class USR>", I[3]);
1362 else {
1363 CXString x;
1364 x.data = (void*) I[3];
1365 x.private_flags = 0;
1366 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1368 I += 4;
1369 continue;
1371 break;
1372 case 12:
1373 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1374 if (I + 2 >= E)
1375 return insufficient_usr(kind, "<class name> <category name>");
1376 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1377 I += 3;
1378 continue;
1380 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1381 if (I + 1 >= E)
1382 return insufficient_usr(kind, "<protocol name>");
1383 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1384 I += 2;
1385 continue;
1387 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1388 if (I + 2 >= E)
1389 return insufficient_usr(kind, "<property name> <class USR>");
1390 if (!isUSR(I[2]))
1391 return not_usr("<class USR>", I[2]);
1392 else {
1393 CXString x;
1394 x.data = (void*) I[2];
1395 x.private_flags = 0;
1396 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1398 I += 3;
1399 continue;
1401 break;
1402 default:
1403 break;
1405 break;
1408 if (I != E) {
1409 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1410 display_usrs();
1411 return 1;
1413 return 0;
1416 int print_usrs_file(const char *file_name) {
1417 char line[2048];
1418 const char *args[128];
1419 unsigned numChars = 0;
1421 FILE *fp = fopen(file_name, "r");
1422 if (!fp) {
1423 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1424 return 1;
1427 /* This code is not really all that safe, but it works fine for testing. */
1428 while (!feof(fp)) {
1429 char c = fgetc(fp);
1430 if (c == '\n') {
1431 unsigned i = 0;
1432 const char *s = 0;
1434 if (numChars == 0)
1435 continue;
1437 line[numChars] = '\0';
1438 numChars = 0;
1440 if (line[0] == '/' && line[1] == '/')
1441 continue;
1443 s = strtok(line, " ");
1444 while (s) {
1445 args[i] = s;
1446 ++i;
1447 s = strtok(0, " ");
1449 if (print_usrs(&args[0], &args[i]))
1450 return 1;
1452 else
1453 line[numChars++] = c;
1456 fclose(fp);
1457 return 0;
1460 /******************************************************************************/
1461 /* Command line processing. */
1462 /******************************************************************************/
1463 int write_pch_file(const char *filename, int argc, const char *argv[]) {
1464 CXIndex Idx;
1465 CXTranslationUnit TU;
1466 struct CXUnsavedFile *unsaved_files = 0;
1467 int num_unsaved_files = 0;
1469 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1471 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1472 clang_disposeIndex(Idx);
1473 return -1;
1476 TU = clang_parseTranslationUnit(Idx, 0,
1477 argv + num_unsaved_files,
1478 argc - num_unsaved_files,
1479 unsaved_files,
1480 num_unsaved_files,
1481 CXTranslationUnit_Incomplete);
1482 if (!TU) {
1483 fprintf(stderr, "Unable to load translation unit!\n");
1484 free_remapped_files(unsaved_files, num_unsaved_files);
1485 clang_disposeIndex(Idx);
1486 return 1;
1489 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
1490 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1491 clang_disposeTranslationUnit(TU);
1492 free_remapped_files(unsaved_files, num_unsaved_files);
1493 clang_disposeIndex(Idx);
1494 return 0;
1497 /******************************************************************************/
1498 /* Command line processing. */
1499 /******************************************************************************/
1501 static CXCursorVisitor GetVisitor(const char *s) {
1502 if (s[0] == '\0')
1503 return FilteredPrintingVisitor;
1504 if (strcmp(s, "-usrs") == 0)
1505 return USRVisitor;
1506 return NULL;
1509 static void print_usage(void) {
1510 fprintf(stderr,
1511 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
1512 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
1513 " c-index-test -cursor-at=<site> <compiler arguments>\n"
1514 " c-index-test -test-file-scan <AST file> <source file> "
1515 "[FileCheck prefix]\n"
1516 " c-index-test -test-load-tu <AST file> <symbol filter> "
1517 "[FileCheck prefix]\n"
1518 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1519 "[FileCheck prefix]\n"
1520 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
1521 fprintf(stderr,
1522 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1523 " {<args>}*\n"
1524 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
1525 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1526 " c-index-test -test-inclusion-stack-source {<args>}*\n"
1527 " c-index-test -test-inclusion-stack-tu <AST file>\n"
1528 " c-index-test -test-print-linkage-source {<args>}*\n"
1529 " c-index-test -test-print-typekind {<args>}*\n"
1530 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1531 fprintf(stderr,
1532 " c-index-test -print-usr-file <file>\n"
1533 " c-index-test -write-pch <file> <compiler arguments>\n\n");
1534 fprintf(stderr,
1535 " <symbol filter> values:\n%s",
1536 " all - load all symbols, including those from PCH\n"
1537 " local - load all symbols except those in PCH\n"
1538 " category - only load ObjC categories (non-PCH)\n"
1539 " interface - only load ObjC interfaces (non-PCH)\n"
1540 " protocol - only load ObjC protocols (non-PCH)\n"
1541 " function - only load functions (non-PCH)\n"
1542 " typedef - only load typdefs (non-PCH)\n"
1543 " scan-function - scan function bodies (non-PCH)\n\n");
1546 /***/
1548 int cindextest_main(int argc, const char **argv) {
1549 clang_enableStackTraces();
1550 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1551 return perform_code_completion(argc, argv, 0);
1552 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1553 return perform_code_completion(argc, argv, 1);
1554 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1555 return inspect_cursor_at(argc, argv);
1556 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
1557 CXCursorVisitor I = GetVisitor(argv[1] + 13);
1558 if (I)
1559 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1560 NULL);
1562 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1563 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1564 if (I) {
1565 int trials = atoi(argv[2]);
1566 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1567 NULL);
1570 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
1571 CXCursorVisitor I = GetVisitor(argv[1] + 17);
1572 if (I)
1573 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
1575 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
1576 return perform_file_scan(argv[2], argv[3],
1577 argc >= 5 ? argv[4] : 0);
1578 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1579 return perform_token_annotation(argc, argv);
1580 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1581 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1582 PrintInclusionStack);
1583 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1584 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1585 PrintInclusionStack);
1586 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1587 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1588 NULL);
1589 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1590 return perform_test_load_source(argc - 2, argv + 2, "all",
1591 PrintTypeKind, 0);
1592 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1593 if (argc > 2)
1594 return print_usrs(argv + 2, argv + argc);
1595 else {
1596 display_usrs();
1597 return 1;
1600 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1601 return print_usrs_file(argv[2]);
1602 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1603 return write_pch_file(argv[2], argc - 3, argv + 3);
1605 print_usage();
1606 return 1;
1609 /***/
1611 /* We intentionally run in a separate thread to ensure we at least minimal
1612 * testing of a multithreaded environment (for example, having a reduced stack
1613 * size). */
1615 typedef struct thread_info {
1616 int argc;
1617 const char **argv;
1618 int result;
1619 } thread_info;
1620 void thread_runner(void *client_data_v) {
1621 thread_info *client_data = client_data_v;
1622 client_data->result = cindextest_main(client_data->argc, client_data->argv);
1625 int main(int argc, const char **argv) {
1626 thread_info client_data;
1628 if (getenv("CINDEXTEST_NOTHREADS"))
1629 return cindextest_main(argc, argv);
1631 client_data.argc = argc;
1632 client_data.argv = argv;
1633 clang_executeOnThread(thread_runner, &client_data, 0);
1634 return client_data.result;