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