[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
[cdt-dev] Printing Code using ASTVisitor
|
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;
}
}