Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
RE: [cdt-dev] insert pragma into AST?

Jimmie,
I don't see how you could generate the exception unless you are changing the statement-array obtained via IASTCompoundStatement.getStatements() somewhere in your code. I am pretty sure that CDT itself does not do that.
--> if you cannot fix the problem on your side, please create a piece of code with which I can reproduce the issue and attach it to a bugzilla.
Markus.
 


From: cdt-dev-bounces@xxxxxxxxxxx [mailto:cdt-dev-bounces@xxxxxxxxxxx] On Behalf Of Jimmie Eriksson
Sent: Monday, July 28, 2008 3:20 PM
To: CDT General developers list.
Subject: Re: [cdt-dev] insert pragma into AST?
Importance: Low

finally got the changes, but it still seems to be problems.. when I try to insert the pragma on a for loop (it works for functions)

java.lang.NullPointerException
    at org.eclipse.cdt.internal.core.dom.rewrite.astwriter.StatementWriter.writeCompoundStatement(StatementWriter.java:380)
    at org.eclipse.cdt.internal.core.dom.rewrite.astwriter.StatementWriter.writeStatement(StatementWriter.java:123)
    at org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriterVisitor.visit(ASTWriterVisitor.java:185)
    at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGeneratorWriterVisitor.visit(ChangeGeneratorWriterVisitor.java:326)
    at org.eclipse.cdt.internal.core.dom.parser.c.CASTCompoundStatement.accept(CASTCompoundStatement.java:53)
    at org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter.write(ASTWriter.java:91)
    at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.synthTreatment(ChangeGenerator.java:215)
    at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.synthTreatment(ChangeGenerator.java:207)
    at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.visit(ChangeGenerator.java:497)
    at org.eclipse.cdt.internal.core.dom.parser.c.CASTCompoundStatement.accept(CASTCompoundStatement.java:53)
    at org.eclipse.cdt.internal.core.dom.parser.c.CASTFunctionDefinition.accept(CASTFunctionDefinition.java:103)
    at org.eclipse.cdt.internal.core.dom.parser.ASTTranslationUnit.accept(ASTTranslationUnit.java:241)
    at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(ChangeGenerator.java:115)
    at org.eclipse.cdt.internal.core.dom.rewrite.changegenerator.ChangeGenerator.generateChange(ChangeGenerator.java:107)
    at org.eclipse.cdt.internal.core.dom.rewrite.ASTRewriteAnalyzer.rewriteAST(ASTRewriteAnalyzer.java:24)
    at org.eclipse.cdt.core.dom.rewrite.ASTRewrite.rewriteAST(ASTRewrite.java:177)
    at modules.CodeNav.insertLiteral(CodeNav.java:207)

// the StatementWriter.java file..

    private void writeCompoundStatement(IASTCompoundStatement compoundStatement) {
        scribe.printLBrace();
        scribe.newLine();
        for (IASTStatement statements : getNestedStatements(compoundStatement)) {
            statements.accept(visitor); // can be null sometimes!
        }

// the C-program that I parse...

#include <stdio.h>

int main(void) {
    int i;
    for(i = 0; i < 4; i++) {
        printf("%d\n", i);
    }
    return 0;
}

/Jimmie

2008/7/21 Schorn, Markus <Markus.Schorn@xxxxxxxxxxxxx>
The following code should do the trick:
 
    ASTRewrite re= ASTRewrite.create(ast);
    IASTNode lit= re.createLiteralNode("#pragma xxx\n");
    re.insertBefore(belowNode, beforeNode lit, null);
    Change change= re.rewriteAST();
    change.perform(new NullProgressMonitor());
A quick test showed that this does not work with 5.0. I have now corrected the ASTWriter, such that the code above will work in 5.0.1 > 20080721.
 
Markus.
 
 


From: cdt-dev-bounces@xxxxxxxxxxx [mailto:cdt-dev-bounces@xxxxxxxxxxx] On Behalf Of Jimmie Eriksson
Sent: Friday, July 18, 2008 5:16 PM
To: CDT General developers list.
Subject: [cdt-dev] insert pragma into AST?
Importance: Low

hi

I want to be able to insert a pragma (a IASTPreprocessorPragmaStatement) in the middle of an existing AST and then in I want to reflect the changes back to the sourcecode accordingly. I've searched some and found the ASTRewrite somewhat interesting but can't get it to work as I want. Can anyone give an example of how to use it or point me somewhere else?

thanks in advance

/Jimmie

_______________________________________________
cdt-dev mailing list
cdt-dev@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/cdt-dev



Back to the top