Search code examples
parsingeclipse-cdtvisitor-pattern

How to get a field's type by using CDT parser


I'm trying to extract c++ source code's info. One is field's type.

when source code like under I want to extract info's Type when info.call() is called.

Info info;
//skip
info.call(); //<- from here

Trough making a visitor which visit IASTName node, I tried to extract type info like under.

public class CDTVisitor extends ASTVisitor {

    public CDTVisitor(boolean visitNodes) {
        super(true);
    }

    public int visit(IASTName node){
        if(node.resolveBinding().getName().toString().equals("info"))
            System.out.println(((IField)node.getBinding()).getType()); 
            // this not work properly. 
            //result is "org.eclipse.cdt.internal.core.dom.parser.ProblemType@86be70a"

        return 3;
    }
}

Solution

  • Self response. The reason I couldn't get a binding object was the type of AST.

    When try to parse C++ source code, I should have used ICPPASTTranslationUnit. There is no code related this, I used IASTTranslationUnit as a return type of AST.

    After using ICPPASTTranslationUnit instead of IASTTranslationUnit, I solved this problem.