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?

Did I find the error... in ASTModificationHelper.java?
...
...
public <T extends IASTNode> T[] createModifiedChildArray(IASTNode parent, T[] unmodifiedChildren, Class<T> clazz){
...
...
            case INSERT_BEFORE:
                T insertedTNode = cast(parentModification.getNewNode(), clazz); // this line returns null.... ... .. even though the getNewNode() is the new literalnode I created... thats why I get a nullpointerexception
...
...
    @SuppressWarnings("unchecked")
    private <T> T cast(IASTNode node, Class<T> clazz) {
        if (clazz.isInstance(node)){ // clazz(=IASTStatement) isNOTInstance (node(=IASTLiteralNode))
            return (T) node;
        }
        return null; // this always return null on the IASTLiteralNode
    }

This code should be equal to what I use... the important part here is that the insertionPoint is of type CASTForStatement and the parent is of type CASTCompoundStatement
        try {
            IASTTranslationUnit tu = insertionPoint.getTranslationUnit();
            ASTRewrite aSTRewrite = ASTRewrite.create(tu);
            IASTNode lit = aSTRewrite.createLiteralNode("Any text that I want... I will use it for a #pragma");
            aSTRewrite.insertBefore(insertionPoint.getParent(), insertionPoint, lit, null);
            Change change = aSTRewrite.rewriteAST();
            change.perform(new NullProgressMonitor());
        } catch (Exception e) {
            e.printStackTrace();
        }

// the c program...

int main() {  // CASTCompoundStatement
    int i;
    for(i = 0; i < 4; i ++) { // CASTForStatement
        printf("hej");
    }
    return 0;
}

Back to the top