std::string tokenize_as_vimson(char const* const* args, size_t const argc) { CXIndex index = clang_createIndex(/*excludeDeclsFromPCH*/ 1, /*displayDiagnostics*/0); translation_unit = clang_parseTranslationUnit(index, file_name.c_str(), args, argc, NULL, 0, CXTranslationUnit_Incomplete); if (translation_unit == NULL) { clang_disposeIndex(index); return "{}"; } auto file_range = get_range_whole_file(); if (clang_Range_isNull(file_range)) { clang_disposeTranslationUnit(translation_unit); clang_disposeIndex(index); return "{}"; } CXToken *tokens_; unsigned int num_tokens; clang_tokenize(translation_unit, file_range, &tokens_, &num_tokens); std::vector<CXToken> tokens(tokens_, tokens_ + num_tokens); auto result = make_vimson_from_tokens(tokens); clang_disposeTokens(translation_unit, tokens_, num_tokens); clang_disposeTranslationUnit(translation_unit); clang_disposeIndex(index); return result; }
std::string libclang_vim::stringize_extent(CXCursor const& cursor) { auto const r = clang_getCursorExtent(cursor); if (clang_Range_isNull(r)) return ""; return "'start':{" + stringize_location(clang_getRangeStart(r)) + "},'end':{" + stringize_location(clang_getRangeEnd(r)) + "}"; }
std::string libclang_vim::stringize_range(CXSourceRange const& range) { if (clang_Range_isNull(range)) { return ""; } return "'range':{'start':{" + stringize_location(clang_getRangeStart(range)) + "},'end':{" + stringize_location(clang_getRangeEnd(range)) + "}},"; }
source_range_t source_range(translation_unit const &trans_unit) { auto &tu(trans_unit.impl); CXFile const file{ clang_getFile(tu, trans_unit.filename.c_str()) }; std::size_t const size{ boost::filesystem::file_size(trans_unit.filename) }; 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" }; } source_range_t const range(clang_getRange(top, bottom)); if(clang_Range_isNull(range)) { throw std::runtime_error{ "cannot retrieve range" }; } return range; }
CXSourceRange get_range_whole_file() const { size_t const file_size = get_file_size(file_name.c_str()); CXFile const file = clang_getFile(translation_unit, file_name.c_str()); auto const file_begin = clang_getLocationForOffset(translation_unit, file, 0); auto const file_end = clang_getLocationForOffset(translation_unit, file, file_size); if(is_null_location(file_begin) || is_null_location(file_end)) { return clang_getNullRange(); } auto const file_range = clang_getRange(file_begin, file_end); if(clang_Range_isNull(file_range)) { return clang_getNullRange(); } return file_range; }
bool source_range::isnull() const { return clang_Range_isNull(range); }
bool SourceRange::isNull() const { return clang_Range_isNull(cxSourceRange); }