c-index-test.c revision f7b714df46cdbdf9e2ebf26eb2fd7881790d83e6
1/* c-index-test.c */ 2 3#include "clang-c/Index.h" 4#include <stdlib.h> 5#include <stdio.h> 6#include <string.h> 7#include <assert.h> 8 9/******************************************************************************/ 10/* Utility functions. */ 11/******************************************************************************/ 12 13#ifdef _MSC_VER 14char *basename(const char* path) 15{ 16 char* base1 = (char*)strrchr(path, '/'); 17 char* base2 = (char*)strrchr(path, '\\'); 18 if (base1 && base2) 19 return((base1 > base2) ? base1 + 1 : base2 + 1); 20 else if (base1) 21 return(base1 + 1); 22 else if (base2) 23 return(base2 + 1); 24 25 return((char*)path); 26} 27#else 28extern char *basename(const char *); 29#endif 30 31static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column, 32 unsigned end_line, unsigned end_column) { 33 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column, 34 end_line, end_column); 35} 36 37static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, 38 CXTranslationUnit *TU) { 39 40 *TU = clang_createTranslationUnit(Idx, file); 41 if (!TU) { 42 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); 43 return 0; 44 } 45 return 1; 46} 47 48void free_remapped_files(struct CXUnsavedFile *unsaved_files, 49 int num_unsaved_files) { 50 int i; 51 for (i = 0; i != num_unsaved_files; ++i) { 52 free((char *)unsaved_files[i].Filename); 53 free((char *)unsaved_files[i].Contents); 54 } 55} 56 57int parse_remapped_files(int argc, const char **argv, int start_arg, 58 struct CXUnsavedFile **unsaved_files, 59 int *num_unsaved_files) { 60 int i; 61 int arg; 62 int prefix_len = strlen("-remap-file="); 63 *unsaved_files = 0; 64 *num_unsaved_files = 0; 65 66 /* Count the number of remapped files. */ 67 for (arg = start_arg; arg < argc; ++arg) { 68 if (strncmp(argv[arg], "-remap-file=", prefix_len)) 69 break; 70 71 ++*num_unsaved_files; 72 } 73 74 if (*num_unsaved_files == 0) 75 return 0; 76 77 *unsaved_files 78 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * 79 *num_unsaved_files); 80 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) { 81 struct CXUnsavedFile *unsaved = *unsaved_files + i; 82 const char *arg_string = argv[arg] + prefix_len; 83 int filename_len; 84 char *filename; 85 char *contents; 86 FILE *to_file; 87 const char *semi = strchr(arg_string, ';'); 88 if (!semi) { 89 fprintf(stderr, 90 "error: -remap-file=from;to argument is missing semicolon\n"); 91 free_remapped_files(*unsaved_files, i); 92 *unsaved_files = 0; 93 *num_unsaved_files = 0; 94 return -1; 95 } 96 97 /* Open the file that we're remapping to. */ 98 to_file = fopen(semi + 1, "r"); 99 if (!to_file) { 100 fprintf(stderr, "error: cannot open file %s that we are remapping to\n", 101 semi + 1); 102 free_remapped_files(*unsaved_files, i); 103 *unsaved_files = 0; 104 *num_unsaved_files = 0; 105 return -1; 106 } 107 108 /* Determine the length of the file we're remapping to. */ 109 fseek(to_file, 0, SEEK_END); 110 unsaved->Length = ftell(to_file); 111 fseek(to_file, 0, SEEK_SET); 112 113 /* Read the contents of the file we're remapping to. */ 114 contents = (char *)malloc(unsaved->Length + 1); 115 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) { 116 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n", 117 (feof(to_file) ? "EOF" : "error"), semi + 1); 118 fclose(to_file); 119 free_remapped_files(*unsaved_files, i); 120 *unsaved_files = 0; 121 *num_unsaved_files = 0; 122 return -1; 123 } 124 contents[unsaved->Length] = 0; 125 unsaved->Contents = contents; 126 127 /* Close the file. */ 128 fclose(to_file); 129 130 /* Copy the file name that we're remapping from. */ 131 filename_len = semi - arg_string; 132 filename = (char *)malloc(filename_len + 1); 133 memcpy(filename, arg_string, filename_len); 134 filename[filename_len] = 0; 135 unsaved->Filename = filename; 136 } 137 138 return 0; 139} 140 141/******************************************************************************/ 142/* Pretty-printing. */ 143/******************************************************************************/ 144 145static void PrintCursor(CXCursor Cursor) { 146 if (clang_isInvalid(Cursor.kind)) { 147 CXString ks = clang_getCursorKindSpelling(Cursor.kind); 148 printf("Invalid Cursor => %s", clang_getCString(ks)); 149 clang_disposeString(ks); 150 } 151 else { 152 CXString string, ks; 153 CXCursor Referenced; 154 unsigned line, column; 155 156 ks = clang_getCursorKindSpelling(Cursor.kind); 157 string = clang_getCursorSpelling(Cursor); 158 printf("%s=%s", clang_getCString(ks), 159 clang_getCString(string)); 160 clang_disposeString(ks); 161 clang_disposeString(string); 162 163 Referenced = clang_getCursorReferenced(Cursor); 164 if (!clang_equalCursors(Referenced, clang_getNullCursor())) { 165 CXSourceLocation Loc = clang_getCursorLocation(Referenced); 166 clang_getInstantiationLocation(Loc, 0, &line, &column, 0); 167 printf(":%d:%d", line, column); 168 } 169 170 if (clang_isCursorDefinition(Cursor)) 171 printf(" (Definition)"); 172 } 173} 174 175static const char* GetCursorSource(CXCursor Cursor) { 176 CXSourceLocation Loc = clang_getCursorLocation(Cursor); 177 CXString source; 178 CXFile file; 179 clang_getInstantiationLocation(Loc, &file, 0, 0, 0); 180 source = clang_getFileName(file); 181 if (!clang_getCString(source)) { 182 clang_disposeString(source); 183 return "<invalid loc>"; 184 } 185 else { 186 const char *b = basename(clang_getCString(source)); 187 clang_disposeString(source); 188 return b; 189 } 190} 191 192/******************************************************************************/ 193/* Callbacks. */ 194/******************************************************************************/ 195 196typedef void (*PostVisitTU)(CXTranslationUnit); 197 198void PrintDiagnostic(CXDiagnostic Diagnostic) { 199 FILE *out = stderr; 200 CXFile file; 201 CXString Msg; 202 unsigned display_opts = CXDiagnostic_DisplaySourceLocation 203 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges; 204 unsigned i, num_fixits; 205 206 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) 207 return; 208 209 Msg = clang_formatDiagnostic(Diagnostic, display_opts); 210 fprintf(stderr, "%s\n", clang_getCString(Msg)); 211 clang_disposeString(Msg); 212 213 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic), 214 &file, 0, 0, 0); 215 if (!file) 216 return; 217 218 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); 219 for (i = 0; i != num_fixits; ++i) { 220 CXSourceRange range; 221 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range); 222 CXSourceLocation start = clang_getRangeStart(range); 223 CXSourceLocation end = clang_getRangeEnd(range); 224 unsigned start_line, start_column, end_line, end_column; 225 CXFile start_file, end_file; 226 clang_getInstantiationLocation(start, &start_file, &start_line, 227 &start_column, 0); 228 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0); 229 if (clang_equalLocations(start, end)) { 230 /* Insertion. */ 231 if (start_file == file) 232 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", 233 clang_getCString(insertion_text), start_line, start_column); 234 } else if (strcmp(clang_getCString(insertion_text), "") == 0) { 235 /* Removal. */ 236 if (start_file == file && end_file == file) { 237 fprintf(out, "FIX-IT: Remove "); 238 PrintExtent(out, start_line, start_column, end_line, end_column); 239 fprintf(out, "\n"); 240 } 241 } else { 242 /* Replacement. */ 243 if (start_file == end_file) { 244 fprintf(out, "FIX-IT: Replace "); 245 PrintExtent(out, start_line, start_column, end_line, end_column); 246 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text)); 247 } 248 break; 249 } 250 clang_disposeString(insertion_text); 251 } 252} 253 254void PrintDiagnostics(CXTranslationUnit TU) { 255 int i, n = clang_getNumDiagnostics(TU); 256 for (i = 0; i != n; ++i) { 257 CXDiagnostic Diag = clang_getDiagnostic(TU, i); 258 PrintDiagnostic(Diag); 259 clang_disposeDiagnostic(Diag); 260 } 261} 262 263/******************************************************************************/ 264/* Logic for testing traversal. */ 265/******************************************************************************/ 266 267static const char *FileCheckPrefix = "CHECK"; 268 269static void PrintCursorExtent(CXCursor C) { 270 CXSourceRange extent = clang_getCursorExtent(C); 271 CXFile begin_file, end_file; 272 unsigned begin_line, begin_column, end_line, end_column; 273 274 clang_getInstantiationLocation(clang_getRangeStart(extent), 275 &begin_file, &begin_line, &begin_column, 0); 276 clang_getInstantiationLocation(clang_getRangeEnd(extent), 277 &end_file, &end_line, &end_column, 0); 278 if (!begin_file || !end_file) 279 return; 280 281 printf(" Extent="); 282 PrintExtent(stdout, begin_line, begin_column, end_line, end_column); 283} 284 285/* Data used by all of the visitors. */ 286typedef struct { 287 CXTranslationUnit TU; 288 enum CXCursorKind *Filter; 289} VisitorData; 290 291 292enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, 293 CXCursor Parent, 294 CXClientData ClientData) { 295 VisitorData *Data = (VisitorData *)ClientData; 296 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { 297 CXSourceLocation Loc = clang_getCursorLocation(Cursor); 298 unsigned line, column; 299 clang_getInstantiationLocation(Loc, 0, &line, &column, 0); 300 printf("// %s: %s:%d:%d: ", FileCheckPrefix, 301 GetCursorSource(Cursor), line, column); 302 PrintCursor(Cursor); 303 PrintCursorExtent(Cursor); 304 printf("\n"); 305 return CXChildVisit_Recurse; 306 } 307 308 return CXChildVisit_Continue; 309} 310 311static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, 312 CXCursor Parent, 313 CXClientData ClientData) { 314 const char *startBuf, *endBuf; 315 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; 316 CXCursor Ref; 317 VisitorData *Data = (VisitorData *)ClientData; 318 319 if (Cursor.kind != CXCursor_FunctionDecl || 320 !clang_isCursorDefinition(Cursor)) 321 return CXChildVisit_Continue; 322 323 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, 324 &startLine, &startColumn, 325 &endLine, &endColumn); 326 /* Probe the entire body, looking for both decls and refs. */ 327 curLine = startLine; 328 curColumn = startColumn; 329 330 while (startBuf < endBuf) { 331 CXSourceLocation Loc; 332 CXFile file; 333 CXString source; 334 335 if (*startBuf == '\n') { 336 startBuf++; 337 curLine++; 338 curColumn = 1; 339 } else if (*startBuf != '\t') 340 curColumn++; 341 342 Loc = clang_getCursorLocation(Cursor); 343 clang_getInstantiationLocation(Loc, &file, 0, 0, 0); 344 345 source = clang_getFileName(file); 346 if (clang_getCString(source)) { 347 CXSourceLocation RefLoc 348 = clang_getLocation(Data->TU, file, curLine, curColumn); 349 Ref = clang_getCursor(Data->TU, RefLoc); 350 if (Ref.kind == CXCursor_NoDeclFound) { 351 /* Nothing found here; that's fine. */ 352 } else if (Ref.kind != CXCursor_FunctionDecl) { 353 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), 354 curLine, curColumn); 355 PrintCursor(Ref); 356 printf("\n"); 357 } 358 } 359 clang_disposeString(source); 360 startBuf++; 361 } 362 363 return CXChildVisit_Continue; 364} 365 366/******************************************************************************/ 367/* USR testing. */ 368/******************************************************************************/ 369 370enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, 371 CXClientData ClientData) { 372 VisitorData *Data = (VisitorData *)ClientData; 373 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) { 374 CXString USR = clang_getCursorUSR(C); 375 if (!clang_getCString(USR)) { 376 clang_disposeString(USR); 377 return CXChildVisit_Continue; 378 } 379 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), 380 clang_getCString(USR)); 381 PrintCursorExtent(C); 382 printf("\n"); 383 clang_disposeString(USR); 384 385 return CXChildVisit_Recurse; 386 } 387 388 return CXChildVisit_Continue; 389} 390 391/******************************************************************************/ 392/* Inclusion stack testing. */ 393/******************************************************************************/ 394 395void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, 396 unsigned includeStackLen, CXClientData data) { 397 398 unsigned i; 399 CXString fname; 400 401 fname = clang_getFileName(includedFile); 402 printf("file: %s\nincluded by:\n", clang_getCString(fname)); 403 clang_disposeString(fname); 404 405 for (i = 0; i < includeStackLen; ++i) { 406 CXFile includingFile; 407 unsigned line, column; 408 clang_getInstantiationLocation(includeStack[i], &includingFile, &line, 409 &column, 0); 410 fname = clang_getFileName(includingFile); 411 printf(" %s:%d:%d\n", clang_getCString(fname), line, column); 412 clang_disposeString(fname); 413 } 414 printf("\n"); 415} 416 417void PrintInclusionStack(CXTranslationUnit TU) { 418 clang_getInclusions(TU, InclusionVisitor, NULL); 419} 420 421/******************************************************************************/ 422/* Linkage testing. */ 423/******************************************************************************/ 424 425static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, 426 CXClientData d) { 427 const char *linkage = 0; 428 429 if (clang_isInvalid(clang_getCursorKind(cursor))) 430 return CXChildVisit_Recurse; 431 432 switch (clang_getCursorLinkage(cursor)) { 433 case CXLinkage_Invalid: break; 434 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break; 435 case CXLinkage_Internal: linkage = "Internal"; break; 436 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break; 437 case CXLinkage_External: linkage = "External"; break; 438 } 439 440 if (linkage) { 441 PrintCursor(cursor); 442 printf("linkage=%s\n", linkage); 443 } 444 445 return CXChildVisit_Recurse; 446} 447 448/******************************************************************************/ 449/* Loading ASTs/source. */ 450/******************************************************************************/ 451 452static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, 453 const char *filter, const char *prefix, 454 CXCursorVisitor Visitor, 455 PostVisitTU PV) { 456 457 if (prefix) 458 FileCheckPrefix = prefix; 459 460 if (Visitor) { 461 enum CXCursorKind K = CXCursor_NotImplemented; 462 enum CXCursorKind *ck = &K; 463 VisitorData Data; 464 465 /* Perform some simple filtering. */ 466 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; 467 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; 468 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; 469 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; 470 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; 471 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; 472 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; 473 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; 474 else { 475 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); 476 return 1; 477 } 478 479 Data.TU = TU; 480 Data.Filter = ck; 481 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); 482 } 483 484 if (PV) 485 PV(TU); 486 487 PrintDiagnostics(TU); 488 clang_disposeTranslationUnit(TU); 489 return 0; 490} 491 492int perform_test_load_tu(const char *file, const char *filter, 493 const char *prefix, CXCursorVisitor Visitor, 494 PostVisitTU PV) { 495 CXIndex Idx; 496 CXTranslationUnit TU; 497 int result; 498 Idx = clang_createIndex(/* excludeDeclsFromPCH */ 499 !strcmp(filter, "local") ? 1 : 0, 500 /* displayDiagnosics=*/1); 501 502 if (!CreateTranslationUnit(Idx, file, &TU)) { 503 clang_disposeIndex(Idx); 504 return 1; 505 } 506 507 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV); 508 clang_disposeIndex(Idx); 509 return result; 510} 511 512int perform_test_load_source(int argc, const char **argv, 513 const char *filter, CXCursorVisitor Visitor, 514 PostVisitTU PV) { 515 const char *UseExternalASTs = 516 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION"); 517 CXIndex Idx; 518 CXTranslationUnit TU; 519 struct CXUnsavedFile *unsaved_files = 0; 520 int num_unsaved_files = 0; 521 int result; 522 523 Idx = clang_createIndex(/* excludeDeclsFromPCH */ 524 !strcmp(filter, "local") ? 1 : 0, 525 /* displayDiagnosics=*/1); 526 527 if (UseExternalASTs && strlen(UseExternalASTs)) 528 clang_setUseExternalASTGeneration(Idx, 1); 529 530 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { 531 clang_disposeIndex(Idx); 532 return -1; 533 } 534 535 TU = clang_createTranslationUnitFromSourceFile(Idx, 0, 536 argc - num_unsaved_files, 537 argv + num_unsaved_files, 538 num_unsaved_files, 539 unsaved_files); 540 if (!TU) { 541 fprintf(stderr, "Unable to load translation unit!\n"); 542 clang_disposeIndex(Idx); 543 return 1; 544 } 545 546 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); 547 free_remapped_files(unsaved_files, num_unsaved_files); 548 clang_disposeIndex(Idx); 549 return result; 550} 551 552/******************************************************************************/ 553/* Logic for testing clang_getCursor(). */ 554/******************************************************************************/ 555 556static void print_cursor_file_scan(CXCursor cursor, 557 unsigned start_line, unsigned start_col, 558 unsigned end_line, unsigned end_col, 559 const char *prefix) { 560 printf("// %s: ", FileCheckPrefix); 561 if (prefix) 562 printf("-%s", prefix); 563 PrintExtent(stdout, start_line, start_col, end_line, end_col); 564 printf(" "); 565 PrintCursor(cursor); 566 printf("\n"); 567} 568 569static int perform_file_scan(const char *ast_file, const char *source_file, 570 const char *prefix) { 571 CXIndex Idx; 572 CXTranslationUnit TU; 573 FILE *fp; 574 CXCursor prevCursor = clang_getNullCursor(); 575 CXFile file; 576 unsigned line = 1, col = 1; 577 unsigned start_line = 1, start_col = 1; 578 579 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, 580 /* displayDiagnosics=*/1))) { 581 fprintf(stderr, "Could not create Index\n"); 582 return 1; 583 } 584 585 if (!CreateTranslationUnit(Idx, ast_file, &TU)) 586 return 1; 587 588 if ((fp = fopen(source_file, "r")) == NULL) { 589 fprintf(stderr, "Could not open '%s'\n", source_file); 590 return 1; 591 } 592 593 file = clang_getFile(TU, source_file); 594 for (;;) { 595 CXCursor cursor; 596 int c = fgetc(fp); 597 598 if (c == '\n') { 599 ++line; 600 col = 1; 601 } else 602 ++col; 603 604 /* Check the cursor at this position, and dump the previous one if we have 605 * found something new. 606 */ 607 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); 608 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && 609 prevCursor.kind != CXCursor_InvalidFile) { 610 print_cursor_file_scan(prevCursor, start_line, start_col, 611 line, col, prefix); 612 start_line = line; 613 start_col = col; 614 } 615 if (c == EOF) 616 break; 617 618 prevCursor = cursor; 619 } 620 621 fclose(fp); 622 return 0; 623} 624 625/******************************************************************************/ 626/* Logic for testing clang_codeComplete(). */ 627/******************************************************************************/ 628 629/* Parse file:line:column from the input string. Returns 0 on success, non-zero 630 on failure. If successful, the pointer *filename will contain newly-allocated 631 memory (that will be owned by the caller) to store the file name. */ 632int parse_file_line_column(const char *input, char **filename, unsigned *line, 633 unsigned *column, unsigned *second_line, 634 unsigned *second_column) { 635 /* Find the second colon. */ 636 const char *last_colon = strrchr(input, ':'); 637 unsigned values[4], i; 638 unsigned num_values = (second_line && second_column)? 4 : 2; 639 640 char *endptr = 0; 641 if (!last_colon || last_colon == input) { 642 if (num_values == 4) 643 fprintf(stderr, "could not parse filename:line:column:line:column in " 644 "'%s'\n", input); 645 else 646 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); 647 return 1; 648 } 649 650 for (i = 0; i != num_values; ++i) { 651 const char *prev_colon; 652 653 /* Parse the next line or column. */ 654 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); 655 if (*endptr != 0 && *endptr != ':') { 656 fprintf(stderr, "could not parse %s in '%s'\n", 657 (i % 2 ? "column" : "line"), input); 658 return 1; 659 } 660 661 if (i + 1 == num_values) 662 break; 663 664 /* Find the previous colon. */ 665 prev_colon = last_colon - 1; 666 while (prev_colon != input && *prev_colon != ':') 667 --prev_colon; 668 if (prev_colon == input) { 669 fprintf(stderr, "could not parse %s in '%s'\n", 670 (i % 2 == 0? "column" : "line"), input); 671 return 1; 672 } 673 674 last_colon = prev_colon; 675 } 676 677 *line = values[0]; 678 *column = values[1]; 679 680 if (second_line && second_column) { 681 *second_line = values[2]; 682 *second_column = values[3]; 683 } 684 685 /* Copy the file name. */ 686 *filename = (char*)malloc(last_colon - input + 1); 687 memcpy(*filename, input, last_colon - input); 688 (*filename)[last_colon - input] = 0; 689 return 0; 690} 691 692const char * 693clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { 694 switch (Kind) { 695 case CXCompletionChunk_Optional: return "Optional"; 696 case CXCompletionChunk_TypedText: return "TypedText"; 697 case CXCompletionChunk_Text: return "Text"; 698 case CXCompletionChunk_Placeholder: return "Placeholder"; 699 case CXCompletionChunk_Informative: return "Informative"; 700 case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; 701 case CXCompletionChunk_LeftParen: return "LeftParen"; 702 case CXCompletionChunk_RightParen: return "RightParen"; 703 case CXCompletionChunk_LeftBracket: return "LeftBracket"; 704 case CXCompletionChunk_RightBracket: return "RightBracket"; 705 case CXCompletionChunk_LeftBrace: return "LeftBrace"; 706 case CXCompletionChunk_RightBrace: return "RightBrace"; 707 case CXCompletionChunk_LeftAngle: return "LeftAngle"; 708 case CXCompletionChunk_RightAngle: return "RightAngle"; 709 case CXCompletionChunk_Comma: return "Comma"; 710 case CXCompletionChunk_ResultType: return "ResultType"; 711 case CXCompletionChunk_Colon: return "Colon"; 712 case CXCompletionChunk_SemiColon: return "SemiColon"; 713 case CXCompletionChunk_Equal: return "Equal"; 714 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; 715 case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; 716 } 717 718 return "Unknown"; 719} 720 721void print_completion_string(CXCompletionString completion_string, FILE *file) { 722 int I, N; 723 724 N = clang_getNumCompletionChunks(completion_string); 725 for (I = 0; I != N; ++I) { 726 CXString text; 727 const char *cstr; 728 enum CXCompletionChunkKind Kind 729 = clang_getCompletionChunkKind(completion_string, I); 730 731 if (Kind == CXCompletionChunk_Optional) { 732 fprintf(file, "{Optional "); 733 print_completion_string( 734 clang_getCompletionChunkCompletionString(completion_string, I), 735 file); 736 fprintf(file, "}"); 737 continue; 738 } 739 740 text = clang_getCompletionChunkText(completion_string, I); 741 cstr = clang_getCString(text); 742 fprintf(file, "{%s %s}", 743 clang_getCompletionChunkKindSpelling(Kind), 744 cstr ? cstr : ""); 745 clang_disposeString(text); 746 } 747 748} 749 750void print_completion_result(CXCompletionResult *completion_result, 751 CXClientData client_data) { 752 FILE *file = (FILE *)client_data; 753 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); 754 755 fprintf(file, "%s:", clang_getCString(ks)); 756 clang_disposeString(ks); 757 758 print_completion_string(completion_result->CompletionString, file); 759 fprintf(file, "\n"); 760} 761 762int perform_code_completion(int argc, const char **argv) { 763 const char *input = argv[1]; 764 char *filename = 0; 765 unsigned line; 766 unsigned column; 767 CXIndex CIdx; 768 int errorCode; 769 struct CXUnsavedFile *unsaved_files = 0; 770 int num_unsaved_files = 0; 771 CXCodeCompleteResults *results = 0; 772 773 input += strlen("-code-completion-at="); 774 if ((errorCode = parse_file_line_column(input, &filename, &line, &column, 775 0, 0))) 776 return errorCode; 777 778 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) 779 return -1; 780 781 CIdx = clang_createIndex(0, 1); 782 results = clang_codeComplete(CIdx, 783 argv[argc - 1], argc - num_unsaved_files - 3, 784 argv + num_unsaved_files + 2, 785 num_unsaved_files, unsaved_files, 786 filename, line, column); 787 788 if (results) { 789 unsigned i, n = results->NumResults; 790 for (i = 0; i != n; ++i) 791 print_completion_result(results->Results + i, stdout); 792 n = clang_codeCompleteGetNumDiagnostics(results); 793 for (i = 0; i != n; ++i) { 794 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); 795 PrintDiagnostic(diag); 796 clang_disposeDiagnostic(diag); 797 } 798 clang_disposeCodeCompleteResults(results); 799 } 800 801 clang_disposeIndex(CIdx); 802 free(filename); 803 804 free_remapped_files(unsaved_files, num_unsaved_files); 805 806 return 0; 807} 808 809typedef struct { 810 char *filename; 811 unsigned line; 812 unsigned column; 813} CursorSourceLocation; 814 815int inspect_cursor_at(int argc, const char **argv) { 816 CXIndex CIdx; 817 int errorCode; 818 struct CXUnsavedFile *unsaved_files = 0; 819 int num_unsaved_files = 0; 820 CXTranslationUnit TU; 821 CXCursor Cursor; 822 CursorSourceLocation *Locations = 0; 823 unsigned NumLocations = 0, Loc; 824 825 /* Count the number of locations. */ 826 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1]) 827 ++NumLocations; 828 829 /* Parse the locations. */ 830 assert(NumLocations > 0 && "Unable to count locations?"); 831 Locations = (CursorSourceLocation *)malloc( 832 NumLocations * sizeof(CursorSourceLocation)); 833 for (Loc = 0; Loc < NumLocations; ++Loc) { 834 const char *input = argv[Loc + 1] + strlen("-cursor-at="); 835 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, 836 &Locations[Loc].line, 837 &Locations[Loc].column, 0, 0))) 838 return errorCode; 839 } 840 841 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, 842 &num_unsaved_files)) 843 return -1; 844 845 CIdx = clang_createIndex(0, 1); 846 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], 847 argc - num_unsaved_files - 2 - NumLocations, 848 argv + num_unsaved_files + 1 + NumLocations, 849 num_unsaved_files, 850 unsaved_files); 851 if (!TU) { 852 fprintf(stderr, "unable to parse input\n"); 853 return -1; 854 } 855 856 for (Loc = 0; Loc < NumLocations; ++Loc) { 857 CXFile file = clang_getFile(TU, Locations[Loc].filename); 858 if (!file) 859 continue; 860 861 Cursor = clang_getCursor(TU, 862 clang_getLocation(TU, file, Locations[Loc].line, 863 Locations[Loc].column)); 864 PrintCursor(Cursor); 865 printf("\n"); 866 free(Locations[Loc].filename); 867 } 868 869 PrintDiagnostics(TU); 870 clang_disposeTranslationUnit(TU); 871 clang_disposeIndex(CIdx); 872 free(Locations); 873 free_remapped_files(unsaved_files, num_unsaved_files); 874 return 0; 875} 876 877int perform_token_annotation(int argc, const char **argv) { 878 const char *input = argv[1]; 879 char *filename = 0; 880 unsigned line, second_line; 881 unsigned column, second_column; 882 CXIndex CIdx; 883 CXTranslationUnit TU = 0; 884 int errorCode; 885 struct CXUnsavedFile *unsaved_files = 0; 886 int num_unsaved_files = 0; 887 CXToken *tokens; 888 unsigned num_tokens; 889 CXSourceRange range; 890 CXSourceLocation startLoc, endLoc; 891 CXFile file = 0; 892 CXCursor *cursors = 0; 893 unsigned i; 894 895 input += strlen("-test-annotate-tokens="); 896 if ((errorCode = parse_file_line_column(input, &filename, &line, &column, 897 &second_line, &second_column))) 898 return errorCode; 899 900 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) 901 return -1; 902 903 CIdx = clang_createIndex(0, 1); 904 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], 905 argc - num_unsaved_files - 3, 906 argv + num_unsaved_files + 2, 907 num_unsaved_files, 908 unsaved_files); 909 if (!TU) { 910 fprintf(stderr, "unable to parse input\n"); 911 clang_disposeIndex(CIdx); 912 free(filename); 913 free_remapped_files(unsaved_files, num_unsaved_files); 914 return -1; 915 } 916 errorCode = 0; 917 918 file = clang_getFile(TU, filename); 919 if (!file) { 920 fprintf(stderr, "file %s is not in this translation unit\n", filename); 921 errorCode = -1; 922 goto teardown; 923 } 924 925 startLoc = clang_getLocation(TU, file, line, column); 926 if (clang_equalLocations(clang_getNullLocation(), startLoc)) { 927 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, 928 column); 929 errorCode = -1; 930 goto teardown; 931 } 932 933 endLoc = clang_getLocation(TU, file, second_line, second_column); 934 if (clang_equalLocations(clang_getNullLocation(), endLoc)) { 935 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, 936 second_line, second_column); 937 errorCode = -1; 938 goto teardown; 939 } 940 941 range = clang_getRange(startLoc, endLoc); 942 clang_tokenize(TU, range, &tokens, &num_tokens); 943 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); 944 clang_annotateTokens(TU, tokens, num_tokens, cursors); 945 for (i = 0; i != num_tokens; ++i) { 946 const char *kind = "<unknown>"; 947 CXString spelling = clang_getTokenSpelling(TU, tokens[i]); 948 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); 949 unsigned start_line, start_column, end_line, end_column; 950 951 switch (clang_getTokenKind(tokens[i])) { 952 case CXToken_Punctuation: kind = "Punctuation"; break; 953 case CXToken_Keyword: kind = "Keyword"; break; 954 case CXToken_Identifier: kind = "Identifier"; break; 955 case CXToken_Literal: kind = "Literal"; break; 956 case CXToken_Comment: kind = "Comment"; break; 957 } 958 clang_getInstantiationLocation(clang_getRangeStart(extent), 959 0, &start_line, &start_column, 0); 960 clang_getInstantiationLocation(clang_getRangeEnd(extent), 961 0, &end_line, &end_column, 0); 962 printf("%s: \"%s\" ", kind, clang_getCString(spelling)); 963 PrintExtent(stdout, start_line, start_column, end_line, end_column); 964 if (!clang_isInvalid(cursors[i].kind)) { 965 printf(" "); 966 PrintCursor(cursors[i]); 967 } 968 printf("\n"); 969 } 970 free(cursors); 971 972 teardown: 973 PrintDiagnostics(TU); 974 clang_disposeTranslationUnit(TU); 975 clang_disposeIndex(CIdx); 976 free(filename); 977 free_remapped_files(unsaved_files, num_unsaved_files); 978 return errorCode; 979} 980 981/******************************************************************************/ 982/* USR printing. */ 983/******************************************************************************/ 984 985static int insufficient_usr(const char *kind, const char *usage) { 986 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage); 987 return 1; 988} 989 990static unsigned isUSR(const char *s) { 991 return s[0] == 'c' && s[1] == ':'; 992} 993 994static int not_usr(const char *s, const char *arg) { 995 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg); 996 return 1; 997} 998 999static void print_usr(CXString usr) { 1000 const char *s = clang_getCString(usr); 1001 printf("%s\n", s); 1002 clang_disposeString(usr); 1003} 1004 1005static void display_usrs() { 1006 fprintf(stderr, "-print-usrs options:\n" 1007 " ObjCCategory <class name> <category name>\n" 1008 " ObjCClass <class name>\n" 1009 " ObjCIvar <ivar name> <class USR>\n" 1010 " ObjCMethod <selector> [0=class method|1=instance method] " 1011 "<class USR>\n" 1012 " ObjCProperty <property name> <class USR>\n" 1013 " ObjCProtocol <protocol name>\n"); 1014} 1015 1016int print_usrs(const char **I, const char **E) { 1017 while (I != E) { 1018 const char *kind = *I; 1019 unsigned len = strlen(kind); 1020 switch (len) { 1021 case 8: 1022 if (memcmp(kind, "ObjCIvar", 8) == 0) { 1023 if (I + 2 >= E) 1024 return insufficient_usr(kind, "<ivar name> <class USR>"); 1025 if (!isUSR(I[2])) 1026 return not_usr("<class USR>", I[2]); 1027 else { 1028 CXString x; 1029 x.Spelling = I[2]; 1030 x.MustFreeString = 0; 1031 print_usr(clang_constructUSR_ObjCIvar(I[1], x)); 1032 } 1033 1034 I += 3; 1035 continue; 1036 } 1037 break; 1038 case 9: 1039 if (memcmp(kind, "ObjCClass", 9) == 0) { 1040 if (I + 1 >= E) 1041 return insufficient_usr(kind, "<class name>"); 1042 print_usr(clang_constructUSR_ObjCClass(I[1])); 1043 I += 2; 1044 continue; 1045 } 1046 break; 1047 case 10: 1048 if (memcmp(kind, "ObjCMethod", 10) == 0) { 1049 if (I + 3 >= E) 1050 return insufficient_usr(kind, "<method selector> " 1051 "[0=class method|1=instance method] <class USR>"); 1052 if (!isUSR(I[3])) 1053 return not_usr("<class USR>", I[3]); 1054 else { 1055 CXString x; 1056 x.Spelling = I[3]; 1057 x.MustFreeString = 0; 1058 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x)); 1059 } 1060 I += 4; 1061 continue; 1062 } 1063 break; 1064 case 12: 1065 if (memcmp(kind, "ObjCCategory", 12) == 0) { 1066 if (I + 2 >= E) 1067 return insufficient_usr(kind, "<class name> <category name>"); 1068 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2])); 1069 I += 3; 1070 continue; 1071 } 1072 if (memcmp(kind, "ObjCProtocol", 12) == 0) { 1073 if (I + 1 >= E) 1074 return insufficient_usr(kind, "<protocol name>"); 1075 print_usr(clang_constructUSR_ObjCProtocol(I[1])); 1076 I += 2; 1077 continue; 1078 } 1079 if (memcmp(kind, "ObjCProperty", 12) == 0) { 1080 if (I + 2 >= E) 1081 return insufficient_usr(kind, "<property name> <class USR>"); 1082 if (!isUSR(I[2])) 1083 return not_usr("<class USR>", I[2]); 1084 else { 1085 CXString x; 1086 x.Spelling = I[2]; 1087 x.MustFreeString = 0; 1088 print_usr(clang_constructUSR_ObjCProperty(I[1], x)); 1089 } 1090 I += 3; 1091 continue; 1092 } 1093 break; 1094 default: 1095 break; 1096 } 1097 break; 1098 } 1099 1100 if (I != E) { 1101 fprintf(stderr, "Invalid USR kind: %s\n", *I); 1102 display_usrs(); 1103 return 1; 1104 } 1105 return 0; 1106} 1107 1108int print_usrs_file(const char *file_name) { 1109 char line[2048]; 1110 const char *args[128]; 1111 unsigned numChars = 0; 1112 1113 FILE *fp = fopen(file_name, "r"); 1114 if (!fp) { 1115 fprintf(stderr, "error: cannot open '%s'\n", file_name); 1116 return 1; 1117 } 1118 1119 /* This code is not really all that safe, but it works fine for testing. */ 1120 while (!feof(fp)) { 1121 char c = fgetc(fp); 1122 if (c == '\n') { 1123 unsigned i = 0; 1124 const char *s = 0; 1125 1126 if (numChars == 0) 1127 continue; 1128 1129 line[numChars] = '\0'; 1130 numChars = 0; 1131 1132 if (line[0] == '/' && line[1] == '/') 1133 continue; 1134 1135 s = strtok(line, " "); 1136 while (s) { 1137 args[i] = s; 1138 ++i; 1139 s = strtok(0, " "); 1140 } 1141 if (print_usrs(&args[0], &args[i])) 1142 return 1; 1143 } 1144 else 1145 line[numChars++] = c; 1146 } 1147 1148 fclose(fp); 1149 return 0; 1150} 1151 1152/******************************************************************************/ 1153/* Command line processing. */ 1154/******************************************************************************/ 1155 1156static CXCursorVisitor GetVisitor(const char *s) { 1157 if (s[0] == '\0') 1158 return FilteredPrintingVisitor; 1159 if (strcmp(s, "-usrs") == 0) 1160 return USRVisitor; 1161 return NULL; 1162} 1163 1164static void print_usage(void) { 1165 fprintf(stderr, 1166 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n" 1167 " c-index-test -cursor-at=<site> <compiler arguments>\n" 1168 " c-index-test -test-file-scan <AST file> <source file> " 1169 "[FileCheck prefix]\n" 1170 " c-index-test -test-load-tu <AST file> <symbol filter> " 1171 "[FileCheck prefix]\n" 1172 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> " 1173 "[FileCheck prefix]\n" 1174 " c-index-test -test-load-source <symbol filter> {<args>}*\n" 1175 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"); 1176 fprintf(stderr, 1177 " c-index-test -test-annotate-tokens=<range> {<args>}*\n" 1178 " c-index-test -test-inclusion-stack-source {<args>}*\n" 1179 " c-index-test -test-inclusion-stack-tu <AST file>\n" 1180 " c-index-test -test-print-linkage-source {<args>}*\n" 1181 " c-index-test -print-usr [<CursorKind> {<args>}]*\n" 1182 " c-index-test -print-usr-file <file>\n\n" 1183 " <symbol filter> values:\n%s", 1184 " all - load all symbols, including those from PCH\n" 1185 " local - load all symbols except those in PCH\n" 1186 " category - only load ObjC categories (non-PCH)\n" 1187 " interface - only load ObjC interfaces (non-PCH)\n" 1188 " protocol - only load ObjC protocols (non-PCH)\n" 1189 " function - only load functions (non-PCH)\n" 1190 " typedef - only load typdefs (non-PCH)\n" 1191 " scan-function - scan function bodies (non-PCH)\n\n"); 1192} 1193 1194int main(int argc, const char **argv) { 1195 clang_enableStackTraces(); 1196 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1]) 1197 return perform_code_completion(argc, argv); 1198 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1]) 1199 return inspect_cursor_at(argc, argv); 1200 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) { 1201 CXCursorVisitor I = GetVisitor(argv[1] + 13); 1202 if (I) 1203 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I, 1204 NULL); 1205 } 1206 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) { 1207 CXCursorVisitor I = GetVisitor(argv[1] + 17); 1208 if (I) 1209 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL); 1210 } 1211 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0) 1212 return perform_file_scan(argv[2], argv[3], 1213 argc >= 5 ? argv[4] : 0); 1214 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1]) 1215 return perform_token_annotation(argc, argv); 1216 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0) 1217 return perform_test_load_source(argc - 2, argv + 2, "all", NULL, 1218 PrintInclusionStack); 1219 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) 1220 return perform_test_load_tu(argv[2], "all", NULL, NULL, 1221 PrintInclusionStack); 1222 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0) 1223 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage, 1224 NULL); 1225 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) { 1226 if (argc > 2) 1227 return print_usrs(argv + 2, argv + argc); 1228 else { 1229 display_usrs(); 1230 return 1; 1231 } 1232 } 1233 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0) 1234 return print_usrs_file(argv[2]); 1235 1236 print_usage(); 1237 return 1; 1238} 1239