From 72e7b6b3db7f9b70865c91c380042528e08a6e17 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Thu, 7 Apr 2011 12:41:03 +0000 Subject: [PATCH] Bug 335202 - [Extract Local Variable] Suggested name is wrong for nested function calls --- .../refactoring/ExtractLocalVariable.rts | 70 +++++++++++++++++++ .../ExtractLocalVariableRefactoring.java | 20 +++++- 2 files changed, 89 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 f07d775a8d4..61431473ef2 100644 --- a/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts +++ b/core/org.eclipse.cdt.ui.tests/resources/refactoring/ExtractLocalVariable.rts @@ -596,3 +596,73 @@ void bar(){ get; } +//!Bug 335202 Suggested name is wrong for nested function calls - outer function call +//#org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable.ExtractLocalVariableRefactoringTest +//@A.cpp + +int getA(){ + return 0; +}; + +int getB(int a){ + return a; +} + +void bar(){ + /*$*/getB(getA())/*$$*/; +} + +//= + +int getA(){ + return 0; +}; + +int getB(int a){ + return a; +} + +void bar(){ + int b = getB(getA()); + b; +} + +//!Bug 335202 Suggested name is wrong for nested function calls - inner function call +//#org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable.ExtractLocalVariableRefactoringTest +//@A.cpp + +int getA(){ + return 0; +}; + +int getB(int a){ + return a; +} + +int getC(int a){ + return a; +} + +void bar(){ + getB(/*$*/getC(getA())/*$$*/); +} + +//= + +int getA(){ + return 0; +}; + +int getB(int a){ + return a; +} + +int getC(int a){ + return a; +} + +void bar(){ + int c = getC(getA()); + getB(c); +} + 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 afe5c8e08f5..c8f3bb5d1b4 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2010 Google and others. All rights reserved. This program and + * Copyright (c) 2008, 2011 Google 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 @@ -37,6 +37,7 @@ 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.IASTIdExpression; import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression; import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNode; @@ -48,6 +49,7 @@ import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.INodeFactory; import org.eclipse.cdt.core.dom.ast.IScope; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier; +import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionCallExpression; import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; import org.eclipse.cdt.core.dom.rewrite.DeclarationGenerator; import org.eclipse.cdt.core.model.ICProject; @@ -363,6 +365,22 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { @Override public int visit(IASTExpression expression) { + + // If the expression starts with a function call with a name, we should only need to guess this name + if(expression == target && expression instanceof ICPPASTFunctionCallExpression) { + ICPPASTFunctionCallExpression functionCallExpression = (ICPPASTFunctionCallExpression) expression; + IASTExpression functionNameExpression = functionCallExpression.getFunctionNameExpression(); + if(functionNameExpression instanceof IASTIdExpression) { + IASTIdExpression idExpression = (IASTIdExpression) functionNameExpression; + if(idExpression.getName() != null) { + addTempName(idExpression.getName().getLastName().toString()); + if(guessedTempNames.size() > 0) { + return PROCESS_ABORT; + } + } + } + } + if (expression instanceof CPPASTLiteralExpression) { CPPASTLiteralExpression literal = (CPPASTLiteralExpression)expression; String name = null;