From 08c8ef800e1b076e08913ae57754c2fe1412d0c8 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Thu, 22 Mar 2012 19:51:08 -0700 Subject: [PATCH] Fixed a bug in Extract Function refactoring. --- .../ExtractFunctionRefactoringTest.java | 12 +++++------- .../corext/refactoring/code/flow/FlowAnalyzer.java | 5 +++++ .../refactoring/code/flow/InputFlowAnalyzer.java | 6 ++++-- .../corext/refactoring/code/flow/Selection.java | 4 ++-- 4 files changed, 16 insertions(+), 11 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java index 10910d20611..2c2a6f34faf 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java @@ -1775,9 +1775,8 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //test.c //void test() { // int i = 0; - // while (i <= 10) { + // while (i <= 10) // /*$*/i++;/*$$*/ - // } //} //==================== //int extracted(int i) { @@ -1787,9 +1786,8 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // //void test() { // int i = 0; - // while (i <= 10) { + // while (i <= 10) // i = extracted(i); - // } //} public void testOutputParametersDetectionInWhileLoop() throws Exception { assertRefactoringSuccess(); @@ -1887,7 +1885,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { //void method() { // loop(); //} - public void testDontReturnVariablesThatArentUsed() throws Exception { + public void testDoNotReturnVariablesThatAreNotUsed() throws Exception { extractedFunctionName = "loop"; assertRefactoringSuccess(); } @@ -1898,7 +1896,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // return;/*$$*/ // //unreachable //} - public void testDontExtractCodeContainingReturn() throws Exception { + public void testDoNotExtractCodeContainingReturn() throws Exception { assertRefactoringFailure(); } @@ -1909,7 +1907,7 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase { // continue;/*$$*/ // } //} - public void testDontExtractCodeContainingContinue() throws Exception { + public void testDoNotExtractCodeContainingContinue() throws Exception { assertRefactoringFailure(); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/FlowAnalyzer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/FlowAnalyzer.java index b5a8f2304af..15bee0552bc 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/FlowAnalyzer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/FlowAnalyzer.java @@ -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.ICPPASTSimpleTypeConstructorExpression; 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.internal.core.dom.parser.cpp.semantics.CPPVariableReadWriteFlags; @@ -668,6 +669,10 @@ abstract class FlowAnalyzer extends ASTGenericVisitor { WhileFlowInfo info= createWhile(); setFlowInfo(node, info); info.mergeCondition(getFlowInfo(node.getCondition()), fFlowContext); + if (node instanceof ICPPASTWhileStatement) { + info.mergeCondition(getFlowInfo(((ICPPASTWhileStatement) node).getConditionDeclaration()), + fFlowContext); + } info.mergeAction(getFlowInfo(node.getBody()), fFlowContext); info.removeLabel(null); return PROCESS_SKIP; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/InputFlowAnalyzer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/InputFlowAnalyzer.java index f2726269bf9..d851d153b1e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/InputFlowAnalyzer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/InputFlowAnalyzer.java @@ -299,15 +299,17 @@ public class InputFlowAnalyzer extends FlowAnalyzer { @Override protected boolean traverseNode(IASTNode node) { + if (fSelection.covers(node)) + return false; if (node instanceof IASTLabelStatement) return true; - return ASTNodes.endOffset(node) > fSelection.getEnd(); + return ASTNodes.endOffset(node) >= fSelection.getEnd(); } @Override protected boolean shouldCreateReturnFlowInfo(IASTReturnStatement node) { // 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. return ASTNodes.offset(node) >= fSelection.getEnd(); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/Selection.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/Selection.java index cc9d2b9bea1..e20dea162b0 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/Selection.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/corext/refactoring/code/flow/Selection.java @@ -108,7 +108,7 @@ public class Selection { return INTERSECTS; } - public int getEndVisitSelectionMode(IASTNode node) { + public int getLeaveSelectionMode(IASTNode node) { IASTFileLocation location = node.getFileLocation(); int nodeStart= location.getNodeOffset(); int nodeEnd= nodeStart + location.getNodeLength(); @@ -116,7 +116,7 @@ public class Selection { return BEFORE; } else if (covers(node)) { return SELECTED; - } else if (nodeEnd >= fEnd) { + } else if (fEnd <= nodeEnd) { return AFTER; } return INTERSECTS;