#include <iostream>
#include <clang-c/Index.h>
template<typename T, void F(T)> struct manager
{
T t;
manager(T t) : t(t) {}
operator T () {return t;}
~manager() {F(t);}
};
int main()
{
manager<CXIndex, clang_disposeIndex> managed_index(clang_createIndex(0, 0));
CXIndex index = managed_index;
if (!index)
{
std::cerr << "failed creating index" << std::endl;
return 1;
}
manager<CXTranslationUnit, clang_disposeTranslationUnit> managed_mickey(
clang_parseTranslationUnit(
index,
"mickey.cpp",
nullptr,
0,
0,
0,
CXTranslationUnit_PrecompiledPreamble));
CXTranslationUnit mickey = managed_mickey;
if (!mickey)
{
std::cerr << "mickey parsing failed" << std::endl;
return 1;
}
clang_reparseTranslationUnit(mickey, 0, nullptr, 0);
manager<CXTranslationUnit, clang_disposeTranslationUnit> managed_minnie(
clang_parseTranslationUnit(
index,
"minnie.cpp",
nullptr,
0,
0,
0,
CXTranslationUnit_PrecompiledPreamble));
CXTranslationUnit minnie = managed_minnie;
if (!minnie)
{
std::cerr << "minnie parsing failed" << std::endl;
return 1;
}
clang_reparseTranslationUnit(minnie, 0, nullptr, 0);
CXFile file;
unsigned line;
unsigned column;
unsigned offset;
CXFile mickey_file = clang_getFile(mickey, "mickey.cpp");
CXSourceLocation mickey_location = clang_getLocation(mickey, mickey_file, 5, 5);
CXCursor mickey_cursor = clang_getCursor(mickey, mickey_location);
CXType mickey_type = clang_getCursorType(mickey_cursor);
CXCursor mickey_decl_cursor = clang_getTypeDeclaration(mickey_type);
CXSourceLocation mickey_decl_location = clang_getCursorLocation(mickey_decl_cursor);
clang_getExpansionLocation(mickey_decl_location, &file, &line, &column, &offset);
CXString mickey_decl_file_name = clang_getFileName(file);
const char *mickey_decl_cstr_file_name = clang_getCString(mickey_decl_file_name);
std::cout << "beginning" << std::endl;
std::cout << mickey_decl_cstr_file_name << " " << line << " " << column << " " << offset << std::endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y2xhbmctYy9JbmRleC5oPgoKdGVtcGxhdGU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