mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-23 14:42:11 +02:00
Bug 487186 - Extraction of template type with function type argument
- Changed DeclarationGeneratorImpl to also consider the declarator for template arguments instead of only the decl specifier. - Adapted DeclarationGeneratorImpl to not add abstract pointer .declarator for function type template arguments. - Test case. Change-Id: Iedec3e62f8234495003b74bfb463979e37c90bda Signed-off-by: Thomas Corbat <tcorbat@hsr.ch>
This commit is contained in:
parent
c81aef4e42
commit
6b8482f866
2 changed files with 47 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2010, 2014 Tomasz Wesolowski
|
* Copyright (c) 2010, 2016 Tomasz Wesolowski
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
|
@ -7,6 +7,7 @@
|
||||||
*
|
*
|
||||||
* Contributors:
|
* Contributors:
|
||||||
* Tomasz Wesolowski - initial API and implementation
|
* Tomasz Wesolowski - initial API and implementation
|
||||||
|
* Thomas Corbat (IFS)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.internal.core.dom.rewrite;
|
package org.eclipse.cdt.internal.core.dom.rewrite;
|
||||||
|
|
||||||
|
@ -51,6 +52,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
|
||||||
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
|
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
|
||||||
import org.eclipse.cdt.core.dom.rewrite.DeclarationGenerator;
|
import org.eclipse.cdt.core.dom.rewrite.DeclarationGenerator;
|
||||||
|
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
|
||||||
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
|
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -126,6 +128,10 @@ public class DeclarationGeneratorImpl extends DeclarationGenerator {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IASTDeclarator createDeclaratorFromType(IType type, char[] name) {
|
public IASTDeclarator createDeclaratorFromType(IType type, char[] name) {
|
||||||
|
return createDeclaratorFromType(type, name, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public IASTDeclarator createDeclaratorFromType(IType type, char[] name, boolean changeInitialFunctionToFuncPtr) {
|
||||||
IASTDeclarator returnedDeclarator = null;
|
IASTDeclarator returnedDeclarator = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -143,14 +149,14 @@ public class DeclarationGeneratorImpl extends DeclarationGenerator {
|
||||||
// If the type is a function, create a declaration of a pointer to this function
|
// If the type is a function, create a declaration of a pointer to this function
|
||||||
// (shorthand notation for function address).
|
// (shorthand notation for function address).
|
||||||
|
|
||||||
boolean changeInitialFunctionToFuncPtr = true;
|
boolean changeNextFunctionToFuncPtr = changeInitialFunctionToFuncPtr;
|
||||||
|
|
||||||
while (typeNeedsNontrivialDeclarator(type)) {
|
while (typeNeedsNontrivialDeclarator(type)) {
|
||||||
if (replaceInitialArrayWithPointer && type instanceof IArrayType) {
|
if (replaceInitialArrayWithPointer && type instanceof IArrayType) {
|
||||||
returnedDeclarator = factory.newDeclarator(newName);
|
returnedDeclarator = factory.newDeclarator(newName);
|
||||||
returnedDeclarator.addPointerOperator(factory.newPointer());
|
returnedDeclarator.addPointerOperator(factory.newPointer());
|
||||||
type = ((IArrayType) type).getType();
|
type = ((IArrayType) type).getType();
|
||||||
} else if (changeInitialFunctionToFuncPtr && type instanceof IFunctionType) {
|
} else if (changeNextFunctionToFuncPtr && type instanceof IFunctionType) {
|
||||||
returnedDeclarator = factory.newDeclarator(newName);
|
returnedDeclarator = factory.newDeclarator(newName);
|
||||||
returnedDeclarator.addPointerOperator(factory.newPointer());
|
returnedDeclarator.addPointerOperator(factory.newPointer());
|
||||||
// Leave type as it is, next iteration will handle the function.
|
// Leave type as it is, next iteration will handle the function.
|
||||||
|
@ -204,7 +210,7 @@ public class DeclarationGeneratorImpl extends DeclarationGenerator {
|
||||||
type = funcType.getReturnType();
|
type = funcType.getReturnType();
|
||||||
}
|
}
|
||||||
replaceInitialArrayWithPointer = false;
|
replaceInitialArrayWithPointer = false;
|
||||||
changeInitialFunctionToFuncPtr = false;
|
changeNextFunctionToFuncPtr = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
finalizePointerOperators(pointerOperatorMap);
|
finalizePointerOperators(pointerOperatorMap);
|
||||||
|
@ -313,14 +319,26 @@ public class DeclarationGeneratorImpl extends DeclarationGenerator {
|
||||||
ICPPNodeFactory cppFactory = (ICPPNodeFactory) factory;
|
ICPPNodeFactory cppFactory = (ICPPNodeFactory) factory;
|
||||||
ICPPASTTemplateId tempId = cppFactory.newTemplateId(templateName.copy(CopyStyle.withLocations));
|
ICPPASTTemplateId tempId = cppFactory.newTemplateId(templateName.copy(CopyStyle.withLocations));
|
||||||
for (ICPPTemplateArgument arg : type.getTemplateArguments()) {
|
for (ICPPTemplateArgument arg : type.getTemplateArguments()) {
|
||||||
IASTDeclSpecifier argDeclSpec = createDeclSpecFromType(arg.isTypeValue() ?
|
IASTTypeId typeId = getTypeIdForTemplateArgument(arg);
|
||||||
arg.getTypeValue() : arg.getTypeOfNonTypeValue());
|
|
||||||
IASTTypeId typeId = cppFactory.newTypeId(argDeclSpec, null);
|
|
||||||
tempId.addTemplateArgument(typeId);
|
tempId.addTemplateArgument(typeId);
|
||||||
}
|
}
|
||||||
return tempId;
|
return tempId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private IASTTypeId getTypeIdForTemplateArgument(ICPPTemplateArgument argument) {
|
||||||
|
IASTDeclSpecifier argDeclSpec;
|
||||||
|
IASTDeclarator typeDeclarator = null;
|
||||||
|
if (argument.isTypeValue()) {
|
||||||
|
IType argumentType = argument.getTypeValue();
|
||||||
|
argDeclSpec = createDeclSpecFromType(argumentType);
|
||||||
|
typeDeclarator = createDeclaratorFromType(argumentType, CharArrayUtils.EMPTY, false);
|
||||||
|
} else {
|
||||||
|
argDeclSpec = createDeclSpecFromType(argument.getTypeOfNonTypeValue());
|
||||||
|
}
|
||||||
|
IASTTypeId typeId = factory.newTypeId(argDeclSpec, typeDeclarator);
|
||||||
|
return typeId;
|
||||||
|
}
|
||||||
|
|
||||||
private IASTNamedTypeSpecifier getDeclSpecForBinding(IBinding binding) {
|
private IASTNamedTypeSpecifier getDeclSpecForBinding(IBinding binding) {
|
||||||
IASTName name = getName(binding);
|
IASTName name = getName(binding);
|
||||||
return factory.newTypedefNameSpecifier(name);
|
return factory.newTypedefNameSpecifier(name);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* Copyright (c) 2008, 2013 Institute for Software, HSR Hochschule fuer Technik
|
* Copyright (c) 2008, 2016 Institute for Software, HSR Hochschule fuer Technik
|
||||||
* Rapperswil, University of applied sciences and others
|
* Rapperswil, University of applied sciences and others
|
||||||
* All rights reserved. This program and the accompanying materials
|
* All rights reserved. This program and the accompanying materials
|
||||||
* are made available under the terms of the Eclipse Public License v1.0
|
* are made available under the terms of the Eclipse Public License v1.0
|
||||||
|
@ -10,6 +10,7 @@
|
||||||
* Institute for Software - initial API and implementation
|
* Institute for Software - initial API and implementation
|
||||||
* Sergey Prigogin (Google)
|
* Sergey Prigogin (Google)
|
||||||
* Marc-Andre Laperle (Ericsson)
|
* Marc-Andre Laperle (Ericsson)
|
||||||
|
* Thomas Corbat (IFS)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
package org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable;
|
package org.eclipse.cdt.ui.tests.refactoring.extractlocalvariable;
|
||||||
|
|
||||||
|
@ -571,4 +572,24 @@ public class ExtractLocalVariableRefactoringTest extends RefactoringTestBase {
|
||||||
assertRefactoringSuccess();
|
assertRefactoringSuccess();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//A.cpp
|
||||||
|
//template<typename T>
|
||||||
|
//struct Tpl {};
|
||||||
|
//
|
||||||
|
//void func() {
|
||||||
|
// Tpl<int(const char*, long int (*)(float))> t;
|
||||||
|
// /*$*/t/*$$*/;
|
||||||
|
//}
|
||||||
|
//====================
|
||||||
|
//template<typename T>
|
||||||
|
//struct Tpl {};
|
||||||
|
//
|
||||||
|
//void func() {
|
||||||
|
// Tpl<int(const char*, long int (*)(float))> t;
|
||||||
|
// Tpl<int(const char*, long int (*)(float))> t0 = t;
|
||||||
|
// t0;
|
||||||
|
//}
|
||||||
|
public void testTemplateWithFunctionArgument_487186() throws Exception {
|
||||||
|
assertRefactoringSuccess();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue