Skip to content

Instantly share code, notes, and snippets.

@jeaye
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeaye/edd9b0f036f9bef91985 to your computer and use it in GitHub Desktop.
Save jeaye/edd9b0f036f9bef91985 to your computer and use it in GitHub Desktop.
// clang++ -std=c++1y -stdlib=libc++ clang_test.cpp -lc++abi -lclang
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <stdexcept>
#include <fstream>
#include <iostream>
#include <array>
#include <clang-c/Index.h>
CXChildVisitResult visit(CXCursor const cursor, CXCursor const parent,
CXClientData const)
{
auto const spell(clang_getCursorSpelling(cursor));
auto const range(clang_getCursorExtent(cursor));
auto const start(clang_getRangeStart(range));
auto const end(clang_getRangeEnd(range));
CXFile cxfile;
unsigned start_line{}, start_col{}, start_offset{};
unsigned end_line{}, end_col{}, end_offset{};
clang_getSpellingLocation(start, &cxfile, &start_line, &start_col, &start_offset);
clang_getSpellingLocation(end, &cxfile, &end_line, &end_col, &end_offset);
std::cout << clang_getCString(spell) << ", ";
std::cout << start_line << ":" << start_col << " -> "
<< end_line << ":" << end_col << ", ";
std::cout <<
clang_getCString(clang_getCursorKindSpelling(clang_getCursorKind(cursor)))
<< ", ";
std::cout <<
clang_getCString(clang_getTypeKindSpelling(clang_getCursorType(cursor).kind))
<< ", ";
std::cout <<
clang_getCString(clang_getCursorDisplayName(cursor))
<< std::endl;
return CXChildVisit_Recurse;
}
int main()
{
char const * const args[]{ "-I/usr/include", "-I.", "-std=c++11" };
std::string const filename{ "foo.cpp" };
CXIndex const index{ clang_createIndex(true, true) };
CXTranslationUnit const tu
{ clang_parseTranslationUnit(index, filename.c_str(),
args, sizeof(args) / sizeof(char const*), nullptr,
0, CXTranslationUnit_None) };
clang_visitChildren(clang_getTranslationUnitCursor(tu), &visit, nullptr);
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment