1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 01:15:29 +02:00

Bug 424876 - Unable to extract a function containing nested loops

This commit is contained in:
Sergey Prigogin 2014-01-03 18:39:31 -08:00
parent 59b67cd28b
commit 3005e7ef26
5 changed files with 46 additions and 5 deletions

View file

@ -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_

View file

@ -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) {

View file

@ -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));

View file

@ -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;

View file

@ -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