void PrintDiagnostic(CXDiagnostic Diagnostic) { FILE *out = stderr; CXFile file; CXString Msg; unsigned display_opts = CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges; unsigned i, num_fixits; if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) return; Msg = clang_formatDiagnostic(Diagnostic, display_opts); fprintf(stderr, "%s\n", clang_getCString(Msg)); clang_disposeString(Msg); clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic), &file, 0, 0, 0); if (!file) return; num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); for (i = 0; i != num_fixits; ++i) { CXSourceRange range; CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range); CXSourceLocation start = clang_getRangeStart(range); CXSourceLocation end = clang_getRangeEnd(range); unsigned start_line, start_column, end_line, end_column; CXFile start_file, end_file; clang_getInstantiationLocation(start, &start_file, &start_line, &start_column, 0); clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0); if (clang_equalLocations(start, end)) { /* Insertion. */ if (start_file == file) fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", clang_getCString(insertion_text), start_line, start_column); } else if (strcmp(clang_getCString(insertion_text), "") == 0) { /* Removal. */ if (start_file == file && end_file == file) { fprintf(out, "FIX-IT: Remove "); PrintExtent(out, start_line, start_column, end_line, end_column); fprintf(out, "\n"); } } else { /* Replacement. */ if (start_file == end_file) { fprintf(out, "FIX-IT: Replace "); PrintExtent(out, start_line, start_column, end_line, end_column); fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text)); } break; } clang_disposeString(insertion_text); } }
static void print_cursor_file_scan(CXCursor cursor, unsigned start_line, unsigned start_col, unsigned end_line, unsigned end_col, const char *prefix) { printf("// %s: ", FileCheckPrefix); if (prefix) printf("-%s", prefix); PrintExtent(stdout, start_line, start_col, end_line, end_col); printf(" "); PrintCursor(cursor); printf("\n"); }
static void PrintCursorExtent(CXCursor C) { CXSourceRange extent = clang_getCursorExtent(C); CXFile begin_file, end_file; unsigned begin_line, begin_column, end_line, end_column; clang_getInstantiationLocation(clang_getRangeStart(extent), &begin_file, &begin_line, &begin_column, 0); clang_getInstantiationLocation(clang_getRangeEnd(extent), &end_file, &end_line, &end_column, 0); if (!begin_file || !end_file) return; printf(" Extent="); PrintExtent(stdout, begin_line, begin_column, end_line, end_column); }
int perform_token_annotation(int argc, const char **argv) { const char *input = argv[1]; char *filename = 0; unsigned line, second_line; unsigned column, second_column; CXIndex CIdx; CXTranslationUnit TU = 0; int errorCode; struct CXUnsavedFile *unsaved_files = 0; int num_unsaved_files = 0; CXToken *tokens; unsigned num_tokens; CXSourceRange range; CXSourceLocation startLoc, endLoc; CXFile file = 0; CXCursor *cursors = 0; unsigned i; input += strlen("-test-annotate-tokens="); if ((errorCode = parse_file_line_column(input, &filename, &line, &column, &second_line, &second_column))) return errorCode; if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) return -1; CIdx = clang_createIndex(0, 1); TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], argc - num_unsaved_files - 3, argv + num_unsaved_files + 2, num_unsaved_files, unsaved_files); if (!TU) { fprintf(stderr, "unable to parse input\n"); clang_disposeIndex(CIdx); free(filename); free_remapped_files(unsaved_files, num_unsaved_files); return -1; } errorCode = 0; file = clang_getFile(TU, filename); if (!file) { fprintf(stderr, "file %s is not in this translation unit\n", filename); errorCode = -1; goto teardown; } startLoc = clang_getLocation(TU, file, line, column); if (clang_equalLocations(clang_getNullLocation(), startLoc)) { fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, column); errorCode = -1; goto teardown; } endLoc = clang_getLocation(TU, file, second_line, second_column); if (clang_equalLocations(clang_getNullLocation(), endLoc)) { fprintf(stderr, "invalid source location %s:%d:%d\n", filename, second_line, second_column); errorCode = -1; goto teardown; } range = clang_getRange(startLoc, endLoc); clang_tokenize(TU, range, &tokens, &num_tokens); cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor)); clang_annotateTokens(TU, tokens, num_tokens, cursors); for (i = 0; i != num_tokens; ++i) { const char *kind = "<unknown>"; CXString spelling = clang_getTokenSpelling(TU, tokens[i]); CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]); unsigned start_line, start_column, end_line, end_column; switch (clang_getTokenKind(tokens[i])) { case CXToken_Punctuation: kind = "Punctuation"; break; case CXToken_Keyword: kind = "Keyword"; break; case CXToken_Identifier: kind = "Identifier"; break; case CXToken_Literal: kind = "Literal"; break; case CXToken_Comment: kind = "Comment"; break; } clang_getInstantiationLocation(clang_getRangeStart(extent), 0, &start_line, &start_column, 0); clang_getInstantiationLocation(clang_getRangeEnd(extent), 0, &end_line, &end_column, 0); printf("%s: \"%s\" ", kind, clang_getCString(spelling)); PrintExtent(stdout, start_line, start_column, end_line, end_column); if (!clang_isInvalid(cursors[i].kind)) { printf(" "); PrintCursor(cursors[i]); } printf("\n"); } free(cursors); teardown: PrintDiagnostics(TU); clang_disposeTranslationUnit(TU); clang_disposeIndex(CIdx); free(filename); free_remapped_files(unsaved_files, num_unsaved_files); return errorCode; }
void PrintDiagnostic(CXDiagnostic Diagnostic) { FILE *out = stderr; CXFile file; CXString text; unsigned display_opts = CXDiagnostic_DisplaySourceLocation | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges; unsigned i, num_fixits; clang_displayDiagnostic(Diagnostic, out, display_opts); if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) return; clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic), &file, 0, 0, 0); if (!file) return; num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); for (i = 0; i != num_fixits; ++i) { switch (clang_getDiagnosticFixItKind(Diagnostic, i)) { case CXFixIt_Insertion: { CXSourceLocation insertion_loc; CXFile insertion_file; unsigned insertion_line, insertion_column; text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc); clang_getInstantiationLocation(insertion_loc, &insertion_file, &insertion_line, &insertion_column, 0); if (insertion_file == file) fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", clang_getCString(text), insertion_line, insertion_column); clang_disposeString(text); break; } case CXFixIt_Removal: { CXFile start_file, end_file; unsigned start_line, start_column, end_line, end_column; CXSourceRange remove_range = clang_getDiagnosticFixItRemoval(Diagnostic, i); clang_getInstantiationLocation(clang_getRangeStart(remove_range), &start_file, &start_line, &start_column, 0); clang_getInstantiationLocation(clang_getRangeEnd(remove_range), &end_file, &end_line, &end_column, 0); if (start_file == file && end_file == file) { fprintf(out, "FIX-IT: Remove "); PrintExtent(out, start_line, start_column, end_line, end_column); fprintf(out, "\n"); } break; } case CXFixIt_Replacement: { CXFile start_file, end_file; unsigned start_line, start_column, end_line, end_column; CXSourceRange remove_range; text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range); clang_getInstantiationLocation(clang_getRangeStart(remove_range), &start_file, &start_line, &start_column, 0); clang_getInstantiationLocation(clang_getRangeEnd(remove_range), &end_file, &end_line, &end_column, 0); if (start_file == end_file) { fprintf(out, "FIX-IT: Replace "); PrintExtent(out, start_line, start_column, end_line, end_column); fprintf(out, " with \"%s\"\n", clang_getCString(text)); } clang_disposeString(text); break; } } } }