1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 333842: The ASTRewriter looses one insertion edits if one both inserts before, and replaces an AST Statement

https://bugs.eclipse.org/bugs/show_bug.cgi?id=333842
This commit is contained in:
Emanuel Graf 2011-01-12 13:17:42 +00:00
parent 3ae08fdeab
commit 687eb68210
3 changed files with 126 additions and 22 deletions

View file

@ -0,0 +1,101 @@
/*******************************************************************************
* Copyright (c) 2011 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.replace;
import junit.framework.Test;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTExpressionStatement;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor;
import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExpressionStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTUnaryExpression;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification;
import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore;
public class ReplaceInsertStatementTest extends ChangeGeneratorTest {
public ReplaceInsertStatementTest() {
super("Replace Insert Statement Test");
}
@Override
protected void setUp() throws Exception {
source = "void main(){\r\n int i = 0;\r\n ++i;\r\n}"; //$NON-NLS-1$
expectedSource = "void main(){\r\n int i = 0;\r\n i = 42;\r\n i++;\r\n}"; //$NON-NLS-1$
super.setUp();
}
public static Test suite() {
return new ReplaceInsertStatementTest();
}
/* (non-Javadoc)
* @see org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest#createModificator(org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore)
*/
@Override
protected CPPASTVisitor createModificator(
final ASTModificationStore modStore) {
return new CPPASTVisitor() {
{
shouldVisitStatements = true;
}
@Override
public int visit(IASTStatement statement) {
if (statement instanceof IASTCompoundStatement) {
IASTCompoundStatement compStmt = (IASTCompoundStatement) statement;
IASTStatement stmt = compStmt.getStatements()[1];
IASTIdExpression id = new CPPASTIdExpression(
new CPPASTName("i".toCharArray()));
IASTLiteralExpression value = new CPPASTLiteralExpression(
IASTLiteralExpression.lk_integer_constant, "42");
IASTExpressionStatement insertStmt = new CPPASTExpressionStatement(
new CPPASTBinaryExpression(
IASTBinaryExpression.op_assign, id, value));
IASTIdExpression incId = new CPPASTIdExpression(
new CPPASTName("i".toCharArray()));
IASTUnaryExpression incExp = new CPPASTUnaryExpression(
IASTUnaryExpression.op_postFixIncr, incId);
IASTExpressionStatement replaceStatement = new CPPASTExpressionStatement(
incExp);
ASTModification replaceMod = new ASTModification(
ASTModification.ModificationKind.REPLACE, stmt,
replaceStatement, null);
modStore.storeModification(null, replaceMod);
ASTModification insertMod = new ASTModification(
ASTModification.ModificationKind.INSERT_BEFORE,
stmt, insertStmt, null);
modStore.storeModification(null, insertMod);
}
return PROCESS_CONTINUE;
}
};
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@ -36,6 +36,7 @@ public class ReplaceTestSuite{
suite.addTest(ArraySizeExpressionTest.suite());
suite.addTest(NewInitializerExpressionTest.suite());
suite.addTest(StatementTest.suite());
suite.addTest(ReplaceInsertStatementTest.suite());
return suite;
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* Copyright (c) 2008, 2011 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
@ -35,26 +35,6 @@ public class ASTModificationHelper {
public <T extends IASTNode> T[] createModifiedChildArray(IASTNode parent, T[] unmodifiedChildren, Class<T> clazz){
ArrayList<T> modifiedChildren = new ArrayList<T>(Arrays.asList(unmodifiedChildren));
for(T currentChild : unmodifiedChildren){
for(ASTModification childModification : modificationsForNode(currentChild)){
try{
final T newNode = cast(childModification.getNewNode(), clazz);
switch(childModification.getKind()){
case REPLACE:
if (newNode != null) {
modifiedChildren.add(modifiedChildren.indexOf(childModification.getTargetNode()), newNode);
}
modifiedChildren.remove(childModification.getTargetNode());
break;
case INSERT_BEFORE:
case APPEND_CHILD:
throw new UnhandledASTModificationException(childModification);
}
}catch(ClassCastException e){
throw new UnhandledASTModificationException(childModification);
}
}
}
for(ASTModification parentModification : modificationsForNode(parent)){
switch(parentModification.getKind()){
@ -91,6 +71,28 @@ public class ASTModificationHelper {
}
}
for (T currentChild : unmodifiedChildren) {
for (ASTModification childModification : modificationsForNode(currentChild)) {
try {
final T newNode = cast(childModification.getNewNode(), clazz);
switch (childModification.getKind()) {
case REPLACE:
if (newNode != null) {
modifiedChildren.add(
modifiedChildren.indexOf(childModification.getTargetNode()),
newNode);
}
modifiedChildren.remove(childModification.getTargetNode());
break;
case INSERT_BEFORE:
case APPEND_CHILD:
throw new UnhandledASTModificationException(childModification);
}
} catch (ClassCastException e) {
throw new UnhandledASTModificationException(childModification);
}
}
}
return modifiedChildren.toArray(newArrayInstance(clazz, modifiedChildren.size()));
}