mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-07-30 04:15:35 +02:00
Fix for superfluous semicolon by Emanuel Graf, bug 226533.
This commit is contained in:
parent
cb728b7643
commit
4ac33cedb7
3 changed files with 124 additions and 11 deletions
|
@ -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();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -30,6 +30,7 @@ public class InsertBeforeTestSuite{
|
||||||
suite.addTest(ArrayModifierTest.suite());
|
suite.addTest(ArrayModifierTest.suite());
|
||||||
suite.addTest(ExpressionTest.suite());
|
suite.addTest(ExpressionTest.suite());
|
||||||
suite.addTest(ArraySizeExpressionTest.suite());
|
suite.addTest(ArraySizeExpressionTest.suite());
|
||||||
|
suite.addTest(AddDeclarationBug.suite());
|
||||||
|
|
||||||
return suite;
|
return suite;
|
||||||
}
|
}
|
||||||
|
|
|
@ -233,7 +233,7 @@ public class ChangeGenerator extends CPPASTVisitor {
|
||||||
int lastCommonPositionInSynthCode = codeComparer
|
int lastCommonPositionInSynthCode = codeComparer
|
||||||
.getLastCommonPositionInSynthCode();
|
.getLastCommonPositionInSynthCode();
|
||||||
int firstPositionOfCommonEndInSynthCode = codeComparer
|
int firstPositionOfCommonEndInSynthCode = codeComparer
|
||||||
.getFirstPositionOfCommonEndInSynthCode(lastCommonPositionInSynthCode);
|
.getFirstPositionOfCommonEndInSynthCode(lastCommonPositionInSynthCode, lastCommonPositionInOriginalCode);
|
||||||
|
|
||||||
int firstPositionOfCommonEndInOriginalCode = codeComparer
|
int firstPositionOfCommonEndInOriginalCode = codeComparer
|
||||||
.getFirstPositionOfCommonEndInOriginalCode(lastCommonPositionInSynthCode);
|
.getFirstPositionOfCommonEndInOriginalCode(lastCommonPositionInSynthCode);
|
||||||
|
@ -444,11 +444,12 @@ public class ChangeGenerator extends CPPASTVisitor {
|
||||||
return originalCode.length() - lastCommonPosition;
|
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 originalCodePosition = -1;
|
||||||
int synthCodePosition = -1;
|
int synthCodePosition = -1;
|
||||||
|
int korOffset = 0;
|
||||||
|
|
||||||
StringBuilder reverseOriginalCode = new StringBuilder(originalCode)
|
StringBuilder reverseOriginalCode = new StringBuilder(originalCode)
|
||||||
.reverse();
|
.reverse();
|
||||||
|
@ -456,11 +457,21 @@ public class ChangeGenerator extends CPPASTVisitor {
|
||||||
.reverse();
|
.reverse();
|
||||||
|
|
||||||
do {
|
do {
|
||||||
|
if (lastCommonPosition >= 0
|
||||||
|
&& lastCommonPositionInOriginal >= 0
|
||||||
|
&& originalCode.charAt(lastCommonPositionInOriginal
|
||||||
|
- korOffset) == reverseSynthCode
|
||||||
|
.charAt(lastCommonPosition)) {
|
||||||
|
++korOffset;
|
||||||
|
} else {
|
||||||
|
korOffset = 0;
|
||||||
|
}
|
||||||
lastCommonPosition = synthCodePosition;
|
lastCommonPosition = synthCodePosition;
|
||||||
originalCodePosition = nextInterrestingPosition(
|
originalCodePosition = nextInterrestingPosition(
|
||||||
reverseOriginalCode, originalCodePosition);
|
reverseOriginalCode, originalCodePosition);
|
||||||
synthCodePosition = nextInterrestingPosition(reverseSynthCode,
|
synthCodePosition = nextInterrestingPosition(reverseSynthCode,
|
||||||
synthCodePosition);
|
synthCodePosition);
|
||||||
|
|
||||||
} while (originalCodePosition > -1
|
} while (originalCodePosition > -1
|
||||||
&& synthCodePosition > -1
|
&& synthCodePosition > -1
|
||||||
&& synthCodePosition < synthCode.length() - limmit
|
&& synthCodePosition < synthCode.length() - limmit
|
||||||
|
@ -472,7 +483,11 @@ public class ChangeGenerator extends CPPASTVisitor {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
return synthCode.length() - lastCommonPosition;
|
if (korOffset > 0) {
|
||||||
|
--korOffset;
|
||||||
|
}
|
||||||
|
|
||||||
|
return synthCode.length() - lastCommonPosition + korOffset;
|
||||||
}
|
}
|
||||||
|
|
||||||
private int nextInterrestingPosition(StringBuilder code, int position) {
|
private int nextInterrestingPosition(StringBuilder code, int position) {
|
||||||
|
@ -519,13 +534,15 @@ public class ChangeGenerator extends CPPASTVisitor {
|
||||||
int lastCommonPositionInSynth = getLastCommonPositionInSynthCode();
|
int lastCommonPositionInSynth = getLastCommonPositionInSynthCode();
|
||||||
int firstOfCommonEndInOriginal = getFirstPositionOfCommonEndInOriginalCode(lastCommonPositionInSynth);
|
int firstOfCommonEndInOriginal = getFirstPositionOfCommonEndInOriginalCode(lastCommonPositionInSynth);
|
||||||
int lastCommonPositionInOriginal = getLastCommonPositionInOriginalCode();
|
int lastCommonPositionInOriginal = getLastCommonPositionInOriginalCode();
|
||||||
int firstOfCommonEndInSynth = getFirstPositionOfCommonEndInSynthCode(lastCommonPositionInSynth);
|
int firstOfCommonEndInSynth = getFirstPositionOfCommonEndInSynthCode(
|
||||||
|
lastCommonPositionInSynth, lastCommonPositionInOriginal);
|
||||||
|
|
||||||
if ((firstOfCommonEndInSynth >= 0 ? firstOfCommonEndInOriginal
|
int i = (firstOfCommonEndInSynth >= 0 ? firstOfCommonEndInOriginal
|
||||||
: originalCode.length())
|
: originalCode.length())
|
||||||
- lastCommonPositionInOriginal <= 0) {
|
- lastCommonPositionInOriginal;
|
||||||
|
if (i <= 0) {
|
||||||
String insertCode = synthCode.substring(
|
String insertCode = synthCode.substring(
|
||||||
lastCommonPositionInSynth, firstOfCommonEndInSynth + 1);
|
lastCommonPositionInSynth, firstOfCommonEndInSynth);
|
||||||
InsertEdit iEdit = new InsertEdit(changeOffset
|
InsertEdit iEdit = new InsertEdit(changeOffset
|
||||||
+ lastCommonPositionInOriginal, insertCode);
|
+ lastCommonPositionInOriginal, insertCode);
|
||||||
edit.addChild(iEdit);
|
edit.addChild(iEdit);
|
||||||
|
|
Loading…
Add table
Reference in a new issue