Bug 23573 - AST: clone & source locations
Summary: AST: clone & source locations
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.0   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: 2.1 M2   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2002-09-13 13:28 EDT by Martin Aeschlimann CLA
Modified: 2002-10-17 10:17 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Aeschlimann CLA 2002-09-13 13:28:24 EDT
20020911

This isn't specifed, so my questions: Are source locations also copied to a 
cloned node (using ASTNode.copySubtrees) ?
It seems to be for some but not all nodes.

I would be happy if the source locations are copied.
(The spec says that properties are not copied; what of course makes sense).
Comment 1 Olivier Thomann CLA 2002-09-13 13:42:40 EDT
Could you please specify for which nodes the positions are preserved? I would
consider positions as a client property and therefore there is no reason for
them to be copied to the clone object.
Jim - any comment?
Comment 2 Olivier Thomann CLA 2002-09-13 13:48:16 EDT
I found only the copy of source locations in SimpleName and this is a bug. This
is boggus code that has no reason to be there. It was an attempt to resolve a
bug, but I forgot to remove this code. I will release the right code immediately.
Comment 3 Olivier Thomann CLA 2002-09-13 13:50:47 EDT
The easiest way for your to copy the positions is probably to use a visitor and
iterate the tree.
Comment 4 Martin Aeschlimann CLA 2002-09-13 14:55:37 EDT
How whould you do that?
It's not easy (or cheap) to find the corresponding element in the other tree.

(Its easier to clear positions!)

Are there good reasons to not copy source locations? I mean its a clone of a 
tre, so the cloned tree as well represents the Ast of a certain part of the 
code.

Comment 5 Olivier Thomann CLA 2002-09-13 15:15:22 EDT
You can use a ast matcher between the clone and the original. It works because
you will iterate the two trees in the same time.
Something like:
class CopyPositionsMatcher extends ASTMatcher {

	private void copyPositions(ASTNode source, ASTNode destination) {
		destination.setSourceRange(source.getStartPosition(), source.getLength());
	}
	
	/*
	 * @see ASTMatcher#match(ArrayType, Object)
	 */
	public boolean match(ArrayType node, Object other) {
		copyPositions(node, (ASTNode) other);
		return super.match(node, other);
	}

	/*
	 * @see ASTMatcher#match(QualifiedName, Object)
	 */
	public boolean match(QualifiedName node, Object other) {
		copyPositions(node, (ASTNode) other);
		return super.match(node, other);
	}

	/*
	 * @see ASTMatcher#match(SimpleName, Object)
	 */
	public boolean match(SimpleName node, Object other) {
		copyPositions(node, (ASTNode) other);
		return super.match(node, other);
	}

	/*
	 * @see ASTMatcher#match(SimpleType, Object)
	 */
	public boolean match(SimpleType node, Object other) {
		copyPositions(node, (ASTNode) other);
		return super.match(node, other);
	}

	/*
	 * @see ASTMatcher#match(PrimitiveType, Object)
	 */
	public boolean match(PrimitiveType node, Object other) {
		copyPositions(node, (ASTNode) other);
		return super.match(node, other);
	}
.....
}
I agree that this is not efficient, but it should work.
Comment 6 Olivier Thomann CLA 2002-09-13 15:18:38 EDT
I forgot to put the usage of this class:
Type clone = (Type) ASTNode.copySubtree(this.ast, type);
type.subtreeMatch(new CopyPositionsMatcher(), clone);
Comment 7 Jim des Rivieres CLA 2002-09-13 15:51:52 EDT
The API spec for ASTNode.copySubtree etc. is silent on whether source 
positions are copied or not. Consequently, no client cannot rely on them
being copied or not copied. I think it would be better for clients if this 
aspect was specified one way or the other. As Martin points out, it is easier
for a client to clear unwanted source positions than to copy them over if the 
do want them. So I suggest changing the spec and implementation to always copy 
source range info.
Comment 8 Olivier Thomann CLA 2002-09-13 15:59:34 EDT
This is what I did in the past and you said that clients might not need the 
source positions and therefore they should pay for it. It is slower to copy all 
positions. I have done that when I tried to get the subpart of an array type in 
the converter. So I change the current implementation to copy the source 
positions?
Comment 9 Olivier Thomann CLA 2002-09-16 08:45:47 EDT
Ok, then change the specs and I will update the implementation as soon as the
specs are changed.
Comment 10 Jim des Rivieres CLA 2002-09-16 09:59:49 EDT
Changed spec for ASTNode.copySubtree/s to say:

* Source range information on the original nodes is automatically copied to 
the new
* nodes. Client properties (<code>properties</code>) are not carried over.

Comment 11 Olivier Thomann CLA 2002-09-16 12:00:29 EDT
Fixed and released in 2.1 stream.
Comment 12 Olivier Thomann CLA 2002-09-19 10:52:04 EDT
Regression tests added.
Verified.
Comment 13 David Audel CLA 2002-10-17 10:17:09 EDT
Verified.