001package eu.cqse.check.util.clang; 002 003import eu.cqse.clang.CXCursor; 004import eu.cqse.clang.CXSourceLocation; 005import eu.cqse.clang.CXToken; 006import eu.cqse.clang.Clang; 007import eu.cqse.clang.ClangBinding; 008import eu.cqse.clang.SWIGTYPE_p_CXTranslationUnitImpl; 009 010/** 011 * A {@link CursorPrinter} that generates a String representation of a CXCursor. 012 * The representation generated by the default implementation contains debug 013 * information (name, kind, spelling, ...). This may be overwritten by 014 * subclasses. 015 */ 016public class CursorPrinter { 017 private final SWIGTYPE_p_CXTranslationUnitImpl translationUnit; 018 019 public CursorPrinter(SWIGTYPE_p_CXTranslationUnitImpl translationUnit) { 020 this.translationUnit = translationUnit; 021 } 022 023 /** 024 * Prints a representation of the given cursor. May optionally use the parent 025 * cursor to determine context of the current cursor. 026 * 027 * The default implementation generates a debug representation (name, kind, 028 * spelling). 029 */ 030 public String getCursorRepresentation(CXCursor current, CXCursor parent) { 031 CXSourceLocation location = Clang.clang_getCursorLocation(current); 032 long line = ClangBinding.getSpellingLocationProperties(location).getLine(); 033 String name = Clang.clang_getCString(Clang.clang_getCursorDisplayName(current)); 034 String kind = Clang.clang_getCursorKind(current).toString(); 035 String typeKind = Clang.clang_getCursorType(current).getKind().toString(); 036 String type = Clang.clang_getCString(Clang.clang_getTypeSpelling(Clang.clang_getCursorType(current))); 037 String spelling = Clang.clang_getCString(Clang.clang_getCursorSpelling(current)); 038 CXToken token = Clang.clang_getToken(translationUnit, Clang.clang_getCursorLocation(current)); 039 String tokenSpelling = "token is null"; 040 if (token != null) { 041 tokenSpelling = Clang.clang_getCString(Clang.clang_getTokenSpelling(translationUnit, token)); 042 } 043 return line + " name:" + name + " kind:" + kind + " type:" + type + "/" + typeKind + " spelling:" + spelling 044 + " tokenSpelling: " + tokenSpelling; 045 } 046}