c-index-test.c revision 4e6a3f7310d3d9232877ed6f439247b1054b1e47
1/* c-index-test.c */ 2 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> 9 10/******************************************************************************/ 11/* Utility functions. */ 12/******************************************************************************/ 13 14#ifdef _MSC_VER 15char *basename(const char* path) 16{ 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); 25 26 return((char*)path); 27} 28#else 29extern char *basename(const char *); 30#endif 31 32/** \brief Return the default parsing options. */ 33static unsigned getDefaultParsingOptions() { 34 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord; 35 36 if (getenv("CINDEXTEST_EDITING")) 37 options |= clang_defaultEditingTranslationUnitOptions(); 38 if (getenv("CINDEXTEST_COMPLETION_CACHING")) 39 options |= CXTranslationUnit_CacheCompletionResults; 40 41 return options; 42} 43 44static 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); 48} 49 50static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, 51 CXTranslationUnit *TU) { 52 53 *TU = clang_createTranslationUnit(Idx, file); 54 if (!*TU) { 55 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); 56 return 0; 57 } 58 return 1; 59} 60 61void 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); 67 } 68 free(unsaved_files); 69} 70 71int 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; 79 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; 84 85 ++*num_unsaved_files; 86 } 87 88 if (*num_unsaved_files == 0) 89 return 0; 90 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; 109 } 110 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; 120 } 121 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); 126 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; 137 } 138 contents[unsaved->Length] = 0; 139 unsaved->Contents = contents; 140 141 /* Close the file. */ 142 fclose(to_file); 143 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; 150 } 151 152 return 0; 153} 154 155/******************************************************************************/ 156/* Pretty-printing. */ 157/******************************************************************************/ 158 159int want_display_name = 0; 160 161static 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); 166 } 167 else { 168 CXString string, ks; 169 CXCursor Referenced; 170 unsigned line, column; 171 CXCursor SpecializationOf; 172 CXCursor *overridden; 173 unsigned num_overridden; 174 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); 182 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(", "); 193 194 Loc = clang_getCursorLocation(Ovl); 195 clang_getSpellingLocation(Loc, 0, &line, &column, 0); 196 printf("%d:%d", line, column); 197 } 198 printf("]"); 199 } else { 200 CXSourceLocation Loc = clang_getCursorLocation(Referenced); 201 clang_getSpellingLocation(Loc, 0, &line, &column, 0); 202 printf(":%d:%d", line, column); 203 } 204 } 205 206 if (clang_isCursorDefinition(Cursor)) 207 printf(" (Definition)"); 208 209 switch (clang_getCursorAvailability(Cursor)) { 210 case CXAvailability_Available: 211 break; 212 213 case CXAvailability_Deprecated: 214 printf(" (deprecated)"); 215 break; 216 217 case CXAvailability_NotAvailable: 218 printf(" (unavailable)"); 219 break; 220 } 221 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); 228 } 229 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; 234 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; 244 } 245 246 printf(" [access=%s isVirtual=%s]", accessStr, 247 isVirtual ? "true" : "false"); 248 } 249 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); 258 } 259 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); 270 } 271 printf("]"); 272 clang_disposeOverriddenCursors(overridden); 273 } 274 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); 280 } 281 } 282} 283 284static 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>"; 293 } 294 else { 295 const char *b = basename(clang_getCString(source)); 296 clang_disposeString(source); 297 return b; 298 } 299} 300 301/******************************************************************************/ 302/* Callbacks. */ 303/******************************************************************************/ 304 305typedef void (*PostVisitTU)(CXTranslationUnit); 306 307void 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; 315 316 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) 317 return; 318 319 Msg = clang_formatDiagnostic(Diagnostic, display_opts); 320 fprintf(stderr, "%s\n", clang_getCString(Msg)); 321 clang_disposeString(Msg); 322 323 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), 324 &file, 0, 0, 0); 325 if (!file) 326 return; 327 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"); 350 } 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)); 357 } 358 break; 359 } 360 clang_disposeString(insertion_text); 361 } 362} 363 364void 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); 370 } 371} 372 373void PrintMemoryUsage(CXTranslationUnit TU) { 374 unsigned long total = 0.0; 375 unsigned i = 0; 376 CXTUMemoryUsage usage = clang_getCXTUMemoryUsage(TU); 377 fprintf(stderr, "Memory usage:\n"); 378 for (i = 0 ; i != usage.numEntries; ++i) { 379 const char *name = clang_getTUMemoryUsageName(usage.entries[i].kind); 380 unsigned long amount = usage.entries[i].amount; 381 total += amount; 382 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount, 383 ((double) amount)/(1024*1024)); 384 } 385 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total, 386 ((double) total)/(1024*1024)); 387 clang_disposeCXTUMemoryUsage(usage); 388} 389 390/******************************************************************************/ 391/* Logic for testing traversal. */ 392/******************************************************************************/ 393 394static const char *FileCheckPrefix = "CHECK"; 395 396static void PrintCursorExtent(CXCursor C) { 397 CXSourceRange extent = clang_getCursorExtent(C); 398 CXFile begin_file, end_file; 399 unsigned begin_line, begin_column, end_line, end_column; 400 401 clang_getSpellingLocation(clang_getRangeStart(extent), 402 &begin_file, &begin_line, &begin_column, 0); 403 clang_getSpellingLocation(clang_getRangeEnd(extent), 404 &end_file, &end_line, &end_column, 0); 405 if (!begin_file || !end_file) 406 return; 407 408 printf(" Extent="); 409 PrintExtent(stdout, begin_line, begin_column, end_line, end_column); 410} 411 412/* Data used by all of the visitors. */ 413typedef struct { 414 CXTranslationUnit TU; 415 enum CXCursorKind *Filter; 416} VisitorData; 417 418 419enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, 420 CXCursor Parent, 421 CXClientData ClientData) { 422 VisitorData *Data = (VisitorData *)ClientData; 423 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { 424 CXSourceLocation Loc = clang_getCursorLocation(Cursor); 425 unsigned line, column; 426 clang_getSpellingLocation(Loc, 0, &line, &column, 0); 427 printf("// %s: %s:%d:%d: ", FileCheckPrefix, 428 GetCursorSource(Cursor), line, column); 429 PrintCursor(Cursor); 430 PrintCursorExtent(Cursor); 431 printf("\n"); 432 return CXChildVisit_Recurse; 433 } 434 435 return CXChildVisit_Continue; 436} 437 438static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, 439 CXCursor Parent, 440 CXClientData ClientData) { 441 const char *startBuf, *endBuf; 442 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; 443 CXCursor Ref; 444 VisitorData *Data = (VisitorData *)ClientData; 445 446 if (Cursor.kind != CXCursor_FunctionDecl || 447 !clang_isCursorDefinition(Cursor)) 448 return CXChildVisit_Continue; 449 450 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, 451 &startLine, &startColumn, 452 &endLine, &endColumn); 453 /* Probe the entire body, looking for both decls and refs. */ 454 curLine = startLine; 455 curColumn = startColumn; 456 457 while (startBuf < endBuf) { 458 CXSourceLocation Loc; 459 CXFile file; 460 CXString source; 461 462 if (*startBuf == '\n') { 463 startBuf++; 464 curLine++; 465 curColumn = 1; 466 } else if (*startBuf != '\t') 467 curColumn++; 468 469 Loc = clang_getCursorLocation(Cursor); 470 clang_getSpellingLocation(Loc, &file, 0, 0, 0); 471 472 source = clang_getFileName(file); 473 if (clang_getCString(source)) { 474 CXSourceLocation RefLoc 475 = clang_getLocation(Data->TU, file, curLine, curColumn); 476 Ref = clang_getCursor(Data->TU, RefLoc); 477 if (Ref.kind == CXCursor_NoDeclFound) { 478 /* Nothing found here; that's fine. */ 479 } else if (Ref.kind != CXCursor_FunctionDecl) { 480 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), 481 curLine, curColumn); 482 PrintCursor(Ref); 483 printf("\n"); 484 } 485 } 486 clang_disposeString(source); 487 startBuf++; 488 } 489 490 return CXChildVisit_Continue; 491} 492 493/******************************************************************************/ 494/* USR testing. */ 495/******************************************************************************/ 496 497enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, 498 CXClientData ClientData) { 499 VisitorData *Data = (VisitorData *)ClientData; 500 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) { 501 CXString USR = clang_getCursorUSR(C); 502 const char *cstr = clang_getCString(USR); 503 if (!cstr || cstr[0] == '\0') { 504 clang_disposeString(USR); 505 return CXChildVisit_Recurse; 506 } 507 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr); 508 509 PrintCursorExtent(C); 510 printf("\n"); 511 clang_disposeString(USR); 512 513 return CXChildVisit_Recurse; 514 } 515 516 return CXChildVisit_Continue; 517} 518 519/******************************************************************************/ 520/* Inclusion stack testing. */ 521/******************************************************************************/ 522 523void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, 524 unsigned includeStackLen, CXClientData data) { 525 526 unsigned i; 527 CXString fname; 528 529 fname = clang_getFileName(includedFile); 530 printf("file: %s\nincluded by:\n", clang_getCString(fname)); 531 clang_disposeString(fname); 532 533 for (i = 0; i < includeStackLen; ++i) { 534 CXFile includingFile; 535 unsigned line, column; 536 clang_getSpellingLocation(includeStack[i], &includingFile, &line, 537 &column, 0); 538 fname = clang_getFileName(includingFile); 539 printf(" %s:%d:%d\n", clang_getCString(fname), line, column); 540 clang_disposeString(fname); 541 } 542 printf("\n"); 543} 544 545void PrintInclusionStack(CXTranslationUnit TU) { 546 clang_getInclusions(TU, InclusionVisitor, NULL); 547} 548 549/******************************************************************************/ 550/* Linkage testing. */ 551/******************************************************************************/ 552 553static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, 554 CXClientData d) { 555 const char *linkage = 0; 556 557 if (clang_isInvalid(clang_getCursorKind(cursor))) 558 return CXChildVisit_Recurse; 559 560 switch (clang_getCursorLinkage(cursor)) { 561 case CXLinkage_Invalid: break; 562 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break; 563 case CXLinkage_Internal: linkage = "Internal"; break; 564 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break; 565 case CXLinkage_External: linkage = "External"; break; 566 } 567 568 if (linkage) { 569 PrintCursor(cursor); 570 printf("linkage=%s\n", linkage); 571 } 572 573 return CXChildVisit_Recurse; 574} 575 576/******************************************************************************/ 577/* Typekind testing. */ 578/******************************************************************************/ 579 580static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p, 581 CXClientData d) { 582 583 if (!clang_isInvalid(clang_getCursorKind(cursor))) { 584 CXType T = clang_getCursorType(cursor); 585 CXString S = clang_getTypeKindSpelling(T.kind); 586 PrintCursor(cursor); 587 printf(" typekind=%s", clang_getCString(S)); 588 if (clang_isConstQualifiedType(T)) 589 printf(" const"); 590 if (clang_isVolatileQualifiedType(T)) 591 printf(" volatile"); 592 if (clang_isRestrictQualifiedType(T)) 593 printf(" restrict"); 594 clang_disposeString(S); 595 /* Print the canonical type if it is different. */ 596 { 597 CXType CT = clang_getCanonicalType(T); 598 if (!clang_equalTypes(T, CT)) { 599 CXString CS = clang_getTypeKindSpelling(CT.kind); 600 printf(" [canonical=%s]", clang_getCString(CS)); 601 clang_disposeString(CS); 602 } 603 } 604 /* Print the return type if it exists. */ 605 { 606 CXType RT = clang_getCursorResultType(cursor); 607 if (RT.kind != CXType_Invalid) { 608 CXString RS = clang_getTypeKindSpelling(RT.kind); 609 printf(" [result=%s]", clang_getCString(RS)); 610 clang_disposeString(RS); 611 } 612 } 613 /* Print if this is a non-POD type. */ 614 printf(" [isPOD=%d]", clang_isPODType(T)); 615 616 printf("\n"); 617 } 618 return CXChildVisit_Recurse; 619} 620 621 622/******************************************************************************/ 623/* Loading ASTs/source. */ 624/******************************************************************************/ 625 626static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, 627 const char *filter, const char *prefix, 628 CXCursorVisitor Visitor, 629 PostVisitTU PV) { 630 631 if (prefix) 632 FileCheckPrefix = prefix; 633 634 if (Visitor) { 635 enum CXCursorKind K = CXCursor_NotImplemented; 636 enum CXCursorKind *ck = &K; 637 VisitorData Data; 638 639 /* Perform some simple filtering. */ 640 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; 641 else if (!strcmp(filter, "all-display") || 642 !strcmp(filter, "local-display")) { 643 ck = NULL; 644 want_display_name = 1; 645 } 646 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; 647 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; 648 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; 649 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; 650 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; 651 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; 652 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; 653 else { 654 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); 655 return 1; 656 } 657 658 Data.TU = TU; 659 Data.Filter = ck; 660 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); 661 } 662 663 if (PV) 664 PV(TU); 665 666 PrintDiagnostics(TU); 667 clang_disposeTranslationUnit(TU); 668 return 0; 669} 670 671int perform_test_load_tu(const char *file, const char *filter, 672 const char *prefix, CXCursorVisitor Visitor, 673 PostVisitTU PV) { 674 CXIndex Idx; 675 CXTranslationUnit TU; 676 int result; 677 Idx = clang_createIndex(/* excludeDeclsFromPCH */ 678 !strcmp(filter, "local") ? 1 : 0, 679 /* displayDiagnosics=*/1); 680 681 if (!CreateTranslationUnit(Idx, file, &TU)) { 682 clang_disposeIndex(Idx); 683 return 1; 684 } 685 686 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV); 687 clang_disposeIndex(Idx); 688 return result; 689} 690 691int perform_test_load_source(int argc, const char **argv, 692 const char *filter, CXCursorVisitor Visitor, 693 PostVisitTU PV) { 694 CXIndex Idx; 695 CXTranslationUnit TU; 696 struct CXUnsavedFile *unsaved_files = 0; 697 int num_unsaved_files = 0; 698 int result; 699 700 Idx = clang_createIndex(/* excludeDeclsFromPCH */ 701 (!strcmp(filter, "local") || 702 !strcmp(filter, "local-display"))? 1 : 0, 703 /* displayDiagnosics=*/0); 704 705 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { 706 clang_disposeIndex(Idx); 707 return -1; 708 } 709 710 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, 711 argc - num_unsaved_files, 712 argv + num_unsaved_files, 713 num_unsaved_files, 714 unsaved_files); 715 if (!TU) { 716 fprintf(stderr, "Unable to load translation unit!\n"); 717 free_remapped_files(unsaved_files, num_unsaved_files); 718 clang_disposeIndex(Idx); 719 return 1; 720 } 721 722 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); 723 free_remapped_files(unsaved_files, num_unsaved_files); 724 clang_disposeIndex(Idx); 725 return result; 726} 727 728int perform_test_reparse_source(int argc, const char **argv, int trials, 729 const char *filter, CXCursorVisitor Visitor, 730 PostVisitTU PV) { 731 CXIndex Idx; 732 CXTranslationUnit TU; 733 struct CXUnsavedFile *unsaved_files = 0; 734 int num_unsaved_files = 0; 735 int result; 736 int trial; 737 738 Idx = clang_createIndex(/* excludeDeclsFromPCH */ 739 !strcmp(filter, "local") ? 1 : 0, 740 /* displayDiagnosics=*/0); 741 742 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { 743 clang_disposeIndex(Idx); 744 return -1; 745 } 746 747 /* Load the initial translation unit -- we do this without honoring remapped 748 * files, so that we have a way to test results after changing the source. */ 749 TU = clang_parseTranslationUnit(Idx, 0, 750 argv + num_unsaved_files, 751 argc - num_unsaved_files, 752 0, 0, getDefaultParsingOptions()); 753 if (!TU) { 754 fprintf(stderr, "Unable to load translation unit!\n"); 755 free_remapped_files(unsaved_files, num_unsaved_files); 756 clang_disposeIndex(Idx); 757 return 1; 758 } 759 760 for (trial = 0; trial < trials; ++trial) { 761 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, 762 clang_defaultReparseOptions(TU))) { 763 fprintf(stderr, "Unable to reparse translation unit!\n"); 764 clang_disposeTranslationUnit(TU); 765 free_remapped_files(unsaved_files, num_unsaved_files); 766 clang_disposeIndex(Idx); 767 return -1; 768 } 769 } 770 771 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); 772 free_remapped_files(unsaved_files, num_unsaved_files); 773 clang_disposeIndex(Idx); 774 return result; 775} 776 777/******************************************************************************/ 778/* Logic for testing clang_getCursor(). */ 779/******************************************************************************/ 780 781static void print_cursor_file_scan(CXCursor cursor, 782 unsigned start_line, unsigned start_col, 783 unsigned end_line, unsigned end_col, 784 const char *prefix) { 785 printf("// %s: ", FileCheckPrefix); 786 if (prefix) 787 printf("-%s", prefix); 788 PrintExtent(stdout, start_line, start_col, end_line, end_col); 789 printf(" "); 790 PrintCursor(cursor); 791 printf("\n"); 792} 793 794static int perform_file_scan(const char *ast_file, const char *source_file, 795 const char *prefix) { 796 CXIndex Idx; 797 CXTranslationUnit TU; 798 FILE *fp; 799 CXCursor prevCursor = clang_getNullCursor(); 800 CXFile file; 801 unsigned line = 1, col = 1; 802 unsigned start_line = 1, start_col = 1; 803 804 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, 805 /* displayDiagnosics=*/1))) { 806 fprintf(stderr, "Could not create Index\n"); 807 return 1; 808 } 809 810 if (!CreateTranslationUnit(Idx, ast_file, &TU)) 811 return 1; 812 813 if ((fp = fopen(source_file, "r")) == NULL) { 814 fprintf(stderr, "Could not open '%s'\n", source_file); 815 return 1; 816 } 817 818 file = clang_getFile(TU, source_file); 819 for (;;) { 820 CXCursor cursor; 821 int c = fgetc(fp); 822 823 if (c == '\n') { 824 ++line; 825 col = 1; 826 } else 827 ++col; 828 829 /* Check the cursor at this position, and dump the previous one if we have 830 * found something new. 831 */ 832 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); 833 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && 834 prevCursor.kind != CXCursor_InvalidFile) { 835 print_cursor_file_scan(prevCursor, start_line, start_col, 836 line, col, prefix); 837 start_line = line; 838 start_col = col; 839 } 840 if (c == EOF) 841 break; 842 843 prevCursor = cursor; 844 } 845 846 fclose(fp); 847 clang_disposeTranslationUnit(TU); 848 clang_disposeIndex(Idx); 849 return 0; 850} 851 852/******************************************************************************/ 853/* Logic for testing clang code completion. */ 854/******************************************************************************/ 855 856/* Parse file:line:column from the input string. Returns 0 on success, non-zero 857 on failure. If successful, the pointer *filename will contain newly-allocated 858 memory (that will be owned by the caller) to store the file name. */ 859int parse_file_line_column(const char *input, char **filename, unsigned *line, 860 unsigned *column, unsigned *second_line, 861 unsigned *second_column) { 862 /* Find the second colon. */ 863 const char *last_colon = strrchr(input, ':'); 864 unsigned values[4], i; 865 unsigned num_values = (second_line && second_column)? 4 : 2; 866 867 char *endptr = 0; 868 if (!last_colon || last_colon == input) { 869 if (num_values == 4) 870 fprintf(stderr, "could not parse filename:line:column:line:column in " 871 "'%s'\n", input); 872 else 873 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); 874 return 1; 875 } 876 877 for (i = 0; i != num_values; ++i) { 878 const char *prev_colon; 879 880 /* Parse the next line or column. */ 881 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); 882 if (*endptr != 0 && *endptr != ':') { 883 fprintf(stderr, "could not parse %s in '%s'\n", 884 (i % 2 ? "column" : "line"), input); 885 return 1; 886 } 887 888 if (i + 1 == num_values) 889 break; 890 891 /* Find the previous colon. */ 892 prev_colon = last_colon - 1; 893 while (prev_colon != input && *prev_colon != ':') 894 --prev_colon; 895 if (prev_colon == input) { 896 fprintf(stderr, "could not parse %s in '%s'\n", 897 (i % 2 == 0? "column" : "line"), input); 898 return 1; 899 } 900 901 last_colon = prev_colon; 902 } 903 904 *line = values[0]; 905 *column = values[1]; 906 907 if (second_line && second_column) { 908 *second_line = values[2]; 909 *second_column = values[3]; 910 } 911 912 /* Copy the file name. */ 913 *filename = (char*)malloc(last_colon - input + 1); 914 memcpy(*filename, input, last_colon - input); 915 (*filename)[last_colon - input] = 0; 916 return 0; 917} 918 919const char * 920clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { 921 switch (Kind) { 922 case CXCompletionChunk_Optional: return "Optional"; 923 case CXCompletionChunk_TypedText: return "TypedText"; 924 case CXCompletionChunk_Text: return "Text"; 925 case CXCompletionChunk_Placeholder: return "Placeholder"; 926 case CXCompletionChunk_Informative: return "Informative"; 927 case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; 928 case CXCompletionChunk_LeftParen: return "LeftParen"; 929 case CXCompletionChunk_RightParen: return "RightParen"; 930 case CXCompletionChunk_LeftBracket: return "LeftBracket"; 931 case CXCompletionChunk_RightBracket: return "RightBracket"; 932 case CXCompletionChunk_LeftBrace: return "LeftBrace"; 933 case CXCompletionChunk_RightBrace: return "RightBrace"; 934 case CXCompletionChunk_LeftAngle: return "LeftAngle"; 935 case CXCompletionChunk_RightAngle: return "RightAngle"; 936 case CXCompletionChunk_Comma: return "Comma"; 937 case CXCompletionChunk_ResultType: return "ResultType"; 938 case CXCompletionChunk_Colon: return "Colon"; 939 case CXCompletionChunk_SemiColon: return "SemiColon"; 940 case CXCompletionChunk_Equal: return "Equal"; 941 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; 942 case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; 943 } 944 945 return "Unknown"; 946} 947 948void print_completion_string(CXCompletionString completion_string, FILE *file) { 949 int I, N; 950 951 N = clang_getNumCompletionChunks(completion_string); 952 for (I = 0; I != N; ++I) { 953 CXString text; 954 const char *cstr; 955 enum CXCompletionChunkKind Kind 956 = clang_getCompletionChunkKind(completion_string, I); 957 958 if (Kind == CXCompletionChunk_Optional) { 959 fprintf(file, "{Optional "); 960 print_completion_string( 961 clang_getCompletionChunkCompletionString(completion_string, I), 962 file); 963 fprintf(file, "}"); 964 continue; 965 } 966 967 if (Kind == CXCompletionChunk_VerticalSpace) { 968 fprintf(file, "{VerticalSpace }"); 969 continue; 970 } 971 972 text = clang_getCompletionChunkText(completion_string, I); 973 cstr = clang_getCString(text); 974 fprintf(file, "{%s %s}", 975 clang_getCompletionChunkKindSpelling(Kind), 976 cstr ? cstr : ""); 977 clang_disposeString(text); 978 } 979 980} 981 982void print_completion_result(CXCompletionResult *completion_result, 983 CXClientData client_data) { 984 FILE *file = (FILE *)client_data; 985 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); 986 987 fprintf(file, "%s:", clang_getCString(ks)); 988 clang_disposeString(ks); 989 990 print_completion_string(completion_result->CompletionString, file); 991 fprintf(file, " (%u)", 992 clang_getCompletionPriority(completion_result->CompletionString)); 993 switch (clang_getCompletionAvailability(completion_result->CompletionString)){ 994 case CXAvailability_Available: 995 break; 996 997 case CXAvailability_Deprecated: 998 fprintf(file, " (deprecated)"); 999 break; 1000 1001 case CXAvailability_NotAvailable: 1002 fprintf(file, " (unavailable)"); 1003 break; 1004 } 1005 fprintf(file, "\n"); 1006} 1007 1008int my_stricmp(const char *s1, const char *s2) { 1009 while (*s1 && *s2) { 1010 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2); 1011 if (c1 < c2) 1012 return -1; 1013 else if (c1 > c2) 1014 return 1; 1015 1016 ++s1; 1017 ++s2; 1018 } 1019 1020 if (*s1) 1021 return 1; 1022 else if (*s2) 1023 return -1; 1024 return 0; 1025} 1026 1027int perform_code_completion(int argc, const char **argv, int timing_only) { 1028 const char *input = argv[1]; 1029 char *filename = 0; 1030 unsigned line; 1031 unsigned column; 1032 CXIndex CIdx; 1033 int errorCode; 1034 struct CXUnsavedFile *unsaved_files = 0; 1035 int num_unsaved_files = 0; 1036 CXCodeCompleteResults *results = 0; 1037 CXTranslationUnit TU = 0; 1038 unsigned I, Repeats = 1; 1039 unsigned completionOptions = clang_defaultCodeCompleteOptions(); 1040 1041 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS")) 1042 completionOptions |= CXCodeComplete_IncludeCodePatterns; 1043 1044 if (timing_only) 1045 input += strlen("-code-completion-timing="); 1046 else 1047 input += strlen("-code-completion-at="); 1048 1049 if ((errorCode = parse_file_line_column(input, &filename, &line, &column, 1050 0, 0))) 1051 return errorCode; 1052 1053 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) 1054 return -1; 1055 1056 CIdx = clang_createIndex(0, 0); 1057 1058 if (getenv("CINDEXTEST_EDITING")) 1059 Repeats = 5; 1060 1061 TU = clang_parseTranslationUnit(CIdx, 0, 1062 argv + num_unsaved_files + 2, 1063 argc - num_unsaved_files - 2, 1064 0, 0, getDefaultParsingOptions()); 1065 if (!TU) { 1066 fprintf(stderr, "Unable to load translation unit!\n"); 1067 return 1; 1068 } 1069 1070 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) { 1071 fprintf(stderr, "Unable to reparse translation init!\n"); 1072 return 1; 1073 } 1074 1075 for (I = 0; I != Repeats; ++I) { 1076 results = clang_codeCompleteAt(TU, filename, line, column, 1077 unsaved_files, num_unsaved_files, 1078 completionOptions); 1079 if (!results) { 1080 fprintf(stderr, "Unable to perform code completion!\n"); 1081 return 1; 1082 } 1083 if (I != Repeats-1) 1084 clang_disposeCodeCompleteResults(results); 1085 } 1086 1087 if (results) { 1088 unsigned i, n = results->NumResults; 1089 if (!timing_only) { 1090 /* Sort the code-completion results based on the typed text. */ 1091 clang_sortCodeCompletionResults(results->Results, results->NumResults); 1092 1093 for (i = 0; i != n; ++i) 1094 print_completion_result(results->Results + i, stdout); 1095 } 1096 n = clang_codeCompleteGetNumDiagnostics(results); 1097 for (i = 0; i != n; ++i) { 1098 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); 1099 PrintDiagnostic(diag); 1100 clang_disposeDiagnostic(diag); 1101 } 1102 clang_disposeCodeCompleteResults(results); 1103 } 1104 clang_disposeTranslationUnit(TU); 1105 clang_disposeIndex(CIdx); 1106 free(filename); 1107 1108 free_remapped_files(unsaved_files, num_unsaved_files); 1109 1110 return 0; 1111} 1112 1113typedef struct { 1114 char *filename; 1115 unsigned line; 1116 unsigned column; 1117} CursorSourceLocation; 1118 1119int inspect_cursor_at(int argc, const char **argv) { 1120 CXIndex CIdx; 1121 int errorCode; 1122 struct CXUnsavedFile *unsaved_files = 0; 1123 int num_unsaved_files = 0; 1124 CXTranslationUnit TU; 1125 CXCursor Cursor; 1126 CursorSourceLocation *Locations = 0; 1127 unsigned NumLocations = 0, Loc; 1128 unsigned Repeats = 1; 1129 unsigned I; 1130 1131 /* Count the number of locations. */ 1132 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) 1133 ++NumLocations; 1134 1135 /* Parse the locations. */ 1136 assert(NumLocations > 0 && "Unable to count locations?"); 1137 Locations = (CursorSourceLocation *)malloc( 1138 NumLocations * sizeof(CursorSourceLocation)); 1139 for (Loc = 0; Loc < NumLocations; ++Loc) { 1140 const char *input = argv[Loc + 1] + strlen("-cursor-at="); 1141 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, 1142 &Locations[Loc].line, 1143 &Locations[Loc].column, 0, 0))) 1144 return errorCode; 1145 } 1146 1147 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, 1148 &num_unsaved_files)) 1149 return -1; 1150 1151 if (getenv("CINDEXTEST_EDITING")) 1152 Repeats = 5; 1153 1154 /* Parse the translation unit. When we're testing clang_getCursor() after 1155 reparsing, don't remap unsaved files until the second parse. */ 1156 CIdx = clang_createIndex(1, 1); 1157 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1], 1158 argv + num_unsaved_files + 1 + NumLocations, 1159 argc - num_unsaved_files - 2 - NumLocations, 1160 unsaved_files, 1161 Repeats > 1? 0 : num_unsaved_files, 1162 getDefaultParsingOptions()); 1163 1164 if (!TU) { 1165 fprintf(stderr, "unable to parse input\n"); 1166 return -1; 1167 } 1168 1169 for (I = 0; I != Repeats; ++I) { 1170 if (Repeats > 1 && 1171 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, 1172 clang_defaultReparseOptions(TU))) { 1173 clang_disposeTranslationUnit(TU); 1174 return 1; 1175 } 1176 1177 for (Loc = 0; Loc < NumLocations; ++Loc) { 1178 CXFile file = clang_getFile(TU, Locations[Loc].filename); 1179 if (!file) 1180 continue; 1181 1182 Cursor = clang_getCursor(TU, 1183 clang_getLocation(TU, file, Locations[Loc].line, 1184 Locations[Loc].column)); 1185 if (I + 1 == Repeats) { 1186 PrintCursor(Cursor); 1187 printf("\n"); 1188 free(Locations[Loc].filename); 1189 } 1190 } 1191 } 1192 1193 PrintDiagnostics(TU); 1194 clang_disposeTranslationUnit(TU); 1195 clang_disposeIndex(CIdx); 1196 free(Locations); 1197 free_remapped_files(unsaved_files, num_unsaved_files); 1198 return 0; 1199} 1200 1201int perform_token_annotation(int argc, const char **argv) { 1202 const char *input = argv[1]; 1203 char *filename = 0; 1204 unsigned line, second_line; 1205 unsigned column, second_column; 1206 CXIndex CIdx; 1207 CXTranslationUnit TU = 0; 1208 int errorCode; 1209 struct CXUnsavedFile *unsaved_files = 0; 1210 int num_unsaved_files = 0; 1211 CXToken *tokens; 1212 unsigned num_tokens; 1213 CXSourceRange range; 1214 CXSourceLocation startLoc, endLoc; 1215 CXFile file = 0; 1216 CXCursor *cursors = 0; 1217 unsigned i; 1218 1219 input += strlen("-test-annotate-tokens="); 1220 if ((errorCode = parse_file_line_column(input, &filename, &line, &column, 1221 &second_line, &second_column))) 1222 return errorCode; 1223 1224 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) 1225 return -1; 1226 1227 CIdx = clang_createIndex(0, 1); 1228 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], 1229 argc - num_unsaved_files - 3, 1230 argv + num_unsaved_files + 2, 1231 num_unsaved_files, 1232 unsaved_files); 1233 if (!TU) { 1234 fprintf(stderr, "unable to parse input\n"); 1235 clang_disposeIndex(CIdx); 1236 free(filename); 1237 free_remapped_files(unsaved_files, num_unsaved_files); 1238 return -1; 1239 } 1240 errorCode = 0; 1241 1242 file = clang_getFile(TU, filename); 1243 if (!file) { 1244 fprintf(stderr, "file %s is not in this translation unit\n", filename); 1245 errorCode = -1; 1246 goto teardown; 1247 } 1248 1249 startLoc = clang_getLocation(TU, file, line, column); 1250 if (clang_equalLocations(clang_getNullLocation(), startLoc)) { 1251 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, 1252 column); 1253 errorCode = -1; 1254 goto teardown; 1255 } 1256 1257 endLoc = clang_getLocation(TU, file, second_line, second_column); 1258 if (clang_equalLocations(clang_getNullLocation(), endLoc)) { 1259 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, 1260 second_line, second_column); 1261 errorCode = -1; 1262 goto teardown; 1263 } 1264 1265 range = clang_getRange(startLoc, endLoc); 1266 clang_tokenize(TU, range, &tokens, &num_tokens); 1267 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); 1268 clang_annotateTokens(TU, tokens, num_tokens, cursors); 1269 for (i = 0; i != num_tokens; ++i) { 1270 const char *kind = "<unknown>"; 1271 CXString spelling = clang_getTokenSpelling(TU, tokens[i]); 1272 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); 1273 unsigned start_line, start_column, end_line, end_column; 1274 1275 switch (clang_getTokenKind(tokens[i])) { 1276 case CXToken_Punctuation: kind = "Punctuation"; break; 1277 case CXToken_Keyword: kind = "Keyword"; break; 1278 case CXToken_Identifier: kind = "Identifier"; break; 1279 case CXToken_Literal: kind = "Literal"; break; 1280 case CXToken_Comment: kind = "Comment"; break; 1281 } 1282 clang_getSpellingLocation(clang_getRangeStart(extent), 1283 0, &start_line, &start_column, 0); 1284 clang_getSpellingLocation(clang_getRangeEnd(extent), 1285 0, &end_line, &end_column, 0); 1286 printf("%s: \"%s\" ", kind, clang_getCString(spelling)); 1287 PrintExtent(stdout, start_line, start_column, end_line, end_column); 1288 if (!clang_isInvalid(cursors[i].kind)) { 1289 printf(" "); 1290 PrintCursor(cursors[i]); 1291 } 1292 printf("\n"); 1293 } 1294 free(cursors); 1295 clang_disposeTokens(TU, tokens, num_tokens); 1296 1297 teardown: 1298 PrintDiagnostics(TU); 1299 clang_disposeTranslationUnit(TU); 1300 clang_disposeIndex(CIdx); 1301 free(filename); 1302 free_remapped_files(unsaved_files, num_unsaved_files); 1303 return errorCode; 1304} 1305 1306/******************************************************************************/ 1307/* USR printing. */ 1308/******************************************************************************/ 1309 1310static int insufficient_usr(const char *kind, const char *usage) { 1311 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage); 1312 return 1; 1313} 1314 1315static unsigned isUSR(const char *s) { 1316 return s[0] == 'c' && s[1] == ':'; 1317} 1318 1319static int not_usr(const char *s, const char *arg) { 1320 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg); 1321 return 1; 1322} 1323 1324static void print_usr(CXString usr) { 1325 const char *s = clang_getCString(usr); 1326 printf("%s\n", s); 1327 clang_disposeString(usr); 1328} 1329 1330static void display_usrs() { 1331 fprintf(stderr, "-print-usrs options:\n" 1332 " ObjCCategory <class name> <category name>\n" 1333 " ObjCClass <class name>\n" 1334 " ObjCIvar <ivar name> <class USR>\n" 1335 " ObjCMethod <selector> [0=class method|1=instance method] " 1336 "<class USR>\n" 1337 " ObjCProperty <property name> <class USR>\n" 1338 " ObjCProtocol <protocol name>\n"); 1339} 1340 1341int print_usrs(const char **I, const char **E) { 1342 while (I != E) { 1343 const char *kind = *I; 1344 unsigned len = strlen(kind); 1345 switch (len) { 1346 case 8: 1347 if (memcmp(kind, "ObjCIvar", 8) == 0) { 1348 if (I + 2 >= E) 1349 return insufficient_usr(kind, "<ivar name> <class USR>"); 1350 if (!isUSR(I[2])) 1351 return not_usr("<class USR>", I[2]); 1352 else { 1353 CXString x; 1354 x.data = (void*) I[2]; 1355 x.private_flags = 0; 1356 print_usr(clang_constructUSR_ObjCIvar(I[1], x)); 1357 } 1358 1359 I += 3; 1360 continue; 1361 } 1362 break; 1363 case 9: 1364 if (memcmp(kind, "ObjCClass", 9) == 0) { 1365 if (I + 1 >= E) 1366 return insufficient_usr(kind, "<class name>"); 1367 print_usr(clang_constructUSR_ObjCClass(I[1])); 1368 I += 2; 1369 continue; 1370 } 1371 break; 1372 case 10: 1373 if (memcmp(kind, "ObjCMethod", 10) == 0) { 1374 if (I + 3 >= E) 1375 return insufficient_usr(kind, "<method selector> " 1376 "[0=class method|1=instance method] <class USR>"); 1377 if (!isUSR(I[3])) 1378 return not_usr("<class USR>", I[3]); 1379 else { 1380 CXString x; 1381 x.data = (void*) I[3]; 1382 x.private_flags = 0; 1383 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x)); 1384 } 1385 I += 4; 1386 continue; 1387 } 1388 break; 1389 case 12: 1390 if (memcmp(kind, "ObjCCategory", 12) == 0) { 1391 if (I + 2 >= E) 1392 return insufficient_usr(kind, "<class name> <category name>"); 1393 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2])); 1394 I += 3; 1395 continue; 1396 } 1397 if (memcmp(kind, "ObjCProtocol", 12) == 0) { 1398 if (I + 1 >= E) 1399 return insufficient_usr(kind, "<protocol name>"); 1400 print_usr(clang_constructUSR_ObjCProtocol(I[1])); 1401 I += 2; 1402 continue; 1403 } 1404 if (memcmp(kind, "ObjCProperty", 12) == 0) { 1405 if (I + 2 >= E) 1406 return insufficient_usr(kind, "<property name> <class USR>"); 1407 if (!isUSR(I[2])) 1408 return not_usr("<class USR>", I[2]); 1409 else { 1410 CXString x; 1411 x.data = (void*) I[2]; 1412 x.private_flags = 0; 1413 print_usr(clang_constructUSR_ObjCProperty(I[1], x)); 1414 } 1415 I += 3; 1416 continue; 1417 } 1418 break; 1419 default: 1420 break; 1421 } 1422 break; 1423 } 1424 1425 if (I != E) { 1426 fprintf(stderr, "Invalid USR kind: %s\n", *I); 1427 display_usrs(); 1428 return 1; 1429 } 1430 return 0; 1431} 1432 1433int print_usrs_file(const char *file_name) { 1434 char line[2048]; 1435 const char *args[128]; 1436 unsigned numChars = 0; 1437 1438 FILE *fp = fopen(file_name, "r"); 1439 if (!fp) { 1440 fprintf(stderr, "error: cannot open '%s'\n", file_name); 1441 return 1; 1442 } 1443 1444 /* This code is not really all that safe, but it works fine for testing. */ 1445 while (!feof(fp)) { 1446 char c = fgetc(fp); 1447 if (c == '\n') { 1448 unsigned i = 0; 1449 const char *s = 0; 1450 1451 if (numChars == 0) 1452 continue; 1453 1454 line[numChars] = '\0'; 1455 numChars = 0; 1456 1457 if (line[0] == '/' && line[1] == '/') 1458 continue; 1459 1460 s = strtok(line, " "); 1461 while (s) { 1462 args[i] = s; 1463 ++i; 1464 s = strtok(0, " "); 1465 } 1466 if (print_usrs(&args[0], &args[i])) 1467 return 1; 1468 } 1469 else 1470 line[numChars++] = c; 1471 } 1472 1473 fclose(fp); 1474 return 0; 1475} 1476 1477/******************************************************************************/ 1478/* Command line processing. */ 1479/******************************************************************************/ 1480int write_pch_file(const char *filename, int argc, const char *argv[]) { 1481 CXIndex Idx; 1482 CXTranslationUnit TU; 1483 struct CXUnsavedFile *unsaved_files = 0; 1484 int num_unsaved_files = 0; 1485 1486 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1); 1487 1488 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { 1489 clang_disposeIndex(Idx); 1490 return -1; 1491 } 1492 1493 TU = clang_parseTranslationUnit(Idx, 0, 1494 argv + num_unsaved_files, 1495 argc - num_unsaved_files, 1496 unsaved_files, 1497 num_unsaved_files, 1498 CXTranslationUnit_Incomplete); 1499 if (!TU) { 1500 fprintf(stderr, "Unable to load translation unit!\n"); 1501 free_remapped_files(unsaved_files, num_unsaved_files); 1502 clang_disposeIndex(Idx); 1503 return 1; 1504 } 1505 1506 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU))) 1507 fprintf(stderr, "Unable to write PCH file %s\n", filename); 1508 clang_disposeTranslationUnit(TU); 1509 free_remapped_files(unsaved_files, num_unsaved_files); 1510 clang_disposeIndex(Idx); 1511 return 0; 1512} 1513 1514/******************************************************************************/ 1515/* Command line processing. */ 1516/******************************************************************************/ 1517 1518static CXCursorVisitor GetVisitor(const char *s) { 1519 if (s[0] == '\0') 1520 return FilteredPrintingVisitor; 1521 if (strcmp(s, "-usrs") == 0) 1522 return USRVisitor; 1523 if (strncmp(s, "-memory-usage", 13) == 0) 1524 return GetVisitor(s + 13); 1525 return NULL; 1526} 1527 1528static void print_usage(void) { 1529 fprintf(stderr, 1530 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" 1531 " c-index-test -code-completion-timing=<site> <compiler arguments>\n" 1532 " c-index-test -cursor-at=<site> <compiler arguments>\n" 1533 " c-index-test -test-file-scan <AST file> <source file> " 1534 "[FileCheck prefix]\n" 1535 " c-index-test -test-load-tu <AST file> <symbol filter> " 1536 "[FileCheck prefix]\n" 1537 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> " 1538 "[FileCheck prefix]\n" 1539 " c-index-test -test-load-source <symbol filter> {<args>}*\n"); 1540 fprintf(stderr, 1541 " c-index-test -test-load-source-memory-usage " 1542 "<symbol filter> {<args>}*\n" 1543 " c-index-test -test-load-source-reparse <trials> <symbol filter> " 1544 " {<args>}*\n" 1545 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n" 1546 " c-index-test -test-load-source-usrs-memory-usage " 1547 "<symbol filter> {<args>}*\n" 1548 " c-index-test -test-annotate-tokens=<range> {<args>}*\n" 1549 " c-index-test -test-inclusion-stack-source {<args>}*\n" 1550 " c-index-test -test-inclusion-stack-tu <AST file>\n"); 1551 fprintf(stderr, 1552 " c-index-test -test-print-linkage-source {<args>}*\n" 1553 " c-index-test -test-print-typekind {<args>}*\n" 1554 " c-index-test -print-usr [<CursorKind> {<args>}]*\n" 1555 " c-index-test -print-usr-file <file>\n" 1556 " c-index-test -write-pch <file> <compiler arguments>\n\n"); 1557 fprintf(stderr, 1558 " <symbol filter> values:\n%s", 1559 " all - load all symbols, including those from PCH\n" 1560 " local - load all symbols except those in PCH\n" 1561 " category - only load ObjC categories (non-PCH)\n" 1562 " interface - only load ObjC interfaces (non-PCH)\n" 1563 " protocol - only load ObjC protocols (non-PCH)\n" 1564 " function - only load functions (non-PCH)\n" 1565 " typedef - only load typdefs (non-PCH)\n" 1566 " scan-function - scan function bodies (non-PCH)\n\n"); 1567} 1568 1569/***/ 1570 1571int cindextest_main(int argc, const char **argv) { 1572 clang_enableStackTraces(); 1573 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) 1574 return perform_code_completion(argc, argv, 0); 1575 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1]) 1576 return perform_code_completion(argc, argv, 1); 1577 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) 1578 return inspect_cursor_at(argc, argv); 1579 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { 1580 CXCursorVisitor I = GetVisitor(argv[1] + 13); 1581 if (I) 1582 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I, 1583 NULL); 1584 } 1585 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){ 1586 CXCursorVisitor I = GetVisitor(argv[1] + 25); 1587 if (I) { 1588 int trials = atoi(argv[2]); 1589 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I, 1590 NULL); 1591 } 1592 } 1593 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) { 1594 CXCursorVisitor I = GetVisitor(argv[1] + 17); 1595 1596 PostVisitTU postVisit = 0; 1597 if (strstr(argv[1], "-memory-usage")) 1598 postVisit = PrintMemoryUsage; 1599 1600 if (I) 1601 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, 1602 postVisit); 1603 } 1604 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) 1605 return perform_file_scan(argv[2], argv[3], 1606 argc >= 5 ? argv[4] : 0); 1607 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1]) 1608 return perform_token_annotation(argc, argv); 1609 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0) 1610 return perform_test_load_source(argc - 2, argv + 2, "all", NULL, 1611 PrintInclusionStack); 1612 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) 1613 return perform_test_load_tu(argv[2], "all", NULL, NULL, 1614 PrintInclusionStack); 1615 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0) 1616 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage, 1617 NULL); 1618 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0) 1619 return perform_test_load_source(argc - 2, argv + 2, "all", 1620 PrintTypeKind, 0); 1621 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) { 1622 if (argc > 2) 1623 return print_usrs(argv + 2, argv + argc); 1624 else { 1625 display_usrs(); 1626 return 1; 1627 } 1628 } 1629 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0) 1630 return print_usrs_file(argv[2]); 1631 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0) 1632 return write_pch_file(argv[2], argc - 3, argv + 3); 1633 1634 print_usage(); 1635 return 1; 1636} 1637 1638/***/ 1639 1640/* We intentionally run in a separate thread to ensure we at least minimal 1641 * testing of a multithreaded environment (for example, having a reduced stack 1642 * size). */ 1643 1644typedef struct thread_info { 1645 int argc; 1646 const char **argv; 1647 int result; 1648} thread_info; 1649void thread_runner(void *client_data_v) { 1650 thread_info *client_data = client_data_v; 1651 client_data->result = cindextest_main(client_data->argc, client_data->argv); 1652} 1653 1654int main(int argc, const char **argv) { 1655 thread_info client_data; 1656 1657 if (getenv("CINDEXTEST_NOTHREADS")) 1658 return cindextest_main(argc, argv); 1659 1660 client_data.argc = argc; 1661 client_data.argv = argv; 1662 clang_executeOnThread(thread_runner, &client_data, 0); 1663 return client_data.result; 1664} 1665