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/6e04855778086c1e5d24 to your computer and use it in GitHub Desktop.
Save jeaye/6e04855778086c1e5d24 to your computer and use it in GitHub Desktop.
// // clang++ -std=c++1y -stdlib=libc++ clang_lambda_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>
size_t get_filesize(std::string const &file)
{
std::ifstream ifs{ file };
if(!ifs.is_open())
{ throw std::runtime_error{ "failed to open " + file }; }
ifs.seekg(0, std::ios::end);
return ifs.tellg();
}
CXSourceRange get_filerange(CXTranslationUnit const &tu, std::string const &filename)
{
CXFile const file{ clang_getFile(tu, filename.c_str()) };
size_t const size{ get_filesize(filename.c_str()) };
CXSourceLocation const top(clang_getLocationForOffset(tu, file, 0));
CXSourceLocation const bottom(clang_getLocationForOffset(tu, file, size));
if(clang_equalLocations(top, clang_getNullLocation()) ||
clang_equalLocations(bottom, clang_getNullLocation()))
{ throw std::runtime_error{ "cannot retrieve location" }; }
CXSourceRange const range(clang_getRange(top, bottom));
if(clang_Range_isNull(range))
{ throw std::runtime_error{ "cannot retrieve range" }; }
return range;
}
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) };
CXSourceRange const range(get_filerange(tu, filename));
CXToken *data{};
unsigned num{};
clang_tokenize(tu, range, &data, &num);
std::vector<CXCursor> cursors(num);
clang_annotateTokens(tu, data, num, cursors.data());
auto cursor(cursors.cbegin());
for(auto token(data); token != data + num; ++token, ++cursor)
{
CXString const spell(clang_getTokenSpelling(tu, *token));
auto const loc(clang_getTokenLocation(tu, *token));
CXFile file{};
unsigned line{}, column{}, offset{};
clang_getFileLocation(loc, &file, &line, &column, &offset);
auto const cur(*cursor);
std::cout << clang_getCString(spell) << ", ";
std::cout <<
clang_getCString(clang_getCursorKindSpelling(clang_getCursorKind(cur)))
<< ", ";
std::cout <<
clang_getCString(clang_getTypeKindSpelling(clang_getCursorType(cur).kind))
<< ", ";
std::cout <<
clang_getCString(clang_getCursorDisplayName(cur))
<< std::endl;
}
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment