1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-12 10:45:37 +02:00

FIXED - bug 239060: const parameters in extracted functions

https://bugs.eclipse.org/bugs/show_bug.cgi?id=239060
This commit is contained in:
Emanuel Graf 2009-01-05 12:48:23 +00:00
parent 4966785528
commit e4581eabb5
5 changed files with 98 additions and 14 deletions

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* Copyright (c) 2008, 2009 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
@ -49,11 +49,15 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReferenceOperator;
import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter;
public class NodeContainer {
public final NameInformation NULL_NAME_INFORMATION = new NameInformation(
new CPPASTName());
private final ArrayList<IASTNode> vec;
private final ArrayList<NameInformation> names;
public class NameInformation {
private IASTName name;
private IASTName declaration;
private final ArrayList<IASTName> references;
@ -61,6 +65,8 @@ public class NodeContainer {
private int lastCachedReferencesHash;
private boolean isReference;
private boolean isReturnValue;
private boolean isConst;
private boolean isWriteAccess;
private boolean userSetIsReference;
private boolean userSetIsReturnValue;
@ -103,7 +109,7 @@ public class NodeContainer {
public ArrayList<IASTName> getReferencesAfterSelection() {
if (referencesAfterCached == null
|| lastCachedReferencesHash == references.hashCode()) {
|| lastCachedReferencesHash != references.hashCode()) {
lastCachedReferencesHash = references.hashCode();
referencesAfterCached = new ArrayList<IASTName>();
@ -139,14 +145,16 @@ public class NodeContainer {
IASTDeclarator declarator;
if (sourceDeclarator instanceof IASTArrayDeclarator) {
IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator)sourceDeclarator;
IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator) sourceDeclarator;
declarator = new CPPASTArrayDeclarator();
IASTArrayModifier[] arrayModifiers = arrDeclarator.getArrayModifiers();
IASTArrayModifier[] arrayModifiers = arrDeclarator
.getArrayModifiers();
for (IASTArrayModifier arrayModifier : arrayModifiers) {
((IASTArrayDeclarator)declarator).addArrayModifier(arrayModifier);
((IASTArrayDeclarator) declarator)
.addArrayModifier(arrayModifier);
}
}else {
} else {
declarator = new CPPASTDeclarator();
}
declarator.setName(new CPPASTName(getDeclaration().toCharArray()));
@ -167,7 +175,7 @@ public class NodeContainer {
}
public boolean hasReferenceOperartor(IASTDeclarator declarator) {
for(IASTPointerOperator pOp :declarator.getPointerOperators()) {
for (IASTPointerOperator pOp : declarator.getPointerOperators()) {
if (pOp instanceof ICPPASTReferenceOperator) {
return true;
}
@ -206,7 +214,8 @@ public class NodeContainer {
@Override
public String toString() {
return Messages.NodeContainer_Name + name + ' ' + isDeclarationInScope();
return Messages.NodeContainer_Name + name + ' '
+ isDeclarationInScope();
}
public boolean isReference() {
@ -248,6 +257,23 @@ public class NodeContainer {
public void setUserSetName(String userSetName) {
this.userSetName = userSetName;
}
public boolean isConst() {
return isConst;
}
public void setConst(boolean isConst) {
this.isConst = isConst;
}
public boolean isWriteAccess() {
return isWriteAccess;
}
public void setWriteAccess(boolean isWriteAceess) {
this.isWriteAccess = isWriteAceess;
}
}
public NodeContainer() {
@ -371,6 +397,14 @@ public class NodeContainer {
declarations.add(nameInf.getDeclaration());
usedAfter.add(nameInf);
} else {
for (NameInformation nameInformation : usedAfter) {
if (nameInf.isWriteAccess()
&& nameInf.getDeclaration() == nameInformation
.getDeclaration()) {
nameInformation.setWriteAccess(true);
}
}
}
}
@ -493,4 +527,5 @@ public class NodeContainer {
public ArrayList<NameInformation> getNames() {
return names;
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* Copyright (c) 2008, 2009 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
@ -30,6 +30,7 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation;
public class ChooserComposite extends Composite {
private static final String COLUMN_RETURN = Messages.ChooserComposite_Return;
@ -71,6 +72,7 @@ public class ChooserComposite extends Composite {
addColumnToTable(table, COLUMN_TYPE);
addColumnToTable(table, COLUMN_NAME);
addColumnToTable(table, COLUMN_REFERENCE);
addColumnToTable(table, Messages.ChooserComposite_const);
if(!info.isExtractExpression()) {
addColumnToTable(table, COLUMN_RETURN);
}
@ -118,6 +120,34 @@ public class ChooserComposite extends Composite {
editor.horizontalAlignment = SWT.CENTER;
referenceButtons.add(referenceButton);
editor.setEditor(referenceButton, item, columnIndex++);
// Cosnt Button
editor = new TableEditor(table);
final Button constButton = new Button(table, SWT.CHECK);
constButton.setSelection(name.isConst());
constButton.setEnabled(!name.isWriteAccess());
constButton.setBackground(table.getBackground());
constButton.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent e) {
name.setConst(constButton
.getSelection());
onVisibilityOrReturnChange(info.getAllUsedNames());
}
public void widgetSelected(SelectionEvent e) {
widgetDefaultSelected(e);
}
});
constButton.pack();
editor.minimumWidth = constButton.getSize().x;
editor.horizontalAlignment = SWT.CENTER;
// referenceButtons.add(referenceButton);
editor.setEditor(constButton, item, columnIndex++);
if(info.isExtractExpression())
continue; // Skip the return radiobutton

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* Copyright (c) 2008, 2009 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
@ -11,6 +11,7 @@
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.extractfunction;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -91,6 +92,8 @@ import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTemplateDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNodeFactory;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVariableReadWriteFlags;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
import org.eclipse.cdt.internal.ui.refactoring.AddDeclarationNodeToClassChange;
import org.eclipse.cdt.internal.ui.refactoring.CRefactoring;
@ -155,6 +158,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
return initStatus;
container.findAllNames();
markWriteAccess();
sm.worked(1);
if (isProgressMonitorCanceld(sm, initStatus))
@ -176,7 +180,7 @@ public class ExtractFunctionRefactoring extends CRefactoring {
} else if (container.getAllDeclaredInScope().size() == 1) {
info.setInScopeDeclaredVariable(container.getAllDeclaredInScope().get(0));
}
extractedFunctionConstructionHelper = ExtractedFunctionConstructionHelper
.createFor(container.getNodesToWrite());
@ -190,6 +194,19 @@ public class ExtractFunctionRefactoring extends CRefactoring {
return status;
}
private void markWriteAccess() throws CoreException {
ArrayList<NameInformation> paras = container.getNames();
for (NameInformation name : paras) {
int flag = CPPVariableReadWriteFlags.getReadWriteFlags(name
.getName());
if ((flag & PDOMName.WRITE_ACCESS) != 0) {
name.setWriteAccess(true);
}
}
}
private void checkForNonExtractableStatements(NodeContainer cont,
RefactoringStatus status) {

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
* Copyright (c) 2008, 2009 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
@ -39,6 +39,7 @@ public final class Messages extends NLS {
public static String ExtractFunctionRefactoring_CreateFunctionCall;
public static String ChooserComposite_Return;
public static String ChooserComposite_CallByRef;
public static String ChooserComposite_const;
public static String ChooserComposite_Name;
public static String ChooserComposite_Type;
public static String ChooserComposite_NoReturnValue;

View file

@ -1,5 +1,5 @@
###############################################################################
# Copyright (c) 2008 Institute for Software, HSR Hochschule fuer Technik
# Copyright (c) 2008, 2009 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
@ -27,6 +27,7 @@ ExtractFunctionRefactoring_CreateMethodCall=Create Method Call
ExtractFunctionRefactoring_CreateFunctionCall=Create Function Call
ChooserComposite_Return=Return
ChooserComposite_CallByRef=Call by Reference
ChooserComposite_const=const
ChooserComposite_Name=Name
ChooserComposite_Type=Type
ChooserComposite_NoReturnValue=No return-value (void)