Modify Abstract syntax tree eclipse cdt C code

I have ac code like this

static S16 test_1603b( const S16 *,  const S16 * );

I want to edit this code pragmatically to be something like this

static S16 test_1603b( const S16 *varName,  const S16 *varName );

So what I did I used Eclipse CDT plugin outside the eclipse, and I have successfully extracted the Abstract syntax tree(AST) and visited all the method parameter declaration, but I can not found any way to rewrite the AST again with the new modification, My Code snippet:

public class RuleChk extends AbstractRule {

public RuleChk(IASTTranslationUnit ast) {
    super("RuleChk", false, ast);
    shouldVisitDeclarations = true;
    shouldVisitParameterDeclarations = true;
}

@Override
public int visit(IASTParameterDeclaration parameterDeclaration) {
    if (!parameterDeclaration.getRawSignature().startsWith("void")) {
        if (parameterDeclaration.getDeclarator().getName().getRawSignature().equals("")) {

            IASTDeclarator oldDec = parameterDeclaration.getDeclarator();

            //Create New Declarator Node
            INodeFactory factory = ast.getASTNodeFactory();
            IASTName name = factory.newName("varName".toCharArray());             
            IASTDeclarator declarator = factory.newDeclarator(name);
            declarator.setParent(oldDec.getParent());
            declarator.setInitializer(oldDec.getInitializer());
            declarator.setName(name);
            declarator.setNestedDeclarator(oldDec.getNestedDeclarator());
            declarator.setPropertyInParent(oldDec.getPropertyInParent());

            //get the rewriter
            final TextEditGroup editGroup = new TextEditGroup("FakeGroup");
            ASTRewrite rewriter = ASTRewrite.create(ast);
            rewriter.replace(declarator,oldDec,editGroup);
            rewriter.rewriteAST();
        }
    }
    return super.visit(parameterDeclaration);
  }
}

After Debugging I found the org.eclipse.cdt.internal.formatter.ChangeFormatter#formatChangedCode, when it try to get the

ICProject project = tu.getCProject();

It throws a null pointer exception because the TransionUnit (tu) is being null from the beginning of the whole application,

ANY IDEAS GEEKS!


A lot of the CDT infrastructure, including ASTRewrite , is not designed to run outside of an Eclipse project / workspace.

What you generally need to do in cases like this is:

  • Create an Eclipse workspace. If you don't otherwise need an Eclipse workspace, you can create a temporary one and delete it when you're done.

  • Create a CDT C project inside your workspace.

  • Make sure the code you want to process is part of the project. If the files are contained inside the project's directory tree, then this happens automatically. Otherwise, you can set up a "linked folder" in the project to refer to a location outside of the project's directory tree.

  • Depending on what your refactoring needs, you may need to run CDT's indexer on the project.

  • Get an ITranslationUnit representing the file you want to process (similar to what you wrote in your comment).

  • Get the IASTTranslationUnit from the ITranslationUnit .

  • The first four steps can be done manually, or automatically using Eclipse APIs.

    链接地址: http://www.djcxy.com/p/68216.html

    上一篇: 使用术语“抽象语法树”

    下一篇: 修改抽象语法树eclipse cdt C代码