c-index-test.c revision bda536df1f5ccd71256eeeab4adbd2cf3769d89e
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 if (getenv("CINDEXTEST_NESTED_MACROS")) 41 options |= CXTranslationUnit_NestedMacroExpansions; 42 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING")) 43 options &= ~CXTranslationUnit_CacheCompletionResults; 44 45 return options; 46} 47 48static int checkForErrors(CXTranslationUnit TU); 49 50static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column, 51 unsigned end_line, unsigned end_column) { 52 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column, 53 end_line, end_column); 54} 55 56static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, 57 CXTranslationUnit *TU) { 58 59 *TU = clang_createTranslationUnit(Idx, file); 60 if (!*TU) { 61 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); 62 return 0; 63 } 64 return 1; 65} 66 67void free_remapped_files(struct CXUnsavedFile *unsaved_files, 68 int num_unsaved_files) { 69 int i; 70 for (i = 0; i != num_unsaved_files; ++i) { 71 free((char *)unsaved_files[i].Filename); 72 free((char *)unsaved_files[i].Contents); 73 } 74 free(unsaved_files); 75} 76 77int parse_remapped_files(int argc, const char **argv, int start_arg, 78 struct CXUnsavedFile **unsaved_files, 79 int *num_unsaved_files) { 80 int i; 81 int arg; 82 int prefix_len = strlen("-remap-file="); 83 *unsaved_files = 0; 84 *num_unsaved_files = 0; 85 86 /* Count the number of remapped files. */ 87 for (arg = start_arg; arg < argc; ++arg) { 88 if (strncmp(argv[arg], "-remap-file=", prefix_len)) 89 break; 90 91 ++*num_unsaved_files; 92 } 93 94 if (*num_unsaved_files == 0) 95 return 0; 96 97 *unsaved_files 98 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * 99 *num_unsaved_files); 100 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) { 101 struct CXUnsavedFile *unsaved = *unsaved_files + i; 102 const char *arg_string = argv[arg] + prefix_len; 103 int filename_len; 104 char *filename; 105 char *contents; 106 FILE *to_file; 107 const char *semi = strchr(arg_string, ';'); 108 if (!semi) { 109 fprintf(stderr, 110 "error: -remap-file=from;to argument is missing semicolon\n"); 111 free_remapped_files(*unsaved_files, i); 112 *unsaved_files = 0; 113 *num_unsaved_files = 0; 114 return -1; 115 } 116 117 /* Open the file that we're remapping to. */ 118 to_file = fopen(semi + 1, "rb"); 119 if (!to_file) { 120 fprintf(stderr, "error: cannot open file %s that we are remapping to\n", 121 semi + 1); 122 free_remapped_files(*unsaved_files, i); 123 *unsaved_files = 0; 124 *num_unsaved_files = 0; 125 return -1; 126 } 127 128 /* Determine the length of the file we're remapping to. */ 129 fseek(to_file, 0, SEEK_END); 130 unsaved->Length = ftell(to_file); 131 fseek(to_file, 0, SEEK_SET); 132 133 /* Read the contents of the file we're remapping to. */ 134 contents = (char *)malloc(unsaved->Length + 1); 135 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) { 136 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n", 137 (feof(to_file) ? "EOF" : "error"), semi + 1); 138 fclose(to_file); 139 free_remapped_files(*unsaved_files, i); 140 *unsaved_files = 0; 141 *num_unsaved_files = 0; 142 return -1; 143 } 144 contents[unsaved->Length] = 0; 145 unsaved->Contents = contents; 146 147 /* Close the file. */ 148 fclose(to_file); 149 150 /* Copy the file name that we're remapping from. */ 151 filename_len = semi - arg_string; 152 filename = (char *)malloc(filename_len + 1); 153 memcpy(filename, arg_string, filename_len); 154 filename[filename_len] = 0; 155 unsaved->Filename = filename; 156 } 157 158 return 0; 159} 160 161/******************************************************************************/ 162/* Pretty-printing. */ 163/******************************************************************************/ 164 165static void PrintRange(CXSourceRange R, const char *str) { 166 CXFile begin_file, end_file; 167 unsigned begin_line, begin_column, end_line, end_column; 168 169 clang_getSpellingLocation(clang_getRangeStart(R), 170 &begin_file, &begin_line, &begin_column, 0); 171 clang_getSpellingLocation(clang_getRangeEnd(R), 172 &end_file, &end_line, &end_column, 0); 173 if (!begin_file || !end_file) 174 return; 175 176 printf(" %s=", str); 177 PrintExtent(stdout, begin_line, begin_column, end_line, end_column); 178} 179 180int want_display_name = 0; 181 182static void PrintCursor(CXCursor Cursor) { 183 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); 184 if (clang_isInvalid(Cursor.kind)) { 185 CXString ks = clang_getCursorKindSpelling(Cursor.kind); 186 printf("Invalid Cursor => %s", clang_getCString(ks)); 187 clang_disposeString(ks); 188 } 189 else { 190 CXString string, ks; 191 CXCursor Referenced; 192 unsigned line, column; 193 CXCursor SpecializationOf; 194 CXCursor *overridden; 195 unsigned num_overridden; 196 unsigned RefNameRangeNr; 197 CXSourceRange CursorExtent; 198 CXSourceRange RefNameRange; 199 200 ks = clang_getCursorKindSpelling(Cursor.kind); 201 string = want_display_name? clang_getCursorDisplayName(Cursor) 202 : clang_getCursorSpelling(Cursor); 203 printf("%s=%s", clang_getCString(ks), 204 clang_getCString(string)); 205 clang_disposeString(ks); 206 clang_disposeString(string); 207 208 Referenced = clang_getCursorReferenced(Cursor); 209 if (!clang_equalCursors(Referenced, clang_getNullCursor())) { 210 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) { 211 unsigned I, N = clang_getNumOverloadedDecls(Referenced); 212 printf("["); 213 for (I = 0; I != N; ++I) { 214 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I); 215 CXSourceLocation Loc; 216 if (I) 217 printf(", "); 218 219 Loc = clang_getCursorLocation(Ovl); 220 clang_getSpellingLocation(Loc, 0, &line, &column, 0); 221 printf("%d:%d", line, column); 222 } 223 printf("]"); 224 } else { 225 CXSourceLocation Loc = clang_getCursorLocation(Referenced); 226 clang_getSpellingLocation(Loc, 0, &line, &column, 0); 227 printf(":%d:%d", line, column); 228 } 229 } 230 231 if (clang_isCursorDefinition(Cursor)) 232 printf(" (Definition)"); 233 234 switch (clang_getCursorAvailability(Cursor)) { 235 case CXAvailability_Available: 236 break; 237 238 case CXAvailability_Deprecated: 239 printf(" (deprecated)"); 240 break; 241 242 case CXAvailability_NotAvailable: 243 printf(" (unavailable)"); 244 break; 245 246 case CXAvailability_NotAccessible: 247 printf(" (inaccessible)"); 248 break; 249 } 250 251 if (clang_CXXMethod_isStatic(Cursor)) 252 printf(" (static)"); 253 if (clang_CXXMethod_isVirtual(Cursor)) 254 printf(" (virtual)"); 255 256 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { 257 CXType T = 258 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor)); 259 CXString S = clang_getTypeKindSpelling(T.kind); 260 printf(" [IBOutletCollection=%s]", clang_getCString(S)); 261 clang_disposeString(S); 262 } 263 264 if (Cursor.kind == CXCursor_CXXBaseSpecifier) { 265 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor); 266 unsigned isVirtual = clang_isVirtualBase(Cursor); 267 const char *accessStr = 0; 268 269 switch (access) { 270 case CX_CXXInvalidAccessSpecifier: 271 accessStr = "invalid"; break; 272 case CX_CXXPublic: 273 accessStr = "public"; break; 274 case CX_CXXProtected: 275 accessStr = "protected"; break; 276 case CX_CXXPrivate: 277 accessStr = "private"; break; 278 } 279 280 printf(" [access=%s isVirtual=%s]", accessStr, 281 isVirtual ? "true" : "false"); 282 } 283 284 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor); 285 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) { 286 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf); 287 CXString Name = clang_getCursorSpelling(SpecializationOf); 288 clang_getSpellingLocation(Loc, 0, &line, &column, 0); 289 printf(" [Specialization of %s:%d:%d]", 290 clang_getCString(Name), line, column); 291 clang_disposeString(Name); 292 } 293 294 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden); 295 if (num_overridden) { 296 unsigned I; 297 printf(" [Overrides "); 298 for (I = 0; I != num_overridden; ++I) { 299 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]); 300 clang_getSpellingLocation(Loc, 0, &line, &column, 0); 301 if (I) 302 printf(", "); 303 printf("@%d:%d", line, column); 304 } 305 printf("]"); 306 clang_disposeOverriddenCursors(overridden); 307 } 308 309 if (Cursor.kind == CXCursor_InclusionDirective) { 310 CXFile File = clang_getIncludedFile(Cursor); 311 CXString Included = clang_getFileName(File); 312 printf(" (%s)", clang_getCString(Included)); 313 clang_disposeString(Included); 314 315 if (clang_isFileMultipleIncludeGuarded(TU, File)) 316 printf(" [multi-include guarded]"); 317 } 318 319 CursorExtent = clang_getCursorExtent(Cursor); 320 RefNameRange = clang_getCursorReferenceNameRange(Cursor, 321 CXNameRange_WantQualifier 322 | CXNameRange_WantSinglePiece 323 | CXNameRange_WantTemplateArgs, 324 0); 325 if (!clang_equalRanges(CursorExtent, RefNameRange)) 326 PrintRange(RefNameRange, "SingleRefName"); 327 328 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) { 329 RefNameRange = clang_getCursorReferenceNameRange(Cursor, 330 CXNameRange_WantQualifier 331 | CXNameRange_WantTemplateArgs, 332 RefNameRangeNr); 333 if (clang_equalRanges(clang_getNullRange(), RefNameRange)) 334 break; 335 if (!clang_equalRanges(CursorExtent, RefNameRange)) 336 PrintRange(RefNameRange, "RefName"); 337 } 338 } 339} 340 341static const char* GetCursorSource(CXCursor Cursor) { 342 CXSourceLocation Loc = clang_getCursorLocation(Cursor); 343 CXString source; 344 CXFile file; 345 clang_getExpansionLocation(Loc, &file, 0, 0, 0); 346 source = clang_getFileName(file); 347 if (!clang_getCString(source)) { 348 clang_disposeString(source); 349 return "<invalid loc>"; 350 } 351 else { 352 const char *b = basename(clang_getCString(source)); 353 clang_disposeString(source); 354 return b; 355 } 356} 357 358/******************************************************************************/ 359/* Callbacks. */ 360/******************************************************************************/ 361 362typedef void (*PostVisitTU)(CXTranslationUnit); 363 364void PrintDiagnostic(CXDiagnostic Diagnostic) { 365 FILE *out = stderr; 366 CXFile file; 367 CXString Msg; 368 unsigned display_opts = CXDiagnostic_DisplaySourceLocation 369 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges 370 | CXDiagnostic_DisplayOption; 371 unsigned i, num_fixits; 372 373 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) 374 return; 375 376 Msg = clang_formatDiagnostic(Diagnostic, display_opts); 377 fprintf(stderr, "%s\n", clang_getCString(Msg)); 378 clang_disposeString(Msg); 379 380 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), 381 &file, 0, 0, 0); 382 if (!file) 383 return; 384 385 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); 386 for (i = 0; i != num_fixits; ++i) { 387 CXSourceRange range; 388 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range); 389 CXSourceLocation start = clang_getRangeStart(range); 390 CXSourceLocation end = clang_getRangeEnd(range); 391 unsigned start_line, start_column, end_line, end_column; 392 CXFile start_file, end_file; 393 clang_getSpellingLocation(start, &start_file, &start_line, 394 &start_column, 0); 395 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0); 396 if (clang_equalLocations(start, end)) { 397 /* Insertion. */ 398 if (start_file == file) 399 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", 400 clang_getCString(insertion_text), start_line, start_column); 401 } else if (strcmp(clang_getCString(insertion_text), "") == 0) { 402 /* Removal. */ 403 if (start_file == file && end_file == file) { 404 fprintf(out, "FIX-IT: Remove "); 405 PrintExtent(out, start_line, start_column, end_line, end_column); 406 fprintf(out, "\n"); 407 } 408 } else { 409 /* Replacement. */ 410 if (start_file == end_file) { 411 fprintf(out, "FIX-IT: Replace "); 412 PrintExtent(out, start_line, start_column, end_line, end_column); 413 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text)); 414 } 415 break; 416 } 417 clang_disposeString(insertion_text); 418 } 419} 420 421void PrintDiagnostics(CXTranslationUnit TU) { 422 int i, n = clang_getNumDiagnostics(TU); 423 for (i = 0; i != n; ++i) { 424 CXDiagnostic Diag = clang_getDiagnostic(TU, i); 425 PrintDiagnostic(Diag); 426 clang_disposeDiagnostic(Diag); 427 } 428} 429 430void PrintMemoryUsage(CXTranslationUnit TU) { 431 unsigned long total = 0; 432 unsigned i = 0; 433 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU); 434 fprintf(stderr, "Memory usage:\n"); 435 for (i = 0 ; i != usage.numEntries; ++i) { 436 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind); 437 unsigned long amount = usage.entries[i].amount; 438 total += amount; 439 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount, 440 ((double) amount)/(1024*1024)); 441 } 442 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total, 443 ((double) total)/(1024*1024)); 444 clang_disposeCXTUResourceUsage(usage); 445} 446 447/******************************************************************************/ 448/* Logic for testing traversal. */ 449/******************************************************************************/ 450 451static const char *FileCheckPrefix = "CHECK"; 452 453static void PrintCursorExtent(CXCursor C) { 454 CXSourceRange extent = clang_getCursorExtent(C); 455 PrintRange(extent, "Extent"); 456} 457 458/* Data used by all of the visitors. */ 459typedef struct { 460 CXTranslationUnit TU; 461 enum CXCursorKind *Filter; 462} VisitorData; 463 464 465enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, 466 CXCursor Parent, 467 CXClientData ClientData) { 468 VisitorData *Data = (VisitorData *)ClientData; 469 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { 470 CXSourceLocation Loc = clang_getCursorLocation(Cursor); 471 unsigned line, column; 472 clang_getSpellingLocation(Loc, 0, &line, &column, 0); 473 printf("// %s: %s:%d:%d: ", FileCheckPrefix, 474 GetCursorSource(Cursor), line, column); 475 PrintCursor(Cursor); 476 PrintCursorExtent(Cursor); 477 printf("\n"); 478 return CXChildVisit_Recurse; 479 } 480 481 return CXChildVisit_Continue; 482} 483 484static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, 485 CXCursor Parent, 486 CXClientData ClientData) { 487 const char *startBuf, *endBuf; 488 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; 489 CXCursor Ref; 490 VisitorData *Data = (VisitorData *)ClientData; 491 492 if (Cursor.kind != CXCursor_FunctionDecl || 493 !clang_isCursorDefinition(Cursor)) 494 return CXChildVisit_Continue; 495 496 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, 497 &startLine, &startColumn, 498 &endLine, &endColumn); 499 /* Probe the entire body, looking for both decls and refs. */ 500 curLine = startLine; 501 curColumn = startColumn; 502 503 while (startBuf < endBuf) { 504 CXSourceLocation Loc; 505 CXFile file; 506 CXString source; 507 508 if (*startBuf == '\n') { 509 startBuf++; 510 curLine++; 511 curColumn = 1; 512 } else if (*startBuf != '\t') 513 curColumn++; 514 515 Loc = clang_getCursorLocation(Cursor); 516 clang_getSpellingLocation(Loc, &file, 0, 0, 0); 517 518 source = clang_getFileName(file); 519 if (clang_getCString(source)) { 520 CXSourceLocation RefLoc 521 = clang_getLocation(Data->TU, file, curLine, curColumn); 522 Ref = clang_getCursor(Data->TU, RefLoc); 523 if (Ref.kind == CXCursor_NoDeclFound) { 524 /* Nothing found here; that's fine. */ 525 } else if (Ref.kind != CXCursor_FunctionDecl) { 526 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), 527 curLine, curColumn); 528 PrintCursor(Ref); 529 printf("\n"); 530 } 531 } 532 clang_disposeString(source); 533 startBuf++; 534 } 535 536 return CXChildVisit_Continue; 537} 538 539/******************************************************************************/ 540/* USR testing. */ 541/******************************************************************************/ 542 543enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, 544 CXClientData ClientData) { 545 VisitorData *Data = (VisitorData *)ClientData; 546 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) { 547 CXString USR = clang_getCursorUSR(C); 548 const char *cstr = clang_getCString(USR); 549 if (!cstr || cstr[0] == '\0') { 550 clang_disposeString(USR); 551 return CXChildVisit_Recurse; 552 } 553 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr); 554 555 PrintCursorExtent(C); 556 printf("\n"); 557 clang_disposeString(USR); 558 559 return CXChildVisit_Recurse; 560 } 561 562 return CXChildVisit_Continue; 563} 564 565/******************************************************************************/ 566/* Inclusion stack testing. */ 567/******************************************************************************/ 568 569void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, 570 unsigned includeStackLen, CXClientData data) { 571 572 unsigned i; 573 CXString fname; 574 575 fname = clang_getFileName(includedFile); 576 printf("file: %s\nincluded by:\n", clang_getCString(fname)); 577 clang_disposeString(fname); 578 579 for (i = 0; i < includeStackLen; ++i) { 580 CXFile includingFile; 581 unsigned line, column; 582 clang_getSpellingLocation(includeStack[i], &includingFile, &line, 583 &column, 0); 584 fname = clang_getFileName(includingFile); 585 printf(" %s:%d:%d\n", clang_getCString(fname), line, column); 586 clang_disposeString(fname); 587 } 588 printf("\n"); 589} 590 591void PrintInclusionStack(CXTranslationUnit TU) { 592 clang_getInclusions(TU, InclusionVisitor, NULL); 593} 594 595/******************************************************************************/ 596/* Linkage testing. */ 597/******************************************************************************/ 598 599static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, 600 CXClientData d) { 601 const char *linkage = 0; 602 603 if (clang_isInvalid(clang_getCursorKind(cursor))) 604 return CXChildVisit_Recurse; 605 606 switch (clang_getCursorLinkage(cursor)) { 607 case CXLinkage_Invalid: break; 608 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break; 609 case CXLinkage_Internal: linkage = "Internal"; break; 610 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break; 611 case CXLinkage_External: linkage = "External"; break; 612 } 613 614 if (linkage) { 615 PrintCursor(cursor); 616 printf("linkage=%s\n", linkage); 617 } 618 619 return CXChildVisit_Recurse; 620} 621 622/******************************************************************************/ 623/* Typekind testing. */ 624/******************************************************************************/ 625 626static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p, 627 CXClientData d) { 628 if (!clang_isInvalid(clang_getCursorKind(cursor))) { 629 CXType T = clang_getCursorType(cursor); 630 CXString S = clang_getTypeKindSpelling(T.kind); 631 PrintCursor(cursor); 632 printf(" typekind=%s", clang_getCString(S)); 633 if (clang_isConstQualifiedType(T)) 634 printf(" const"); 635 if (clang_isVolatileQualifiedType(T)) 636 printf(" volatile"); 637 if (clang_isRestrictQualifiedType(T)) 638 printf(" restrict"); 639 clang_disposeString(S); 640 /* Print the canonical type if it is different. */ 641 { 642 CXType CT = clang_getCanonicalType(T); 643 if (!clang_equalTypes(T, CT)) { 644 CXString CS = clang_getTypeKindSpelling(CT.kind); 645 printf(" [canonical=%s]", clang_getCString(CS)); 646 clang_disposeString(CS); 647 } 648 } 649 /* Print the return type if it exists. */ 650 { 651 CXType RT = clang_getCursorResultType(cursor); 652 if (RT.kind != CXType_Invalid) { 653 CXString RS = clang_getTypeKindSpelling(RT.kind); 654 printf(" [result=%s]", clang_getCString(RS)); 655 clang_disposeString(RS); 656 } 657 } 658 /* Print if this is a non-POD type. */ 659 printf(" [isPOD=%d]", clang_isPODType(T)); 660 661 printf("\n"); 662 } 663 return CXChildVisit_Recurse; 664} 665 666 667/******************************************************************************/ 668/* Loading ASTs/source. */ 669/******************************************************************************/ 670 671static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, 672 const char *filter, const char *prefix, 673 CXCursorVisitor Visitor, 674 PostVisitTU PV) { 675 676 if (prefix) 677 FileCheckPrefix = prefix; 678 679 if (Visitor) { 680 enum CXCursorKind K = CXCursor_NotImplemented; 681 enum CXCursorKind *ck = &K; 682 VisitorData Data; 683 684 /* Perform some simple filtering. */ 685 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; 686 else if (!strcmp(filter, "all-display") || 687 !strcmp(filter, "local-display")) { 688 ck = NULL; 689 want_display_name = 1; 690 } 691 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; 692 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; 693 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; 694 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; 695 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; 696 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; 697 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; 698 else { 699 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); 700 return 1; 701 } 702 703 Data.TU = TU; 704 Data.Filter = ck; 705 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); 706 } 707 708 if (PV) 709 PV(TU); 710 711 PrintDiagnostics(TU); 712 clang_disposeTranslationUnit(TU); 713 return 0; 714} 715 716int perform_test_load_tu(const char *file, const char *filter, 717 const char *prefix, CXCursorVisitor Visitor, 718 PostVisitTU PV) { 719 CXIndex Idx; 720 CXTranslationUnit TU; 721 int result; 722 Idx = clang_createIndex(/* excludeDeclsFromPCH */ 723 !strcmp(filter, "local") ? 1 : 0, 724 /* displayDiagnosics=*/1); 725 726 if (!CreateTranslationUnit(Idx, file, &TU)) { 727 clang_disposeIndex(Idx); 728 return 1; 729 } 730 731 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV); 732 clang_disposeIndex(Idx); 733 return result; 734} 735 736int perform_test_load_source(int argc, const char **argv, 737 const char *filter, CXCursorVisitor Visitor, 738 PostVisitTU PV) { 739 CXIndex Idx; 740 CXTranslationUnit TU; 741 struct CXUnsavedFile *unsaved_files = 0; 742 int num_unsaved_files = 0; 743 int result; 744 745 Idx = clang_createIndex(/* excludeDeclsFromPCH */ 746 (!strcmp(filter, "local") || 747 !strcmp(filter, "local-display"))? 1 : 0, 748 /* displayDiagnosics=*/0); 749 750 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { 751 clang_disposeIndex(Idx); 752 return -1; 753 } 754 755 TU = clang_parseTranslationUnit(Idx, 0, 756 argv + num_unsaved_files, 757 argc - num_unsaved_files, 758 unsaved_files, num_unsaved_files, 759 getDefaultParsingOptions()); 760 if (!TU) { 761 fprintf(stderr, "Unable to load translation unit!\n"); 762 free_remapped_files(unsaved_files, num_unsaved_files); 763 clang_disposeIndex(Idx); 764 return 1; 765 } 766 767 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); 768 free_remapped_files(unsaved_files, num_unsaved_files); 769 clang_disposeIndex(Idx); 770 return result; 771} 772 773int perform_test_reparse_source(int argc, const char **argv, int trials, 774 const char *filter, CXCursorVisitor Visitor, 775 PostVisitTU PV) { 776 CXIndex Idx; 777 CXTranslationUnit TU; 778 struct CXUnsavedFile *unsaved_files = 0; 779 int num_unsaved_files = 0; 780 int result; 781 int trial; 782 int remap_after_trial = 0; 783 char *endptr = 0; 784 785 Idx = clang_createIndex(/* excludeDeclsFromPCH */ 786 !strcmp(filter, "local") ? 1 : 0, 787 /* displayDiagnosics=*/0); 788 789 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { 790 clang_disposeIndex(Idx); 791 return -1; 792 } 793 794 /* Load the initial translation unit -- we do this without honoring remapped 795 * files, so that we have a way to test results after changing the source. */ 796 TU = clang_parseTranslationUnit(Idx, 0, 797 argv + num_unsaved_files, 798 argc - num_unsaved_files, 799 0, 0, getDefaultParsingOptions()); 800 if (!TU) { 801 fprintf(stderr, "Unable to load translation unit!\n"); 802 free_remapped_files(unsaved_files, num_unsaved_files); 803 clang_disposeIndex(Idx); 804 return 1; 805 } 806 807 if (checkForErrors(TU) != 0) 808 return -1; 809 810 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) { 811 remap_after_trial = 812 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10); 813 } 814 815 for (trial = 0; trial < trials; ++trial) { 816 if (clang_reparseTranslationUnit(TU, 817 trial >= remap_after_trial ? num_unsaved_files : 0, 818 trial >= remap_after_trial ? unsaved_files : 0, 819 clang_defaultReparseOptions(TU))) { 820 fprintf(stderr, "Unable to reparse translation unit!\n"); 821 clang_disposeTranslationUnit(TU); 822 free_remapped_files(unsaved_files, num_unsaved_files); 823 clang_disposeIndex(Idx); 824 return -1; 825 } 826 827 if (checkForErrors(TU) != 0) 828 return -1; 829 } 830 831 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); 832 833 if (checkForErrors(TU) != 0) 834 return -1; 835 836 free_remapped_files(unsaved_files, num_unsaved_files); 837 clang_disposeIndex(Idx); 838 return result; 839} 840 841/******************************************************************************/ 842/* Logic for testing clang_getCursor(). */ 843/******************************************************************************/ 844 845static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor, 846 unsigned start_line, unsigned start_col, 847 unsigned end_line, unsigned end_col, 848 const char *prefix) { 849 printf("// %s: ", FileCheckPrefix); 850 if (prefix) 851 printf("-%s", prefix); 852 PrintExtent(stdout, start_line, start_col, end_line, end_col); 853 printf(" "); 854 PrintCursor(cursor); 855 printf("\n"); 856} 857 858static int perform_file_scan(const char *ast_file, const char *source_file, 859 const char *prefix) { 860 CXIndex Idx; 861 CXTranslationUnit TU; 862 FILE *fp; 863 CXCursor prevCursor = clang_getNullCursor(); 864 CXFile file; 865 unsigned line = 1, col = 1; 866 unsigned start_line = 1, start_col = 1; 867 868 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, 869 /* displayDiagnosics=*/1))) { 870 fprintf(stderr, "Could not create Index\n"); 871 return 1; 872 } 873 874 if (!CreateTranslationUnit(Idx, ast_file, &TU)) 875 return 1; 876 877 if ((fp = fopen(source_file, "r")) == NULL) { 878 fprintf(stderr, "Could not open '%s'\n", source_file); 879 return 1; 880 } 881 882 file = clang_getFile(TU, source_file); 883 for (;;) { 884 CXCursor cursor; 885 int c = fgetc(fp); 886 887 if (c == '\n') { 888 ++line; 889 col = 1; 890 } else 891 ++col; 892 893 /* Check the cursor at this position, and dump the previous one if we have 894 * found something new. 895 */ 896 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); 897 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && 898 prevCursor.kind != CXCursor_InvalidFile) { 899 print_cursor_file_scan(TU, prevCursor, start_line, start_col, 900 line, col, prefix); 901 start_line = line; 902 start_col = col; 903 } 904 if (c == EOF) 905 break; 906 907 prevCursor = cursor; 908 } 909 910 fclose(fp); 911 clang_disposeTranslationUnit(TU); 912 clang_disposeIndex(Idx); 913 return 0; 914} 915 916/******************************************************************************/ 917/* Logic for testing clang code completion. */ 918/******************************************************************************/ 919 920/* Parse file:line:column from the input string. Returns 0 on success, non-zero 921 on failure. If successful, the pointer *filename will contain newly-allocated 922 memory (that will be owned by the caller) to store the file name. */ 923int parse_file_line_column(const char *input, char **filename, unsigned *line, 924 unsigned *column, unsigned *second_line, 925 unsigned *second_column) { 926 /* Find the second colon. */ 927 const char *last_colon = strrchr(input, ':'); 928 unsigned values[4], i; 929 unsigned num_values = (second_line && second_column)? 4 : 2; 930 931 char *endptr = 0; 932 if (!last_colon || last_colon == input) { 933 if (num_values == 4) 934 fprintf(stderr, "could not parse filename:line:column:line:column in " 935 "'%s'\n", input); 936 else 937 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); 938 return 1; 939 } 940 941 for (i = 0; i != num_values; ++i) { 942 const char *prev_colon; 943 944 /* Parse the next line or column. */ 945 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); 946 if (*endptr != 0 && *endptr != ':') { 947 fprintf(stderr, "could not parse %s in '%s'\n", 948 (i % 2 ? "column" : "line"), input); 949 return 1; 950 } 951 952 if (i + 1 == num_values) 953 break; 954 955 /* Find the previous colon. */ 956 prev_colon = last_colon - 1; 957 while (prev_colon != input && *prev_colon != ':') 958 --prev_colon; 959 if (prev_colon == input) { 960 fprintf(stderr, "could not parse %s in '%s'\n", 961 (i % 2 == 0? "column" : "line"), input); 962 return 1; 963 } 964 965 last_colon = prev_colon; 966 } 967 968 *line = values[0]; 969 *column = values[1]; 970 971 if (second_line && second_column) { 972 *second_line = values[2]; 973 *second_column = values[3]; 974 } 975 976 /* Copy the file name. */ 977 *filename = (char*)malloc(last_colon - input + 1); 978 memcpy(*filename, input, last_colon - input); 979 (*filename)[last_colon - input] = 0; 980 return 0; 981} 982 983const char * 984clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { 985 switch (Kind) { 986 case CXCompletionChunk_Optional: return "Optional"; 987 case CXCompletionChunk_TypedText: return "TypedText"; 988 case CXCompletionChunk_Text: return "Text"; 989 case CXCompletionChunk_Placeholder: return "Placeholder"; 990 case CXCompletionChunk_Informative: return "Informative"; 991 case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; 992 case CXCompletionChunk_LeftParen: return "LeftParen"; 993 case CXCompletionChunk_RightParen: return "RightParen"; 994 case CXCompletionChunk_LeftBracket: return "LeftBracket"; 995 case CXCompletionChunk_RightBracket: return "RightBracket"; 996 case CXCompletionChunk_LeftBrace: return "LeftBrace"; 997 case CXCompletionChunk_RightBrace: return "RightBrace"; 998 case CXCompletionChunk_LeftAngle: return "LeftAngle"; 999 case CXCompletionChunk_RightAngle: return "RightAngle"; 1000 case CXCompletionChunk_Comma: return "Comma"; 1001 case CXCompletionChunk_ResultType: return "ResultType"; 1002 case CXCompletionChunk_Colon: return "Colon"; 1003 case CXCompletionChunk_SemiColon: return "SemiColon"; 1004 case CXCompletionChunk_Equal: return "Equal"; 1005 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; 1006 case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; 1007 } 1008 1009 return "Unknown"; 1010} 1011 1012static int checkForErrors(CXTranslationUnit TU) { 1013 unsigned Num, i; 1014 CXDiagnostic Diag; 1015 CXString DiagStr; 1016 1017 if (!getenv("CINDEXTEST_FAILONERROR")) 1018 return 0; 1019 1020 Num = clang_getNumDiagnostics(TU); 1021 for (i = 0; i != Num; ++i) { 1022 Diag = clang_getDiagnostic(TU, i); 1023 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) { 1024 DiagStr = clang_formatDiagnostic(Diag, 1025 clang_defaultDiagnosticDisplayOptions()); 1026 fprintf(stderr, "%s\n", clang_getCString(DiagStr)); 1027 clang_disposeString(DiagStr); 1028 clang_disposeDiagnostic(Diag); 1029 return -1; 1030 } 1031 clang_disposeDiagnostic(Diag); 1032 } 1033 1034 return 0; 1035} 1036 1037void print_completion_string(CXCompletionString completion_string, FILE *file) { 1038 int I, N; 1039 1040 N = clang_getNumCompletionChunks(completion_string); 1041 for (I = 0; I != N; ++I) { 1042 CXString text; 1043 const char *cstr; 1044 enum CXCompletionChunkKind Kind 1045 = clang_getCompletionChunkKind(completion_string, I); 1046 1047 if (Kind == CXCompletionChunk_Optional) { 1048 fprintf(file, "{Optional "); 1049 print_completion_string( 1050 clang_getCompletionChunkCompletionString(completion_string, I), 1051 file); 1052 fprintf(file, "}"); 1053 continue; 1054 } 1055 1056 if (Kind == CXCompletionChunk_VerticalSpace) { 1057 fprintf(file, "{VerticalSpace }"); 1058 continue; 1059 } 1060 1061 text = clang_getCompletionChunkText(completion_string, I); 1062 cstr = clang_getCString(text); 1063 fprintf(file, "{%s %s}", 1064 clang_getCompletionChunkKindSpelling(Kind), 1065 cstr ? cstr : ""); 1066 clang_disposeString(text); 1067 } 1068 1069} 1070 1071void print_completion_result(CXCompletionResult *completion_result, 1072 CXClientData client_data) { 1073 FILE *file = (FILE *)client_data; 1074 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); 1075 unsigned annotationCount; 1076 1077 fprintf(file, "%s:", clang_getCString(ks)); 1078 clang_disposeString(ks); 1079 1080 print_completion_string(completion_result->CompletionString, file); 1081 fprintf(file, " (%u)", 1082 clang_getCompletionPriority(completion_result->CompletionString)); 1083 switch (clang_getCompletionAvailability(completion_result->CompletionString)){ 1084 case CXAvailability_Available: 1085 break; 1086 1087 case CXAvailability_Deprecated: 1088 fprintf(file, " (deprecated)"); 1089 break; 1090 1091 case CXAvailability_NotAvailable: 1092 fprintf(file, " (unavailable)"); 1093 break; 1094 1095 case CXAvailability_NotAccessible: 1096 fprintf(file, " (inaccessible)"); 1097 break; 1098 } 1099 1100 annotationCount = clang_getCompletionNumAnnotations( 1101 completion_result->CompletionString); 1102 if (annotationCount) { 1103 unsigned i; 1104 fprintf(file, " ("); 1105 for (i = 0; i < annotationCount; ++i) { 1106 if (i != 0) 1107 fprintf(file, ", "); 1108 fprintf(file, "\"%s\"", 1109 clang_getCString(clang_getCompletionAnnotation( 1110 completion_result->CompletionString, i))); 1111 } 1112 fprintf(file, ")"); 1113 } 1114 1115 fprintf(file, "\n"); 1116} 1117 1118void print_completion_contexts(unsigned long long contexts, FILE *file) { 1119 fprintf(file, "Completion contexts:\n"); 1120 if (contexts == CXCompletionContext_Unknown) { 1121 fprintf(file, "Unknown\n"); 1122 } 1123 if (contexts & CXCompletionContext_AnyType) { 1124 fprintf(file, "Any type\n"); 1125 } 1126 if (contexts & CXCompletionContext_AnyValue) { 1127 fprintf(file, "Any value\n"); 1128 } 1129 if (contexts & CXCompletionContext_ObjCObjectValue) { 1130 fprintf(file, "Objective-C object value\n"); 1131 } 1132 if (contexts & CXCompletionContext_ObjCSelectorValue) { 1133 fprintf(file, "Objective-C selector value\n"); 1134 } 1135 if (contexts & CXCompletionContext_CXXClassTypeValue) { 1136 fprintf(file, "C++ class type value\n"); 1137 } 1138 if (contexts & CXCompletionContext_DotMemberAccess) { 1139 fprintf(file, "Dot member access\n"); 1140 } 1141 if (contexts & CXCompletionContext_ArrowMemberAccess) { 1142 fprintf(file, "Arrow member access\n"); 1143 } 1144 if (contexts & CXCompletionContext_ObjCPropertyAccess) { 1145 fprintf(file, "Objective-C property access\n"); 1146 } 1147 if (contexts & CXCompletionContext_EnumTag) { 1148 fprintf(file, "Enum tag\n"); 1149 } 1150 if (contexts & CXCompletionContext_UnionTag) { 1151 fprintf(file, "Union tag\n"); 1152 } 1153 if (contexts & CXCompletionContext_StructTag) { 1154 fprintf(file, "Struct tag\n"); 1155 } 1156 if (contexts & CXCompletionContext_ClassTag) { 1157 fprintf(file, "Class name\n"); 1158 } 1159 if (contexts & CXCompletionContext_Namespace) { 1160 fprintf(file, "Namespace or namespace alias\n"); 1161 } 1162 if (contexts & CXCompletionContext_NestedNameSpecifier) { 1163 fprintf(file, "Nested name specifier\n"); 1164 } 1165 if (contexts & CXCompletionContext_ObjCInterface) { 1166 fprintf(file, "Objective-C interface\n"); 1167 } 1168 if (contexts & CXCompletionContext_ObjCProtocol) { 1169 fprintf(file, "Objective-C protocol\n"); 1170 } 1171 if (contexts & CXCompletionContext_ObjCCategory) { 1172 fprintf(file, "Objective-C category\n"); 1173 } 1174 if (contexts & CXCompletionContext_ObjCInstanceMessage) { 1175 fprintf(file, "Objective-C instance method\n"); 1176 } 1177 if (contexts & CXCompletionContext_ObjCClassMessage) { 1178 fprintf(file, "Objective-C class method\n"); 1179 } 1180 if (contexts & CXCompletionContext_ObjCSelectorName) { 1181 fprintf(file, "Objective-C selector name\n"); 1182 } 1183 if (contexts & CXCompletionContext_MacroName) { 1184 fprintf(file, "Macro name\n"); 1185 } 1186 if (contexts & CXCompletionContext_NaturalLanguage) { 1187 fprintf(file, "Natural language\n"); 1188 } 1189} 1190 1191int my_stricmp(const char *s1, const char *s2) { 1192 while (*s1 && *s2) { 1193 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2); 1194 if (c1 < c2) 1195 return -1; 1196 else if (c1 > c2) 1197 return 1; 1198 1199 ++s1; 1200 ++s2; 1201 } 1202 1203 if (*s1) 1204 return 1; 1205 else if (*s2) 1206 return -1; 1207 return 0; 1208} 1209 1210int perform_code_completion(int argc, const char **argv, int timing_only) { 1211 const char *input = argv[1]; 1212 char *filename = 0; 1213 unsigned line; 1214 unsigned column; 1215 CXIndex CIdx; 1216 int errorCode; 1217 struct CXUnsavedFile *unsaved_files = 0; 1218 int num_unsaved_files = 0; 1219 CXCodeCompleteResults *results = 0; 1220 CXTranslationUnit TU = 0; 1221 unsigned I, Repeats = 1; 1222 unsigned completionOptions = clang_defaultCodeCompleteOptions(); 1223 1224 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS")) 1225 completionOptions |= CXCodeComplete_IncludeCodePatterns; 1226 1227 if (timing_only) 1228 input += strlen("-code-completion-timing="); 1229 else 1230 input += strlen("-code-completion-at="); 1231 1232 if ((errorCode = parse_file_line_column(input, &filename, &line, &column, 1233 0, 0))) 1234 return errorCode; 1235 1236 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) 1237 return -1; 1238 1239 CIdx = clang_createIndex(0, 0); 1240 1241 if (getenv("CINDEXTEST_EDITING")) 1242 Repeats = 5; 1243 1244 TU = clang_parseTranslationUnit(CIdx, 0, 1245 argv + num_unsaved_files + 2, 1246 argc - num_unsaved_files - 2, 1247 0, 0, getDefaultParsingOptions()); 1248 if (!TU) { 1249 fprintf(stderr, "Unable to load translation unit!\n"); 1250 return 1; 1251 } 1252 1253 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) { 1254 fprintf(stderr, "Unable to reparse translation init!\n"); 1255 return 1; 1256 } 1257 1258 for (I = 0; I != Repeats; ++I) { 1259 results = clang_codeCompleteAt(TU, filename, line, column, 1260 unsaved_files, num_unsaved_files, 1261 completionOptions); 1262 if (!results) { 1263 fprintf(stderr, "Unable to perform code completion!\n"); 1264 return 1; 1265 } 1266 if (I != Repeats-1) 1267 clang_disposeCodeCompleteResults(results); 1268 } 1269 1270 if (results) { 1271 unsigned i, n = results->NumResults, containerIsIncomplete = 0; 1272 unsigned long long contexts; 1273 enum CXCursorKind containerKind; 1274 CXString objCSelector; 1275 const char *selectorString; 1276 if (!timing_only) { 1277 /* Sort the code-completion results based on the typed text. */ 1278 clang_sortCodeCompletionResults(results->Results, results->NumResults); 1279 1280 for (i = 0; i != n; ++i) 1281 print_completion_result(results->Results + i, stdout); 1282 } 1283 n = clang_codeCompleteGetNumDiagnostics(results); 1284 for (i = 0; i != n; ++i) { 1285 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); 1286 PrintDiagnostic(diag); 1287 clang_disposeDiagnostic(diag); 1288 } 1289 1290 contexts = clang_codeCompleteGetContexts(results); 1291 print_completion_contexts(contexts, stdout); 1292 1293 containerKind = clang_codeCompleteGetContainerKind(results, 1294 &containerIsIncomplete); 1295 1296 if (containerKind != CXCursor_InvalidCode) { 1297 /* We have found a container */ 1298 CXString containerUSR, containerKindSpelling; 1299 containerKindSpelling = clang_getCursorKindSpelling(containerKind); 1300 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling)); 1301 clang_disposeString(containerKindSpelling); 1302 1303 if (containerIsIncomplete) { 1304 printf("Container is incomplete\n"); 1305 } 1306 else { 1307 printf("Container is complete\n"); 1308 } 1309 1310 containerUSR = clang_codeCompleteGetContainerUSR(results); 1311 printf("Container USR: %s\n", clang_getCString(containerUSR)); 1312 clang_disposeString(containerUSR); 1313 } 1314 1315 objCSelector = clang_codeCompleteGetObjCSelector(results); 1316 selectorString = clang_getCString(objCSelector); 1317 if (selectorString && strlen(selectorString) > 0) { 1318 printf("Objective-C selector: %s\n", selectorString); 1319 } 1320 clang_disposeString(objCSelector); 1321 1322 clang_disposeCodeCompleteResults(results); 1323 } 1324 clang_disposeTranslationUnit(TU); 1325 clang_disposeIndex(CIdx); 1326 free(filename); 1327 1328 free_remapped_files(unsaved_files, num_unsaved_files); 1329 1330 return 0; 1331} 1332 1333typedef struct { 1334 char *filename; 1335 unsigned line; 1336 unsigned column; 1337} CursorSourceLocation; 1338 1339static int inspect_cursor_at(int argc, const char **argv) { 1340 CXIndex CIdx; 1341 int errorCode; 1342 struct CXUnsavedFile *unsaved_files = 0; 1343 int num_unsaved_files = 0; 1344 CXTranslationUnit TU; 1345 CXCursor Cursor; 1346 CursorSourceLocation *Locations = 0; 1347 unsigned NumLocations = 0, Loc; 1348 unsigned Repeats = 1; 1349 unsigned I; 1350 1351 /* Count the number of locations. */ 1352 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) 1353 ++NumLocations; 1354 1355 /* Parse the locations. */ 1356 assert(NumLocations > 0 && "Unable to count locations?"); 1357 Locations = (CursorSourceLocation *)malloc( 1358 NumLocations * sizeof(CursorSourceLocation)); 1359 for (Loc = 0; Loc < NumLocations; ++Loc) { 1360 const char *input = argv[Loc + 1] + strlen("-cursor-at="); 1361 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, 1362 &Locations[Loc].line, 1363 &Locations[Loc].column, 0, 0))) 1364 return errorCode; 1365 } 1366 1367 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, 1368 &num_unsaved_files)) 1369 return -1; 1370 1371 if (getenv("CINDEXTEST_EDITING")) 1372 Repeats = 5; 1373 1374 /* Parse the translation unit. When we're testing clang_getCursor() after 1375 reparsing, don't remap unsaved files until the second parse. */ 1376 CIdx = clang_createIndex(1, 1); 1377 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1], 1378 argv + num_unsaved_files + 1 + NumLocations, 1379 argc - num_unsaved_files - 2 - NumLocations, 1380 unsaved_files, 1381 Repeats > 1? 0 : num_unsaved_files, 1382 getDefaultParsingOptions()); 1383 1384 if (!TU) { 1385 fprintf(stderr, "unable to parse input\n"); 1386 return -1; 1387 } 1388 1389 if (checkForErrors(TU) != 0) 1390 return -1; 1391 1392 for (I = 0; I != Repeats; ++I) { 1393 if (Repeats > 1 && 1394 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, 1395 clang_defaultReparseOptions(TU))) { 1396 clang_disposeTranslationUnit(TU); 1397 return 1; 1398 } 1399 1400 if (checkForErrors(TU) != 0) 1401 return -1; 1402 1403 for (Loc = 0; Loc < NumLocations; ++Loc) { 1404 CXFile file = clang_getFile(TU, Locations[Loc].filename); 1405 if (!file) 1406 continue; 1407 1408 Cursor = clang_getCursor(TU, 1409 clang_getLocation(TU, file, Locations[Loc].line, 1410 Locations[Loc].column)); 1411 1412 if (checkForErrors(TU) != 0) 1413 return -1; 1414 1415 if (I + 1 == Repeats) { 1416 CXCompletionString completionString = clang_getCursorCompletionString( 1417 Cursor); 1418 PrintCursor(Cursor); 1419 if (completionString != NULL) { 1420 printf("\nCompletion string: "); 1421 print_completion_string(completionString, stdout); 1422 } 1423 printf("\n"); 1424 free(Locations[Loc].filename); 1425 } 1426 } 1427 } 1428 1429 PrintDiagnostics(TU); 1430 clang_disposeTranslationUnit(TU); 1431 clang_disposeIndex(CIdx); 1432 free(Locations); 1433 free_remapped_files(unsaved_files, num_unsaved_files); 1434 return 0; 1435} 1436 1437static enum CXVisitorResult findFileRefsVisit(void *context, 1438 CXCursor cursor, CXSourceRange range) { 1439 if (clang_Range_isNull(range)) 1440 return CXVisit_Continue; 1441 1442 PrintCursor(cursor); 1443 PrintRange(range, ""); 1444 printf("\n"); 1445 return CXVisit_Continue; 1446} 1447 1448static int find_file_refs_at(int argc, const char **argv) { 1449 CXIndex CIdx; 1450 int errorCode; 1451 struct CXUnsavedFile *unsaved_files = 0; 1452 int num_unsaved_files = 0; 1453 CXTranslationUnit TU; 1454 CXCursor Cursor; 1455 CursorSourceLocation *Locations = 0; 1456 unsigned NumLocations = 0, Loc; 1457 unsigned Repeats = 1; 1458 unsigned I; 1459 1460 /* Count the number of locations. */ 1461 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1]) 1462 ++NumLocations; 1463 1464 /* Parse the locations. */ 1465 assert(NumLocations > 0 && "Unable to count locations?"); 1466 Locations = (CursorSourceLocation *)malloc( 1467 NumLocations * sizeof(CursorSourceLocation)); 1468 for (Loc = 0; Loc < NumLocations; ++Loc) { 1469 const char *input = argv[Loc + 1] + strlen("-file-refs-at="); 1470 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, 1471 &Locations[Loc].line, 1472 &Locations[Loc].column, 0, 0))) 1473 return errorCode; 1474 } 1475 1476 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, 1477 &num_unsaved_files)) 1478 return -1; 1479 1480 if (getenv("CINDEXTEST_EDITING")) 1481 Repeats = 5; 1482 1483 /* Parse the translation unit. When we're testing clang_getCursor() after 1484 reparsing, don't remap unsaved files until the second parse. */ 1485 CIdx = clang_createIndex(1, 1); 1486 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1], 1487 argv + num_unsaved_files + 1 + NumLocations, 1488 argc - num_unsaved_files - 2 - NumLocations, 1489 unsaved_files, 1490 Repeats > 1? 0 : num_unsaved_files, 1491 getDefaultParsingOptions()); 1492 1493 if (!TU) { 1494 fprintf(stderr, "unable to parse input\n"); 1495 return -1; 1496 } 1497 1498 if (checkForErrors(TU) != 0) 1499 return -1; 1500 1501 for (I = 0; I != Repeats; ++I) { 1502 if (Repeats > 1 && 1503 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, 1504 clang_defaultReparseOptions(TU))) { 1505 clang_disposeTranslationUnit(TU); 1506 return 1; 1507 } 1508 1509 if (checkForErrors(TU) != 0) 1510 return -1; 1511 1512 for (Loc = 0; Loc < NumLocations; ++Loc) { 1513 CXFile file = clang_getFile(TU, Locations[Loc].filename); 1514 if (!file) 1515 continue; 1516 1517 Cursor = clang_getCursor(TU, 1518 clang_getLocation(TU, file, Locations[Loc].line, 1519 Locations[Loc].column)); 1520 1521 if (checkForErrors(TU) != 0) 1522 return -1; 1523 1524 if (I + 1 == Repeats) { 1525 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit }; 1526 PrintCursor(Cursor); 1527 printf("\n"); 1528 clang_findReferencesInFile(Cursor, file, visitor); 1529 free(Locations[Loc].filename); 1530 1531 if (checkForErrors(TU) != 0) 1532 return -1; 1533 } 1534 } 1535 } 1536 1537 PrintDiagnostics(TU); 1538 clang_disposeTranslationUnit(TU); 1539 clang_disposeIndex(CIdx); 1540 free(Locations); 1541 free_remapped_files(unsaved_files, num_unsaved_files); 1542 return 0; 1543} 1544 1545typedef struct { 1546 const char *check_prefix; 1547 int first_check_printed; 1548 int fail_for_error; 1549} IndexData; 1550 1551static void printCheck(IndexData *data) { 1552 if (data->check_prefix) { 1553 if (data->first_check_printed) { 1554 printf("// %s-NEXT: ", data->check_prefix); 1555 } else { 1556 printf("// %s : ", data->check_prefix); 1557 data->first_check_printed = 1; 1558 } 1559 } 1560} 1561 1562static void printCXIndexFile(CXIdxClientFile file) { 1563 CXString filename = clang_getFileName((CXFile)file); 1564 printf("%s", clang_getCString(filename)); 1565 clang_disposeString(filename); 1566} 1567 1568static void printCXIndexLoc(CXIdxLoc loc) { 1569 CXString filename; 1570 const char *cname, *end; 1571 CXIdxClientFile file; 1572 unsigned line, column; 1573 int isHeader; 1574 1575 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); 1576 if (line == 0) { 1577 printf("<null loc>"); 1578 return; 1579 } 1580 filename = clang_getFileName((CXFile)file); 1581 cname = clang_getCString(filename); 1582 end = cname + strlen(cname); 1583 isHeader = (end[-2] == '.' && end[-1] == 'h'); 1584 1585 if (isHeader) { 1586 printCXIndexFile(file); 1587 printf(":"); 1588 } 1589 printf("%d:%d", line, column); 1590} 1591 1592static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info, 1593 CXIdxLoc loc) { 1594 const char *name; 1595 char *newStr; 1596 CXIdxClientFile file; 1597 unsigned line, column; 1598 1599 name = info->name; 1600 if (!name) 1601 name = "<anon-tag>"; 1602 1603 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0); 1604 /* FIXME: free these.*/ 1605 newStr = (char *)malloc(strlen(name) + 10); 1606 sprintf(newStr, "%s:%d:%d", name, line, column); 1607 return (CXIdxClientContainer)newStr; 1608} 1609 1610static void printCXIndexContainer(CXIdxClientContainer container) { 1611 printf("[%s]", (const char *)container); 1612} 1613 1614static const char *getEntityKindString(CXIdxEntityKind kind) { 1615 switch (kind) { 1616 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>"; 1617 case CXIdxEntity_Typedef: return "typedef"; 1618 case CXIdxEntity_Function: return "function"; 1619 case CXIdxEntity_Variable: return "variable"; 1620 case CXIdxEntity_Field: return "field"; 1621 case CXIdxEntity_EnumConstant: return "enumerator"; 1622 case CXIdxEntity_ObjCClass: return "objc-class"; 1623 case CXIdxEntity_ObjCProtocol: return "objc-protocol"; 1624 case CXIdxEntity_ObjCCategory: return "objc-category"; 1625 case CXIdxEntity_ObjCMethod: return "objc-method"; 1626 case CXIdxEntity_ObjCProperty: return "objc-property"; 1627 case CXIdxEntity_ObjCIvar: return "objc-ivar"; 1628 case CXIdxEntity_Enum: return "enum"; 1629 case CXIdxEntity_Struct: return "struct"; 1630 case CXIdxEntity_Union: return "union"; 1631 case CXIdxEntity_CXXClass: return "c++-class"; 1632 } 1633 assert(0 && "Garbage entity kind"); 1634 return 0; 1635} 1636 1637static void printEntityInfo(const char *cb, 1638 CXClientData client_data, 1639 const CXIdxEntityInfo *info) { 1640 const char *name; 1641 IndexData *index_data; 1642 index_data = (IndexData *)client_data; 1643 printCheck(index_data); 1644 1645 name = info->name; 1646 if (!name) 1647 name = "<anon-tag>"; 1648 1649 printf("%s: kind: %s", cb, getEntityKindString(info->kind)); 1650 printf(" | name: %s", name); 1651 printf(" | USR: %s", info->USR); 1652} 1653 1654static void index_diagnostic(CXClientData client_data, 1655 CXDiagnostic diag, void *reserved) { 1656 CXString str; 1657 const char *cstr; 1658 IndexData *index_data; 1659 index_data = (IndexData *)client_data; 1660 printCheck(index_data); 1661 1662 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions()); 1663 cstr = clang_getCString(str); 1664 printf("[diagnostic]: %s\n", cstr); 1665 clang_disposeString(str); 1666 1667 if (getenv("CINDEXTEST_FAILONERROR") && 1668 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) { 1669 index_data->fail_for_error = 1; 1670 } 1671} 1672 1673static CXIdxClientFile index_enteredMainFile(CXClientData client_data, 1674 CXFile file, void *reserved) { 1675 IndexData *index_data; 1676 index_data = (IndexData *)client_data; 1677 printCheck(index_data); 1678 1679 printf("[enteredMainFile]: "); 1680 printCXIndexFile((CXIdxClientFile)file); 1681 printf("\n"); 1682 1683 return (CXIdxClientFile)file; 1684} 1685 1686static CXIdxClientFile index_ppIncludedFile(CXClientData client_data, 1687 const CXIdxIncludedFileInfo *info) { 1688 IndexData *index_data; 1689 index_data = (IndexData *)client_data; 1690 printCheck(index_data); 1691 1692 printf("[ppIncludedFile]: "); 1693 printCXIndexFile((CXIdxClientFile)info->file); 1694 printf(" | name: \"%s\"", info->filename); 1695 printf(" | hash loc: "); 1696 printCXIndexLoc(info->hashLoc); 1697 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled); 1698 1699 return (CXIdxClientFile)info->file; 1700} 1701 1702static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data, 1703 void *reserved) { 1704 IndexData *index_data; 1705 index_data = (IndexData *)client_data; 1706 printCheck(index_data); 1707 1708 printf("[startedTranslationUnit]\n"); 1709 return (CXIdxClientContainer)"TU"; 1710} 1711 1712static void index_indexDeclaration(CXClientData client_data, 1713 const CXIdxDeclInfo *info, 1714 const CXIdxDeclOut *outData) { 1715 IndexData *index_data; 1716 const CXIdxObjCCategoryDeclInfo *CatInfo; 1717 const CXIdxObjCInterfaceDeclInfo *InterInfo; 1718 const CXIdxObjCProtocolDeclInfo *ProtoInfo; 1719 unsigned i; 1720 index_data = (IndexData *)client_data; 1721 1722 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo); 1723 printf(" | cursor: "); 1724 PrintCursor(info->cursor); 1725 printf(" | loc: "); 1726 printCXIndexLoc(info->loc); 1727 printf(" | container: "); 1728 printCXIndexContainer(info->container); 1729 printf(" | isRedecl: %d", info->isRedeclaration); 1730 printf(" | isDef: %d\n", info->isDefinition); 1731 1732 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) { 1733 const char *kindName = 0; 1734 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind; 1735 switch (K) { 1736 case CXIdxObjCContainer_ForwardRef: 1737 kindName = "forward-ref"; break; 1738 case CXIdxObjCContainer_Interface: 1739 kindName = "interface"; break; 1740 case CXIdxObjCContainer_Implementation: 1741 kindName = "implementation"; break; 1742 } 1743 printCheck(index_data); 1744 printf(" <ObjCContainerInfo>: kind: %s\n", kindName); 1745 } 1746 1747 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) { 1748 printEntityInfo(" <ObjCCategoryInfo>: class", client_data, 1749 CatInfo->objcClass); 1750 printf("\n"); 1751 } 1752 1753 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) { 1754 if (InterInfo->superInfo) { 1755 printEntityInfo(" <ObjCInterfaceInfo>: base", client_data, 1756 InterInfo->superInfo->base); 1757 printf(" | cursor: "); 1758 PrintCursor(InterInfo->superInfo->cursor); 1759 printf(" | loc: "); 1760 printCXIndexLoc(InterInfo->superInfo->loc); 1761 printf("\n"); 1762 } 1763 for (i = 0; i < InterInfo->numProtocols; ++i) { 1764 printEntityInfo(" <ObjCInterfaceInfo>: protocol", client_data, 1765 InterInfo->protocols[i]->protocol); 1766 printf(" | cursor: "); 1767 PrintCursor(InterInfo->protocols[i]->cursor); 1768 printf(" | loc: "); 1769 printCXIndexLoc(InterInfo->protocols[i]->loc); 1770 printf("\n"); 1771 } 1772 } 1773 1774 if ((ProtoInfo = clang_index_getObjCProtocolDeclInfo(info))) { 1775 for (i = 0; i < ProtoInfo->numProtocols; ++i) { 1776 printEntityInfo(" <ObjCProtocolInfo>: protocol", client_data, 1777 ProtoInfo->protocols[i]->protocol); 1778 printf(" | cursor: "); 1779 PrintCursor(ProtoInfo->protocols[i]->cursor); 1780 printf(" | loc: "); 1781 printCXIndexLoc(ProtoInfo->protocols[i]->loc); 1782 printf("\n"); 1783 } 1784 } 1785 1786 if (outData->outContainer) 1787 *outData->outContainer = makeClientContainer(info->entityInfo, info->loc); 1788} 1789 1790static void index_indexEntityReference(CXClientData client_data, 1791 const CXIdxEntityRefInfo *info) { 1792 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity); 1793 printf(" | cursor: "); 1794 PrintCursor(info->cursor); 1795 printf(" | loc: "); 1796 printCXIndexLoc(info->loc); 1797 printEntityInfo(" | <parent>:", client_data, info->parentEntity); 1798 printf(" | container: "); 1799 printCXIndexContainer(info->container); 1800 printf(" | kind: "); 1801 switch (info->kind) { 1802 case CXIdxEntityRef_Direct: printf("direct"); break; 1803 case CXIdxEntityRef_ImplicitProperty: printf("implicit prop"); break; 1804 } 1805 printf("\n"); 1806} 1807 1808static IndexerCallbacks IndexCB = { 1809 0, /*abortQuery*/ 1810 index_diagnostic, 1811 index_enteredMainFile, 1812 index_ppIncludedFile, 1813 0, /*importedASTFile*/ 1814 index_startedTranslationUnit, 1815 index_indexDeclaration, 1816 index_indexEntityReference 1817}; 1818 1819static int index_file(int argc, const char **argv) { 1820 const char *check_prefix; 1821 CXIndex CIdx; 1822 IndexData index_data; 1823 int result; 1824 1825 check_prefix = 0; 1826 if (argc > 0) { 1827 if (strstr(argv[0], "-check-prefix=") == argv[0]) { 1828 check_prefix = argv[0] + strlen("-check-prefix="); 1829 ++argv; 1830 --argc; 1831 } 1832 } 1833 1834 if (argc == 0) { 1835 fprintf(stderr, "no compiler arguments\n"); 1836 return -1; 1837 } 1838 1839 CIdx = clang_createIndex(0, 1); 1840 index_data.check_prefix = check_prefix; 1841 index_data.first_check_printed = 0; 1842 index_data.fail_for_error = 0; 1843 1844 result = clang_indexTranslationUnit(CIdx, &index_data, 1845 &IndexCB,sizeof(IndexCB), 1846 0, 0, argv, argc, 0, 0, 0, 0); 1847 if (index_data.fail_for_error) 1848 return -1; 1849 1850 return result; 1851} 1852 1853int perform_token_annotation(int argc, const char **argv) { 1854 const char *input = argv[1]; 1855 char *filename = 0; 1856 unsigned line, second_line; 1857 unsigned column, second_column; 1858 CXIndex CIdx; 1859 CXTranslationUnit TU = 0; 1860 int errorCode; 1861 struct CXUnsavedFile *unsaved_files = 0; 1862 int num_unsaved_files = 0; 1863 CXToken *tokens; 1864 unsigned num_tokens; 1865 CXSourceRange range; 1866 CXSourceLocation startLoc, endLoc; 1867 CXFile file = 0; 1868 CXCursor *cursors = 0; 1869 unsigned i; 1870 1871 input += strlen("-test-annotate-tokens="); 1872 if ((errorCode = parse_file_line_column(input, &filename, &line, &column, 1873 &second_line, &second_column))) 1874 return errorCode; 1875 1876 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) 1877 return -1; 1878 1879 CIdx = clang_createIndex(0, 1); 1880 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1], 1881 argv + num_unsaved_files + 2, 1882 argc - num_unsaved_files - 3, 1883 unsaved_files, 1884 num_unsaved_files, 1885 getDefaultParsingOptions()); 1886 if (!TU) { 1887 fprintf(stderr, "unable to parse input\n"); 1888 clang_disposeIndex(CIdx); 1889 free(filename); 1890 free_remapped_files(unsaved_files, num_unsaved_files); 1891 return -1; 1892 } 1893 errorCode = 0; 1894 1895 if (checkForErrors(TU) != 0) 1896 return -1; 1897 1898 if (getenv("CINDEXTEST_EDITING")) { 1899 for (i = 0; i < 5; ++i) { 1900 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, 1901 clang_defaultReparseOptions(TU))) { 1902 fprintf(stderr, "Unable to reparse translation unit!\n"); 1903 errorCode = -1; 1904 goto teardown; 1905 } 1906 } 1907 } 1908 1909 if (checkForErrors(TU) != 0) { 1910 errorCode = -1; 1911 goto teardown; 1912 } 1913 1914 file = clang_getFile(TU, filename); 1915 if (!file) { 1916 fprintf(stderr, "file %s is not in this translation unit\n", filename); 1917 errorCode = -1; 1918 goto teardown; 1919 } 1920 1921 startLoc = clang_getLocation(TU, file, line, column); 1922 if (clang_equalLocations(clang_getNullLocation(), startLoc)) { 1923 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, 1924 column); 1925 errorCode = -1; 1926 goto teardown; 1927 } 1928 1929 endLoc = clang_getLocation(TU, file, second_line, second_column); 1930 if (clang_equalLocations(clang_getNullLocation(), endLoc)) { 1931 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, 1932 second_line, second_column); 1933 errorCode = -1; 1934 goto teardown; 1935 } 1936 1937 range = clang_getRange(startLoc, endLoc); 1938 clang_tokenize(TU, range, &tokens, &num_tokens); 1939 1940 if (checkForErrors(TU) != 0) { 1941 errorCode = -1; 1942 goto teardown; 1943 } 1944 1945 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); 1946 clang_annotateTokens(TU, tokens, num_tokens, cursors); 1947 1948 if (checkForErrors(TU) != 0) { 1949 errorCode = -1; 1950 goto teardown; 1951 } 1952 1953 for (i = 0; i != num_tokens; ++i) { 1954 const char *kind = "<unknown>"; 1955 CXString spelling = clang_getTokenSpelling(TU, tokens[i]); 1956 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); 1957 unsigned start_line, start_column, end_line, end_column; 1958 1959 switch (clang_getTokenKind(tokens[i])) { 1960 case CXToken_Punctuation: kind = "Punctuation"; break; 1961 case CXToken_Keyword: kind = "Keyword"; break; 1962 case CXToken_Identifier: kind = "Identifier"; break; 1963 case CXToken_Literal: kind = "Literal"; break; 1964 case CXToken_Comment: kind = "Comment"; break; 1965 } 1966 clang_getSpellingLocation(clang_getRangeStart(extent), 1967 0, &start_line, &start_column, 0); 1968 clang_getSpellingLocation(clang_getRangeEnd(extent), 1969 0, &end_line, &end_column, 0); 1970 printf("%s: \"%s\" ", kind, clang_getCString(spelling)); 1971 PrintExtent(stdout, start_line, start_column, end_line, end_column); 1972 if (!clang_isInvalid(cursors[i].kind)) { 1973 printf(" "); 1974 PrintCursor(cursors[i]); 1975 } 1976 printf("\n"); 1977 } 1978 free(cursors); 1979 clang_disposeTokens(TU, tokens, num_tokens); 1980 1981 teardown: 1982 PrintDiagnostics(TU); 1983 clang_disposeTranslationUnit(TU); 1984 clang_disposeIndex(CIdx); 1985 free(filename); 1986 free_remapped_files(unsaved_files, num_unsaved_files); 1987 return errorCode; 1988} 1989 1990/******************************************************************************/ 1991/* USR printing. */ 1992/******************************************************************************/ 1993 1994static int insufficient_usr(const char *kind, const char *usage) { 1995 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage); 1996 return 1; 1997} 1998 1999static unsigned isUSR(const char *s) { 2000 return s[0] == 'c' && s[1] == ':'; 2001} 2002 2003static int not_usr(const char *s, const char *arg) { 2004 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg); 2005 return 1; 2006} 2007 2008static void print_usr(CXString usr) { 2009 const char *s = clang_getCString(usr); 2010 printf("%s\n", s); 2011 clang_disposeString(usr); 2012} 2013 2014static void display_usrs() { 2015 fprintf(stderr, "-print-usrs options:\n" 2016 " ObjCCategory <class name> <category name>\n" 2017 " ObjCClass <class name>\n" 2018 " ObjCIvar <ivar name> <class USR>\n" 2019 " ObjCMethod <selector> [0=class method|1=instance method] " 2020 "<class USR>\n" 2021 " ObjCProperty <property name> <class USR>\n" 2022 " ObjCProtocol <protocol name>\n"); 2023} 2024 2025int print_usrs(const char **I, const char **E) { 2026 while (I != E) { 2027 const char *kind = *I; 2028 unsigned len = strlen(kind); 2029 switch (len) { 2030 case 8: 2031 if (memcmp(kind, "ObjCIvar", 8) == 0) { 2032 if (I + 2 >= E) 2033 return insufficient_usr(kind, "<ivar name> <class USR>"); 2034 if (!isUSR(I[2])) 2035 return not_usr("<class USR>", I[2]); 2036 else { 2037 CXString x; 2038 x.data = (void*) I[2]; 2039 x.private_flags = 0; 2040 print_usr(clang_constructUSR_ObjCIvar(I[1], x)); 2041 } 2042 2043 I += 3; 2044 continue; 2045 } 2046 break; 2047 case 9: 2048 if (memcmp(kind, "ObjCClass", 9) == 0) { 2049 if (I + 1 >= E) 2050 return insufficient_usr(kind, "<class name>"); 2051 print_usr(clang_constructUSR_ObjCClass(I[1])); 2052 I += 2; 2053 continue; 2054 } 2055 break; 2056 case 10: 2057 if (memcmp(kind, "ObjCMethod", 10) == 0) { 2058 if (I + 3 >= E) 2059 return insufficient_usr(kind, "<method selector> " 2060 "[0=class method|1=instance method] <class USR>"); 2061 if (!isUSR(I[3])) 2062 return not_usr("<class USR>", I[3]); 2063 else { 2064 CXString x; 2065 x.data = (void*) I[3]; 2066 x.private_flags = 0; 2067 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x)); 2068 } 2069 I += 4; 2070 continue; 2071 } 2072 break; 2073 case 12: 2074 if (memcmp(kind, "ObjCCategory", 12) == 0) { 2075 if (I + 2 >= E) 2076 return insufficient_usr(kind, "<class name> <category name>"); 2077 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2])); 2078 I += 3; 2079 continue; 2080 } 2081 if (memcmp(kind, "ObjCProtocol", 12) == 0) { 2082 if (I + 1 >= E) 2083 return insufficient_usr(kind, "<protocol name>"); 2084 print_usr(clang_constructUSR_ObjCProtocol(I[1])); 2085 I += 2; 2086 continue; 2087 } 2088 if (memcmp(kind, "ObjCProperty", 12) == 0) { 2089 if (I + 2 >= E) 2090 return insufficient_usr(kind, "<property name> <class USR>"); 2091 if (!isUSR(I[2])) 2092 return not_usr("<class USR>", I[2]); 2093 else { 2094 CXString x; 2095 x.data = (void*) I[2]; 2096 x.private_flags = 0; 2097 print_usr(clang_constructUSR_ObjCProperty(I[1], x)); 2098 } 2099 I += 3; 2100 continue; 2101 } 2102 break; 2103 default: 2104 break; 2105 } 2106 break; 2107 } 2108 2109 if (I != E) { 2110 fprintf(stderr, "Invalid USR kind: %s\n", *I); 2111 display_usrs(); 2112 return 1; 2113 } 2114 return 0; 2115} 2116 2117int print_usrs_file(const char *file_name) { 2118 char line[2048]; 2119 const char *args[128]; 2120 unsigned numChars = 0; 2121 2122 FILE *fp = fopen(file_name, "r"); 2123 if (!fp) { 2124 fprintf(stderr, "error: cannot open '%s'\n", file_name); 2125 return 1; 2126 } 2127 2128 /* This code is not really all that safe, but it works fine for testing. */ 2129 while (!feof(fp)) { 2130 char c = fgetc(fp); 2131 if (c == '\n') { 2132 unsigned i = 0; 2133 const char *s = 0; 2134 2135 if (numChars == 0) 2136 continue; 2137 2138 line[numChars] = '\0'; 2139 numChars = 0; 2140 2141 if (line[0] == '/' && line[1] == '/') 2142 continue; 2143 2144 s = strtok(line, " "); 2145 while (s) { 2146 args[i] = s; 2147 ++i; 2148 s = strtok(0, " "); 2149 } 2150 if (print_usrs(&args[0], &args[i])) 2151 return 1; 2152 } 2153 else 2154 line[numChars++] = c; 2155 } 2156 2157 fclose(fp); 2158 return 0; 2159} 2160 2161/******************************************************************************/ 2162/* Command line processing. */ 2163/******************************************************************************/ 2164int write_pch_file(const char *filename, int argc, const char *argv[]) { 2165 CXIndex Idx; 2166 CXTranslationUnit TU; 2167 struct CXUnsavedFile *unsaved_files = 0; 2168 int num_unsaved_files = 0; 2169 int result = 0; 2170 2171 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1); 2172 2173 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { 2174 clang_disposeIndex(Idx); 2175 return -1; 2176 } 2177 2178 TU = clang_parseTranslationUnit(Idx, 0, 2179 argv + num_unsaved_files, 2180 argc - num_unsaved_files, 2181 unsaved_files, 2182 num_unsaved_files, 2183 CXTranslationUnit_Incomplete); 2184 if (!TU) { 2185 fprintf(stderr, "Unable to load translation unit!\n"); 2186 free_remapped_files(unsaved_files, num_unsaved_files); 2187 clang_disposeIndex(Idx); 2188 return 1; 2189 } 2190 2191 switch (clang_saveTranslationUnit(TU, filename, 2192 clang_defaultSaveOptions(TU))) { 2193 case CXSaveError_None: 2194 break; 2195 2196 case CXSaveError_TranslationErrors: 2197 fprintf(stderr, "Unable to write PCH file %s: translation errors\n", 2198 filename); 2199 result = 2; 2200 break; 2201 2202 case CXSaveError_InvalidTU: 2203 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n", 2204 filename); 2205 result = 3; 2206 break; 2207 2208 case CXSaveError_Unknown: 2209 default: 2210 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename); 2211 result = 1; 2212 break; 2213 } 2214 2215 clang_disposeTranslationUnit(TU); 2216 free_remapped_files(unsaved_files, num_unsaved_files); 2217 clang_disposeIndex(Idx); 2218 return result; 2219} 2220 2221/******************************************************************************/ 2222/* Serialized diagnostics. */ 2223/******************************************************************************/ 2224 2225static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) { 2226 switch (error) { 2227 case CXLoadDiag_CannotLoad: return "Cannot Load File"; 2228 case CXLoadDiag_None: break; 2229 case CXLoadDiag_Unknown: return "Unknown"; 2230 case CXLoadDiag_InvalidFile: return "Invalid File"; 2231 } 2232 return "None"; 2233} 2234 2235static const char *getSeverityString(enum CXDiagnosticSeverity severity) { 2236 switch (severity) { 2237 case CXDiagnostic_Note: return "note"; 2238 case CXDiagnostic_Error: return "error"; 2239 case CXDiagnostic_Fatal: return "fatal"; 2240 case CXDiagnostic_Ignored: return "ignored"; 2241 case CXDiagnostic_Warning: return "warning"; 2242 } 2243 return "unknown"; 2244} 2245 2246static void printIndent(unsigned indent) { 2247 if (indent == 0) 2248 return; 2249 fprintf(stderr, "+"); 2250 --indent; 2251 while (indent > 0) { 2252 fprintf(stderr, "-"); 2253 --indent; 2254 } 2255} 2256 2257static void printLocation(CXSourceLocation L) { 2258 CXFile File; 2259 CXString FileName; 2260 unsigned line, column, offset; 2261 2262 clang_getExpansionLocation(L, &File, &line, &column, &offset); 2263 FileName = clang_getFileName(File); 2264 2265 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column); 2266 clang_disposeString(FileName); 2267} 2268 2269static void printRanges(CXDiagnostic D, unsigned indent) { 2270 unsigned i, n = clang_getDiagnosticNumRanges(D); 2271 2272 for (i = 0; i < n; ++i) { 2273 CXSourceLocation Start, End; 2274 CXSourceRange SR = clang_getDiagnosticRange(D, i); 2275 Start = clang_getRangeStart(SR); 2276 End = clang_getRangeEnd(SR); 2277 2278 printIndent(indent); 2279 fprintf(stderr, "Range: "); 2280 printLocation(Start); 2281 fprintf(stderr, " "); 2282 printLocation(End); 2283 fprintf(stderr, "\n"); 2284 } 2285} 2286 2287static void printFixIts(CXDiagnostic D, unsigned indent) { 2288 unsigned i, n = clang_getDiagnosticNumFixIts(D); 2289 for (i = 0 ; i < n; ++i) { 2290 CXSourceRange ReplacementRange; 2291 CXString text; 2292 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange); 2293 2294 printIndent(indent); 2295 fprintf(stderr, "FIXIT: ("); 2296 printLocation(clang_getRangeStart(ReplacementRange)); 2297 fprintf(stderr, " - "); 2298 printLocation(clang_getRangeEnd(ReplacementRange)); 2299 fprintf(stderr, "): \"%s\"\n", clang_getCString(text)); 2300 clang_disposeString(text); 2301 } 2302} 2303 2304static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) { 2305 unsigned i, n; 2306 2307 if (!Diags) 2308 return; 2309 2310 n = clang_getNumDiagnosticsInSet(Diags); 2311 for (i = 0; i < n; ++i) { 2312 CXSourceLocation DiagLoc; 2313 CXDiagnostic D; 2314 CXFile File; 2315 CXString FileName, DiagSpelling, DiagOption; 2316 unsigned line, column, offset; 2317 const char *DiagOptionStr = 0; 2318 2319 D = clang_getDiagnosticInSet(Diags, i); 2320 DiagLoc = clang_getDiagnosticLocation(D); 2321 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset); 2322 FileName = clang_getFileName(File); 2323 DiagSpelling = clang_getDiagnosticSpelling(D); 2324 2325 printIndent(indent); 2326 2327 fprintf(stderr, "%s:%d:%d: %s: %s", 2328 clang_getCString(FileName), 2329 line, 2330 column, 2331 getSeverityString(clang_getDiagnosticSeverity(D)), 2332 clang_getCString(DiagSpelling)); 2333 2334 DiagOption = clang_getDiagnosticOption(D, 0); 2335 DiagOptionStr = clang_getCString(DiagOption); 2336 if (DiagOptionStr) { 2337 fprintf(stderr, " [%s]", DiagOptionStr); 2338 } 2339 2340 fprintf(stderr, "\n"); 2341 2342 printRanges(D, indent); 2343 printFixIts(D, indent); 2344 2345 /* Print subdiagnostics. */ 2346 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2); 2347 2348 clang_disposeString(FileName); 2349 clang_disposeString(DiagSpelling); 2350 clang_disposeString(DiagOption); 2351 } 2352} 2353 2354static int read_diagnostics(const char *filename) { 2355 enum CXLoadDiag_Error error; 2356 CXString errorString; 2357 CXDiagnosticSet Diags = 0; 2358 2359 Diags = clang_loadDiagnostics(filename, &error, &errorString); 2360 if (!Diags) { 2361 fprintf(stderr, "Trouble deserializing file (%s): %s\n", 2362 getDiagnosticCodeStr(error), 2363 clang_getCString(errorString)); 2364 clang_disposeString(errorString); 2365 return 1; 2366 } 2367 2368 printDiagnosticSet(Diags, 0); 2369 fprintf(stderr, "Number of diagnostics: %d\n", 2370 clang_getNumDiagnosticsInSet(Diags)); 2371 clang_disposeDiagnosticSet(Diags); 2372 return 0; 2373} 2374 2375/******************************************************************************/ 2376/* Command line processing. */ 2377/******************************************************************************/ 2378 2379static CXCursorVisitor GetVisitor(const char *s) { 2380 if (s[0] == '\0') 2381 return FilteredPrintingVisitor; 2382 if (strcmp(s, "-usrs") == 0) 2383 return USRVisitor; 2384 if (strncmp(s, "-memory-usage", 13) == 0) 2385 return GetVisitor(s + 13); 2386 return NULL; 2387} 2388 2389static void print_usage(void) { 2390 fprintf(stderr, 2391 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" 2392 " c-index-test -code-completion-timing=<site> <compiler arguments>\n" 2393 " c-index-test -cursor-at=<site> <compiler arguments>\n" 2394 " c-index-test -file-refs-at=<site> <compiler arguments>\n" 2395 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n" 2396 " c-index-test -test-file-scan <AST file> <source file> " 2397 "[FileCheck prefix]\n"); 2398 fprintf(stderr, 2399 " c-index-test -test-load-tu <AST file> <symbol filter> " 2400 "[FileCheck prefix]\n" 2401 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> " 2402 "[FileCheck prefix]\n" 2403 " c-index-test -test-load-source <symbol filter> {<args>}*\n"); 2404 fprintf(stderr, 2405 " c-index-test -test-load-source-memory-usage " 2406 "<symbol filter> {<args>}*\n" 2407 " c-index-test -test-load-source-reparse <trials> <symbol filter> " 2408 " {<args>}*\n" 2409 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n" 2410 " c-index-test -test-load-source-usrs-memory-usage " 2411 "<symbol filter> {<args>}*\n" 2412 " c-index-test -test-annotate-tokens=<range> {<args>}*\n" 2413 " c-index-test -test-inclusion-stack-source {<args>}*\n" 2414 " c-index-test -test-inclusion-stack-tu <AST file>\n"); 2415 fprintf(stderr, 2416 " c-index-test -test-print-linkage-source {<args>}*\n" 2417 " c-index-test -test-print-typekind {<args>}*\n" 2418 " c-index-test -print-usr [<CursorKind> {<args>}]*\n" 2419 " c-index-test -print-usr-file <file>\n" 2420 " c-index-test -write-pch <file> <compiler arguments>\n"); 2421 fprintf(stderr, 2422 " c-index-test -read-diagnostics <file>\n\n"); 2423 fprintf(stderr, 2424 " <symbol filter> values:\n%s", 2425 " all - load all symbols, including those from PCH\n" 2426 " local - load all symbols except those in PCH\n" 2427 " category - only load ObjC categories (non-PCH)\n" 2428 " interface - only load ObjC interfaces (non-PCH)\n" 2429 " protocol - only load ObjC protocols (non-PCH)\n" 2430 " function - only load functions (non-PCH)\n" 2431 " typedef - only load typdefs (non-PCH)\n" 2432 " scan-function - scan function bodies (non-PCH)\n\n"); 2433} 2434 2435/***/ 2436 2437int cindextest_main(int argc, const char **argv) { 2438 clang_enableStackTraces(); 2439 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0) 2440 return read_diagnostics(argv[2]); 2441 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) 2442 return perform_code_completion(argc, argv, 0); 2443 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1]) 2444 return perform_code_completion(argc, argv, 1); 2445 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) 2446 return inspect_cursor_at(argc, argv); 2447 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1]) 2448 return find_file_refs_at(argc, argv); 2449 if (argc > 2 && strcmp(argv[1], "-index-file") == 0) 2450 return index_file(argc - 2, argv + 2); 2451 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { 2452 CXCursorVisitor I = GetVisitor(argv[1] + 13); 2453 if (I) 2454 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I, 2455 NULL); 2456 } 2457 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){ 2458 CXCursorVisitor I = GetVisitor(argv[1] + 25); 2459 if (I) { 2460 int trials = atoi(argv[2]); 2461 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I, 2462 NULL); 2463 } 2464 } 2465 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) { 2466 CXCursorVisitor I = GetVisitor(argv[1] + 17); 2467 2468 PostVisitTU postVisit = 0; 2469 if (strstr(argv[1], "-memory-usage")) 2470 postVisit = PrintMemoryUsage; 2471 2472 if (I) 2473 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, 2474 postVisit); 2475 } 2476 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) 2477 return perform_file_scan(argv[2], argv[3], 2478 argc >= 5 ? argv[4] : 0); 2479 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1]) 2480 return perform_token_annotation(argc, argv); 2481 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0) 2482 return perform_test_load_source(argc - 2, argv + 2, "all", NULL, 2483 PrintInclusionStack); 2484 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) 2485 return perform_test_load_tu(argv[2], "all", NULL, NULL, 2486 PrintInclusionStack); 2487 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0) 2488 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage, 2489 NULL); 2490 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0) 2491 return perform_test_load_source(argc - 2, argv + 2, "all", 2492 PrintTypeKind, 0); 2493 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) { 2494 if (argc > 2) 2495 return print_usrs(argv + 2, argv + argc); 2496 else { 2497 display_usrs(); 2498 return 1; 2499 } 2500 } 2501 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0) 2502 return print_usrs_file(argv[2]); 2503 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0) 2504 return write_pch_file(argv[2], argc - 3, argv + 3); 2505 2506 print_usage(); 2507 return 1; 2508} 2509 2510/***/ 2511 2512/* We intentionally run in a separate thread to ensure we at least minimal 2513 * testing of a multithreaded environment (for example, having a reduced stack 2514 * size). */ 2515 2516typedef struct thread_info { 2517 int argc; 2518 const char **argv; 2519 int result; 2520} thread_info; 2521void thread_runner(void *client_data_v) { 2522 thread_info *client_data = client_data_v; 2523 client_data->result = cindextest_main(client_data->argc, client_data->argv); 2524} 2525 2526int main(int argc, const char **argv) { 2527 thread_info client_data; 2528 2529 if (getenv("CINDEXTEST_NOTHREADS")) 2530 return cindextest_main(argc, argv); 2531 2532 client_data.argc = argc; 2533 client_data.argv = argv; 2534 clang_executeOnThread(thread_runner, &client_data, 0); 2535 return client_data.result; 2536} 2537