1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-31 12:55:40 +02:00

Fixed a bug in Extract Function refactoring.

This commit is contained in:
Sergey Prigogin 2012-03-22 19:51:08 -07:00
parent 4d2a4b2d7c
commit 08c8ef800e
4 changed files with 16 additions and 11 deletions

View file

@ -1775,9 +1775,8 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase {
//test.c //test.c
//void test() { //void test() {
// int i = 0; // int i = 0;
// while (i <= 10) { // while (i <= 10)
// /*$*/i++;/*$$*/ // /*$*/i++;/*$$*/
// }
//} //}
//==================== //====================
//int extracted(int i) { //int extracted(int i) {
@ -1787,9 +1786,8 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase {
// //
//void test() { //void test() {
// int i = 0; // int i = 0;
// while (i <= 10) { // while (i <= 10)
// i = extracted(i); // i = extracted(i);
// }
//} //}
public void testOutputParametersDetectionInWhileLoop() throws Exception { public void testOutputParametersDetectionInWhileLoop() throws Exception {
assertRefactoringSuccess(); assertRefactoringSuccess();
@ -1887,7 +1885,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase {
//void method() { //void method() {
// loop(); // loop();
//} //}
public void testDontReturnVariablesThatArentUsed() throws Exception { public void testDoNotReturnVariablesThatAreNotUsed() throws Exception {
extractedFunctionName = "loop"; extractedFunctionName = "loop";
assertRefactoringSuccess(); assertRefactoringSuccess();
} }
@ -1898,7 +1896,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase {
// return;/*$$*/ // return;/*$$*/
// //unreachable // //unreachable
//} //}
public void testDontExtractCodeContainingReturn() throws Exception { public void testDoNotExtractCodeContainingReturn() throws Exception {
assertRefactoringFailure(); assertRefactoringFailure();
} }
@ -1909,7 +1907,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase {
// continue;/*$$*/ // continue;/*$$*/
// } // }
//} //}
public void testDontExtractCodeContainingContinue() throws Exception { public void testDoNotExtractCodeContainingContinue() throws Exception {
assertRefactoringFailure(); assertRefactoringFailure();
} }

View file

@ -79,6 +79,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVariableReadWriteFlags; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVariableReadWriteFlags;
@ -668,6 +669,10 @@ abstract class FlowAnalyzer extends ASTGenericVisitor {
WhileFlowInfo info= createWhile(); WhileFlowInfo info= createWhile();
setFlowInfo(node, info); setFlowInfo(node, info);
info.mergeCondition(getFlowInfo(node.getCondition()), fFlowContext); info.mergeCondition(getFlowInfo(node.getCondition()), fFlowContext);
if (node instanceof ICPPASTWhileStatement) {
info.mergeCondition(getFlowInfo(((ICPPASTWhileStatement) node).getConditionDeclaration()),
fFlowContext);
}
info.mergeAction(getFlowInfo(node.getBody()), fFlowContext); info.mergeAction(getFlowInfo(node.getBody()), fFlowContext);
info.removeLabel(null); info.removeLabel(null);
return PROCESS_SKIP; return PROCESS_SKIP;

View file

@ -299,15 +299,17 @@ public class InputFlowAnalyzer extends FlowAnalyzer {
@Override @Override
protected boolean traverseNode(IASTNode node) { protected boolean traverseNode(IASTNode node) {
if (fSelection.covers(node))
return false;
if (node instanceof IASTLabelStatement) if (node instanceof IASTLabelStatement)
return true; return true;
return ASTNodes.endOffset(node) > fSelection.getEnd(); return ASTNodes.endOffset(node) >= fSelection.getEnd();
} }
@Override @Override
protected boolean shouldCreateReturnFlowInfo(IASTReturnStatement node) { protected boolean shouldCreateReturnFlowInfo(IASTReturnStatement node) {
// Make sure that the whole return statement is located after the selection. // Make sure that the whole return statement is located after the selection.
// There can be cases like return i + [x + 10] * 10; In this case we must not create // There can be cases like return i + (x + 10) * 10; In this case we must not create
// a return info node. // a return info node.
return ASTNodes.offset(node) >= fSelection.getEnd(); return ASTNodes.offset(node) >= fSelection.getEnd();
} }

View file

@ -108,7 +108,7 @@ public class Selection {
return INTERSECTS; return INTERSECTS;
} }
public int getEndVisitSelectionMode(IASTNode node) { public int getLeaveSelectionMode(IASTNode node) {
IASTFileLocation location = node.getFileLocation(); IASTFileLocation location = node.getFileLocation();
int nodeStart= location.getNodeOffset(); int nodeStart= location.getNodeOffset();
int nodeEnd= nodeStart + location.getNodeLength(); int nodeEnd= nodeStart + location.getNodeLength();
@ -116,7 +116,7 @@ public class Selection {
return BEFORE; return BEFORE;
} else if (covers(node)) { } else if (covers(node)) {
return SELECTED; return SELECTED;
} else if (nodeEnd >= fEnd) { } else if (fEnd <= nodeEnd) {
return AFTER; return AFTER;
} }
return INTERSECTS; return INTERSECTS;