1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 10:16:03 +02:00

Bug 319279. Passing parameter and return value by reference.

This commit is contained in:
Sergey Prigogin 2011-12-05 19:51:33 -08:00
parent 693a79de5e
commit 253ba9ddef
5 changed files with 107 additions and 282 deletions

View file

@ -14,7 +14,9 @@ import java.io.IOException;
import junit.framework.TestSuite;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.parser.util.ASTPrinter;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ClassTypeHelper;
import org.eclipse.cdt.internal.core.parser.ParserException;
@ -39,6 +41,13 @@ public class ClassTypeHelperTests extends AST2BaseTest {
return new BindingAssertionHelper(code, true);
}
// int a;
// const int& b;
public void testTemp() throws Exception {
BindingAssertionHelper helper = getAssertionHelper();
ASTPrinter.print(helper.getTranslationUnit());
}
// struct A {
// A(const A& a);
// };

View file

@ -52,10 +52,9 @@ public class TypeHelper {
}
SizeofCalculator calc = ((ASTTranslationUnit) ast).getSizeofCalculator();
SizeAndAlignment sizeofPointer = calc.sizeAndAlignmentOfPointer();
if (sizeofPointer == null)
return true;
long maxSize = sizeofPointer != null ? sizeofPointer.size : 4;
SizeAndAlignment sizeofType = calc.sizeAndAlignment(type);
if (sizeofType == null || sizeofType.size > sizeofPointer.size)
if (sizeofType == null || sizeofType.size > maxSize)
return true;
}
return false;

View file

@ -30,7 +30,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -70,7 +70,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -132,7 +132,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -177,7 +177,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -240,7 +240,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -281,7 +281,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -343,7 +343,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -384,7 +384,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -450,7 +450,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -499,7 +499,7 @@ public:
this->systemId = systemId;
}
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -704,53 +704,37 @@ class test {
#endif /* TEST_H_ */
//!Generate Getters and Setters One Getter Selection Separate Definition
//!Generate Getters and Setters, Pass by Reference, Separate Definition
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
//@.config
filename=C.h
getters=name
setters=name
definitionSeparate=true
//@C.cpp
#include "C.h"
int Person::SocSecNo() {
return socSecNo;
}
int main(int argc, char** argv) {
}
//=
#include "C.h"
char* Person::getName() const {
return name;
}
int Person::SocSecNo() {
return socSecNo;
}
int main(int argc, char** argv) {
}
//@C.h
#ifndef C_H_
#define C_H_
struct FullName {
const char* first;
const char* last;
FullName(const FullName& other);
~FullName();
};
class Person {
private:
int systemId;
protected:
char* name;
FullName name;
public:
const int socSecNo;
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -774,20 +758,28 @@ int gooo = 1;
#ifndef C_H_
#define C_H_
struct FullName {
const char* first;
const char* last;
FullName(const FullName& other);
~FullName();
};
class Person {
private:
int systemId;
protected:
char* name;
FullName name;
public:
const int socSecNo;
Person myFriend;
char* getName() const;
const FullName& getName() const;
void setName(const FullName& name);
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -807,6 +799,34 @@ public:
int gooo = 1;
#endif /* C_H_ */
//@C.cpp
#include "C.h"
int Person::SocSecNo() {
return socSecNo;
}
int main(int argc, char** argv) {
}
//=
#include "C.h"
const FullName& Person::getName() const {
return name;
}
void Person::setName(const FullName& name) {
this->name = name;
}
int Person::SocSecNo() {
return socSecNo;
}
int main(int argc, char** argv) {
}
//!Generate Getters and Setters One Getter Selection with Namespace Separate Definition
//#org.eclipse.cdt.ui.tests.refactoring.gettersandsetters.GenerateGettersAndSettersTest
//@.config
@ -863,7 +883,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -908,7 +928,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -984,7 +1004,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -1025,7 +1045,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -1102,7 +1122,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor
@ -1144,7 +1164,7 @@ public:
Person myFriend;
Person(int socSecNo); // contructor
Person(int socSecNo); // constructor
~Person(); // destructor

View file

@ -1,203 +0,0 @@
/*******************************************************************************
* Copyright (c) 2008, 2011 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
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Institute for Software - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/
package org.eclipse.cdt.internal.ui.refactoring.gettersandsetters;
import java.util.Arrays;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode.CopyStyle;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTBinaryExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTCompoundStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTExpressionStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFieldReference;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTFunctionDeclarator;
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.CPPASTLiteralExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTParameterDeclaration;
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.CPPASTSimpleDeclaration;
public class FunctionFactory {
public static IASTFunctionDefinition createGetterDefinition(IASTName fieldName,
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
IASTFunctionDefinition getter = new CPPASTFunctionDefinition();
getter.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy(CopyStyle.withLocations));
IASTDeclarator getterDeclarator = getGetterDeclarator(fieldName, fieldDeclaration, name);
// IASTFunctionDefinition. expects the outermost IASTFunctionDeclarator in declarator hierarchy
while (!(getterDeclarator instanceof IASTFunctionDeclarator)) {
getterDeclarator = getterDeclarator.getNestedDeclarator();
}
getter.setDeclarator((IASTFunctionDeclarator) getterDeclarator);
getter.setBody(getGetterBody(fieldName));
return getter;
}
private static CPPASTCompoundStatement getGetterBody(IASTName fieldName) {
CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
CPPASTReturnStatement returnStatement = new CPPASTReturnStatement();
CPPASTIdExpression idExpr = new CPPASTIdExpression();
CPPASTName returnVal = new CPPASTName();
returnVal.setName(fieldName.toCharArray());
idExpr.setName(returnVal);
returnStatement.setReturnValue(idExpr);
compound.addStatement(returnStatement);
return compound;
}
private static IASTDeclarator getGetterDeclarator(IASTName fieldName,
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
CPPASTName getterName = new CPPASTName();
getterName.setName(GetterSetterNameGenerator.generateGetterName(fieldName).toCharArray());
// copy declarator hierarchy
IASTDeclarator topDeclarator = fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations);
// find the innermost declarator in hierarchy
IASTDeclarator innermost = topDeclarator;
while (innermost.getNestedDeclarator() != null) {
innermost = innermost.getNestedDeclarator();
}
// create a new innermost function declarator basing on the field declarator
CPPASTFunctionDeclarator functionDeclarator = new CPPASTFunctionDeclarator();
functionDeclarator.setConst(true);
if (name != null) {
name.addName(getterName);
functionDeclarator.setName(name);
} else {
functionDeclarator.setName(getterName);
}
for (IASTPointerOperator pointer : innermost.getPointerOperators()){
functionDeclarator.addPointerOperator(pointer.copy(CopyStyle.withLocations));
}
// replace innermost with functionDeclarator and return the whole declarator tree
if (innermost == topDeclarator) {
// no tree
return functionDeclarator;
} else {
IASTDeclarator parent = (IASTDeclarator) innermost.getParent();
parent.setNestedDeclarator(functionDeclarator);
return topDeclarator;
}
}
public static IASTFunctionDefinition createSetterDefinition(IASTName fieldName,
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
IASTFunctionDefinition setter = new CPPASTFunctionDefinition();
setter.setDeclSpecifier(getVoidDeclSpec());
setter.setDeclarator(getSetterDeclarator(fieldName, fieldDeclaration, name));
setter.setBody(getSetterBody(fieldDeclaration));
return setter;
}
private static CPPASTCompoundStatement getSetterBody(IASTSimpleDeclaration fieldDeclaration) {
CPPASTCompoundStatement compound = new CPPASTCompoundStatement();
CPPASTExpressionStatement exprStmt = new CPPASTExpressionStatement();
CPPASTBinaryExpression binExpr = new CPPASTBinaryExpression();
IASTDeclarator innerDeclarator = fieldDeclaration.getDeclarators()[0];
while (innerDeclarator.getNestedDeclarator() != null) {
innerDeclarator = innerDeclarator.getNestedDeclarator();
}
IASTName fieldName = innerDeclarator.getName();
CPPASTName parameterName = getSetterParameterName(fieldName);
if (Arrays.equals(fieldName.getSimpleID(), parameterName.getSimpleID())) {
CPPASTFieldReference fieldRef = new CPPASTFieldReference();
CPPASTLiteralExpression litExpr = new CPPASTLiteralExpression();
litExpr.setValue(Keywords.cTHIS);
fieldRef.setFieldOwner(litExpr);
fieldRef.setIsPointerDereference(true);
fieldRef.setFieldName(fieldName.copy(CopyStyle.withLocations));
binExpr.setOperand1(fieldRef);
} else {
CPPASTIdExpression idExpr = new CPPASTIdExpression(fieldName.copy(CopyStyle.withLocations));
binExpr.setOperand1(idExpr);
}
binExpr.setOperator(IASTBinaryExpression.op_assign);
CPPASTIdExpression idExpr = new CPPASTIdExpression(parameterName);
binExpr.setOperand2(idExpr);
exprStmt.setExpression(binExpr);
compound.addStatement(exprStmt);
return compound;
}
private static CPPASTFunctionDeclarator getSetterDeclarator(IASTName fieldName,
IASTSimpleDeclaration fieldDeclaration, ICPPASTQualifiedName name) {
CPPASTName setterName = new CPPASTName();
setterName.setName(GetterSetterNameGenerator.generateSetterName(fieldName).toCharArray());
CPPASTFunctionDeclarator declarator = new CPPASTFunctionDeclarator();
if (name != null) {
name.addName(setterName);
declarator.setName(name);
} else {
declarator.setName(setterName);
}
CPPASTParameterDeclaration parameterDeclaration = new CPPASTParameterDeclaration();
IASTDeclarator parameterDeclarator = fieldDeclaration.getDeclarators()[0].copy(CopyStyle.withLocations);
parameterDeclarator.setName(getSetterParameterName(fieldName));
parameterDeclaration.setDeclarator(parameterDeclarator);
parameterDeclaration.setDeclSpecifier(fieldDeclaration.getDeclSpecifier().copy(
CopyStyle.withLocations));
declarator.addParameterDeclaration(parameterDeclaration.copy(CopyStyle.withLocations));
return declarator;
}
private static CPPASTName getSetterParameterName(IASTName fieldName) {
String parameterName = GetterSetterNameGenerator.generateSetterParameterName(fieldName);
return new CPPASTName(parameterName.toCharArray());
}
private static CPPASTSimpleDeclSpecifier getVoidDeclSpec() {
CPPASTSimpleDeclSpecifier declSpecifier = new CPPASTSimpleDeclSpecifier();
declSpecifier.setType(IASTSimpleDeclSpecifier.t_void);
return declSpecifier;
}
public static IASTSimpleDeclaration createGetterDeclaration(IASTName fieldName,
IASTSimpleDeclaration fieldDeclaration) {
IASTSimpleDeclaration getter = new CPPASTSimpleDeclaration();
IASTDeclSpecifier declSpec = fieldDeclaration.getDeclSpecifier();
getter.setDeclSpecifier(declSpec.copy(CopyStyle.withLocations));
// TODO(sprigogin): Implement return by reference
// IType type = CPPVisitor.createType(declSpec);
// if (TypeHelper.shouldBePassedByReference(type, fieldDeclaration.getTranslationUnit())) {
// declSpec.s
// }
getter.addDeclarator(getGetterDeclarator(fieldName, fieldDeclaration, null));
return getter;
}
public static IASTSimpleDeclaration createSetterDeclaration(IASTName fieldName,
IASTSimpleDeclaration fieldDeclaration) {
IASTSimpleDeclaration setter = new CPPASTSimpleDeclaration();
setter.setDeclSpecifier(getVoidDeclSpec());
setter.addDeclarator(getSetterDeclarator(fieldName, fieldDeclaration, null));
return setter;
}
}

View file

@ -31,14 +31,14 @@ public class GetterSetterInsertEditProvider implements Comparable<GetterSetterIn
private IASTSimpleDeclaration functionDeclaration;
private AccessorKind kind;
private IASTName fieldName;
private IASTSimpleDeclaration fieldDeclaration;
private GetterSetterFactory getterSetterFactory;
public GetterSetterInsertEditProvider(IASTName fieldName, IASTSimpleDeclaration fieldDeclaration,
AccessorKind kind) {
this.kind = kind;
this.fieldName = fieldName;
this.fieldDeclaration = fieldDeclaration;
this.getterSetterFactory = new GetterSetterFactory(fieldName, fieldDeclaration);
createFunctionDeclaration();
}
@ -46,10 +46,10 @@ public class GetterSetterInsertEditProvider implements Comparable<GetterSetterIn
public void createFunctionDeclaration() {
switch (this.kind) {
case GETTER:
this.functionDeclaration = FunctionFactory.createGetterDeclaration(fieldName, fieldDeclaration);
this.functionDeclaration = getterSetterFactory.createGetterDeclaration();
break;
case SETTER:
this.functionDeclaration = FunctionFactory.createSetterDeclaration(fieldName, fieldDeclaration);
this.functionDeclaration = getterSetterFactory.createSetterDeclaration();
break;
}
}
@ -67,23 +67,23 @@ public class GetterSetterInsertEditProvider implements Comparable<GetterSetterIn
IASTFunctionDefinition definition = null;
ICPPASTQualifiedName qname;
if (qualifedName) {
qname = getClassname();
qname = getClassName();
} else {
qname = null;
}
switch (kind) {
case GETTER:
definition = FunctionFactory.createGetterDefinition(fieldName, fieldDeclaration, qname);
definition = getterSetterFactory.createGetterDefinition(qname);
break;
case SETTER:
definition = FunctionFactory.createSetterDefinition(fieldName, fieldDeclaration, qname);
definition = getterSetterFactory.createSetterDefinition(qname);
break;
}
return definition;
}
private ICPPASTQualifiedName getClassname() {
private ICPPASTQualifiedName getClassName() {
IASTNode node = fieldDeclaration.getParent();
while (!(node instanceof IASTCompositeTypeSpecifier)) {
node = node.getParent();