From 3a1fa5bf5b546f0e86c0a1b18f0f9dbcbb8d00ee Mon Sep 17 00:00:00 2001 From: Emanuel Graf Date: Tue, 8 Sep 2009 11:54:06 +0000 Subject: [PATCH] FIXED - bug 288268: c-refactoring creates c++-parameters https://bugs.eclipse.org/bugs/show_bug.cgi?id=288268 --- .../resources/refactoring/ExtractMethod.rts | 30 ++++++++++++++++++- .../ui/refactoring/NodeContainer.java | 15 ++++++++-- .../ExtractFunctionRefactoring.java | 6 ++-- .../ExtractedFunctionConstructionHelper.java | 9 +++--- 4 files changed, 49 insertions(+), 11 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts index 432405b473e..637f90427e8 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts @@ -2766,7 +2766,7 @@ int main() } //= -void exp(int b, int & a) +void exp(int b, int *a) { b = a * 2; } @@ -2877,3 +2877,31 @@ returnparameterindex=0 virtual=true visibility=public +//!Bug 288268: c-refactoring creates c++-parameters +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@main.c +int main() +{ + int a,b; + /*$*/a = b*2;/*$$*/ + return a; +} + +//= +void test(int *a, int b) +{ + a = b * 2; +} + +int main() +{ + int a,b; + test(a, b); + return a; +} + +//@.config +filename=main.c +methodname=test +replaceduplicates=false +returnvalue=false 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 e0034f70ce9..7e6362201fe 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 @@ -40,8 +40,10 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding; import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter; +import org.eclipse.cdt.core.parser.ParserLanguage; import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.internal.core.dom.parser.c.CASTPointer; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTArrayDeclarator; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarator; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; @@ -129,14 +131,14 @@ public class NodeContainer { } public ICPPASTParameterDeclaration getICPPASTParameterDeclaration( - boolean isReference) { + boolean isReference, ParserLanguage lang) { ICPPASTParameterDeclaration para = new CPPASTParameterDeclaration(); IASTDeclarator sourceDeclarator = (IASTDeclarator) getDeclaration() .getParent(); if (sourceDeclarator.getParent() instanceof IASTSimpleDeclaration) { IASTSimpleDeclaration decl = (IASTSimpleDeclaration) sourceDeclarator - .getParent(); + .getParent(); para.setDeclSpecifier(decl.getDeclSpecifier().copy()); } else if (sourceDeclarator.getParent() instanceof IASTParameterDeclaration) { IASTParameterDeclaration decl = (IASTParameterDeclaration) sourceDeclarator @@ -165,7 +167,14 @@ public class NodeContainer { } if (isReference && !hasReferenceOperartor(declarator)) { - declarator.addPointerOperator(new CPPASTReferenceOperator()); + switch (lang) { + case C: + declarator.addPointerOperator(new CASTPointer()); + break; + case CPP: + declarator.addPointerOperator(new CPPASTReferenceOperator()); + break; + } } declarator.setNestedDeclarator(sourceDeclarator 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 76aa8e8c9b6..8682806a125 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 @@ -663,7 +663,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { IASTStandardFunctionDeclarator createdFunctionDeclarator = extractedFunctionConstructionHelper .createFunctionDeclarator(qname, info.getDeclarator(), info .getReturnVariable(), container.getNodesToWrite(), info - .getAllUsedNames()); + .getAllUsedNames(), unit.getParserLanguage()); func.setDeclarator(createdFunctionDeclarator); IASTCompoundStatement compound = new CPPASTCompoundStatement(); @@ -868,7 +868,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { IASTStandardFunctionDeclarator declarator = extractedFunctionConstructionHelper .createFunctionDeclarator(name, info.getDeclarator(), info .getReturnVariable(), container.getNodesToWrite(), info - .getAllUsedNames()); + .getAllUsedNames(), unit.getParserLanguage()); simpleDecl.addDeclarator(declarator); return simpleDecl; } @@ -883,7 +883,7 @@ public class ExtractFunctionRefactoring extends CRefactoring { ICPPASTFunctionDeclarator declarator = extractedFunctionConstructionHelper .createFunctionDeclarator(name, info.getDeclarator(), info .getReturnVariable(), container.getNodesToWrite(), info - .getAllUsedNames()); + .getAllUsedNames(), unit.getParserLanguage()); simpleDecl.addDeclarator(declarator); return simpleDecl; } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractedFunctionConstructionHelper.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractedFunctionConstructionHelper.java index deb5542f6c9..7e4d41b90bb 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractedFunctionConstructionHelper.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractedFunctionConstructionHelper.java @@ -28,6 +28,7 @@ import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration; import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; +import org.eclipse.cdt.core.parser.ParserLanguage; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTPointer; @@ -58,7 +59,7 @@ public abstract class ExtractedFunctionConstructionHelper { return false; } - ICPPASTFunctionDeclarator createFunctionDeclarator(IASTName name, ICPPASTFunctionDeclarator functionDeclarator, NameInformation returnVariable, List nodesToWrite, Collection allUsedNames) { + ICPPASTFunctionDeclarator createFunctionDeclarator(IASTName name, ICPPASTFunctionDeclarator functionDeclarator, NameInformation returnVariable, List nodesToWrite, Collection allUsedNames, ParserLanguage lang) { ICPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator(); declarator.setName(name); @@ -74,7 +75,7 @@ public abstract class ExtractedFunctionConstructionHelper { } } - for (ICPPASTParameterDeclaration param : getParameterDeclarations(allUsedNames)) { + for (ICPPASTParameterDeclaration param : getParameterDeclarations(allUsedNames, lang)) { declarator.addParameterDeclaration(param); } @@ -85,11 +86,11 @@ public abstract class ExtractedFunctionConstructionHelper { return declarator; } - public Collection getParameterDeclarations(Collection allUsedNames) { + public Collection getParameterDeclarations(Collection allUsedNames, ParserLanguage lang) { Collection result = new ArrayList(); for (NameInformation name : allUsedNames) { if(!name.isDeclarationInScope()){ - result.add(name.getICPPASTParameterDeclaration(name.isUserSetIsReference())); + result.add(name.getICPPASTParameterDeclaration(name.isUserSetIsReference(), lang)); } } return result;