Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[cdt-dev] Refactoring with pragmas

My group has been working on refactoring C/C++ code using the ASTRewrite class. A critical need our refactorings is inserting preprocessor pragmas, but insertion of these is not directly supported. We've tried using literal nodes, but the text from the literal nodes fails to appear in the refactored code even when the rest of the refactoring was successful. This occurs only when using a nested rewrite object. Our question is: why do the literal nodes fail to appear? 

For example, this code:

    IASTForStatement loop = getLoop();
    IASTStatement body = loop.getBody();
    ICNodeFactory factory = ASTNodeFactoryFactory.getDefaultCNodeFactory();
    IASTCompoundStatement c = factory.newCompoundStatement();
    IASTRewrite rewriter_cmpd = rewriter.replace(body, c, null);
    rewriter_cmpd.insertBefore(c, null, rewriter.createLiteralNode("#pragma test\n"), null);

refactors this C for loop:
    for (int i = 0; i < 100; i++)
       a = 2;

into this:
    for (int i = 0; i < 100; i++)
        {
    }

We have also tried using TextEdits, but in some of our refactorings, we can't know the insertion offset ahead of time to make them work.

Back to the top