From 4ac33cedb711f29f6552515ae036c706dde2a7c4 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Mon, 21 Apr 2008 08:44:07 +0000 Subject: [PATCH] Fix for superfluous semicolon by Emanuel Graf, bug 226533. --- .../insertbefore/AddDeclarationBug.java | 95 +++++++++++++++++++ .../insertbefore/InsertBeforeTestSuite.java | 1 + .../changegenerator/ChangeGenerator.java | 39 +++++--- 3 files changed, 124 insertions(+), 11 deletions(-) create mode 100644 core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/AddDeclarationBug.java diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/AddDeclarationBug.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/AddDeclarationBug.java new file mode 100644 index 00000000000..72daee698d1 --- /dev/null +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/AddDeclarationBug.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * Copyright (c) 2008 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 (IFS)- initial API and implementation + ******************************************************************************/ +package org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.insertbefore; + +import junit.framework.Test; + +import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration; +import org.eclipse.cdt.core.parser.tests.rewrite.changegenerator.ChangeGeneratorTest; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclaration; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModificationStore; +import org.eclipse.cdt.internal.core.dom.rewrite.ASTModification.ModificationKind; + +/** + * @author Emanuel Graf IFS + * + */ +public class AddDeclarationBug extends ChangeGeneratorTest { + + + + + public AddDeclarationBug() { + super("Add Declaration Bug Test"); + } + + @Override + protected void setUp() throws Exception { + source = "class A\n{\npublic:\n A();\n virtual ~A();\n int foo();\n \nprivate:\n int help();\n};"; //$NON-NLS-1$ + expectedSource = "class A\n{\npublic:\n A();\n virtual ~A();\n int foo();\n \nprivate:\n int help();\n int exp(int i);\n};"; //$NON-NLS-1$ + super.setUp(); + } + + @Override + protected CPPASTVisitor createModificator(final ASTModificationStore modStore) { + return new CPPASTVisitor() { + { + shouldVisitDeclSpecifiers = true; + } + + @Override + public int visit(IASTDeclSpecifier declSpec) { + if (declSpec instanceof ICPPASTCompositeTypeSpecifier) { + ICPPASTCompositeTypeSpecifier classNode = (ICPPASTCompositeTypeSpecifier) declSpec; + IASTSimpleDeclaration newDecl = new CPPASTSimpleDeclaration(); + + IASTSimpleDeclSpecifier returnTyp = new CPPASTSimpleDeclSpecifier(); + returnTyp.setType(IASTSimpleDeclSpecifier.t_int); + newDecl.setDeclSpecifier(returnTyp); + + IASTStandardFunctionDeclarator declarator = new CPPASTFunctionDeclarator(new CPPASTName("exp".toCharArray())); + IASTSimpleDeclSpecifier paramTyp = new CPPASTSimpleDeclSpecifier(); + paramTyp.setType(IASTSimpleDeclSpecifier.t_int); + IASTDeclarator decl = new CPPASTDeclarator(new CPPASTName("i".toCharArray())); + ICPPASTParameterDeclaration param = new CPPASTParameterDeclaration(paramTyp, decl); + declarator.addParameterDeclaration(param); + newDecl.addDeclarator(declarator); + + ASTModification mod= new ASTModification(ModificationKind.APPEND_CHILD, classNode, newDecl, null); + modStore.storeModification(null, mod); + } + return PROCESS_CONTINUE; + } + + + }; + } + + public static Test suite() { + return new AddDeclarationBug(); + + } + +} diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/InsertBeforeTestSuite.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/InsertBeforeTestSuite.java index 659b221861a..111fb8c3d22 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/InsertBeforeTestSuite.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/rewrite/changegenerator/insertbefore/InsertBeforeTestSuite.java @@ -30,6 +30,7 @@ public class InsertBeforeTestSuite{ suite.addTest(ArrayModifierTest.suite()); suite.addTest(ExpressionTest.suite()); suite.addTest(ArraySizeExpressionTest.suite()); + suite.addTest(AddDeclarationBug.suite()); return suite; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java index b7b757843d5..04c2ba7ddfb 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGenerator.java @@ -233,7 +233,7 @@ public class ChangeGenerator extends CPPASTVisitor { int lastCommonPositionInSynthCode = codeComparer .getLastCommonPositionInSynthCode(); int firstPositionOfCommonEndInSynthCode = codeComparer - .getFirstPositionOfCommonEndInSynthCode(lastCommonPositionInSynthCode); + .getFirstPositionOfCommonEndInSynthCode(lastCommonPositionInSynthCode, lastCommonPositionInOriginalCode); int firstPositionOfCommonEndInOriginalCode = codeComparer .getFirstPositionOfCommonEndInOriginalCode(lastCommonPositionInSynthCode); @@ -444,35 +444,50 @@ public class ChangeGenerator extends CPPASTVisitor { return originalCode.length() - lastCommonPosition; } - public int getFirstPositionOfCommonEndInSynthCode(int limmit) { + public int getFirstPositionOfCommonEndInSynthCode(int limmit, int lastCommonPositionInOriginal) { - int lastCommonPosition = -1; + int lastCommonPosition = 0; int originalCodePosition = -1; int synthCodePosition = -1; + int korOffset = 0; StringBuilder reverseOriginalCode = new StringBuilder(originalCode) - .reverse(); + .reverse(); StringBuilder reverseSynthCode = new StringBuilder(synthCode) - .reverse(); + .reverse(); do { + if (lastCommonPosition >= 0 + && lastCommonPositionInOriginal >= 0 + && originalCode.charAt(lastCommonPositionInOriginal + - korOffset) == reverseSynthCode + .charAt(lastCommonPosition)) { + ++korOffset; + } else { + korOffset = 0; + } lastCommonPosition = synthCodePosition; originalCodePosition = nextInterrestingPosition( reverseOriginalCode, originalCodePosition); synthCodePosition = nextInterrestingPosition(reverseSynthCode, synthCodePosition); + } while (originalCodePosition > -1 && synthCodePosition > -1 && synthCodePosition < synthCode.length() - limmit && reverseOriginalCode.charAt(originalCodePosition) == reverseSynthCode - .charAt(synthCodePosition)); + .charAt(synthCodePosition)); if (lastCommonPosition < 0 || lastCommonPosition >= synthCode.length()) { return -1; } - return synthCode.length() - lastCommonPosition; + if (korOffset > 0) { + --korOffset; + } + + return synthCode.length() - lastCommonPosition + korOffset; } private int nextInterrestingPosition(StringBuilder code, int position) { @@ -519,13 +534,15 @@ public class ChangeGenerator extends CPPASTVisitor { int lastCommonPositionInSynth = getLastCommonPositionInSynthCode(); int firstOfCommonEndInOriginal = getFirstPositionOfCommonEndInOriginalCode(lastCommonPositionInSynth); int lastCommonPositionInOriginal = getLastCommonPositionInOriginalCode(); - int firstOfCommonEndInSynth = getFirstPositionOfCommonEndInSynthCode(lastCommonPositionInSynth); + int firstOfCommonEndInSynth = getFirstPositionOfCommonEndInSynthCode( + lastCommonPositionInSynth, lastCommonPositionInOriginal); - if ((firstOfCommonEndInSynth >= 0 ? firstOfCommonEndInOriginal + int i = (firstOfCommonEndInSynth >= 0 ? firstOfCommonEndInOriginal : originalCode.length()) - - lastCommonPositionInOriginal <= 0) { + - lastCommonPositionInOriginal; + if (i <= 0) { String insertCode = synthCode.substring( - lastCommonPositionInSynth, firstOfCommonEndInSynth + 1); + lastCommonPositionInSynth, firstOfCommonEndInSynth); InsertEdit iEdit = new InsertEdit(changeOffset + lastCommonPositionInOriginal, insertCode); edit.addChild(iEdit);