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

That's very close to what I want. It seems like the code is not
complete though. It doesn't print out comments, includes, or #defines.
Am I using it wrong or is the code incomplete? I checked the master
branch on git and it's the same sources I'm using. A line with a
PreprocessorStatementWriter object on it is commented out.

Code:
NodeCommentMap m = new NodeCommentMap();
ASTWriterVisitor w = new ASTWriterVisitor(m);
ast.accept(w);
p(w.toString());

Input:
-------------------------------
#include <stdio.h>
#define Test "Works"
using namespace std;

//call foo
int foo() {
	// call printf
	printf(Test);
	return 0;
}
--------------------

Output:
------------------------------
using namespace std;

int foo()
{
    printf(Test);
    return 0;
}
------------------------


On Fri, Jun 22, 2012 at 1:33 PM, Sergey Prigogin
<eclipse.sprigogin@xxxxxxxxx> wrote:
> 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
>
>
>
> _______________________________________________
> cdt-dev mailing list
> cdt-dev@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/cdt-dev
>


Back to the top