From bec6503afa39cc5b70fe2ec2c7a27a131fb17f54 Mon Sep 17 00:00:00 2001 From: Emanuel Graf Date: Tue, 26 May 2009 08:53:43 +0000 Subject: [PATCH] FIXED - bug 277065: extract local variable fails to extract from for loop https://bugs.eclipse.org/bugs/show_bug.cgi?id=277065 --- .../resources/refactoring/ExtractLocalVariable.rts | 13 +++++++++++++ .../extractfunction/ExtractExpression.java | 9 +++++++++ .../ExtractLocalVariableRefactoring.java | 3 ++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts index d3e65a9713d..f3a96d6519a 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts @@ -315,3 +315,16 @@ int A::foo() return i * 15; } +//!Bug #277065 extract local variable fails to extract from for loop +//#org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable.ExtractLocalVariableRefactoringTest +//@A.cpp +void foo(){ + for(int n = /*$*/5 + 2/*$$*/; n < 10; ++n); +} + +//= +void foo(){ + int i = 5 + 2; + for(int n = i; n < 10; ++n); +} + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java index 57c6abc7563..b40794800cc 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractfunction/ExtractExpression.java @@ -26,6 +26,7 @@ import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier; +import org.eclipse.cdt.core.dom.ast.IBasicType; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression; @@ -111,6 +112,14 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper { } if(declSpecifier == null) { + if(extractedNode instanceof IASTExpression) { + IType type = ((IASTExpression)extractedNode).getExpressionType(); + if(type instanceof IBasicType) { + try { + return createSimpleDeclSpecifier(((IBasicType)type).getType()); + } catch (DOMException e) {} //make it void + } + } return createSimpleDeclSpecifier(IASTSimpleDeclSpecifier.t_void); } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java index f4aa264942a..baccfce2df9 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/refactoring/extractlocalvariable/ExtractLocalVariableRefactoring.java @@ -31,6 +31,7 @@ import org.eclipse.cdt.core.dom.ast.IASTDeclarationStatement; import org.eclipse.cdt.core.dom.ast.IASTDeclarator; import org.eclipse.cdt.core.dom.ast.IASTExpression; import org.eclipse.cdt.core.dom.ast.IASTFileLocation; +import org.eclipse.cdt.core.dom.ast.IASTForStatement; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression; import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression; @@ -259,7 +260,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { private IASTStatement getParentStatement(IASTNode node) { while (node != null) { - if (node instanceof IASTStatement) + if (node instanceof IASTStatement && !(node.getParent() instanceof IASTForStatement)) return (IASTStatement) node; node = node.getParent(); }