I have a header file with an Enum with a set of enumerators, e.g.,
typedef enum MyEnum{
A = 0,
B = 1
}
I want to add a new enumerator like C = 2 to the end of it.
So I tried to do it using the ASTRewrite like below:
public void addEnumerator(IASTTranslationUnit ast, String enumName, String name, String value){
INodeFactory nodeFactory = ast.getASTNodeFactory();
ASTRewrite astRewrite = ASTRewrite.create(ast);
ast.accept(new ASTVisitor(true) {
@Override
public int visit(IASTDeclSpecifier declSpec) {
if (declSpec instanceof IASTEnumerationSpecifier
&& ((IASTEnumerationSpecifier) declSpec).getName().getRawSignature().equals(enumName)) {
IASTName nameNode = nodeFactory.newName(name);
IASTExpression valueNode = nodeFactory.newLiteralExpression
(IASTLiteralExpression.lk_integer_constant, value);
IASTEnumerator newEnumerator = nodeFactory.newEnumerator(nameNode, valueNode);
astRewrite.insertBefore(declSpec, null, newEnumerator, null);
return ASTVisitor.PROCESS_SKIP;
}
return super.visit(declSpec);
}
});
Change change = rewrite.rewriteAST();
try {
change.perform(new NullProgressMonitor());
} catch (CoreException exp) {
LOGGER.log(Level.SEVERE, ExceptionUtils.getStackTrace(exp));
}
}
However, the change object does not reflect the changes!
Based on my checks on CDT src code, apprently, only IASTDeclaration and IASTStatement are allowed for APPEND_CHILD modification operation but I don't understand why and how I can do the modification that I need (which is of type IASTEnumerator)!