mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-22 14:12:10 +02:00
FIXED - bug 277065: extract local variable fails to extract from for loop
https://bugs.eclipse.org/bugs/show_bug.cgi?id=277065
This commit is contained in:
parent
c16d31b327
commit
bec6503afa
3 changed files with 24 additions and 1 deletions
|
@ -315,3 +315,16 @@ int A::foo()
|
||||||
return i * 15;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.IASTName;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
import org.eclipse.cdt.core.dom.ast.IASTNode;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
|
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.IBinding;
|
||||||
import org.eclipse.cdt.core.dom.ast.IType;
|
import org.eclipse.cdt.core.dom.ast.IType;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTBinaryExpression;
|
||||||
|
@ -111,6 +112,14 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(declSpecifier == null) {
|
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);
|
return createSimpleDeclSpecifier(IASTSimpleDeclSpecifier.t_void);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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.IASTDeclarator;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTFileLocation;
|
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.IASTFunctionDefinition;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTInitializerExpression;
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
|
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
|
||||||
|
@ -259,7 +260,7 @@ public class ExtractLocalVariableRefactoring extends CRefactoring {
|
||||||
|
|
||||||
private IASTStatement getParentStatement(IASTNode node) {
|
private IASTStatement getParentStatement(IASTNode node) {
|
||||||
while (node != null) {
|
while (node != null) {
|
||||||
if (node instanceof IASTStatement)
|
if (node instanceof IASTStatement && !(node.getParent() instanceof IASTForStatement))
|
||||||
return (IASTStatement) node;
|
return (IASTStatement) node;
|
||||||
node = node.getParent();
|
node = node.getParent();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue