Skip to main content

Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » ASTRewrite: Preserving comments and formatting?
ASTRewrite: Preserving comments and formatting? [message #1021830] Wed, 20 March 2013 18:53
Erika Redmark is currently offline Erika RedmarkFriend
Messages: 22
Registered: January 2013
Junior Member
I am currently trying to use the ASTRewrite class, which so far is the only thing that has worked, to generate code using an AST. Currently, I am trying to add nodes to an AST. My method is to generate an AST from a file, use ASTRewrite to modify the file, and then re-parse the file to get an up-to-date AST for it, effectively 'rewriting' the AST.

I managed to get it to work, but there are two issues I am not sure how to solve. First, here is the text, as it is in the .c file I parsed:
void nothing(void) {
    int x; // comment
    int y = x + 25;

I get that node, and then attempt to add the exact same node to another file. Here is the text result:

void nothing(void)
    int x;
    int y = x + 25;

The formatting is changed, and the comments are removed. The relevant code I am using for adding an node is here:

ASTRewrite rewriter =

// parent is translation unit for 'blank' file, node is the IASTFunctionDefinition
// that I found parsing the original source.
rewriter.insertBefore(parent, null, node, null);

// private function that does some extra work; the principle thing it does
// is it calls Change.perform on the returned Change object.
performChange(rewriter.rewriteAST() );
return initFromProjectFile(sourceRef);

First: I thought the AST stored comments? I tried looking at the node's properties in the debugger and I couldn't find the comments. I use CoreModelUtil.findTranslationUnit to get the translation unit for the file, and from there I find the entire function definition node and store it to add to another file later. Is there a way I can preserve comments?

Second: I am taking a guess that I have to somehow use my own TextEditGroup to control the formatting of the output (I never used this class before, so I don't know yet). I know the AST stores at least some formatting information because I used offsets from location nodes to highlight function names for a parser we also created. What I want to know is if it is in any way possible to get an AST that stores enough to completely reconstruct the code exactly as it appears from source to destination?

INFO: I am using CDT 7.0.2 on Helios

EDIT: Added detail about performChange method: forgot it was a method I wrote.

[Updated on: Thu, 21 March 2013 15:24]

Report message to a moderator

Previous Topic:CDT inconsistant behaviour in Juno
Next Topic:Add Include working without a selection?
Goto Forum:

Current Time: Sat Jun 23 10:01:02 GMT 2018

Powered by FUDForum. Page generated in 0.01491 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top