Skip to main content



      Home
Home » Language IDEs » C / C++ IDE (CDT) » DOM/AST Questions
DOM/AST Questions [message #146285] Tue, 31 May 2005 00:30 Go to next message
Eclipse UserFriend
Hello,
I have a some questions regarding the new DOM/AST in the CDT 3.0.

I have code that visits specific AST nodes that modifies the AST as it
visits (which is unsafe). The code (from an ASTVisitor's visit method)
for instance:

-----------------------------
/*
* This code finds WhileStatements, changes them to IfStatements,
* and then inserts it into the AST where the WhileStatement was.
*/
public int visit(IASTStatement statement) {
if(statement instanceof IASTWhileStatement){
IASTWhileStatement ws = (IASTWhileStatement)statement;
IASTExpression cond = ws.getCondition();
IASTStatement body = ws.getBody();

CASTIfStatement ifstmt = new CASTIfStatement();
ifstmt.setCondition(cond);
ifstmt.setThenClause(body);

IASTStatement parent = (IASTStatement)statement.getParent();
if(parent instanceof IASTCompoundStatement){
IASTCompoundStatement parentCS = (IASTCompoundStatement)parent;
IASTStatement[] children = parentCS.getStatements();
for(int i=0; i<children.length; ++i){
if(children[i] == ws){
children[i] = ifstmt;
break;
}
}
ifstmt.setParent(statement.getParent());
//statement.setParent(null);
}
}
return PROCESS_CONTINUE;
}
-----------------------------
[statement.setParent(null);] can be done, however, it prevents the
visitation of any nodes not yet visited since it cuts off the link back up
to the parent.

Questions:
1 - Probably a dumb one..but besides manually copying the AST into a new
type of tree structure, is there a better way to do this without directly
changing the working AST?
2 - Is the creation of AST nodes without declaring internal classes
available? ASTNodeFactory perhaps?
3 - The code successfully changes the nodes to IfStatements, however, when
I use the getRawSignature() method, it still shows the code as being while
statements...how does the raw signature relate to the AST if it does at
all?
4 - Say all AST manipulations work as intended..is there a
pretty-printer/unparser/formatter in the implementation (since the
getRawSignature() method doesn't seem to reflect AST changes)?

Any help or comments is appreciated.

Thanks!
-=-Mike
Re: DOM/AST Questions [message #146294 is a reply to message #146285] Tue, 31 May 2005 00:35 Go to previous messageGo to next message
Eclipse UserFriend
Sorry, formatting was off when I pasted it...

/*
* This code finds WhileStatements, changes them to IfStatements,
* and then inserts it into the AST where the WhileStatement was.
*/
public int visit(IASTStatement statement) {
if(statement instanceof IASTWhileStatement){
IASTWhileStatement ws = (IASTWhileStatement)statement;
IASTExpression cond = ws.getCondition();
IASTStatement body = ws.getBody();

CASTIfStatement ifstmt = new CASTIfStatement();
ifstmt.setCondition(cond);
ifstmt.setThenClause(body);

IASTStatement parent = (IASTStatement)statement.getParent();
if(parent instanceof IASTCompoundStatement){
IASTCompoundStatement parentCS = (IASTCompoundStatement)parent;
IASTStatement[] children = parentCS.getStatements();
for(int i=0; i<children.length; ++i){
if(children[i] == ws){
children[i] = ifstmt;
break;
}
}
ifstmt.setParent(statement.getParent());
//statement.setParent(null); }
}
return PROCESS_CONTINUE;
}
Re: DOM/AST Questions [message #146344 is a reply to message #146285] Tue, 31 May 2005 11:38 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

> Questions:
> 1 - Probably a dumb one..but besides manually copying the AST into a new
> type of tree structure, is there a better way to do this without directly
> changing the working AST?

I would visit first to identify the nodes you want to edit, and then edit
them serially afterwards.
A visit is not an expensive operation (~50 milliseconds on ~4MB source code
parsed).

> 2 - Is the creation of AST nodes without declaring internal classes
> available? ASTNodeFactory perhaps?
Currently not available.

> 3 - The code successfully changes the nodes to IfStatements, however, when
> I use the getRawSignature() method, it still shows the code as being while
> statements...how does the raw signature relate to the AST if it does at
> all?

Raw Signature maps back to what is actually in the file using the location
information
(offsets, files, macros, etc.)

> 4 - Say all AST manipulations work as intended..is there a
> pretty-printer/unparser/formatter in the implementation (since the
> getRawSignature() method doesn't seem to reflect AST changes)?

Refer to org.eclipse.cdt.core.dom.ast.ASTSignatureUtil.
There is also ASTTypeUtil.
Unfortunately, up to this point we have not made a pervasive "toString()"
style representation that is available for absolutely every IASTNode.

Regarding writing code using our AST : we have designed this AST for use
with an ASTRewriter.
Currently, that component is not available. Its one thing to generate code
off of an AST; it is entirely
another to tweak code slightly programmatically, without losing formatting
and comment structure.
Caveat emptor.

JohnC
Re: DOM/AST Questions [message #849324 is a reply to message #146344] Wed, 18 April 2012 23:43 Go to previous message
Eclipse UserFriend
Hi John,

Can you please elaborate by giving an example on how to use ASTRewrite?
It will be helpful if you can show this by using ASTRewrite.insertBefore(parentNode,insertionNode,newNode, editGroup) method to insert a node to a source code.

I could not find any successful usage of insertBefore method anywhere. Programmers are complaining about getting IllegalArgumentException when they used this.

Thanks in advance.

Previous Topic:Download the org.eclipse.cdt.ui.tests project
Next Topic:Debug-Output of e.g. Qt-Types
Goto Forum:
  


Current Time: Sat May 10 23:29:17 EDT 2025

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

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

Back to the top