1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-12 10:45:37 +02:00

fix for bug 241717: Extract Function not working with Counted Ptr

https://bugs.eclipse.org/bugs/show_bug.cgi?id=241717
This commit is contained in:
Emanuel Graf 2008-07-23 09:22:22 +00:00
parent 16b1c0a53c
commit 95e813e03a
2 changed files with 130 additions and 43 deletions

View file

@ -2146,3 +2146,83 @@ int A::foo(int& a)
exp(a, b, c); exp(a, b, c);
return a; return a;
} }
//!Bug 241717: Typdef causes void as return type
//#org.eclipse.cdt.ui.tests.refactoring.extractfunction.ExtractFunctionRefactoringTest
//@.config
filename=Test.cpp
//@Test.h
#ifndef TEST_H_
#define TEST_H_
class RetValueType{
};
typedef RetValueType RetType;
class Test {
public:
Test();
virtual ~Test();
private:
void test();
};
#endif /* TEST_H_ */
//=
#ifndef TEST_H_
#define TEST_H_
class RetValueType{
};
typedef RetValueType RetType;
class Test {
public:
Test();
virtual ~Test();
private:
void test();
RetType exp();
};
#endif /* TEST_H_ */
//@Test.cpp
#include "Test.h"
Test::Test() {
}
Test::~Test() {
}
void Test::test()
{
RetType v = //$RetType()$//;
}
//=
#include "Test.h"
Test::Test() {
}
Test::~Test() {
}
RetType Test::exp()
{
return RetType();
}
void Test::test()
{
RetType v = exp();
}

View file

@ -25,16 +25,19 @@ 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.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;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.rewrite.ASTRewrite; import org.eclipse.cdt.core.dom.rewrite.ASTRewrite;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFieldReference; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFieldReference;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDefinition; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDefinition;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression; 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.CPPASTLiteralExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNamedTypeSpecifier; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNamedTypeSpecifier;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReturnStatement; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTReturnStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier; import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTSimpleDeclSpecifier;
@ -152,7 +155,7 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
return declSpec; return declSpec;
} }
private static CPPFunction findCalledFunction(IASTFunctionCallExpression callExpression) { private static IASTName findCalledFunctionName(IASTFunctionCallExpression callExpression) {
IASTExpression functionNameExpression = callExpression.getFunctionNameExpression(); IASTExpression functionNameExpression = callExpression.getFunctionNameExpression();
IASTName functionName = null; IASTName functionName = null;
@ -162,39 +165,38 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
} else if(functionNameExpression instanceof CPPASTFieldReference) { } else if(functionNameExpression instanceof CPPASTFieldReference) {
CPPASTFieldReference fieldReference = (CPPASTFieldReference) functionNameExpression; CPPASTFieldReference fieldReference = (CPPASTFieldReference) functionNameExpression;
functionName = fieldReference.getFieldName(); functionName = fieldReference.getFieldName();
} else { }
return null; return functionName;
}
if (functionName.resolveBinding() instanceof CPPFunction) {
return (CPPFunction) functionName.resolveBinding();
}
return null;
} }
private static IASTDeclSpecifier handleFunctionCallExpression(IASTFunctionCallExpression callExpression) { private static IASTDeclSpecifier handleFunctionCallExpression(IASTFunctionCallExpression callExpression) {
CPPFunction function = findCalledFunction(callExpression); IASTName functionName = findCalledFunctionName(callExpression);
if(functionName != null) {
if (function != null) { IBinding binding = functionName.resolveBinding();
if(function.getDefinition() != null) { if (binding instanceof CPPFunction) {
IASTNode parent = function.getDefinition().getParent(); CPPFunction function = (CPPFunction) binding;
if(parent instanceof CPPASTFunctionDefinition) { if(function.getDefinition() != null) {
CPPASTFunctionDefinition definition = (CPPASTFunctionDefinition) parent; IASTNode parent = function.getDefinition().getParent();
return definition.getDeclSpecifier(); if(parent instanceof CPPASTFunctionDefinition) {
} CPPASTFunctionDefinition definition = (CPPASTFunctionDefinition) parent;
} else if(hasDeclaration(function)) { return definition.getDeclSpecifier();
IASTNode parent = function.getDeclarations()[0].getParent(); }
if (parent instanceof CPPASTSimpleDeclaration) { } else if(hasDeclaration(function)) {
CPPASTSimpleDeclaration declaration = (CPPASTSimpleDeclaration) parent; IASTNode parent = function.getDeclarations()[0].getParent();
return declaration.getDeclSpecifier(); if (parent instanceof CPPASTSimpleDeclaration) {
CPPASTSimpleDeclaration declaration = (CPPASTSimpleDeclaration) parent;
return declaration.getDeclSpecifier();
}
} }
}else if(binding instanceof CPPTypedef) {
CPPTypedef typedef = (CPPTypedef) binding;
return new CPPASTNamedTypeSpecifier(new CPPASTName(typedef.getNameCharArray()), false);
} }
}
}
return null; return null;
} }
@Override @Override
protected boolean isReturnTypeAPointer(IASTNode node) { protected boolean isReturnTypeAPointer(IASTNode node) {
if(node instanceof ICPPASTNewExpression) { if(node instanceof ICPPASTNewExpression) {
@ -202,25 +204,30 @@ public class ExtractExpression extends ExtractedFunctionConstructionHelper {
} else if(!(node instanceof IASTFunctionCallExpression)) { } else if(!(node instanceof IASTFunctionCallExpression)) {
return false; return false;
} }
CPPFunction function = findCalledFunction((IASTFunctionCallExpression) node); IASTName functionName = findCalledFunctionName((IASTFunctionCallExpression) node);
if(functionName != null) {
if (function != null) { IBinding binding = functionName.resolveBinding();
if(function.getDefinition() != null) { if (binding instanceof CPPFunction) {
IASTNode parent = function.getDefinition().getParent(); CPPFunction function = (CPPFunction) binding;
if(parent instanceof CPPASTFunctionDefinition) {
CPPASTFunctionDefinition definition = (CPPASTFunctionDefinition) parent; if (function != null) {
return definition.getDeclarator().getPointerOperators().length > 0; if(function.getDefinition() != null) {
} IASTNode parent = function.getDefinition().getParent();
} else if(hasDeclaration(function)) { if(parent instanceof CPPASTFunctionDefinition) {
IASTNode parent = function.getDeclarations()[0].getParent(); CPPASTFunctionDefinition definition = (CPPASTFunctionDefinition) parent;
if (parent instanceof CPPASTSimpleDeclaration) { return definition.getDeclarator().getPointerOperators().length > 0;
CPPASTSimpleDeclaration declaration = (CPPASTSimpleDeclaration) parent; }
return declaration.getDeclarators().length > 0 && declaration.getDeclarators()[0].getPointerOperators().length > 0; } else if(hasDeclaration(function)) {
IASTNode parent = function.getDeclarations()[0].getParent();
if (parent instanceof CPPASTSimpleDeclaration) {
CPPASTSimpleDeclaration declaration = (CPPASTSimpleDeclaration) parent;
return declaration.getDeclarators().length > 0 && declaration.getDeclarators()[0].getPointerOperators().length > 0;
}
}
} }
} }
} }
return false; return false;
} }