Skip to content

Instantly share code, notes, and snippets.

@micbou
Last active July 8, 2018 11:57
Show Gist options
  • Save micbou/c22a6fa68f4597249a05909c52ffc40a to your computer and use it in GitHub Desktop.
Save micbou/c22a6fa68f4597249a05909c52ffc40a to your computer and use it in GitHub Desktop.
libclang macro redefinition bug
#include <iostream>
#include <string>
#include <clang-c/Index.h>
const char *flags[] = {
"clang",
"-isystem", "include",
"-DTEST=2"
};
int main(int argc, const char *argv[]) {
if (argc != 2) {
std::cout << argv[0] << " file.cpp\n";
return EXIT_FAILURE;
}
CXIndex index = clang_createIndex(0, 0);
if (!index) {
std::cerr << "createIndex failed\n";
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) {
std::cerr << "clang_parseTranslationUnit2FullArgv failed with error " << result << "\n";
return EXIT_FAILURE;
}
int failure = clang_reparseTranslationUnit(tu, 0, nullptr, options);
if (failure) {
std::cerr << "Reparse failed\n";
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);
std::string filename("none");
CXString text = clang_getFileName(file);
if (text.data) {
filename = std::string(clang_getCString(text));
}
std::cout << "file " << filename << " line " << line << " column " << column << " " << clang_getCString(clang_getDiagnosticSpelling(diagnostic)) << "\n";
clang_disposeDiagnostic(diagnostic);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment