diff --git a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF index 1546a7debae..72b19e108b5 100644 --- a/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.core/META-INF/MANIFEST.MF @@ -48,7 +48,7 @@ Export-Package: org.eclipse.cdt.core, org.eclipse.cdt.internal.core.dom.parser.cpp;x-friends:="org.eclipse.cdt.ui", org.eclipse.cdt.internal.core.dom.parser.cpp.semantics;x-friends:="org.eclipse.cdt.ui", org.eclipse.cdt.internal.core.dom.rewrite;x-friends:="org.eclipse.cdt.core.tests,org.eclipse.cdt.ui", - org.eclipse.cdt.internal.core.dom.rewrite.astwriter;x-internal:=true, + org.eclipse.cdt.internal.core.dom.rewrite.astwriter;x-friends:="org.eclipse.cdt.ui", org.eclipse.cdt.internal.core.dom.rewrite.changegenerator;x-internal:=true, org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;x-internal:=true, org.eclipse.cdt.internal.core.envvar;x-friends:="org.eclipse.cdt.ui", diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/MacroExpansionHandler.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/MacroExpansionHandler.java index 5a1344f5fb9..08562c0426f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/MacroExpansionHandler.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/astwriter/MacroExpansionHandler.java @@ -62,7 +62,7 @@ public class MacroExpansionHandler { protected boolean checkisMacroExpansionNode(IASTNode node, boolean write) { IASTNodeLocation[] locs = node.getNodeLocations(); - if(locs.length ==1) { + if (locs != null && locs.length ==1) { if (locs[0] instanceof IASTMacroExpansionLocation) { IASTMacroExpansionLocation macroNode = (IASTMacroExpansionLocation) locs[0]; diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java index 9ffbc001c6f..fb1c516dee8 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ASTModificationHelper.java @@ -34,22 +34,23 @@ public class ASTModificationHelper { } - @SuppressWarnings("unchecked") - public T[] createModifiedChildArray(IASTNode parent, T[] unmodifiedChildren){ + public T[] createModifiedChildArray(IASTNode parent, T[] unmodifiedChildren, Class clazz){ ArrayList modifiedChildren = new ArrayList(Arrays.asList(unmodifiedChildren)); for(T currentChild : unmodifiedChildren){ for(ASTModification childModification : modificationsForNode(currentChild)){ try{ - T newNode = (T) childModification.getNewNode(); + final T newNode = cast(childModification.getNewNode(), clazz); switch(childModification.getKind()){ case REPLACE: - if(childModification.getNewNode() != null){ + if (newNode != null) { modifiedChildren.add(modifiedChildren.indexOf(childModification.getTargetNode()), newNode); } modifiedChildren.remove(childModification.getTargetNode()); break; case INSERT_BEFORE: - modifiedChildren.add(modifiedChildren.indexOf(childModification.getTargetNode()), newNode); + if (newNode != null) { + modifiedChildren.add(modifiedChildren.indexOf(childModification.getTargetNode()), newNode); + } break; case APPEND_CHILD: throw new UnhandledASTModificationException(childModification); @@ -61,27 +62,43 @@ public class ASTModificationHelper { } } - Class componentType = unmodifiedChildren.getClass().getComponentType(); for(ASTModification parentModification : modificationsForNode(parent)){ if(parentModification.getKind() == ModificationKind.APPEND_CHILD){ IASTNode newNode = parentModification.getNewNode(); - if(componentType.isAssignableFrom(newNode.getClass())){ - modifiedChildren.add((T) newNode); + T newTNode= cast(newNode, clazz); + if (newTNode != null) { + modifiedChildren.add(newTNode); } - else if(newNode instanceof ContainerNode){ + else if (newNode instanceof ContainerNode){ ContainerNode nodeContainer = (ContainerNode) newNode; for(IASTNode currentNode : nodeContainer.getNodes()){ - if(componentType.isAssignableFrom(currentNode.getClass())){ - modifiedChildren.add((T)currentNode); + T tnode= cast(currentNode, clazz); + if(tnode != null){ + modifiedChildren.add(tnode); } } } } } - return modifiedChildren.toArray((T[]) Array.newInstance(componentType, 0)); + return modifiedChildren.toArray(newArrayInstance(clazz, modifiedChildren.size())); } + + @SuppressWarnings("unchecked") + private T[] newArrayInstance(Class clazz, int size) { + return (T[]) Array.newInstance(clazz, size); + } + + @SuppressWarnings("unchecked") + private T cast(IASTNode node, Class clazz) { + if (clazz.isInstance(node)){ + return (T) node; + } + return null; + } + + public List modificationsForNode( IASTNode targetNode) { ASTModificationMap rootModifications = modificationStore.getRootModifications(); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGeneratorWriterVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGeneratorWriterVisitor.java index b34302feed7..8821f0414ba 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGeneratorWriterVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ChangeGeneratorWriterVisitor.java @@ -306,8 +306,7 @@ public class ChangeGeneratorWriterVisitor extends ASTWriterVisitor { ASTModificationHelper helper = new ASTModificationHelper( modificationStore); - IASTDeclaration[] declarations = helper.createModifiedChildArray(tu, tu - .getDeclarations()); + IASTDeclaration[] declarations = helper.createModifiedChildArray(tu, tu.getDeclarations(), IASTDeclaration.class); for (IASTDeclaration currentDeclaration : declarations) { currentDeclaration.accept(this); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclSpecWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclSpecWriter.java index de0dbc79d2c..2e249dbfe3a 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclSpecWriter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclSpecWriter.java @@ -31,7 +31,7 @@ public class ModifiedASTDeclSpecWriter extends DeclSpecWriter { @Override protected IASTDeclaration[] getMembers( IASTCompositeTypeSpecifier compDeclSpec) { - return modificationHelper.createModifiedChildArray(compDeclSpec, compDeclSpec.getMembers()); + return modificationHelper.createModifiedChildArray(compDeclSpec, compDeclSpec.getMembers(), IASTDeclaration.class); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclaratorWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclaratorWriter.java index 729b8496096..1a3db3621af 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclaratorWriter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTDeclaratorWriter.java @@ -44,14 +44,14 @@ public class ModifiedASTDeclaratorWriter extends DeclaratorWriter { protected void writeParameterDeclarations( IASTStandardFunctionDeclarator funcDec, IASTParameterDeclaration[] paraDecls) { - IASTParameterDeclaration[] modifiedParameters = modificationHelper.createModifiedChildArray(funcDec, paraDecls); + IASTParameterDeclaration[] modifiedParameters = modificationHelper.createModifiedChildArray(funcDec, paraDecls, IASTParameterDeclaration.class); super.writeParameterDeclarations(funcDec, modifiedParameters); } @Override protected void writePointerOperators(IASTDeclarator declarator,IASTPointerOperator[] unmodifiedPointerOperations) { - IASTPointerOperator[] modifiedPointer = modificationHelper.createModifiedChildArray(declarator, unmodifiedPointerOperations); + IASTPointerOperator[] modifiedPointer = modificationHelper.createModifiedChildArray(declarator, unmodifiedPointerOperations, IASTPointerOperator.class); super.writePointerOperators(declarator, modifiedPointer); } @@ -60,20 +60,20 @@ public class ModifiedASTDeclaratorWriter extends DeclaratorWriter { @Override protected void writeCtorChainInitializer(ICPPASTFunctionDeclarator funcDec, ICPPASTConstructorChainInitializer[] ctorInitChain) { - ICPPASTConstructorChainInitializer[] modifiedChainInitializer = modificationHelper.createModifiedChildArray(funcDec, ctorInitChain); + ICPPASTConstructorChainInitializer[] modifiedChainInitializer = modificationHelper.createModifiedChildArray(funcDec, ctorInitChain, ICPPASTConstructorChainInitializer.class); super.writeCtorChainInitializer(funcDec, modifiedChainInitializer); } @Override protected void writeArrayModifiers(IASTArrayDeclarator arrDecl, IASTArrayModifier[] arrMods) { - IASTArrayModifier[] modifiedModifiers = modificationHelper.createModifiedChildArray(arrDecl, arrMods); + IASTArrayModifier[] modifiedModifiers = modificationHelper.createModifiedChildArray(arrDecl, arrMods, IASTArrayModifier.class); super.writeArrayModifiers(arrDecl, modifiedModifiers); } @Override protected void writeExceptionSpecification(ICPPASTFunctionDeclarator funcDec, IASTTypeId[] exceptions ) { - IASTTypeId[] modifiedExceptions = modificationHelper.createModifiedChildArray(funcDec, exceptions); + IASTTypeId[] modifiedExceptions = modificationHelper.createModifiedChildArray(funcDec, exceptions, IASTTypeId.class); super.writeExceptionSpecification(funcDec, modifiedExceptions); } @@ -83,7 +83,7 @@ public class ModifiedASTDeclaratorWriter extends DeclaratorWriter { protected void writeKnRParameterDeclarations( ICASTKnRFunctionDeclarator knrFunct, IASTDeclaration[] knrDeclarations) { - IASTDeclaration[] modifiedDeclarations = modificationHelper.createModifiedChildArray(knrFunct, knrDeclarations); + IASTDeclaration[] modifiedDeclarations = modificationHelper.createModifiedChildArray(knrFunct, knrDeclarations, IASTDeclaration.class); super.writeKnRParameterDeclarations(knrFunct, modifiedDeclarations); } @@ -91,7 +91,7 @@ public class ModifiedASTDeclaratorWriter extends DeclaratorWriter { @Override protected void writeKnRParameterNames( ICASTKnRFunctionDeclarator knrFunct, IASTName[] parameterNames) { - IASTName[] modifiedNames = modificationHelper.createModifiedChildArray(knrFunct, parameterNames); + IASTName[] modifiedNames = modificationHelper.createModifiedChildArray(knrFunct, parameterNames, IASTName.class); super.writeKnRParameterNames(knrFunct, modifiedNames); } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTExpressionWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTExpressionWriter.java index 62dadb7858d..9940c0fdc6f 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTExpressionWriter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTExpressionWriter.java @@ -38,7 +38,7 @@ public class ModifiedASTExpressionWriter extends ExpressionWriter { @Override protected void writeExpressions(IASTExpressionList expList, IASTExpression[] expressions) { - IASTExpression[] modifiedExpressions = modificationHelper.createModifiedChildArray(expList, expressions); + IASTExpression[] modifiedExpressions = modificationHelper.createModifiedChildArray(expList, expressions, IASTExpression.class); super.writeExpressions(expList, modifiedExpressions); } @@ -81,7 +81,7 @@ public class ModifiedASTExpressionWriter extends ExpressionWriter { @Override protected IASTExpression[] getNewTypeIdArrayExpressions( ICPPASTNewExpression newExp, IASTExpression[] expressions) { - IASTExpression[] modifiedExpressions = modificationHelper.createModifiedChildArray(newExp, expressions); + IASTExpression[] modifiedExpressions = modificationHelper.createModifiedChildArray(newExp, expressions, IASTExpression.class); return modifiedExpressions; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTStatementWriter.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTStatementWriter.java index 5232835d4c4..156a84ffd5a 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTStatementWriter.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/rewrite/changegenerator/ModifiedASTStatementWriter.java @@ -44,7 +44,7 @@ public class ModifiedASTStatementWriter extends StatementWriter { @Override protected IASTStatement[] getNestedStatements(IASTCompoundStatement compoundStatement) { - return modificationHelper.createModifiedChildArray(compoundStatement, compoundStatement.getStatements()); + return modificationHelper.createModifiedChildArray(compoundStatement, compoundStatement.getStatements(), IASTStatement.class); } diff --git a/core/org.eclipse.cdt.ui.tests/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.ui.tests/META-INF/MANIFEST.MF index 0ae4b96ec9f..2fe1e1e20b4 100644 --- a/core/org.eclipse.cdt.ui.tests/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.ui.tests/META-INF/MANIFEST.MF @@ -10,6 +10,7 @@ Export-Package: org.eclipse.cdt.ui.testplugin, org.eclipse.cdt.ui.tests, org.eclipse.cdt.ui.tests.DOMAST, org.eclipse.cdt.ui.tests.chelp, + org.eclipse.cdt.ui.tests.refactoring, org.eclipse.cdt.ui.tests.refactoring.rename, org.eclipse.cdt.ui.tests.text, org.eclipse.cdt.ui.tests.text.contentassist, diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts index e1a000aa607..54829d9960b 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractConstant.rts @@ -46,7 +46,7 @@ A::~A() int A::foo() { - return //$42$//; + return //$42$//; //Hallo } void A::bar() @@ -68,7 +68,7 @@ A::~A() int A::foo() { - return theAnswer; + return theAnswer; //Hallo } void A::bar() diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractExpression.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractExpression.rts new file mode 100644 index 00000000000..15ea0199ad6 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractExpression.rts @@ -0,0 +1,429 @@ +//!Extract boolean comparison from if-condition. +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=check + +//@test.h +class Test +{ + void test(); +}; + + +//= +class Test +{ + void test(); + bool check(); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + if(//$5 == 3 + 2$//) { + //... + } +} + +//= +#include "test.h" + +bool Test::check() +{ + return 5 == 3 + 2; +} + +void Test::test() +{ + if(check()) { + //... + } +} + +//!Extract boolean comparison from if-condition with parameter. +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=check + +//@test.h +class Test +{ + void test(); +}; + + +//= +class Test +{ + void test(); + bool check(int num); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + int num = 1; + if(//$5 != 3 + num$//) { + //... + } +} + +//= +#include "test.h" + +bool Test::check(int num) +{ + return 5 != 3 + num; +} + +void Test::test() +{ + int num = 1; + if(check(num)) { + //... + } +} + +//!Extract binary expression that results in a function with the same return type (BasicType) as the LHS. +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=add + +//@test.h +class Test +{ + void test(); +}; + + +//= +class Test +{ + void test(); + int add(int five, int six); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + int five = 5; + int six = 6; + int result = //$five + six$//; +} + +//= +#include "test.h" + +int Test::add(int five, int six) +{ + return five + six; +} + +void Test::test() +{ + int five = 5; + int six = 6; + int result = add(five, six); +} + +//!Extract binary expression that results in a function with the same return type (ClassType) as the LHS +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=cat + +//@test.h +struct helper {}; + +class Test +{ + void test(); +}; + + +//= +struct helper {}; + +class Test +{ + void test(); + helper cat(helper s1, helper s2); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + helper s1 = "a"; + helper s2 = "b"; + helper result = //$s1 + s2$//; +} + +//= +#include "test.h" + +helper Test::cat(helper s1, helper s2) +{ + return s1 + s2; +} + +void Test::test() +{ + helper s1 = "a"; + helper s2 = "b"; + helper result = cat(s1, s2); +} + +//!Extract binary expression that results in a function with the same return type (Typedef) as the LHS +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=cat + +//@test.h +struct helper {}; +typedef helper new_helper; + +class Test +{ + void test(); +}; + + +//= +struct helper {}; +typedef helper new_helper; + +class Test +{ + void test(); + new_helper cat(new_helper s1, new_helper s2); +}; + + +//@test.cpp +#include "test.h" + +void Test::test() +{ + new_helper s1 = "a"; + new_helper s2 = "b"; + new_helper result = //$s1 + s2$//; +} + +//= +#include "test.h" + +new_helper Test::cat(new_helper s1, new_helper s2) +{ + return s1 + s2; +} + +void Test::test() +{ + new_helper s1 = "a"; + new_helper s2 = "b"; + new_helper result = cat(s1, s2); +} + +//!Extract new-Expression +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=new_helper + +//@test.cpp + +struct helper {}; + +int main(int argc, char** argv) +{ + helper* h = //$new helper$//; + return 0; +} +//= + +struct helper {}; + +helper *new_helper() +{ + return new helper(); +} + +int main(int argc, char** argv) +{ + helper* h = new_helper(); + return 0; +} +//!Extract function call +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=join_with_world + +//@test.cpp +class string {}; + +string join(string s1, char* s2) +{ + return s1 + " " + s2; +} + +int main() +{ + string hello = "Hello"; + cout << //$join(hello, "World")$// << endl; +} +//= +class string {}; + +string join(string s1, char* s2) +{ + return s1 + " " + s2; +} + +string join_with_world(string hello) +{ + return join(hello, "World"); +} + +int main() +{ + string hello = "Hello"; + cout << join_with_world(hello) << endl; +} +//!Extract method call +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=value_from + +//@test.cpp +struct other +{ + bool value() {} +}; + +class Klass +{ + void set(bool b){}; + void test() + { + other o; + this->set(//$o.value()$//); + } +}; + +//= +struct other +{ + bool value() {} +}; + +class Klass +{ + void set(bool b){}; + bool value_from(other o) + { + return o.value(); + } + + void test() + { + other o; + this->set(value_from(o)); + } +}; + +//!Extract function call [we only have the declaration] that returns a pointer +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=has + +//@test.cpp +class Cursor{}; + +Cursor* contains(const Cursor& pos); + +int main() { + Cursor c; + contains(//$contains(c)$//); +} + +//= +class Cursor{}; + +Cursor* contains(const Cursor& pos); + +Cursor *has(Cursor c) +{ + return contains(c); +} + +int main() { + Cursor c; + contains(has(c)); +} + +//!Extract function call [we have the definition] that returns a pointer +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest + +//@.config +filename=test.cpp +methodname=has + +//@test.cpp +class Cursor{}; + +Cursor* contains(const Cursor& pos) +{ + ; +} + +int main() { + Cursor c; + contains(//$contains(c)$//); +} + +//= +class Cursor{}; + +Cursor* contains(const Cursor& pos) +{ + ; +} + +Cursor *has(Cursor c) +{ + return contains(c); +} + +int main() { + Cursor c; + contains(has(c)); +} + diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractFunctionTemplates.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractFunctionTemplates.rts new file mode 100644 index 00000000000..2f1fa2e1733 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractFunctionTemplates.rts @@ -0,0 +1,98 @@ +//!Extract template function +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.cpp +void test(){ +}template +int tempFunct(){ + + T i; + i = 0; + //$i++; + i+=3;$// + + return 0; +} + +//= +void test(){ +} +template +void exp(T i) +{ + i++; + i += 3; +} + +template +int tempFunct(){ + + T i; + i = 0; + exp(i); + + return 0; +} + +//!Extract template function with template parameter Bug #12 +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.cpp +void test(){ +}template +int tempFunct(T p){ + + //$++p; + p + 4;$// + return 0; +} + +//= +void test(){ +} +template +void exp(T p) +{ + ++p; + p + 4; +} + +template +int tempFunct(T p){ + + exp(p); + return 0; +} + +//!Extract template function with template type declaration Bug #11 +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.cpp +void test(){ +}template +int tempFunct(){ + + //$T p; + p = 0; + p + 4;$// + p + 2; + return 0; +} + +//= +void test(){ +} +template +T exp() +{ + T p; + p = 0; + p + 4; + return p; +} + +template +int tempFunct(){ + + T p = exp(); + p + 2; + return 0; +} + diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts new file mode 100644 index 00000000000..58d114c9318 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethod.rts @@ -0,0 +1,2139 @@ +//!ExtractFunctionRefactoringTest +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int & i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + //$++i; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int & i) +{ + ++i; + help(); +} + +int A::foo() +{ + int i = 2; + exp(i); + return i; +} + +int A::help() +{ + return 42; +} + +//@.config +filename=A.cpp +methodname=exp +replaceduplicates=false +returnvalue=false +returnparameterindex=0 + +//!Extract Function first extracted statement with leading comment +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@main.cpp +int main(){ + + int i; + //$i= 7;$// + return i; +} + +//= +void exp(int & i) +{ + i = 7; +} + +int main(){ + + int i; + exp(i); + return i; +} + +//@.config +filename=main.cpp +methodname=exp +replaceduplicates=false +returnvalue=false +returnparameterindex=0 + +//!Extract Function last extracted statement with trailling comment +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@main.cpp +int main(){ + + int i; + //$i= 7;$// + return i; +} + +//= +void exp(int & i) +{ + i = 7; +} + +int main(){ + + int i; + exp(i); + return i; +} + +//@.config +filename=main.cpp +methodname=exp +replaceduplicates=false +returnvalue=false +returnparameterindex=0 + +//!ExtractFunctionRefactoringTest variable defined in scope +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + int exp(); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +}int A::foo() +{ + //$int i = 2; + ++i; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::exp() +{ + int i = 2; + ++i; + help(); + return i; +} + +int A::foo() +{ + int i = exp(); + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest with two variable defined in scope +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +fatalerror=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} + +int A::foo() +{ + //$int o = 1; + int i = 2; + ++i; + o++; + help();$// + o++; + return i; +}int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest with named typed field +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + B b; + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + B b; + +private: + int help(); + void exp(); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::foo() +{ + //$b = new B(); + help();$// +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::exp() +{ + b = new B(); + help(); +} + +void A::foo() +{ + exp(); +} + +int A::help() +{ + return 42; +} + +//@B.h +#ifndef B_H_ +#define B_H_ + +class B +{ +public: + B(); + virtual ~B(); +}; + +#endif /*B_H_*/ + +//!ExtractFunctionRefactoringTest with named typed variable defined in scope +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + B b; + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + B b; + +private: + int help(); + void exp(); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::foo() +{ + //$b = new B(); + help();$// +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::exp() +{ + b = new B(); + help(); +} + +void A::foo() +{ + exp(); +} + +int A::help() +{ + return 42; +} + +//@B.h +#ifndef B_H_ +#define B_H_ + +class B +{ +public: + B(); + virtual ~B(); +}; + +#endif /*B_H_*/ + +//!ExtractFunctionRefactoringTest with ObjectStyleMacro +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int & i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +#define ZWO 2 + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + //$++i; + i += ZWO; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +#define ZWO 2 + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int & i) +{ + ++i; + i += ZWO; + help(); +} + +int A::foo() +{ + int i = 2; + exp(i); + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest with FunctionStyleMacro +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int & i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +#define ADD(a,b) a + b + 2 + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + //$++i; + i = ADD(i, 42); + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +#define ADD(a,b) a + b + 2 + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int & i) +{ + ++i; + i = ADD(i, 42); + help(); +} + +int A::foo() +{ + int i = 2; + exp(i); + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractMethod with Pointer +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int *& i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int* i = new int(2); + //$++*i; + help();$// + return *i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int *& i) +{ + ++*i; + help(); +} + +int A::foo() +{ + int* i = new int(2); + exp(i); + return *i; +} + +int A::help() +{ + return 42; +} + +//!ExtractMethod with Pointer and Comment at the end +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int *& i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int* i = new int(2); + //$++*i; + help(); + //A end-comment$// + return *i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int *& i) +{ + ++*i; + help(); +} + +int A::foo() +{ + int* i = new int(2); + exp(i); + //A end-comment + return *i; +} + +int A::help() +{ + return 42; +} + +//!ExtractMethod with Pointer and Comment +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int *& i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + //A beautiful comment + int* i = new int(2); + //$++*i; + help();$// + return *i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int *& i) +{ + ++*i; + help(); +} + +int A::foo() +{ + //A beautiful comment + int *i = new int(2); + exp(i); + return *i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest with Return Value +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + int exp(int i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + //$++i; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::exp(int i) +{ + ++i; + help(); + return i; +} + +int A::foo() +{ + int i = 2; + i = exp(i); + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest with Return Value and Ref Parameter +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + int exp(int i, int & b); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + int b = i; + //$++i; + i = i + b; + help();$// + ++b; + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::exp(int i, int & b) +{ + ++i; + i = i + b; + help(); + return i; +} + +int A::foo() +{ + int i = 2; + int b = i; + i = exp(i, b); + ++b; + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest with Return Value and Ref Parameter and some more not used aferwards +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + int exp(int i, B *b, int y, float & x); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + //$++i; + b->hello(y); + i = i + x; + help();$// + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::exp(int i, B *b, int y, float & x) +{ + ++i; + b->hello(y); + i = i + x; + help(); + return i; +} + +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + i = exp(i, b, y, x); + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest with Return Value take the second and Ref Parameter and some more not used aferwards +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +returnparameterindex=1 +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + float exp(int & i, B *b, int y, float x); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + //$++i; + b->hello(y); + i = i + x; + help();$// + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +float A::exp(int & i, B *b, int y, float x) +{ + ++i; + b->hello(y); + i = i + x; + help(); + return x; +} + +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + x = exp(i, b, y, x); + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//@B.h +#ifndef B_H_ +#define B_H_ + +class B +{ +public: + B(); + virtual ~B(); + void hello(float y); +}; + +#endif /*B_H_*/ + +//!ExtractFunctionRefactoringTest with Return Value and a lot Ref Parameter +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + int exp(int i, B *& b, int & y, float & x); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + //$++i; + b->hello(y); + i = i + x; + help();$// + b->hello(y); + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::exp(int i, B *& b, int & y, float & x) +{ + ++i; + b->hello(y); + i = i + x; + help(); + return i; +} + +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + i = exp(i, b, y, x); + b->hello(y); + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//@B.h +#ifndef B_H_ +#define B_H_ + +class B +{ +public: + B(); + virtual ~B(); + void hello(float y); +}; + +#endif /*B_H_*/ +//!ExtractMethod Bug #18 (problem with Qt's Q_FOREACH macro) +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +methodname=add_to_sum +filename=main.cpp +//@main.cpp +#define for_each_int_in_array_with_index(list) for(int i = 0; i < sizeof(list) / sizeof(int); i++) + +struct Foreach +{ + int run() + { + int sum = 0; + int list [] = { 5, 6, 7 }; + for_each_int_in_array_with_index(list) + { + //$sum += list[i];$// + } + return sum; + } +}; + +//= +#define for_each_int_in_array_with_index(list) for(int i = 0; i < sizeof(list) / sizeof(int); i++) + +struct Foreach +{ + void add_to_sum(int & sum, int list[], int i) + { + sum += list[i]; + } + + int run() + { + int sum = 0; + int list [] = { 5, 6, 7 }; + for_each_int_in_array_with_index(list) + { + add_to_sum(sum, list, i); + } + return sum; + } +}; + +//!ExtractFunctionRefactoringTest with Return Value take the second and Ref Parameter +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +returnparameterindex=1 +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + B *exp(int & i, B *b, int & y, float & x); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + //$++i; + b->hello(y); + i = i + x; + help();$// + b->hello(y); + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +B *A::exp(int & i, B *b, int & y, float & x) +{ + ++i; + b->hello(y); + i = i + x; + help(); + return b; +} + +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + b = exp(i, b, y, x); + b->hello(y); + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//@B.h +#ifndef B_H_ +#define B_H_ + +class B +{ +public: + B(); + virtual ~B(); + void hello(float y); +}; + +#endif /*B_H_*/ + +//!ExtractFunctionRefactoringTest protected visibility +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +protected: + void exp(int & i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + //$++i; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int & i) +{ + ++i; + help(); +} + +int A::foo() +{ + int i = 2; + exp(i); + return i; +} + +int A::help() +{ + return 42; +} + +//@.config +filename=A.cpp +methodname=exp +replaceduplicates=false +returnvalue=false +returnparameterindex=0 +visibility=protected + +//!ExtractFunctionRefactoringTest public visibility +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + void exp(int & i); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + //$++i; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int & i) +{ + ++i; + help(); +} + +int A::foo() +{ + int i = 2; + exp(i); + return i; +} + +int A::help() +{ + return 42; +} + +//@.config +filename=A.cpp +methodname=exp +replaceduplicates=false +returnvalue=false +returnparameterindex=0 +visibility=public + +//!ExtractFunctionRefactoringTest const Method Bug # 46 +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo() const; + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo() const; + +private: + int help(); + void exp(int & i) const; +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() const +{ + int i = 2; + //$++i; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int & i) const +{ + ++i; + help(); +} + +int A::foo() const +{ + int i = 2; + exp(i); + return i; +} + +int A::help() +{ + return 42; +} + +//@.config +filename=A.cpp +methodname=exp +replaceduplicates=false +returnvalue=false +returnparameterindex=0 + +//!don't return variables that aren't used +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +filename=main.h +methodname=loop +//@main.h + +void method() +{ + //$for (int var = 0; var < 100; ++var) { + if(var < 50) + continue; + }$// +} +//= + +void loop() +{ + for(int var = 0;var < 100;++var){ + if(var < 50) + continue; + + } +} + +void method() +{ + loop(); +} +//!don't extract code that contains 'return' +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +filename=main.h +fatalerror=true +//@main.h + +void method() +{ + //$if(true) + return;$// + //unreachable +} +//!test if we don't allow to extract 'continue' Bug #53 +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +fatalerror=true +filename=A.h +//@A.h + +void function() +{ + for (int var = 0; var < 100; ++var) { + //$if(var < 50) + continue;$// + } +} + +//!ExtractMethod with Macros Bug #120 Extract Function generates superfluous characters +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +filename=Test.cpp +methodname=createSuite +//@Test.cpp +#define CUTE(name) cute::test((&name),(#name)) + +void runSuite(){ + //$cute::suite s; + s.push_back(CUTE(thisIsATest)); + s.push_back(CUTE(testFuerRainer));$// + cute::ide_listener lis; + cute::makeRunner(lis)(s, "The Suite"); +} +//= +#define CUTE(name) cute::test((&name),(#name)) + + +cute::suite createSuite() +{ + cute::suite s; + s.push_back(CUTE(thisIsATest)); + s.push_back(CUTE(testFuerRainer)); + return s; +} + +void runSuite(){ + cute::suite s = createSuite(); + cute::ide_listener lis; + cute::makeRunner(lis)(s, "The Suite"); +} +//! Bug #124 Extract Function with a Macro call in selected code "forgets" the macro +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +filename=Test.cpp +methodname=runTest +//@Test.cpp +#define ASSERTM(msg,cond) if (!(cond)) throw cute::test_failure((msg),__FILE__,__LINE__) +#define ASSERT(cond) ASSERTM(#cond, cond) + +void testFuerRainer(){ + int i=int(); + //$++i; + ASSERT (i); + --i;$// +} + +//= +#define ASSERTM(msg,cond) if (!(cond)) throw cute::test_failure((msg),__FILE__,__LINE__) +#define ASSERT(cond) ASSERTM(#cond, cond) + +void runTest(int i) +{ + ++i; + ASSERT (i); + --i; +} + +void testFuerRainer(){ + int i=int(); + runTest(i); +} + +//! Bug #124 with comments Extract Function with a Macro call in selected code "forgets" the macro +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +filename=Test.cpp +methodname=runTest +//@Test.cpp +#define ASSERTM(msg,cond) if (!(cond)) throw cute::test_failure((msg),__FILE__,__LINE__) +#define ASSERT(cond) ASSERTM(#cond, cond) + +void testFuerRainer(){ + int i=int(); + //$++i; + ASSERT (i); + --i;$// +} + +//= +#define ASSERTM(msg,cond) if (!(cond)) throw cute::test_failure((msg),__FILE__,__LINE__) +#define ASSERT(cond) ASSERTM(#cond, cond) + +void runTest(int i) +{ + ++i; + ASSERT (i); + --i; +} + +void testFuerRainer(){ + int i=int(); + runTest(i); +} + +//! Bug #137 String Array problem in Extract Function +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +filename=Test.cpp +methodname=runTest +//@Test.cpp +#include + +using namespace std; + +int const INITIAL_CAPACITY = 10; + +int main(){ + int m_capacity; + //$m_capacity += INITIAL_CAPACITY; + string* newElements = new string[m_capacity];$// + newElements[0] = "s"; +} + +//= +#include + +using namespace std; + +int const INITIAL_CAPACITY = 10; + + +string *runTest(int m_capacity) +{ + m_capacity += INITIAL_CAPACITY; + string *newElements = new string[m_capacity]; + return newElements; +} + +int main(){ + int m_capacity; + string *newElements = runTest(m_capacity); + newElements[0] = "s"; +} + diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethodDuplicates.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethodDuplicates.rts new file mode 100644 index 00000000000..38844b321af --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethodDuplicates.rts @@ -0,0 +1,1096 @@ +//!ExtractFunctionRefactoringTest with duplicates +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int & i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + ++i; + help(); +} +int A::foo() +{ + int i = 2; + //$++i; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + exp(i); +} +void A::exp(int & i) +{ + ++i; + help(); +} + +int A::foo() +{ + int i = 2; + exp(i); + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest duplicates with different Names +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int & i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int oo = 99; + ++oo; + help(); +} +int A::foo() +{ + int i = 2; + //$++i; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int oo = 99; + exp(oo); +} +void A::exp(int & i) +{ + ++i; + help(); +} + +int A::foo() +{ + int i = 2; + exp(i); + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest dublicate with field +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + int i; + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + int i; + +private: + int help(); + int exp(int j, int & a); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int j = 0; + i++; + j++; + help(); +} +void A::foo() +{ + int j = 0; + int a = 1; + //$j++; + a++; + help();$// + a++; + j++; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int j = 0; + i = exp(i, j); +} +int A::exp(int j, int & a) +{ + j++; + a++; + help(); + return j; +} + +void A::foo() +{ + int j = 0; + int a = 1; + j = exp(j, a); + a++; + j++; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest dublicate with field in marked scope +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + int i; + int field; + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + int i; + int field; + +private: + int help(); + int exp(int j); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int j = 0; + int a = 1; + a++; + j++; + help(); +} +void A::foo() +{ + int j = 0; + + //$field++; + j++; + help();$// + field++; + j++; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int j = 0; + int a = 1; + a++; + j++; + help(); +} +int A::exp(int j) +{ + field++; + j++; + help(); + return j; +} + +void A::foo() +{ + int j = 0; + + j = exp(j); + field++; + j++; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest duplicates with different Names and return type +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + int exp(int i, float & j); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int oo = 99; + float blabla = 0; + ++oo; + blabla += 1; + help(); + blabla += 1; +} +int A::foo() +{ + int i = 2; + float j = 8989; + //$++i; + j+=1; + help();$// + j++; + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int oo = 99; + float blabla = 0; + oo = exp(oo, blabla); + blabla += 1; +} +int A::exp(int i, float & j) +{ + ++i; + j += 1; + help(); + return i; +} + +int A::foo() +{ + int i = 2; + float j = 8989; + i = exp(i, j); + j++; + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest duplicates with a lot of different Names an variable not used afterwards in the duplicate +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int & i, float j); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int oo = 99; + float blabla = 0; + ++oo; + blabla += 1; + help(); +} +int A::foo() +{ + int i = 2; + float j = 8989; + //$++i; + j+=1; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int oo = 99; + float blabla = 0; + exp(oo, blabla); +} +void A::exp(int & i, float j) +{ + ++i; + j += 1; + help(); +} + +int A::foo() +{ + int i = 2; + float j = 8989; + exp(i, j); + return i; +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest with duplicates name used afterwards in duplicate but not in original selection this is no dublicate +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + void foo(); + +private: + int help(); + void exp(int i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + ++i;// No Duplicate + help(); + ++i;// this is the reason +} +void A::foo() +{ + int i = 2; + //$++i; + help();$// +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + ++i;// No Duplicate + help(); + ++i;// this is the reason +} +void A::exp(int i) +{ + ++i; + help(); +} + +void A::foo() +{ + int i = 2; + exp(i); +} + +int A::help() +{ + return 42; +} + +//!ExtractFunctionRefactoringTest with Return Value and a lot Ref Parameter and a method call +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + int exp(int i, B *& b, int & y, float & x); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + ++i; + b->hello(y); + i = i + x; + help(); + b->hello(y); + ++x; + i++; +} +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + //$++i; + b->hello(y); + i = i + x; + help();$// + b->hello(y); + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + i = exp(i, b, y, x); + b->hello(y); + ++x; + i++; +} +int A::exp(int i, B *& b, int & y, float & x) +{ + ++i; + b->hello(y); + i = i + x; + help(); + return i; +} + +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + i = exp(i, b, y, x); + b->hello(y); + ++x; + return i; +} + +int A::help() +{ + return 42; +} + +//@B.h +#ifndef B_H_ +#define B_H_ + +class B +{ +public: + B(); + virtual ~B(); + void hello(float y); +}; + +#endif /*B_H_*/ + +//!ExtractFunctionRefactoringTest with Return Value and a lot Ref Parameter and a method call, duplicate is not similar +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +returnvalue=true +//@A.h +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +#include "B.h" + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + int exp(int i, B *& b, int & y, float x); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + ++i; + b->hello(y); + i = i + x; + help(); + b->hello(y); + ++x; + i++; +} +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + //$++i; + b->hello(y); + i = i + x; + help();$// + b->hello(y); + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + ++i; + b->hello(y); + i = i + x; + help(); + b->hello(y); + ++x; + i++; +} +int A::exp(int i, B *& b, int & y, float x) +{ + ++i; + b->hello(y); + i = i + x; + help(); + return i; +} + +int A::foo() +{ + int i = 2; + float x = i; + B* b = new B(); + int y = x + i; + i = exp(i, b, y, x); + b->hello(y); + return i; +} + +int A::help() +{ + return 42; +} + +//@B.h +#ifndef B_H_ +#define B_H_ + +class B +{ +public: + B(); + virtual ~B(); + void hello(float y); +}; + +#endif /*B_H_*/ + +//!ExtractFunctionRefactoringTest with duplicates and comments +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +replaceduplicates=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int & i); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + ++i; + help(); +} +int A::foo() +{ + int i = 2; + //$++i; + help();$// + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +A::A() +{ +} + +A::~A() +{ + int i = 2; + exp(i); +} +void A::exp(int & i) +{ + ++i; + help(); +} + +int A::foo() +{ + int i = 2; + exp(i); + return i; +} + +int A::help() +{ + return 42; +} + diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethodPreprocessor.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethodPreprocessor.rts new file mode 100644 index 00000000000..9263c03fb96 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractMethodPreprocessor.rts @@ -0,0 +1,295 @@ +//!ExtractFunctionRefactoringTest with FunctionStyleMacro2 +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); + +private: + int help(); + void exp(int & ii); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +#define ADD(a,ab) a + ab + 2 + a + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int ii = 2; + //$++ii; + ii = ADD(ii, 42); + help();$// + return ii; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +#define ADD(a,ab) a + ab + 2 + a + +A::A() +{ +} + +A::~A() +{ +} +void A::exp(int & ii) +{ + ++ii; + ii = ADD(ii, 42); + help(); +} + +int A::foo() +{ + int ii = 2; + exp(ii); + return ii; +} + +int A::help() +{ + return 42; +} + +//!Extract Method return value assinged to Macrocall Bug +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +returnvalue=true +returnparameterindex=1 +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); +private: + int help(); + int exp(int & i, int b); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +#define ADD(b) b = b + 2 + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + int b = 42; + //$++i; + help(); + ADD(b);$// + b += 2; + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +#define ADD(b) b = b + 2 + +A::A() +{ +} + +A::~A() +{ +} +int A::exp(int & i, int b) +{ + ++i; + help(); + ADD(b); + return b; +} + +int A::foo() +{ + int i = 2; + int b = 42; + b = exp(i, b); + b += 2; + return i; +} + +int A::help() +{ + return 42; +} + +//!Extract Method Methodlength with more than 1 Macrocall Bug +//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest +//@.config +returnvalue=true +returnparameterindex=0 +replaceduplicates=true +//@A.h +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); +private: + int help(); +}; + +#endif /*A_H_*/ + +//= +#ifndef A_H_ +#define A_H_ + +class A +{ +public: + A(); + virtual ~A(); + int foo(); +private: + int help(); + int exp(int bb); +}; + +#endif /*A_H_*/ + +//@A.cpp +#include "A.h" + +#define ADD(b) b = b + 2 + +A::A() +{ +} + +A::~A() +{ +} +int A::foo() +{ + int i = 2; + int bb = 42; + ++i; + //$ADD(bb); + ADD(bb);$// + bb += 2; + return i; +} + +int A::help() +{ + return 42; +} + +//= +#include "A.h" + +#define ADD(b) b = b + 2 + +A::A() +{ +} + +A::~A() +{ +} +int A::exp(int bb) +{ + ADD(bb); + ADD(bb); + return bb; +} + +int A::foo() +{ + int i = 2; + int bb = 42; + ++i; + bb = exp(bb); + bb += 2; + return i; +} + +int A::help() +{ + return 42; +} + diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTestSuite.java index 7a3fbf47520..1a0f3e7c193 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTestSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/RefactoringTestSuite.java @@ -14,6 +14,7 @@ package org.eclipse.cdt.ui.tests.refactoring; import junit.framework.Test; import junit.framework.TestSuite; +import org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionTestSuite; import org.eclipse.cdt.ui.tests.refactoring.rename.RenameRegressionTests; import org.eclipse.cdt.ui.tests.refactoring.utils.UtilTestSuite; @@ -26,6 +27,7 @@ public class RefactoringTestSuite extends TestSuite { public static Test suite() throws Exception { TestSuite suite = new RefactoringTestSuite(); suite.addTest(RenameRegressionTests.suite()); + suite.addTest(ExtractFunctionTestSuite.suite()); suite.addTest(RefactoringTester.suite("ExtractConstantRefactoringTests", "resources/refactoring/ExtractConstant.rts")); suite.addTest(UtilTestSuite.suite()); return suite; 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 new file mode 100644 index 00000000000..4fac779c9d1 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionRefactoringTest.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright (c) 2008 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.refactoring.extractfunction; + +import java.util.Properties; +import java.util.Vector; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.RefactoringStatus; + +import org.eclipse.cdt.ui.tests.refactoring.RefactoringTest; +import org.eclipse.cdt.ui.tests.refactoring.TestSourceFile; + +import org.eclipse.cdt.internal.ui.refactoring.CRefactoring; +import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation; +import org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionInformation; +import org.eclipse.cdt.internal.ui.refactoring.extractfunction.ExtractFunctionRefactoring; +import org.eclipse.cdt.internal.ui.refactoring.utils.VisibilityEnum; + +/** + * @author Emanuel Graf + * + */ +public class ExtractFunctionRefactoringTest extends RefactoringTest { + + protected String methodName; + protected boolean replaceDuplicates; + protected boolean returnValue; + protected int returnParameterIndex; + protected boolean fatalError; + private VisibilityEnum visibility; + + /** + * @param name + * @param files + */ + public ExtractFunctionRefactoringTest(String name, Vector files) { + super(name, files); + } + + @Override + protected void runTest() throws Throwable { + IFile refFile = project.getFile(fileName); + ExtractFunctionInformation info = new ExtractFunctionInformation(); + CRefactoring refactoring = new ExtractFunctionRefactoring( refFile, selection, info); + RefactoringStatus checkInitialConditions = refactoring.checkInitialConditions(NULL_PROGRESS_MONITOR); + + if(fatalError){ + assertConditionsFatalError(checkInitialConditions); + return; + } + else{ + assertConditionsOk(checkInitialConditions); + executeRefactoring(info, refactoring); + } + + + } + + private void executeRefactoring(ExtractFunctionInformation info, CRefactoring refactoring) throws CoreException, Exception { + info.setMethodName(methodName); + info.setReplaceDuplicates(replaceDuplicates); + if(info.getInScopeDeclaredVariable() == null){ + if(returnValue) { + info.setReturnVariable(info.getAllAfterUsedNames().elementAt(returnParameterIndex)); + } + } else { + info.setReturnVariable( info.getInScopeDeclaredVariable() ); + } + info.setVisibility(visibility); + + for (NameInformation name : info.getAllAfterUsedNames()) { + if(!name.isUserSetIsReturnValue()){ + name.setUserSetIsReference(name.isReference()); + } + } + + Change createChange = refactoring.createChange(NULL_PROGRESS_MONITOR); + RefactoringStatus finalConditions = refactoring.checkFinalConditions(NULL_PROGRESS_MONITOR); + assertConditionsOk(finalConditions); + createChange.perform(NULL_PROGRESS_MONITOR); + + compareFiles(fileMap); + } + + + @Override + protected void configureRefactoring(Properties refactoringProperties) { + methodName = refactoringProperties.getProperty("methodname", "exp"); //$NON-NLS-1$ //$NON-NLS-2$ + replaceDuplicates = Boolean.valueOf(refactoringProperties.getProperty("replaceduplicates", "false")).booleanValue(); //$NON-NLS-1$ //$NON-NLS-2$ + returnValue = Boolean.valueOf(refactoringProperties.getProperty("returnvalue", "false")).booleanValue(); //$NON-NLS-1$//$NON-NLS-2$ + returnParameterIndex = new Integer(refactoringProperties.getProperty("returnparameterindex", "0")).intValue(); //$NON-NLS-1$ //$NON-NLS-2$ + fatalError = Boolean.valueOf(refactoringProperties.getProperty("fatalerror", "false")).booleanValue(); //$NON-NLS-1$ //$NON-NLS-2$ + visibility = VisibilityEnum.getEnumForStringRepresentation(refactoringProperties.getProperty("visibility", VisibilityEnum.v_private.toString())); //$NON-NLS-1$ + } + +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionTestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionTestSuite.java new file mode 100644 index 00000000000..613e88bd22d --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractfunction/ExtractFunctionTestSuite.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2008 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.refactoring.extractfunction; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.ui.tests.refactoring.RefactoringTester; + +/** + * @author Emanuel Graf + * + */ +public class ExtractFunctionTestSuite extends TestSuite { + + @SuppressWarnings("nls") + public static Test suite() throws Exception { + TestSuite suite = new ExtractFunctionTestSuite(); + suite.addTest(RefactoringTester.suite("Extract Expression", "resources/refactoring/ExtractExpression.rts")); + suite.addTest(RefactoringTester.suite("ExtractMethodRefactoringTests", "resources/refactoring/ExtractMethod.rts")); + suite.addTest(RefactoringTester.suite("ExtractMethodPreprocessorRefactoringTests", "resources/refactoring/ExtractMethodPreprocessor.rts")); + suite.addTest(RefactoringTester.suite("ExtractMethodDuplicatesTests", "resources/refactoring/ExtractMethodDuplicates.rts")); + suite.addTest(RefactoringTester.suite("Extract Function Templates Tests", "resources/refactoring/ExtractFunctionTemplates.rts")); + return suite; + } + +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/UtilTestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/UtilTestSuite.java index b7270e19e01..37bf539f4fc 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/UtilTestSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/utils/UtilTestSuite.java @@ -20,10 +20,10 @@ import org.eclipse.cdt.ui.tests.refactoring.RefactoringTester; * @author Thomas Corbat * */ -public class UtilTestSuite { +public class UtilTestSuite extends TestSuite { public static Test suite() throws Exception { - TestSuite suite = new TestSuite("UtilTests"); //$NON-NLS-1$ + UtilTestSuite suite = new UtilTestSuite(); suite.addTest(IdentifierHelperTest.suite()); suite.addTest(RefactoringTester.suite("TranslationUnitHelperTest", "resources/refactoring/TranslationunitHelper.rts")); //$NON-NLS-1$ //$NON-NLS-2$ return suite; diff --git a/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF b/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF index bfc38b4679c..a0b98067114 100644 --- a/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF +++ b/core/org.eclipse.cdt.ui/META-INF/MANIFEST.MF @@ -31,6 +31,7 @@ Export-Package: org.eclipse.cdt.internal.corext;x-internal:=true, org.eclipse.cdt.internal.ui.refactoring;x-friends:="org.eclipse.cdt.ui.tests", org.eclipse.cdt.internal.ui.refactoring.dialogs, org.eclipse.cdt.internal.ui.refactoring.extractconstant;x-friends:="org.eclipse.cdt.ui.tests", + org.eclipse.cdt.internal.ui.refactoring.extractfunction, org.eclipse.cdt.internal.ui.refactoring.rename;x-friends:="org.eclipse.cdt.ui.tests", org.eclipse.cdt.internal.ui.refactoring.utils;x-friends:="org.eclipse.cdt.ui.tests", org.eclipse.cdt.internal.ui.search;x-internal:=true, diff --git a/core/org.eclipse.cdt.ui/plugin.properties b/core/org.eclipse.cdt.ui/plugin.properties index abe99c4098d..fa7e0163118 100644 --- a/core/org.eclipse.cdt.ui/plugin.properties +++ b/core/org.eclipse.cdt.ui/plugin.properties @@ -143,6 +143,8 @@ ActionDefinition.renameElement.name= Rename - Refactoring ActionDefinition.renameElement.description= Rename the selected element ActionDefinition.extractConstant.name= Extract Constant - Refactoring ActionDefinition.extractConstant.description= Extract a constant for the selected expression +ActionDefinition.extractFunction.name= Extract Function - Refactoring +ActionDefinition.extractFunction.description= Extract a function for the selected list of expressions or statements # Action Set CodingActionSet.label= C/C++ Coding @@ -151,6 +153,7 @@ CodingActionSet.description= Action set containing coding related C/C++ actions Refactoring.menu.label= Refac&tor Refactoring.renameAction.label=Re&name... Refactoring.extractConstant.label=Extr&act Constant... +Refactoring.extractFunction.label=Extract &Function... (work in progress) CEditor.name=C/C++ Editor diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index d9c4e83d501..3dc49bc5ebb 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1150,6 +1150,13 @@ id="org.eclipse.cdt.ui.actions.Rename" retarget="true"> + + + @@ -1729,21 +1739,18 @@ @@ -1972,6 +1979,12 @@ categoryId="org.eclipse.cdt.ui.category.refactoring" id="org.eclipse.cdt.ui.refactor.extract.constant"> + + "org.eclipse.cdt.ui.refactor.extract.constant"). */ public static final String EXTRACT_CONSTANT= "org.eclipse.cdt.ui.refactor.extract.constant"; //$NON-NLS-1$ + + /** + * Action definition ID of the refactor -> extract function action (value + * "org.eclipse.cdt.ui.refactor.extract.function"). + */ + public static final String EXTRACT_FUNCTION = "org.eclipse.cdt.ui.refactor.extract.function"; //$NON-NLS-1$ /** * Action definition ID of the refactor -> undo action diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java index a261cbe2602..61fd238d171 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/CRefactoring.java @@ -65,10 +65,11 @@ import org.eclipse.cdt.internal.core.dom.parser.IASTDeclarationAmbiguity; public abstract class CRefactoring extends Refactoring { protected static final String EMPTY_STRING = ""; //$NON-NLS-1$ private static final int AST_STYLE = ITranslationUnit.AST_CONFIGURE_USING_SOURCE_CONTEXT | ITranslationUnit.AST_SKIP_INDEXED_HEADERS; + public static final String NEWLINE = "\n"; // mstodo //$NON-NLS-1$ protected String name = Messages.HSRRefactoring_name; protected IFile file; - private ISelection selection; + protected ISelection selection; protected RefactoringStatus initStatus; protected IASTTranslationUnit unit; private IIndex fIndex; @@ -353,6 +354,13 @@ public abstract class CRefactoring extends Refactoring { } return false; } + + protected boolean isSelectedFile(ITextSelection textSelection, IASTNode node) { + if( isInSameFile(node) ) { + return isExpressionWhollyInSelection(textSelection, node); + } + return false; + } protected MethodContext findContext(IASTNode node) { boolean found = false; @@ -400,6 +408,16 @@ public abstract class CRefactoring extends Refactoring { return af.ambiguityFound(); } + protected IASTSimpleDeclaration findSimpleDeclarationInParents(IASTNode node) { + while(node != null){ + if (node instanceof IASTSimpleDeclaration) { + return (IASTSimpleDeclaration) node; + } + node = node.getParent(); + } + return null; + } + public void lockIndex() throws CoreException, InterruptedException { if (fIndex == null) { ICProject[] projects= CoreModel.getDefault().getCModel().getCProjects(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeTreeSet.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeTreeSet.java new file mode 100644 index 00000000000..3e5ea98cbde --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/ChangeTreeSet.java @@ -0,0 +1,56 @@ +/******************************************************************************* + * Copyright (c) 2008 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring; + +import java.util.Comparator; +import java.util.TreeSet; + +import org.eclipse.ltk.core.refactoring.Change; +import org.eclipse.ltk.core.refactoring.CompositeChange; + +import org.eclipse.cdt.ui.refactoring.CTextFileChange; + +/** + * @author Emanuel Graf + * + */ +public class ChangeTreeSet { + + private static final class ChangePositionComparator implements Comparator { + public int compare(CTextFileChange o1, CTextFileChange o2) { + if(o1.getFile().equals(o2.getFile())){ + return o2.getEdit().getOffset() - o1.getEdit().getOffset(); + } + return o2.getFile().hashCode() - o1.getFile().hashCode(); + } + } + + private final TreeSet changes = new TreeSet(new ChangePositionComparator()); + + public void add(CTextFileChange change) { + changes.add(change); + } + + public CompositeChange getCompositeChange(String name) { + CompositeChange allChanges = new CompositeChange(name); + + for (Change change : changes) { + allChanges.add(change); + } + return allChanges; + } + + @Override + public String toString() { + return changes.toString(); + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/EqualityChecker.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/EqualityChecker.java new file mode 100644 index 00000000000..42f5d0e12fd --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/EqualityChecker.java @@ -0,0 +1,16 @@ +/******************************************************************************* + * Copyright (c) 2008 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring; + +public interface EqualityChecker { + boolean isEquals(T object1, T object2); +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.java index 87eaa0e3483..c796a39e9ec 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/Messages.java @@ -38,6 +38,8 @@ public final class Messages extends NLS { public static String HSRRefactoring_SelectionNotValid; public static String HSRRefactoring_CantLoadTU; public static String HSRRefactoring_Ambiguity; + public static String NodeContainer_Name; + public static String NodeContainer_Space; public static String NO_FILE; static { 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 new file mode 100644 index 00000000000..913ecb7f761 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/NodeContainer.java @@ -0,0 +1,486 @@ +/******************************************************************************* + * Copyright (c) 2008 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring; + +import java.util.List; +import java.util.Vector; + +import org.eclipse.core.runtime.ILog; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; + +import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTArrayModifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IASTDeclarator; +import org.eclipse.cdt.core.dom.ast.IASTFileLocation; +import org.eclipse.cdt.core.dom.ast.IASTMacroExpansionLocation; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.dom.ast.IASTNode; +import org.eclipse.cdt.core.dom.ast.IASTNodeLocation; +import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTPointerOperator; +import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; +import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IBinding; +import org.eclipse.cdt.core.dom.ast.cpp.CPPASTVisitor; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration; +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.ui.CUIPlugin; + +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; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration; +import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReferenceOperator; +import org.eclipse.cdt.internal.core.dom.rewrite.astwriter.ASTWriter; + +public class NodeContainer { + + private final Vector vec; + private final Vector names; + + public class NameInformation { + private IASTName name; + private IASTName declaration; + private final Vector references; + private Vector referencesAfterCached; + private int lastCachedReferencesHash; + private boolean isReference; + private boolean isReturnValue; + + private boolean userSetIsReference; + private boolean userSetIsReturnValue; + private String userSetName; + private int userOrder; + + public int getUserOrder() { + return userOrder; + } + + public void setUserOrder(int userOrder) { + this.userOrder = userOrder; + } + + public NameInformation(IASTName name) { + super(); + this.name = name; + references = new Vector(); + } + + public IASTName getDeclaration() { + return declaration; + } + + public void setDeclaration(IASTName declaration) { + this.declaration = declaration; + } + + public IASTName getName() { + return name; + } + + public void setName(IASTName name) { + this.name = name; + } + + public void addReference(IASTName name) { + references.add(name); + } + + public Vector getReferencesAfterSelection() { + if (referencesAfterCached == null + || lastCachedReferencesHash == references.hashCode()) { + + lastCachedReferencesHash = references.hashCode(); + referencesAfterCached = new Vector(); + for (IASTName ref : references) { + IASTFileLocation loc = ref.getFileLocation(); + if (loc.getNodeOffset() >= getEndOffset()) { + referencesAfterCached.add(ref); + } + } + } + return referencesAfterCached; + } + + public boolean isUsedAfterReferences() { + return getReferencesAfterSelection().size() > 0; + } + + public ICPPASTParameterDeclaration getICPPASTParameterDeclaration( + boolean isReference) { + ICPPASTParameterDeclaration para = new CPPASTParameterDeclaration(); + IASTDeclarator sourceDeclarator = (IASTDeclarator) getDeclaration() + .getParent(); + + if (sourceDeclarator.getParent() instanceof IASTSimpleDeclaration) { + IASTSimpleDeclaration decl = (IASTSimpleDeclaration) sourceDeclarator + .getParent(); + para.setDeclSpecifier(decl.getDeclSpecifier()); + } else if (sourceDeclarator.getParent() instanceof IASTParameterDeclaration) { + IASTParameterDeclaration decl = (IASTParameterDeclaration) sourceDeclarator + .getParent(); + para.setDeclSpecifier(decl.getDeclSpecifier()); + } + + IASTDeclarator declarator; + if (sourceDeclarator instanceof IASTArrayDeclarator) { + IASTArrayDeclarator arrDeclarator = (IASTArrayDeclarator)sourceDeclarator; + declarator = new CPPASTArrayDeclarator(); + IASTArrayModifier[] arrayModifiers = arrDeclarator.getArrayModifiers(); + for (IASTArrayModifier arrayModifier : arrayModifiers) { + ((IASTArrayDeclarator)declarator).addArrayModifier(arrayModifier); + } + + }else { + declarator = new CPPASTDeclarator(); + } + declarator.setName(new CPPASTName(getDeclaration().toCharArray())); + for (IASTPointerOperator pointerOp : sourceDeclarator + .getPointerOperators()) { + declarator.addPointerOperator(pointerOp); + } + + if (isReference) { + declarator.addPointerOperator(new CPPASTReferenceOperator()); + } + + declarator.setNestedDeclarator(sourceDeclarator + .getNestedDeclarator()); + para.setDeclarator(declarator); + + return para; + } + + public String getType() { + IASTDeclSpecifier declSpec = null; + + IASTNode node = getDeclaration().getParent(); + if (node instanceof ICPPASTSimpleTypeTemplateParameter) { + ICPPASTSimpleTypeTemplateParameter parameter = (ICPPASTSimpleTypeTemplateParameter) node; + return parameter.getName().toString(); + } + IASTDeclarator sourceDeclarator = (IASTDeclarator) node; + if (sourceDeclarator.getParent() instanceof IASTSimpleDeclaration) { + IASTSimpleDeclaration decl = (IASTSimpleDeclaration) sourceDeclarator + .getParent(); + declSpec = decl.getDeclSpecifier(); + } else if (sourceDeclarator.getParent() instanceof IASTParameterDeclaration) { + IASTParameterDeclaration decl = (IASTParameterDeclaration) sourceDeclarator + .getParent(); + declSpec = decl.getDeclSpecifier(); + } + + ASTWriter writer = new ASTWriter(); + return writer.write(declSpec); + } + + public boolean isDeclarationInScope() { + int declOffset = declaration.getFileLocation().getNodeOffset(); + return declOffset >= getStartOffset() + && declOffset <= getEndOffset(); + } + + @Override + public String toString() { + return Messages.NodeContainer_Name + name + Messages.NodeContainer_Space + isDeclarationInScope(); + } + + public boolean isReference() { + return isReference; + } + + public void setReference(boolean isReference) { + this.isReference = isReference; + } + + public boolean isReturnValue() { + return isReturnValue; + } + + public void setReturnValue(boolean isReturnValue) { + this.isReturnValue = isReturnValue; + } + + public boolean isUserSetIsReference() { + return userSetIsReference; + } + + public void setUserSetIsReference(boolean userSetIsReference) { + this.userSetIsReference = userSetIsReference; + } + + public boolean isUserSetIsReturnValue() { + return userSetIsReturnValue; + } + + public void setUserSetIsReturnValue(boolean userSetIsReturnValue) { + this.userSetIsReturnValue = userSetIsReturnValue; + } + + public String getUserSetName() { + return userSetName; + } + + public void setUserSetName(String userSetName) { + this.userSetName = userSetName; + } + } + + public NodeContainer() { + super(); + vec = new Vector(); + names = new Vector(); + } + + public int size() { + return vec.size(); + } + + public void add(IASTNode node) { + vec.add(node); + } + + public void findAllNames() { + for (IASTNode node : vec) { + node.accept(new CPPASTVisitor() { + { + shouldVisitNames = true; + } + + @Override + public int visit(IASTName name) { + IBinding bind = name.resolveBinding(); + + if (bind instanceof ICPPBinding + && !(bind instanceof ICPPTemplateTypeParameter)) { + ICPPBinding cppBind = (ICPPBinding) bind; + try { + if (!cppBind.isGloballyQualified()) { + NameInformation nameInformation = new NameInformation( + name); + + IASTName[] refs = name.getTranslationUnit() + .getReferences(bind); + for (IASTName ref : refs) { + nameInformation.addReference(ref); + } + names.add(nameInformation); + } + } catch (DOMException e) { + ILog logger = CUIPlugin.getDefault().getLog(); + IStatus status = new Status(IStatus.WARNING, + CUIPlugin.PLUGIN_ID, IStatus.OK, e + .getMessage(), e); + logger.log(status); + } + } + return super.visit(name); + } + }); + } + + for (NameInformation nameInf : names) { + IASTName name = nameInf.getName(); + + IASTTranslationUnit unit = name.getTranslationUnit(); + IASTName[] decls = unit.getDeclarationsInAST(name.resolveBinding()); + for (IASTName declaration : decls) { + nameInf.setDeclaration(declaration); + } + } + } + + /* + * Returns all local names in the selection which will be used after the + * selection expected the ones which are pointers + */ + public Vector getAllAfterUsedNames() { + Vector declarations = new Vector(); + Vector usedAfter = new Vector(); + + if (names.size() <= 0) { + findAllNames(); + } + + for (NameInformation nameInf : names) { + if (!declarations.contains(nameInf.getDeclaration())) { + + declarations.add(nameInf.getDeclaration()); + if (nameInf.isUsedAfterReferences()) { + usedAfter.add(nameInf); + nameInf.setReference(true); + } + } + } + + return usedAfter; + } + + public Vector getAllAfterUsedNamesChoosenByUser() { + Vector declarations = new Vector(); + Vector usedAfter = new Vector(); + + for (NameInformation nameInf : names) { + if (!declarations.contains(nameInf.getDeclaration())) { + + declarations.add(nameInf.getDeclaration()); + if (nameInf.isUserSetIsReference() + || nameInf.isUserSetIsReturnValue()) { + usedAfter.add(nameInf); + } + } + } + + return usedAfter; + } + + public Vector getUsedNamesUnique() { + Vector declarations = new Vector(); + Vector usedAfter = new Vector(); + + if (names.size() <= 0) { + findAllNames(); + } + + for (NameInformation nameInf : names) { + if (!declarations.contains(nameInf.getDeclaration())) { + + declarations.add(nameInf.getDeclaration()); + usedAfter.add(nameInf); + } + } + + return usedAfter; + } + + /* + * Returns all local names in the selection which will be used after the + * selection expected the ones which are pointers + * XXX Was soll dieser Kommentar aussagen? --Mirko + */ + public Vector getAllDeclaredInScope() { + Vector declarations = new Vector(); + Vector usedAfter = new Vector(); + + for (NameInformation nameInf : names) { + if (nameInf.isDeclarationInScope() + && !declarations.contains(nameInf.getDeclaration()) && nameInf.isUsedAfterReferences()) { + + declarations.add(nameInf.getDeclaration()); + usedAfter.add(nameInf); + // is return value candidate, set returnvalue to true and + // reference to false + nameInf.setReturnValue(true); + nameInf.setReference(false); + } + } + + return usedAfter; + } + + public List getNodesToWrite() { + return vec; + } + + public int getStartOffset() { + return getOffset(false); + } + + public int getStartOffsetIncludingComments() { + return getOffset(true); + } + + private int getOffset(boolean includeComments) { + int start = Integer.MAX_VALUE; + + for (IASTNode node : vec) { + int nodeStart = Integer.MAX_VALUE; + + IASTNodeLocation[] nodeLocations = node.getNodeLocations(); + if (nodeLocations.length != 1) { + for (IASTNodeLocation location : nodeLocations) { + int nodeOffset; + if (location instanceof IASTMacroExpansionLocation) { + IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location; + nodeOffset = macroLoc.asFileLocation().getNodeOffset(); + }else { + nodeOffset = node.getFileLocation().getNodeOffset(); + } + if(nodeOffset < nodeStart) { + nodeStart = nodeOffset; + } + } + } else { + nodeStart = node.getFileLocation().getNodeOffset(); + } + if (nodeStart < start) { + start = nodeStart; + } + } + + return start; + } + + public int getEndOffset() { + return getEndOffset(false); + } + + public int getEndOffsetIncludingComments() { + return getEndOffset(true); + } + + private int getEndOffset(boolean includeComments) { + int end = 0; + + for (IASTNode node : vec) { + int fileOffset = 0; + int length = 0; + + IASTNodeLocation[] nodeLocations = node.getNodeLocations(); + for (IASTNodeLocation location : nodeLocations) { + int nodeOffset, nodeLength; + if (location instanceof IASTMacroExpansionLocation) { + IASTMacroExpansionLocation macroLoc = (IASTMacroExpansionLocation) location; + nodeOffset = macroLoc.asFileLocation().getNodeOffset(); + nodeLength = macroLoc.asFileLocation().getNodeLength(); + }else { + nodeOffset = location.getNodeOffset(); + nodeLength = location.getNodeLength(); + } + if(fileOffset < nodeOffset) { + fileOffset = nodeOffset; + length = nodeLength; + } + } + int endNode = fileOffset + length; + if (endNode > end) { + end = endNode; + } + } + + return end; + } + + @Override + public String toString() { + return vec.toString(); + } + + public Vector getNames() { + return names; + } +} diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringRunner.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringRunner.java index f7ab9356367..d5b1805f593 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringRunner.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/RefactoringRunner.java @@ -13,8 +13,7 @@ package org.eclipse.cdt.internal.ui.refactoring; import org.eclipse.core.resources.IFile; import org.eclipse.jface.viewers.ISelection; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.PlatformUI; +import org.eclipse.jface.window.IShellProvider; /** * Base class for all refactoring runners. @@ -26,12 +25,12 @@ public abstract class RefactoringRunner { protected IFile file; protected ISelection selection; - protected Shell shell; + protected IShellProvider shellProvider; - public RefactoringRunner(IFile file, ISelection selection) { + public RefactoringRunner(IFile file, ISelection selection, IShellProvider shellProvider) { this.file = file; this.selection = selection; - shell = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(); + this.shellProvider= shellProvider; } public abstract void run(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringRunner.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringRunner.java index 5933a61e7b0..2a6f285f0e8 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringRunner.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractconstant/ExtractConstantRefactoringRunner.java @@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.ui.refactoring.extractconstant; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.window.IShellProvider; import org.eclipse.ltk.ui.refactoring.RefactoringWizardOpenOperation; import org.eclipse.cdt.ui.CUIPlugin; @@ -28,8 +29,8 @@ import org.eclipse.cdt.internal.ui.refactoring.RefactoringRunner; */ public class ExtractConstantRefactoringRunner extends RefactoringRunner { - public ExtractConstantRefactoringRunner(IFile file, ISelection selection) { - super(file, selection); + public ExtractConstantRefactoringRunner(IFile file, ISelection selection, IShellProvider shellProvider) { + super(file, selection, shellProvider); } @Override @@ -42,7 +43,7 @@ public class ExtractConstantRefactoringRunner extends RefactoringRunner { try { refactoring.lockIndex(); try { - operator.run(shell, refactoring.getName()); + operator.run(shellProvider.getShell(), refactoring.getName()); } finally { refactoring.unlockIndex(); diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java new file mode 100644 index 00000000000..83ac0597d65 --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ChooserComposite.java @@ -0,0 +1,203 @@ +/******************************************************************************* + * Copyright (c) 2008 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 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Institute for Software - initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.internal.ui.refactoring.extractfunction; + +import java.util.Vector; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.custom.TableEditor; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.events.SelectionListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Table; +import org.eclipse.swt.widgets.TableColumn; +import org.eclipse.swt.widgets.TableItem; + +import org.eclipse.cdt.internal.ui.refactoring.NodeContainer.NameInformation; + +public class ChooserComposite extends Composite { + + private static final String COLUMN_RETURN = Messages.ChooserComposite_Return; + private static final String COLUMN_REFERENCE = Messages.ChooserComposite_CallByRef; + private static final String COLUMN_NAME = Messages.ChooserComposite_Name; + private static final String COLUMN_TYPE = Messages.ChooserComposite_Type; + + + private Button voidReturn; + + private final ExtractFunctionInputPage ip; + + public ChooserComposite(Composite parent, + final ExtractFunctionInformation info, ExtractFunctionInputPage ip) { + super(parent, SWT.NONE); + + this.ip = ip; + + GridLayout layout = new GridLayout(); + setLayout(layout); + + boolean hasNoPredefinedReturnValue = true; + if (info.getInScopeDeclaredVariable() != null) { + info.getInScopeDeclaredVariable().setUserSetIsReturnValue(true); + hasNoPredefinedReturnValue = false; + } + + final Vector