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