From b6c0cb12db4b6038a48a0455aae52c898680e8cb Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Mon, 1 Jul 2013 11:55:23 -0400 Subject: [PATCH] Bug 412032 - Extract local variable doesn't suggest a name in C source files Change-Id: I73e7f80713132521d757482c6ffbf839b5d2679f Signed-off-by: Marc-Andre Laperle Reviewed-on: https://git.eclipse.org/r/14168 Reviewed-by: Sergey Prigogin --- .../ExtractLocalVariableRefactoringTest.java | 34 ++++++++++++++++++- .../ExtractLocalVariableRefactoring.java | 14 ++++---- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractlocalvariable/ExtractLocalVariableRefactoringTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractlocalvariable/ExtractLocalVariableRefactoringTest.java index c3582ae8c3f..b483a2ed545 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractlocalvariable/ExtractLocalVariableRefactoringTest.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/refactoring/extractlocalvariable/ExtractLocalVariableRefactoringTest.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2012 Institute for Software, HSR Hochschule fuer Technik + * Copyright (c) 2008, 2013 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 @@ -9,6 +9,7 @@ * Contributors: * Institute for Software - initial API and implementation * Sergey Prigogin (Google) + * Marc-Andre Laperle (Ericsson) *******************************************************************************/ package org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable; @@ -537,4 +538,35 @@ public class ExtractLocalVariableRefactoringTest extends RefactoringTestBase { public void testLocalVariableFromForLoop() throws Exception { assertRefactoringSuccess(); } + + //main.c + //void f(){ + // int x = /*$*/2/*$$*/; + //} + //==================== + //void f(){ + // int i = 2; + // int x = i; + //} + public void testSuggestedNameCFile_Bug412032() throws Exception { + assertRefactoringSuccess(); + } + + //main.c + //int getSomething(int x) { return 0; } + // + //void f(){ + // /*$*/getSomething(getSomething(0))/*$$*/; + //} + //==================== + //int getSomething(int x) { return 0; } + // + //void f(){ + // int something = getSomething(getSomething(0)); + // something; + //} + public void testSuggestedNameCFile_Bug412032_2() throws Exception { + assertRefactoringSuccess(); + } + } 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 61a77465292..5c421c5bec4 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, 2012 Google and others. All rights reserved. This program and + * Copyright (c) 2008, 2013 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 @@ -7,6 +7,7 @@ * Contributors: * Tom Ball (Google) - Initial API and implementation * Sergey Prigogin (Google) + * Marc-Andre Laperle (Ericsson) *******************************************************************************/ package org.eclipse.cdt.internal.ui.refactoring.extractlocalvariable; @@ -37,6 +38,7 @@ import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer; 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.IASTFunctionCallExpression; import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition; import org.eclipse.cdt.core.dom.ast.IASTIdExpression; import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression; @@ -51,7 +53,6 @@ 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.ICElement; @@ -62,7 +63,6 @@ import org.eclipse.cdt.ui.PreferenceConstants; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTDeclarationStatement; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTEqualsInitializer; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; -import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunction; import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor; @@ -358,8 +358,8 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { 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; + if (expression == target && expression instanceof IASTFunctionCallExpression) { + IASTFunctionCallExpression functionCallExpression = (IASTFunctionCallExpression) expression; IASTExpression functionNameExpression = functionCallExpression.getFunctionNameExpression(); if (functionNameExpression instanceof IASTIdExpression) { IASTIdExpression idExpression = (IASTIdExpression) functionNameExpression; @@ -372,8 +372,8 @@ public class ExtractLocalVariableRefactoring extends CRefactoring { } } - if (expression instanceof CPPASTLiteralExpression) { - CPPASTLiteralExpression literal = (CPPASTLiteralExpression) expression; + if (expression instanceof IASTLiteralExpression) { + IASTLiteralExpression literal = (IASTLiteralExpression) expression; String name = null; switch (literal.getKind()) { case IASTLiteralExpression.lk_char_constant: