1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-09-03 05:33:33 +02:00

Fixed unnecessary passing parameters by reference.

This commit is contained in:
Sergey Prigogin 2012-01-16 22:18:59 -08:00
parent 91866c57a2
commit 9b5c46e407
11 changed files with 161 additions and 143 deletions

View file

@ -73,20 +73,20 @@ public class ExtractFunctionRefactoringTest extends RefactoringTest {
private void setValues(ExtractFunctionInformation info) { private void setValues(ExtractFunctionInformation info) {
info.setMethodName(methodName); info.setMethodName(methodName);
info.setReplaceDuplicates(replaceDuplicates); info.setReplaceDuplicates(replaceDuplicates);
if (info.getInScopeDeclaredVariable() == null) { if (info.getMandatoryReturnVariable() == null) {
if (returnValue) { if (returnValue) {
info.setReturnVariable(info.getAllAfterUsedNames().get(returnParameterIndex)); info.setReturnVariable(info.getNamesUsedAfter().get(returnParameterIndex));
info.getAllAfterUsedNames().get(returnParameterIndex).setUserSetIsReference(false); info.getNamesUsedAfter().get(returnParameterIndex).setUserSetIsReference(false);
} }
} else { } else {
info.setReturnVariable(info.getInScopeDeclaredVariable()); info.setReturnVariable(info.getMandatoryReturnVariable());
} }
info.setVisibility(visibility); info.setVisibility(visibility);
info.setVirtual(virtual); info.setVirtual(virtual);
for (NameInformation name : info.getAllAfterUsedNames()) { for (NameInformation name : info.getNamesUsedAfter()) {
if (!name.isUserSetIsReturnValue()) { if (!name.isUserSetIsReturnValue()) {
name.setUserSetIsReference(name.isReference()); name.setUserSetIsReference(name.isOutput());
} }
} }
} }

View file

@ -12,7 +12,9 @@
package org.eclipse.cdt.internal.ui.refactoring; package org.eclipse.cdt.internal.ui.refactoring;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import org.eclipse.core.runtime.ILog; import org.eclipse.core.runtime.ILog;
import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.IStatus;
@ -51,7 +53,7 @@ public class NodeContainer {
public final NameInformation NULL_NAME_INFORMATION = new NameInformation(new CPPASTName()); public final NameInformation NULL_NAME_INFORMATION = new NameInformation(new CPPASTName());
private final List<IASTNode> nodes; private final List<IASTNode> nodes;
private final List<NameInformation> names; private List<NameInformation> names;
public class NameInformation { public class NameInformation {
private IASTName name; private IASTName name;
@ -59,7 +61,7 @@ public class NodeContainer {
private final List<IASTName> references; private final List<IASTName> references;
private List<IASTName> referencesAfterCached; private List<IASTName> referencesAfterCached;
private int lastCachedReferencesHash; private int lastCachedReferencesHash;
private boolean isReference; private boolean isOutput;
private boolean isReturnValue; private boolean isReturnValue;
private boolean isConst; private boolean isConst;
private boolean isWriteAccess; private boolean isWriteAccess;
@ -117,8 +119,8 @@ public class NodeContainer {
return referencesAfterCached; return referencesAfterCached;
} }
public boolean isUsedAfterReferences() { public boolean isReferencedAfterSelection() {
return getReferencesAfterSelection().size() > 0; return !getReferencesAfterSelection().isEmpty();
} }
public IASTParameterDeclaration getParameterDeclaration(boolean isReference, public IASTParameterDeclaration getParameterDeclaration(boolean isReference,
@ -195,7 +197,7 @@ public class NodeContainer {
return writer.write(declSpec); return writer.write(declSpec);
} }
public boolean isDeclarationExtracted() { public boolean isDeclaredInSelection() {
if (declaration != null && declaration.toCharArray().length > 0) { if (declaration != null && declaration.toCharArray().length > 0) {
int declOffset = declaration.getFileLocation().getNodeOffset(); int declOffset = declaration.getFileLocation().getNodeOffset();
return declOffset >= getStartOffset() && declOffset <= getEndOffset(); return declOffset >= getStartOffset() && declOffset <= getEndOffset();
@ -205,15 +207,15 @@ public class NodeContainer {
@Override @Override
public String toString() { public String toString() {
return name.toString() + ": " + (isDeclarationExtracted() ? "with declaration" : "without declaration"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ return name.toString() + (isDeclaredInSelection() ? " (declared inside)" : ""); //$NON-NLS-1$//$NON-NLS-2$
} }
public boolean isReference() { public boolean isOutput() {
return isReference; return isOutput;
} }
public void setReference(boolean isReference) { public void setOutput(boolean output) {
this.isReference = isReference; this.isOutput = output;
} }
public boolean isReturnValue() { public boolean isReturnValue() {
@ -268,7 +270,6 @@ public class NodeContainer {
public NodeContainer() { public NodeContainer() {
super(); super();
nodes = new ArrayList<IASTNode>(); nodes = new ArrayList<IASTNode>();
names = new ArrayList<NameInformation>();
} }
public final int size() { public final int size() {
@ -283,7 +284,11 @@ public class NodeContainer {
nodes.add(node); nodes.add(node);
} }
public void findAllNames() { private void findAllNames() {
if (names != null) {
return;
}
names = new ArrayList<NameInformation>();
for (IASTNode node : nodes) { for (IASTNode node : nodes) {
node.accept(new ASTVisitor() { node.accept(new ASTVisitor() {
{ {
@ -294,8 +299,7 @@ public class NodeContainer {
public int visit(IASTName name) { public int visit(IASTName name) {
IBinding bind = name.resolveBinding(); IBinding bind = name.resolveBinding();
if (bind instanceof ICPPBinding if (bind instanceof ICPPBinding && !(bind instanceof ICPPTemplateTypeParameter)) {
&& !(bind instanceof ICPPTemplateTypeParameter)) {
ICPPBinding cppBind = (ICPPBinding) bind; ICPPBinding cppBind = (ICPPBinding) bind;
try { try {
if (!cppBind.isGloballyQualified()) { if (!cppBind.isGloballyQualified()) {
@ -326,35 +330,33 @@ public class NodeContainer {
}); });
} }
for (NameInformation nameInf : names) { for (NameInformation nameInfo : names) {
IASTName name = nameInf.getName(); IASTName name = nameInfo.getName();
IASTTranslationUnit unit = name.getTranslationUnit(); IASTTranslationUnit unit = name.getTranslationUnit();
IASTName[] decls = unit.getDeclarationsInAST(name.resolveBinding()); IASTName[] decls = unit.getDeclarationsInAST(name.resolveBinding());
for (IASTName declaration : decls) { for (IASTName declaration : decls) {
nameInf.setDeclaration(declaration); nameInfo.setDeclaration(declaration);
}
if (nameInfo.isReferencedAfterSelection()) {
nameInfo.setOutput(true);
} }
} }
} }
/* /*
* Returns all local names in the selection which will be used after the * Returns all local names in the selection which are referenced after the selection.
* selection expected the ones which are pointers
*/ */
public List<NameInformation> getAllAfterUsedNames() { public List<NameInformation> getNamesUsedAfter() {
ArrayList<IASTName> declarations = new ArrayList<IASTName>(); findAllNames();
ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();
if (names.size() <= 0) { Set<IASTName> declarations = new HashSet<IASTName>();
findAllNames(); List<NameInformation> usedAfter = new ArrayList<NameInformation>();
}
for (NameInformation nameInf : names) { for (NameInformation nameInfo : names) {
if (!declarations.contains(nameInf.getDeclaration())) { if (declarations.add(nameInfo.getDeclaration())) {
declarations.add(nameInf.getDeclaration()); if (nameInfo.isReferencedAfterSelection()) {
if (nameInf.isUsedAfterReferences()) { usedAfter.add(nameInfo);
usedAfter.add(nameInf);
nameInf.setReference(true);
} }
} }
} }
@ -362,15 +364,16 @@ public class NodeContainer {
return usedAfter; return usedAfter;
} }
public List<NameInformation> getAllAfterUsedNamesChoosenByUser() { public List<NameInformation> getNamesUsedAfterChoosenByUser() {
ArrayList<IASTName> declarations = new ArrayList<IASTName>(); findAllNames();
ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();
for (NameInformation nameInf : names) { Set<IASTName> declarations = new HashSet<IASTName>();
if (!declarations.contains(nameInf.getDeclaration())) { List<NameInformation> usedAfter = new ArrayList<NameInformation>();
declarations.add(nameInf.getDeclaration());
if (nameInf.isUserSetIsReference() || nameInf.isUserSetIsReturnValue()) { for (NameInformation nameInfo : names) {
usedAfter.add(nameInf); if (declarations.add(nameInfo.getDeclaration())) {
if (nameInfo.isUserSetIsReference() || nameInfo.isUserSetIsReturnValue()) {
usedAfter.add(nameInfo);
} }
} }
} }
@ -379,21 +382,18 @@ public class NodeContainer {
} }
public List<NameInformation> getUsedNamesUnique() { public List<NameInformation> getUsedNamesUnique() {
ArrayList<IASTName> declarations = new ArrayList<IASTName>(); findAllNames();
ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>();
if (names.size() <= 0) { Set<IASTName> declarations = new HashSet<IASTName>();
findAllNames(); List<NameInformation> usedAfter = new ArrayList<NameInformation>();
}
for (NameInformation nameInf : names) { for (NameInformation nameInfo : names) {
if (!declarations.contains(nameInf.getDeclaration())) { if (declarations.add(nameInfo.getDeclaration())) {
declarations.add(nameInf.getDeclaration()); usedAfter.add(nameInfo);
usedAfter.add(nameInf);
} else { } else {
for (NameInformation nameInformation : usedAfter) { for (NameInformation nameInformation : usedAfter) {
if (nameInf.isWriteAccess() if (nameInfo.isWriteAccess() &&
&& nameInf.getDeclaration() == nameInformation.getDeclaration()) { nameInfo.getDeclaration() == nameInformation.getDeclaration()) {
nameInformation.setWriteAccess(true); nameInformation.setWriteAccess(true);
} }
} }
@ -403,24 +403,21 @@ public class NodeContainer {
return usedAfter; return usedAfter;
} }
/* /**
* Returns all local names in the selection which will be used after the * Returns all variables declared in the selection, which will are referenced after
* selection expected the ones which are pointers * the selection.
* XXX Was soll dieser Kommentar aussagen? --Mirko
*/ */
public List<NameInformation> getAllDeclaredInScope() { public List<NameInformation> getReturnValueCandidates() {
ArrayList<IASTName> declarations = new ArrayList<IASTName>(); Set<IASTName> declarations = new HashSet<IASTName>();
ArrayList<NameInformation> usedAfter = new ArrayList<NameInformation>(); List<NameInformation> usedAfter = new ArrayList<NameInformation>();
for (NameInformation nameInfo : names) { for (NameInformation nameInfo : names) {
if (nameInfo.isDeclarationExtracted() && if (nameInfo.isDeclaredInSelection() && nameInfo.isReferencedAfterSelection() &&
!declarations.contains(nameInfo.getDeclaration()) && declarations.add(nameInfo.getDeclaration())) {
nameInfo.isUsedAfterReferences()) {
declarations.add(nameInfo.getDeclaration());
usedAfter.add(nameInfo); usedAfter.add(nameInfo);
// Is return value candidate, set return value to true and reference to false // It's a return value candidate, set return value to true and reference to false
nameInfo.setReturnValue(true); nameInfo.setReturnValue(true);
nameInfo.setReference(false); nameInfo.setOutput(false);
} }
} }
@ -516,6 +513,7 @@ public class NodeContainer {
} }
public List<NameInformation> getNames() { public List<NameInformation> getNames() {
findAllNames();
return names; return names;
} }
} }

View file

@ -51,7 +51,7 @@ public class ChooserComposite extends Composite {
setLayout(layout); setLayout(layout);
boolean hasNoPredefinedReturnValue = true; boolean hasNoPredefinedReturnValue = true;
if (info.getInScopeDeclaredVariable() != null) { if (info.getMandatoryReturnVariable() != null) {
hasNoPredefinedReturnValue = false; hasNoPredefinedReturnValue = false;
} }
@ -75,14 +75,14 @@ public class ChooserComposite extends Composite {
} }
addColumnToTable(table, ""); //$NON-NLS-1$ addColumnToTable(table, ""); //$NON-NLS-1$
for (int i = 0; i < info.getAllUsedNames().size(); i++) { for (int i = 0; i < info.getParameterCandidates().size(); i++) {
if (!info.getAllUsedNames().get(i).isDeclarationExtracted()) { if (!info.getParameterCandidates().get(i).isDeclaredInSelection()) {
TableItem item = new TableItem(table, SWT.NONE); TableItem item = new TableItem(table, SWT.NONE);
TableEditor editor = new TableEditor(table); TableEditor editor = new TableEditor(table);
int columnIndex = 0; int columnIndex = 0;
final NameInformation name = info.getAllUsedNames().get(i); final NameInformation name = info.getParameterCandidates().get(i);
// Text // Text
item.setText(columnIndex++, name.getType()); item.setText(columnIndex++, name.getType());
@ -95,14 +95,14 @@ public class ChooserComposite extends Composite {
referenceButton.setSelection(true); referenceButton.setSelection(true);
referenceButton.setEnabled(false); referenceButton.setEnabled(false);
} else { } else {
referenceButton.setSelection(name.isReference()); referenceButton.setSelection(name.isOutput());
} }
referenceButton.setBackground(table.getBackground()); referenceButton.setBackground(table.getBackground());
referenceButton.addSelectionListener(new SelectionListener() { referenceButton.addSelectionListener(new SelectionListener() {
@Override @Override
public void widgetDefaultSelected(SelectionEvent e) { public void widgetDefaultSelected(SelectionEvent e) {
name.setUserSetIsReference(referenceButton.getSelection()); name.setUserSetIsReference(referenceButton.getSelection());
onVisibilityOrReturnChange(info.getAllUsedNames()); onVisibilityOrReturnChange(info.getParameterCandidates());
} }
@Override @Override
@ -128,7 +128,7 @@ public class ChooserComposite extends Composite {
@Override @Override
public void widgetDefaultSelected(SelectionEvent e) { public void widgetDefaultSelected(SelectionEvent e) {
name.setConst(constButton.getSelection()); name.setConst(constButton.getSelection());
onVisibilityOrReturnChange(info.getAllUsedNames()); onVisibilityOrReturnChange(info.getParameterCandidates());
} }
@Override @Override
@ -149,7 +149,7 @@ public class ChooserComposite extends Composite {
editor = new TableEditor(table); editor = new TableEditor(table);
final Button returnButton = new Button(table, SWT.RADIO); final Button returnButton = new Button(table, SWT.RADIO);
returnButton.setSelection(name.isReturnValue()); returnButton.setSelection(name.isReturnValue());
name.setUserSetIsReference(name.isReference()); name.setUserSetIsReference(name.isOutput());
returnButton.setEnabled(hasNoPredefinedReturnValue); returnButton.setEnabled(hasNoPredefinedReturnValue);
returnButton.setBackground(table.getBackground()); returnButton.setBackground(table.getBackground());
returnButton.addSelectionListener(new SelectionListener() { returnButton.addSelectionListener(new SelectionListener() {
@ -159,11 +159,11 @@ public class ChooserComposite extends Composite {
if (returnButton.getSelection()) { if (returnButton.getSelection()) {
referenceButton.setSelection(false); referenceButton.setSelection(false);
referenceButton.notifyListeners(SWT.Selection, new Event()); referenceButton.notifyListeners(SWT.Selection, new Event());
} else if (name.isReference()) { } else if (name.isOutput()) {
referenceButton.setSelection(true); referenceButton.setSelection(true);
referenceButton.notifyListeners(SWT.Selection, new Event()); referenceButton.notifyListeners(SWT.Selection, new Event());
} }
onVisibilityOrReturnChange(info.getAllUsedNames()); onVisibilityOrReturnChange(info.getParameterCandidates());
} }
@Override @Override
@ -216,7 +216,7 @@ public class ChooserComposite extends Composite {
void onVisibilityOrReturnChange(List<NameInformation> name) { void onVisibilityOrReturnChange(List<NameInformation> name) {
String variableUsedAfterBlock = null; String variableUsedAfterBlock = null;
for (NameInformation information : name) { for (NameInformation information : name) {
if (information.isUsedAfterReferences() && if (information.isReferencedAfterSelection() &&
!(information.isUserSetIsReference() || information.isUserSetIsReturnValue())) { !(information.isUserSetIsReference() || information.isUserSetIsReturnValue())) {
variableUsedAfterBlock = information.getName().toString(); variableUsedAfterBlock = information.getName().toString();
} }

View file

@ -32,7 +32,7 @@ public class ExtractFunctionComposite extends Composite {
private final ExtractFunctionInformation info; private final ExtractFunctionInformation info;
public ExtractFunctionComposite(Composite parent, ExtractFunctionInformation info, public ExtractFunctionComposite(Composite parent, ExtractFunctionInformation info,
ExtractFunctionInputPage ip) { ExtractFunctionInputPage page) {
super(parent, SWT.NONE); super(parent, SWT.NONE);
this.info = info; this.info = info;
setLayout(new GridLayout()); setLayout(new GridLayout());
@ -40,7 +40,7 @@ public class ExtractFunctionComposite extends Composite {
createNewMethodNameComposite(this); createNewMethodNameComposite(this);
Group returnGroup = createReturnGroup(nameVisiComp); Group returnGroup = createReturnGroup(nameVisiComp);
createReturnValueChooser(returnGroup, info, ip); createReturnValueChooser(returnGroup, info, page);
createReplaceCheckBox(nameVisiComp); createReplaceCheckBox(nameVisiComp);
@ -65,11 +65,11 @@ public class ExtractFunctionComposite extends Composite {
} }
private void createReturnValueChooser(Composite parent, ExtractFunctionInformation info, private void createReturnValueChooser(Composite parent, ExtractFunctionInformation info,
ExtractFunctionInputPage ip) { ExtractFunctionInputPage page) {
GridData gridData = new GridData(); GridData gridData = new GridData();
gridData.horizontalAlignment = GridData.FILL; gridData.horizontalAlignment = GridData.FILL;
gridData.grabExcessHorizontalSpace = true; gridData.grabExcessHorizontalSpace = true;
comp = new ChooserComposite(parent, info, ip); comp = new ChooserComposite(parent, info, page);
comp.setLayoutData(gridData); comp.setLayoutData(gridData);
comp.redraw(); comp.redraw();
} }

View file

@ -24,9 +24,9 @@ public class ExtractFunctionInformation {
private VisibilityEnum visibility = VisibilityEnum.v_private; private VisibilityEnum visibility = VisibilityEnum.v_private;
private String methodName; private String methodName;
private boolean replaceDuplicates; private boolean replaceDuplicates;
private List<NameInformation> allAfterUsedNames; private List<NameInformation> namesUsedAfter;
private List<NameInformation> allUsedNames; private List<NameInformation> parameterCandidates;
private NameInformation inScopeDeclaredVariable; private NameInformation mandatoryReturnVariable;
private NameInformation returnVariable; private NameInformation returnVariable;
private ICPPASTFunctionDeclarator declarator; private ICPPASTFunctionDeclarator declarator;
private MethodContext context; private MethodContext context;
@ -62,21 +62,21 @@ public class ExtractFunctionInformation {
this.replaceDuplicates = replaceDuplicates; this.replaceDuplicates = replaceDuplicates;
} }
public List<NameInformation> getAllAfterUsedNames() { public List<NameInformation> getNamesUsedAfter() {
if (allAfterUsedNames == null) { if (namesUsedAfter == null) {
allAfterUsedNames = new ArrayList<NameInformation>(); namesUsedAfter = new ArrayList<NameInformation>();
for (NameInformation name : getAllUsedNames()) { for (NameInformation name : getParameterCandidates()) {
if (name.isReference()||name.isReturnValue()) { if (name.isOutput() || name.isReturnValue()) {
allAfterUsedNames.add(name); namesUsedAfter.add(name);
} }
} }
} }
return allAfterUsedNames; return namesUsedAfter;
} }
public void setAllAfterUsedNames(ArrayList<NameInformation> allAfterUsedNames) { public void setNamesUsedAfter(List<NameInformation> names) {
this.allAfterUsedNames = allAfterUsedNames; this.namesUsedAfter = names;
} }
public NameInformation getReturnVariable() { public NameInformation getReturnVariable() {
@ -90,20 +90,20 @@ public class ExtractFunctionInformation {
this.returnVariable = returnVariable; this.returnVariable = returnVariable;
} }
public NameInformation getInScopeDeclaredVariable() { public NameInformation getMandatoryReturnVariable() {
return inScopeDeclaredVariable; return mandatoryReturnVariable;
} }
public void setInScopeDeclaredVariable(NameInformation inScopeDeclaredVariable) { public void setMandatoryReturnVariable(NameInformation variable) {
this.inScopeDeclaredVariable = inScopeDeclaredVariable; this.mandatoryReturnVariable = variable;
} }
public List<NameInformation> getAllUsedNames() { public List<NameInformation> getParameterCandidates() {
return allUsedNames; return parameterCandidates;
} }
public void setAllUsedNames(List<NameInformation> allUsedNames) { public void setParameterCandidates(List<NameInformation> names) {
this.allUsedNames = allUsedNames; this.parameterCandidates = names;
} }
public VisibilityEnum getVisibility() { public VisibilityEnum getVisibility() {

View file

@ -53,7 +53,6 @@ public class ExtractFunctionInputPage extends UserInputWizardPage {
for (Control buttons : comp.getVisibiltyGroup().getChildren()) { for (Control buttons : comp.getVisibiltyGroup().getChildren()) {
buttons.addMouseListener(new MouseAdapter() { buttons.addMouseListener(new MouseAdapter() {
@Override @Override
public void mouseUp(MouseEvent e) { public void mouseUp(MouseEvent e) {
String text = ((Button)e.getSource()).getText(); String text = ((Button)e.getSource()).getText();

View file

@ -165,15 +165,14 @@ public class ExtractFunctionRefactoring extends CRefactoring {
if (isProgressMonitorCanceld(sm, initStatus)) if (isProgressMonitorCanceld(sm, initStatus))
return initStatus; return initStatus;
container.findAllNames();
markWriteAccess(); markWriteAccess();
sm.worked(1); sm.worked(1);
if (isProgressMonitorCanceld(sm, initStatus)) if (isProgressMonitorCanceld(sm, initStatus))
return initStatus; return initStatus;
container.getAllAfterUsedNames(); container.getNamesUsedAfter();
info.setAllUsedNames(container.getUsedNamesUnique()); info.setParameterCandidates(container.getUsedNamesUnique());
if (container.size() < 1) { if (container.size() < 1) {
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_NoStmtSelected); initStatus.addFatalError(Messages.ExtractFunctionRefactoring_NoStmtSelected);
@ -181,10 +180,11 @@ public class ExtractFunctionRefactoring extends CRefactoring {
return initStatus; return initStatus;
} }
if (container.getAllDeclaredInScope().size() > 1) { List<NameInformation> returnValueCandidates = container.getReturnValueCandidates();
if (returnValueCandidates.size() > 1) {
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_TooManySelected); initStatus.addFatalError(Messages.ExtractFunctionRefactoring_TooManySelected);
} else if (container.getAllDeclaredInScope().size() == 1) { } else if (returnValueCandidates.size() == 1) {
info.setInScopeDeclaredVariable(container.getAllDeclaredInScope().get(0)); info.setMandatoryReturnVariable(returnValueCandidates.get(0));
} }
extractedFunctionConstructionHelper = extractedFunctionConstructionHelper =
@ -197,14 +197,14 @@ public class ExtractFunctionRefactoring extends CRefactoring {
MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex()); MethodContext context = NodeHelper.findMethodContext(container.getNodesToWrite().get(0), getIndex());
info.setMethodContext(context); info.setMethodContext(context);
if (info.getInScopeDeclaredVariable() != null) { if (info.getMandatoryReturnVariable() != null) {
info.getInScopeDeclaredVariable().setUserSetIsReturnValue(true); info.getMandatoryReturnVariable().setUserSetIsReturnValue(true);
} }
for (int i = 0; i < info.getAllUsedNames().size(); i++) { for (int i = 0; i < info.getParameterCandidates().size(); i++) {
if (!info.getAllUsedNames().get(i).isDeclarationExtracted()) { if (!info.getParameterCandidates().get(i).isDeclaredInSelection()) {
NameInformation name = info.getAllUsedNames().get(i); NameInformation name = info.getParameterCandidates().get(i);
if (!name.isReturnValue()) { if (!name.isReturnValue()) {
name.setUserSetIsReference(name.isReference()); name.setUserSetIsReference(name.isOutput());
} }
} }
} }
@ -219,9 +219,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
} }
private void markWriteAccess() throws CoreException { private void markWriteAccess() throws CoreException {
List<NameInformation> paras = container.getNames(); for (NameInformation name : container.getNames()) {
for (NameInformation name : paras) {
int flag = CPPVariableReadWriteFlags.getReadWriteFlags(name.getName()); int flag = CPPVariableReadWriteFlags.getReadWriteFlags(name.getName());
if ((flag & PDOMName.WRITE_ACCESS) != 0) { if ((flag & PDOMName.WRITE_ACCESS) != 0) {
name.setWriteAccess(true); name.setWriteAccess(true);
@ -230,22 +228,22 @@ public class ExtractFunctionRefactoring extends CRefactoring {
} }
private void checkForNonExtractableStatements(NodeContainer cont, RefactoringStatus status) { private void checkForNonExtractableStatements(NodeContainer cont, RefactoringStatus status) {
NonExtractableStmtFinder vis = new NonExtractableStmtFinder(); NonExtractableStmtFinder finder = new NonExtractableStmtFinder();
for (IASTNode node : cont.getNodesToWrite()) { for (IASTNode node : cont.getNodesToWrite()) {
node.accept(vis); node.accept(finder);
if (vis.containsContinue()) { if (finder.containsContinue()) {
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_Error_Continue); initStatus.addFatalError(Messages.ExtractFunctionRefactoring_Error_Continue);
break; break;
} else if (vis.containsBreak()) { } else if (finder.containsBreak()) {
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_Error_Break); initStatus.addFatalError(Messages.ExtractFunctionRefactoring_Error_Break);
break; break;
} }
} }
ReturnStatementFinder rFinder = new ReturnStatementFinder(); ReturnStatementFinder returnFinder = new ReturnStatementFinder();
for (IASTNode node : cont.getNodesToWrite()) { for (IASTNode node : cont.getNodesToWrite()) {
node.accept(rFinder); node.accept(returnFinder);
if (rFinder.containsReturn()) { if (returnFinder.containsReturn()) {
initStatus.addFatalError(Messages.ExtractFunctionRefactoring_Error_Return); initStatus.addFatalError(Messages.ExtractFunctionRefactoring_Error_Return);
break; break;
} }
@ -287,7 +285,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
return finalConditions; return finalConditions;
} }
} }
for (NameInformation name : info.getAllUsedNames()) { for (NameInformation name : info.getParameterCandidates()) {
if (name.isUserSetIsReturnValue()) { if (name.isUserSetIsReturnValue()) {
info.setReturnVariable(name); info.setReturnVariable(name);
} }
@ -611,7 +609,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
IASTStandardFunctionDeclarator createdFunctionDeclarator = IASTStandardFunctionDeclarator createdFunctionDeclarator =
extractedFunctionConstructionHelper.createFunctionDeclarator(qname, extractedFunctionConstructionHelper.createFunctionDeclarator(qname,
info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(), info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(),
info.getAllUsedNames(), ast.getASTNodeFactory()); info.getParameterCandidates(), ast.getASTNodeFactory());
func.setDeclarator(createdFunctionDeclarator); func.setDeclarator(createdFunctionDeclarator);
IASTCompoundStatement compound = new CPPASTCompoundStatement(); IASTCompoundStatement compound = new CPPASTCompoundStatement();
@ -752,7 +750,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
private IASTNode getReturnAssignment(IASTExpressionStatement stmt, private IASTNode getReturnAssignment(IASTExpressionStatement stmt,
IASTFunctionCallExpression callExpression, IASTName retname) { IASTFunctionCallExpression callExpression, IASTName retname) {
if (info.getReturnVariable().equals(info.getInScopeDeclaredVariable())) { if (info.getReturnVariable().equals(info.getMandatoryReturnVariable())) {
IASTSimpleDeclaration orgDecl = NodeHelper.findSimpleDeclarationInParents(info IASTSimpleDeclaration orgDecl = NodeHelper.findSimpleDeclarationInParents(info
.getReturnVariable().getDeclaration()); .getReturnVariable().getDeclaration());
IASTSimpleDeclaration decl = new CPPASTSimpleDeclaration(); IASTSimpleDeclaration decl = new CPPASTSimpleDeclaration();
@ -796,7 +794,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
IASTStandardFunctionDeclarator declarator = IASTStandardFunctionDeclarator declarator =
extractedFunctionConstructionHelper.createFunctionDeclarator(name, extractedFunctionConstructionHelper.createFunctionDeclarator(name,
info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(), info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(),
info.getAllUsedNames(), ast.getASTNodeFactory()); info.getParameterCandidates(), ast.getASTNodeFactory());
simpleDecl.addDeclarator(declarator); simpleDecl.addDeclarator(declarator);
return simpleDecl; return simpleDecl;
} }
@ -811,7 +809,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
IASTStandardFunctionDeclarator declarator = IASTStandardFunctionDeclarator declarator =
extractedFunctionConstructionHelper.createFunctionDeclarator(name, extractedFunctionConstructionHelper.createFunctionDeclarator(name,
info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(), info.getDeclarator(), info.getReturnVariable(), container.getNodesToWrite(),
info.getAllUsedNames(), ast.getASTNodeFactory()); info.getParameterCandidates(), ast.getASTNodeFactory());
simpleDecl.addDeclarator(declarator); simpleDecl.addDeclarator(declarator);
return simpleDecl; return simpleDecl;
} }
@ -855,9 +853,9 @@ public class ExtractFunctionRefactoring extends CRefactoring {
} }
private void addParameterIfPossible(List<IASTInitializerClause> args, private void addParameterIfPossible(List<IASTInitializerClause> args,
List<IASTName> declarations, NameInformation nameInfo) { List<IASTName> declarations, NameInformation nameInfо) {
if (!nameInfo.isDeclarationExtracted()) { if (!nameInfо.isDeclaredInSelection()) {
IASTName declaration = nameInfo.getDeclaration(); IASTName declaration = nameInfо.getDeclaration();
if (!declarations.contains(declaration)) { if (!declarations.contains(declaration)) {
declarations.add(declaration); declarations.add(declaration);
IASTIdExpression expression = new CPPASTIdExpression(); IASTIdExpression expression = new CPPASTIdExpression();

View file

@ -95,7 +95,7 @@ public abstract class ExtractedFunctionConstructionHelper {
Collection<NameInformation> allUsedNames, INodeFactory nodeFactory) { Collection<NameInformation> allUsedNames, INodeFactory nodeFactory) {
List<IASTParameterDeclaration> result = new ArrayList<IASTParameterDeclaration>(); List<IASTParameterDeclaration> result = new ArrayList<IASTParameterDeclaration>();
for (NameInformation name : allUsedNames) { for (NameInformation name : allUsedNames) {
if (!name.isDeclarationExtracted()) { if (!name.isDeclaredInSelection()) {
result.add(name.getParameterDeclaration(name.isUserSetIsReference(), nodeFactory)); result.add(name.getParameterDeclaration(name.isUserSetIsReference(), nodeFactory));
} }
} }

View file

@ -63,7 +63,7 @@ final class SimilarFinderVisitor extends ASTVisitor {
if (statementCount == statements.size()) { if (statementCount == statements.size()) {
// Found similar code // Found similar code
boolean similarOnReturnWays = true; boolean similarOnReturnWays = true;
for (NameInformation nameInfo : similarContainer.getAllAfterUsedNames()) { for (NameInformation nameInfo : similarContainer.getNamesUsedAfter()) {
if (refactoring.names.containsKey(nameInfo.getDeclaration().getRawSignature())) { if (refactoring.names.containsKey(nameInfo.getDeclaration().getRawSignature())) {
Integer nameOrderNumber = refactoring.names.get(nameInfo.getDeclaration().getRawSignature()); Integer nameOrderNumber = refactoring.names.get(nameInfo.getDeclaration().getRawSignature());
if (refactoring.nameTrail.containsValue(nameOrderNumber)) { if (refactoring.nameTrail.containsValue(nameOrderNumber)) {
@ -75,7 +75,7 @@ final class SimilarFinderVisitor extends ASTVisitor {
} }
} }
if (orgName != null) { if (orgName != null) {
for (NameInformation orgNameInfo : refactoring.container.getAllAfterUsedNamesChoosenByUser()) { for (NameInformation orgNameInfo : refactoring.container.getNamesUsedAfterChoosenByUser()) {
if (orgName.equals(orgNameInfo.getDeclaration().getRawSignature())) { if (orgName.equals(orgNameInfo.getDeclaration().getRawSignature())) {
found = true; found = true;
} }

View file

@ -133,10 +133,9 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
if (isProgressMonitorCanceld(sm, initStatus)) if (isProgressMonitorCanceld(sm, initStatus))
return initStatus; return initStatus;
container.findAllNames();
sm.worked(1); sm.worked(1);
container.getAllAfterUsedNames(); container.getNamesUsedAfter();
info.addNamesToUsedNames(findAllDeclaredNames()); info.addNamesToUsedNames(findAllDeclaredNames());
sm.worked(1); sm.worked(1);

View file

@ -1884,10 +1884,10 @@ public class PreferenceConstants {
* @since 5.3 * @since 5.3
*/ */
public static final int NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE = 4; public static final int NAME_STYLE_CAPITALIZATION_LOWER_CAMEL_CASE = 4;
/** /**
* A named preference that controls order of private/protected/public class members in generated * A named preference that controls the order of private/protected/public class members in
* code. * generated code.
* <p> * <p>
* Value is of type <code>Boolean</code>. The <code>true</code> value means that private members * Value is of type <code>Boolean</code>. The <code>true</code> value means that private members
* are before public ones. The default is to put public members before private ones. * are before public ones. The default is to put public members before private ones.
@ -1896,6 +1896,28 @@ public class PreferenceConstants {
*/ */
public static final String CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER = "class_member_ascending_visibility_order"; //$NON-NLS-1$ public static final String CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER = "class_member_ascending_visibility_order"; //$NON-NLS-1$
/**
* A named preference that controls the order of parameters of generated functions.
* <p>
* Value is of type <code>Boolean</code>. The <code>true</code> value means that output
* parameters are before the input ones. The default is to put outputparameters after the input
* ones.
*
* @since 5.4
*/
public static final String FUNCTION_OUTPUT_PARAMETERS_BEFORE_INPUT = "function_output_parameters_before_input"; //$NON-NLS-1$
/**
* A named preference that controls whether output parameters of generated functions are passed
* by pointer or by reference.
* <p>
* Value is of type <code>Boolean</code>. The <code>true</code> value means that output
* parameters are passed by pointer. The default is to pass output parameters by reference.
*
* @since 5.4
*/
public static final String FUNCTION_PASS_OUTPUT_PARAMETERS_BY_POINTER = "function_pass_output_parameters_by_pointer"; //$NON-NLS-1$
/** /**
* Returns the CDT-UI preference store. * Returns the CDT-UI preference store.
@ -2103,6 +2125,8 @@ public class PreferenceConstants {
// Code style // Code style
store.setDefault(CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER, false); store.setDefault(CLASS_MEMBER_ASCENDING_VISIBILITY_ORDER, false);
store.setDefault(FUNCTION_OUTPUT_PARAMETERS_BEFORE_INPUT, false);
store.setDefault(FUNCTION_PASS_OUTPUT_PARAMETERS_BY_POINTER, false);
// Colors that are set by the current theme // Colors that are set by the current theme
CUIPreferenceInitializer.setThemeBasedPreferences(store, false); CUIPreferenceInitializer.setThemeBasedPreferences(store, false);