Last active
March 24, 2018 08:37
-
-
Save micbou/f13cf9388c0d898faf0aabc03e694408 to your computer and use it in GitHub Desktop.
Show diagnostics of a file 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 <iostream> | |
#include <clang-c/Index.h> | |
using namespace std; | |
const char *flags[] = { | |
"clang", | |
}; | |
int main(int argc, const char *argv[]) { | |
if (argc != 2) { | |
cout << argv[0] << " file.cpp" << endl; | |
return EXIT_FAILURE; | |
} | |
CXIndex index = clang_createIndex(0, 0); | |
if (!index) { | |
cerr << "createIndex failed" << endl; | |
return EXIT_FAILURE; | |
} | |
unsigned options = clang_defaultEditingTranslationUnitOptions(); | |
CXTranslationUnit tu; | |
CXErrorCode result = clang_parseTranslationUnit2FullArgv( | |
index, argv[1], flags, sizeof(flags) / sizeof(flags[0]), nullptr, 0, | |
options, | |
&tu); | |
if (result != CXError_Success) { | |
cerr << "clang_parseTranslationUnit2FullArgv failed with error " << result << endl; | |
return EXIT_FAILURE; | |
} | |
int failure = clang_reparseTranslationUnit(tu, 0, nullptr, options); | |
if (failure) { | |
cerr << "Reparse failed" << endl; | |
return EXIT_FAILURE; | |
} | |
size_t num_diagnostics = clang_getNumDiagnostics(tu); | |
for (size_t i = 0; i < num_diagnostics; ++i) { | |
CXFile file; | |
unsigned line; | |
unsigned column; | |
unsigned offset; | |
CXDiagnostic diagnostic = clang_getDiagnostic(tu, i); | |
CXSourceLocation location = clang_getDiagnosticLocation(diagnostic); | |
clang_getExpansionLocation(location, &file, &line, &column, &offset); | |
cout << "line " << line << " column " << column << " " << clang_getCString(clang_getDiagnosticSpelling(diagnostic)) << endl; | |
clang_disposeDiagnostic(diagnostic); | |
} | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment