Created
April 22, 2014 22:11
-
-
Save bolinfest/11196200 to your computer and use it in GitHub Desktop.
Dump source ranges for diagnostics using libclang.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <clang-c/Index.h> | |
#include <stdio.h> | |
void parseMalformedFile(char* source) { | |
CXIndex index = clang_createIndex( | |
/* excludeDeclarationsFromPCH */ 0, | |
/* displayDiagnostics */ 0); | |
const char *args[] = { | |
"-I/usr/include", | |
"-I.", | |
"-fdiagnostics-print-source-range-info", | |
}; | |
int numArgs = sizeof(args) / sizeof(*args); | |
CXTranslationUnit tu = clang_parseTranslationUnit(index, source, args, numArgs, NULL, 0, CXTranslationUnit_None); | |
unsigned diagnosticCount = clang_getNumDiagnostics(tu); | |
printf("Diagnostic count: %d\n", diagnosticCount); | |
for (unsigned i = 0; i < diagnosticCount; i++) { | |
CXDiagnostic diagnostic = clang_getDiagnostic(tu, i); | |
CXString text = clang_getDiagnosticSpelling(diagnostic); | |
printf("\nDiagnostic spelling: %s\n", clang_getCString(text)); | |
clang_disposeString(text); | |
unsigned options = CXDiagnostic_DisplaySourceLocation | | |
CXDiagnostic_DisplayColumn | | |
CXDiagnostic_DisplayOption | | |
CXDiagnostic_DisplaySourceRanges | | |
CXDiagnostic_DisplayCategoryId | | |
CXDiagnostic_DisplayCategoryName; | |
CXString formattedDiagnostic = clang_formatDiagnostic(diagnostic, options); | |
printf("Formatted diagnostic: %s\n", clang_getCString(formattedDiagnostic)); | |
clang_disposeString(formattedDiagnostic); | |
// List the ranges. | |
unsigned rangeCount = clang_getDiagnosticNumRanges(diagnostic); | |
if (rangeCount == 0) { | |
printf("No source ranges for diagnostic.\n"); | |
} | |
for (unsigned j = 0; j < rangeCount; j++) { | |
CXSourceRange range = clang_getDiagnosticRange(diagnostic, j); | |
unsigned startLine, startColumn; | |
CXSourceLocation start = clang_getRangeStart(range); | |
clang_getSpellingLocation(start, /* file */ NULL, &startLine, &startColumn, /* offset */ NULL); | |
unsigned endLine, endColumn; | |
CXSourceLocation end = clang_getRangeEnd(range); | |
clang_getSpellingLocation(end, /* file */ NULL, &endLine, &endColumn, /* offset */ NULL); | |
printf("Range: %d,%d;%d,%d\n", startLine, startColumn, endLine, endColumn); | |
} | |
clang_disposeDiagnostic(diagnostic); | |
} | |
clang_disposeTranslationUnit(tu); | |
clang_disposeIndex(index); | |
} | |
int main(int argc, char **argv) { | |
if (argc != 2) { | |
printf("ERROR: Must specify a single file to compile.\n"); | |
return 1; | |
} | |
char* source = argv[1]; | |
parseMalformedFile(source); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ther is an error, you need add -I/usr/lib/llvm-3.8/lib/clang/3.8.0/include (the number depend on your libclang version)