Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSergey Prigogin2014-01-04 02:39:31 +0000
committerSergey Prigogin2014-01-04 02:39:31 +0000
commit3005e7ef26b52779956691d7ae54516eba21e390 (patch)
tree82d50ad0ffbb13c592fb5313eb5368fa8945f95e
parent59b67cd28b0980e51c8216a274e0e7a7045f565b (diff)
downloadorg.eclipse.cdt-3005e7ef26b52779956691d7ae54516eba21e390.tar.gz
org.eclipse.cdt-3005e7ef26b52779956691d7ae54516eba21e390.tar.xz
org.eclipse.cdt-3005e7ef26b52779956691d7ae54516eba21e390.zip
Bug 424876 - Unable to extract a function containing nested loops
-rw-r--r--core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java27
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java16
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java2
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java2
-rw-r--r--core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties4
5 files changed, 46 insertions, 5 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 3b624783913..ca6e9851d95 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
@@ -568,6 +568,33 @@ public class ExtractFunctionRefactoringTest extends RefactoringTestBase {
public void testNamedTypedField() throws Exception {
assertRefactoringSuccess();
}
+
+ //A.cpp
+ //void test() {
+ // for (int i = 0; i < 2; i++) {
+ // /*$*/for (int j = 0; j < i; j++) {
+ // for (int k = 0; k < j; k++) {
+ // }
+ // }/*$$*/
+ // }
+ //}
+ //====================
+ //void extracted(int i) {
+ // for (int j = 0; j < i; j++) {
+ // for (int k = 0; k < j; k++) {
+ // }
+ // }
+ //}
+ //
+ //void test() {
+ // for (int i = 0; i < 2; i++) {
+ // extracted(i);
+ // }
+ //}
+ public void testNestedLoops_Bug424876() throws Exception {
+ assertRefactoringSuccess();
+ }
+
//A.h
//#ifndef A_H_
//#define A_H_
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java
index 8018d70810b..ce8610dea43 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java
@@ -15,6 +15,7 @@ package org.eclipse.cdt.internal.ui.refactoring;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
+import java.util.Iterator;
import java.util.List;
import java.util.Set;
@@ -44,6 +45,7 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.PreferenceConstants;
+import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVariableReadWriteFlags;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
import org.eclipse.cdt.internal.core.dom.rewrite.util.ASTNodes;
@@ -214,7 +216,19 @@ public class NodeContainer {
InputFlowAnalyzer analyzer = new InputFlowAnalyzer(flowContext, selection, true);
FlowInfo argInfo= analyzer.perform(enclosingFunction);
- return argInfo.get(flowContext, FlowInfo.READ | FlowInfo.READ_POTENTIAL | FlowInfo.UNKNOWN);
+ Set<IVariable> variables = argInfo.get(flowContext, FlowInfo.READ | FlowInfo.READ_POTENTIAL | FlowInfo.UNKNOWN);
+
+ // Remove variables with scopes limited to the selection.
+ for (Iterator<IVariable> iter = variables.iterator(); iter.hasNext();) {
+ IVariable var = iter.next();
+ try {
+ IASTNode scopeNode = ASTInternal.getPhysicalNodeOfScope(var.getScope());
+ if (selection.covers(scopeNode))
+ iter.remove();
+ } catch (DOMException e) {
+ }
+ }
+ return variables;
}
public static boolean hasReferenceOperator(IASTDeclarator declarator) {
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java
index c7b9717be4f..f2db53bd27f 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractFunctionRefactoring.java
@@ -186,7 +186,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
List<NameInformation> returnValueCandidates = container.getReturnValueCandidates();
if (returnValueCandidates.size() > 1) {
- initStatus.addFatalError(Messages.ExtractFunctionRefactoring_TooManySelected);
+ initStatus.addFatalError(Messages.ExtractFunctionRefactoring_TooManyDeclarations);
return initStatus;
} else if (returnValueCandidates.size() == 1) {
info.setMandatoryReturnVariable(returnValueCandidates.get(0));
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java
index 59bf277538e..f8d4a56c525 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.java
@@ -17,7 +17,7 @@ import org.eclipse.osgi.util.NLS;
final class Messages extends NLS {
public static String ExtractFunctionRefactoring_ExtractFunction;
public static String ExtractFunctionRefactoring_NoStmtSelected;
- public static String ExtractFunctionRefactoring_TooManySelected;
+ public static String ExtractFunctionRefactoring_TooManyDeclarations;
public static String ExtractFunctionRefactoring_no_declaration_of_surrounding_method;
public static String ExtractFunctionRefactoring_name_in_use;
public static String ExtractFunctionRefactoring_parameter_name_in_use;
diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties
index ed09621d986..5c1ea138b11 100644
--- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties
+++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/Messages.properties
@@ -1,5 +1,5 @@
###############################################################################
-# Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik
+# Copyright (c) 2008, 2014 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
@@ -12,7 +12,7 @@
###############################################################################
ExtractFunctionRefactoring_ExtractFunction=Extract Function
ExtractFunctionRefactoring_NoStmtSelected=No statement selected
-ExtractFunctionRefactoring_TooManySelected=Too many declarations in selection.
+ExtractFunctionRefactoring_TooManyDeclarations=More than one externally referenced variable declared in selection.
ExtractFunctionRefactoring_no_declaration_of_surrounding_method=Unable to find declaration of the surrounding method.
ExtractFunctionRefactoring_name_in_use=Name already in use.
ExtractFunctionRefactoring_parameter_name_in_use=''{0}'' is already used as a name in the selected code

Back to the top