CXChildVisitResult visitor( CXCursor cursor, CXCursor parent, CXClientData client_data) { ClangTools::TranslationUnit::AstWalker * astWalker = reinterpret_cast<ClangTools::TranslationUnit::AstWalker *>(client_data); std::vector<std::string> & funcNames = *(reinterpret_cast<std::vector<std::string> *>(astWalker->getClientData())); auto sourceLocation = clang_getCursorLocation(cursor); CXFile file; clang_getFileLocation(sourceLocation, &file, 0, 0, 0); auto fileName = clang_getFileName(file); auto fileNameStr = ClangTools::String(fileName); if (fileNameStr != astWalker->getFileName()) { return CXChildVisit_Recurse; } auto func = std::string{}; auto def = bool{false}; if (clang_isCursorDefinition(cursor)) { def = true; } switch (clang_getCursorKind(cursor)) { case CXCursor_FunctionDecl: func = "Function "; break; case CXCursor_FunctionTemplate: func = "FunctionTemplate "; break; case CXCursor_CXXMethod: func = "CXXMethod "; break; default: return CXChildVisit_Recurse; break; } func += clang_isCursorDefinition(cursor) ? "definition: " : "declaration: "; auto semanticParent = clang_getCursorSemanticParent(cursor); auto semanticParentSpelling = clang_getCursorSpelling(semanticParent); func += ClangTools::String(semanticParentSpelling) + "::"; auto cursorSpelling = clang_getCursorSpelling(cursor); func += ClangTools::String(cursorSpelling); auto lexicalParent = clang_getCursorLexicalParent(cursor); auto lexicalParentSpelling = clang_getCursorSpelling(lexicalParent); func += " found in: " + ClangTools::String(lexicalParentSpelling); funcNames.push_back(func); return CXChildVisit_Recurse; }
int main(int argc, char *argv[]) { auto index = clang_createIndex(0, 0); auto options = clang_defaultEditingTranslationUnitOptions(); char const *args[] = { "-x", "c++", "-std=c++11" }; auto arg_count = sizeof( args ) / sizeof( *args ); filename = argv[1]; CXUnsavedFile *unsaved_files = NULL; auto unsaved_file_count = 0; tu = clang_parseTranslationUnit(index, filename.c_str(), args, arg_count, unsaved_files, unsaved_file_count, options ); if ( !tu ) { std::cout << "Translation Unit Parse Failed!\n"; return -1; } std::stringstream ss( argv[2] ); int line, col; ss >> line; ss.get(); ss >> col; std::cout << "Hello " << line << ":" << col << "\n"; auto file = clang_getFile( tu, filename.c_str() ); auto location = clang_getLocation( tu, file, line, col ); clang_visitChildren( clang_getTranslationUnitCursor( tu ), visitor, reinterpret_cast<CXClientData>(0) ); auto cursor = clang_getCursor( tu, location ); auto refcursor = clang_getCursorReferenced( cursor ); auto rrefcursor = clang_getCursorReferenced( refcursor ); auto arf = clang_getTypeKindSpelling( clang_getCursorType( cursor ).kind ); auto foo = clang_getCanonicalCursor( cursor ); auto semparent = clang_getCursorSemanticParent( cursor ); auto lexparent = clang_getCursorLexicalParent( cursor ); std::cout << cursor << "\n"; std::cout << refcursor << "\n"; std::cout << rrefcursor << "\n"; std::cout << clang_getCString(arf) << "\n"; std::cout << foo << "\n"; std::cout << "Parent: " << semparent << "\n"; std::cout << "LexParent: " << lexparent << "\n"; //clang_visitChildren( semparent, visitor, reinterpret_cast<CXClientData>(0) ); clang_disposeString( arf ); return 0; }
static inline bool isInline(const CXCursor &cursor) { switch (clang_getCursorKind(clang_getCursorLexicalParent(cursor))) { case CXCursor_ClassDecl: case CXCursor_ClassTemplate: case CXCursor_StructDecl: return true; default: return false; } }
Cursor Cursor::lexical_parent() const { if ( !m_lexical_parent ) { CXCursor cx_cursor(clang_getCursorLexicalParent(m_cx_cursor)); if ( is_null(cx_cursor) ) { CLANGXX_THROW_LogicError("Error determining the lexical parent of this cursor."); } m_lexical_parent.reset(new Cursor(std::move(cx_cursor), m_translation_unit)); } return *m_lexical_parent; }
SEXP R_clang_getCursorLexicalParent(SEXP r_cursor) { SEXP r_ans = R_NilValue; CXCursor cursor = * GET_REF(r_cursor, CXCursor); CXCursor ans; ans = clang_getCursorLexicalParent(cursor); r_ans = R_makeCXCursor(ans) ; return(r_ans); }
std::string libclang_vim::stringize_parent(CXCursor const& cursor, CXCursor const& parent) { auto const semantic_parent = clang_getCursorSemanticParent(cursor); auto const lexical_parent = clang_getCursorLexicalParent(cursor); cxstring_ptr parent_name = clang_getCursorSpelling(parent); cxstring_ptr semantic_parent_name = clang_getCursorSpelling(semantic_parent); cxstring_ptr lexical_parent_name = clang_getCursorSpelling(lexical_parent); return stringize_key_value("parent", parent_name) + stringize_key_value("semantic_parent", semantic_parent_name) + stringize_key_value("lexical_parent", lexical_parent_name); }
cursor cursor::lexicalParent() { return { clang_getCursorLexicalParent(cur) }; }
Cursor Cursor::GetLexicalParent() const { return clang_getCursorLexicalParent(cursor_); }
Cursor Cursor::lexicalParent() const { return clang_getCursorLexicalParent(cxCursor); }