1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-07 09:46:02 +02:00

Bug 45203. Added a test.

This commit is contained in:
Sergey Prigogin 2013-08-05 11:47:30 -07:00
parent a9914d3620
commit 44dab709d9
2 changed files with 41 additions and 29 deletions

View file

@ -208,13 +208,31 @@ public class BindingClassifierTest extends OneSourceMultipleHeadersTestCase {
// void test() { // void test() {
// f(""); // f("");
// } // }
public void testFunctionCallWithTypeConversion() throws Exception { public void testFunctionCallWithTypeConversion_1() throws Exception {
IPreferenceStore preferenceStore = getPreferenceStore(); IPreferenceStore preferenceStore = getPreferenceStore();
preferenceStore.setValue(PreferenceConstants.FORWARD_DECLARE_FUNCTIONS, true); preferenceStore.setValue(PreferenceConstants.FORWARD_DECLARE_FUNCTIONS, true);
// A header declaring the function is responsible for defining the parameter type that
// provides constructor that can be used for implicit conversion.
assertDefined(); assertDefined();
assertDeclared("f"); assertDeclared("f");
} }
// struct A {};
// struct B { operator A(); };
// void f(A p);
// void test(B b) {
// f(b);
// }
public void testFunctionCallWithTypeConversion_2() throws Exception {
IPreferenceStore preferenceStore = getPreferenceStore();
preferenceStore.setValue(PreferenceConstants.FORWARD_DECLARE_FUNCTIONS, true);
// A header declaring the function is not responsible for defining the parameter type since
// the implicit conversion from B to A is provided externally to parameter type.
assertDefined("A", "B");
assertDeclared("f");
}
// struct A {}; // struct A {};
// struct B {}; // struct B {};

View file

@ -162,18 +162,22 @@ public class BindingClassifier {
IParameter[] parameters = function.getParameters(); IParameter[] parameters = function.getParameters();
for (int i = 0; i < parameters.length; i++) { for (int i = 0; i < parameters.length; i++) {
IType parameterType = parameters[i].getType(); IType parameterType = parameters[i].getType();
IType argumentType = null; parameterType = getNestedType(parameterType, REF | ALLCVQ);
IASTInitializerClause argument = null;
boolean canBeDeclared = false; boolean canBeDeclared = false;
if (parameterType instanceof IPointerType || parameterType instanceof ICPPReferenceType) { if (i >= arguments.length) {
// The declared parameter type is a pointer or reference type. A declaration is // This is a default value parameter. The function call itself doesn't need
// sufficient if it matches the actual parameter type. // a definition of this parameter type.
parameterType = getNestedType(parameterType, REF | ALLCVQ); canBeDeclared = true;
if (i < arguments.length) { } else {
// This argument is present within the function call expression. // This argument is present within the function call expression.
// It's therefore not a default parameter. // It's therefore not a default parameter.
IASTInitializerClause argument = arguments[i]; argument = arguments[i];
if (parameterType instanceof IPointerType || parameterType instanceof ICPPReferenceType) {
// The declared parameter type is a pointer or reference type. A declaration is
// sufficient if it matches the actual parameter type.
if (argument instanceof IASTExpression) { if (argument instanceof IASTExpression) {
argumentType = ((IASTExpression) argument).getExpressionType(); IType argumentType = ((IASTExpression) argument).getExpressionType();
argumentType = getNestedType(argumentType, REF | ALLCVQ); argumentType = getNestedType(argumentType, REF | ALLCVQ);
if (parameterType instanceof IPointerType && argumentType instanceof IPointerType) { if (parameterType instanceof IPointerType && argumentType instanceof IPointerType) {
@ -184,10 +188,6 @@ public class BindingClassifier {
canBeDeclared = true; canBeDeclared = true;
} }
} }
} else {
// This is a default value parameter. The function call itself doesn't need
// a definition of this parameter type.
canBeDeclared = true;
} }
} }
@ -196,23 +196,17 @@ public class BindingClassifier {
// because this type doesn't appear within the AST. // because this type doesn't appear within the AST.
declareType(parameterType); declareType(parameterType);
} else { } else {
parameterType = getNestedType(parameterType, REF | ALLCVQ); assert argument != null;
if (i < arguments.length) { if (argument instanceof IASTExpression) {
// This argument is present within the function call expression. IType argumentType = ((IASTExpression) argument).getExpressionType();
// It's therefore not a default parameter. // The type of the argument requires a full definition.
IASTInitializerClause argument = arguments[i]; defineTypeExceptTypedefOrNonFixedEnum(argumentType);
if (argument instanceof IASTExpression) {
argumentType = ((IASTExpression) argument).getExpressionType();
// The type of the argument requires a full definition.
defineTypeExceptTypedefOrNonFixedEnum(argumentType);
}
} }
// As a matter of policy, a header declaring the function is responsible for // As a matter of policy, a header declaring the function is responsible for
// defining parameter types that allow implicit conversion. // defining parameter types that allow implicit conversion.
if (i >= arguments.length || if (!(parameterType instanceof ICPPClassType) ||
!(parameterType instanceof ICPPClassType) ||
fAst.getDeclarationsInAST(function).length != 0 || fAst.getDeclarationsInAST(function).length != 0 ||
!hasConvertingConstructor((ICPPClassType) parameterType, arguments[i])) { !hasConvertingConstructor((ICPPClassType) parameterType, argument)) {
defineTypeExceptTypedefOrNonFixedEnum(parameterType); defineTypeExceptTypedefOrNonFixedEnum(parameterType);
} }
} }
@ -236,7 +230,7 @@ public class BindingClassifier {
lookupData.qualified = true; lookupData.qualified = true;
try { try {
IBinding constructor = CPPSemantics.resolveFunction(lookupData, ClassTypeHelper.getConstructors(classType, argument), false); IBinding constructor = CPPSemantics.resolveFunction(lookupData, ClassTypeHelper.getConstructors(classType, argument), false);
if (constructor instanceof ICPPConstructor) if (constructor instanceof ICPPConstructor && !((ICPPConstructor) constructor).isExplicit())
return true; return true;
} catch (DOMException e) { } catch (DOMException e) {
} }