Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cdt-dev] Printing Code using ASTVisitor

Take a look at org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter. Does it do what you need?

-sergey

On Fri, Jun 22, 2012 at 10:27 AM, Joseph Paul Cohen <jcccnet@xxxxxxxxx> wrote:
Can anyone shed some light on how to print out a piece of C++ code
using an ASTVisitor? Eventually I want to be able to walk the AST and
print out or transform what I want.  I made this ASTVisitor but it
seems I have to override each visit method and even then I get
overlapping code. It will print out an entire function definition and
then later I will visit the expressions inside it and print those out.
Any suggestions?

Output:
...
CPPASTFunctionDefinition int called2(){

    called3();
    return 9;
}
CPPASTFunctionCallExpression called3()
CPPASTIdExpression called3
CPPASTLiteralExpression 9
...


Code:

class CodePrintVisitor extends ASTVisitor {
    CodePrintVisitor() {
            shouldVisitExpressions= true;
            shouldVisitDeclarations = true;
    }

    @Override
    public int visit(IASTExpression _expression_) {

            String line = "";

            line += _expression_.getClass().getSimpleName();
            line += " ";
            line += _expression_.getRawSignature();

            print("Code", line);
            return PROCESS_CONTINUE;
    }

    @Override
    public int visit(IASTDeclaration declaration) {
            String line = "";

            line += declaration.getClass().getSimpleName();
            line += " ";
            line += declaration.getRawSignature();

            print("Code", line);
            return PROCESS_CONTINUE;
    }
}
_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev


Back to the top