From 91afefb63181d5463ed81d568db85b4512520956 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Thu, 4 Dec 2008 10:21:02 +0000 Subject: [PATCH] Handle usage of 'void' for parameter type in method definition, bug 257376. --- .../core/parser/tests/ast2/AST2CPPTests.java | 9 +++ .../parser/cpp/semantics/CPPSemantics.java | 77 +++++++++---------- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java index 914dc250492..99a5cd71619 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPTests.java @@ -6237,4 +6237,13 @@ public class AST2CPPTests extends AST2BaseTest { assertProblemBinding(IProblemBinding.SEMANTIC_INVALID_REDECLARATION, nc.getName(6).resolveBinding()); assertProblemBinding(IProblemBinding.SEMANTIC_INVALID_REDEFINITION, nc.getName(8).resolveBinding()); } + + // struct Foo { + // void foo(); + // }; + // void Foo::foo(void) { + // } + public void testVoidParamInDefinition_257376() throws Exception { + parseAndCheckBindings(getAboveComment(), ParserLanguage.CPP); + } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java index fbe60386d28..6c86b5e4423 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPSemantics.java @@ -1854,14 +1854,25 @@ public class CPPSemantics { return false; } - static private void reduceToViable(LookupData data, IBinding[] functions) throws DOMException{ + static private void reduceToViable(LookupData data, IBinding[] functions) throws DOMException { if (functions == null || functions.length == 0) return; - Object[] fParams = data.functionParameters; - int numParameters = (fParams != null) ? fParams.length : 0; - int num; - boolean def = data.forFunctionDeclaration(); + final Object[] funcArgs = data.functionParameters; + int numArgs = (funcArgs != null) ? funcArgs.length : 0; + final boolean def = data.forFunctionDeclaration(); + + if (def && numArgs == 1) { + // check for parameter of type void + IType[] argTypes= getSourceParameterTypes(funcArgs); + if (argTypes.length == 1) { + IType t= argTypes[0]; + if (t instanceof IBasicType && ((IBasicType)t).getType() == IBasicType.t_void) { + numArgs= 0; + } + } + } + // Trim the list down to the set of viable functions IFunction function = null; int size = functions.length; @@ -1878,44 +1889,32 @@ public class CPPSemantics { continue; } - num = function.getParameters().length; + final IParameter[] params = function.getParameters(); + int numPars = params.length; + if (numArgs == 0 && numPars == 1) { + // check for void + IType t = params[0].getType(); + if (t instanceof IBasicType && ((IBasicType)t).getType() == IBasicType.t_void) + numPars= 0; + } - // if there are m arguments in the list, all candidate functions having m parameters - // are viable - if (num == numParameters) { - if (def && !isMatchingFunctionDeclaration(function, data)) { + if (def) { + if (numPars != numArgs || !isMatchingFunctionDeclaration(function, data)) { functions[i] = null; } - continue; - } else if (numParameters == 0 && num == 1) { - // check for void - IParameter param = function.getParameters()[0]; - IType t = param.getType(); - if (t instanceof IBasicType && ((IBasicType)t).getType() == IBasicType.t_void) - continue; - } - - if (def) { - // if this is for a definition, we had to match the number of parameters. - functions[i] = null; - continue; - } - - if (num < numParameters) { - // A candidate function having fewer than m parameters is viable only if it has an - // ellipsis in its parameter list. - if (function.takesVarArgs()) - continue; - // not enough parameters, remove it - functions[i] = null; } else { - //a candidate function having more than m parameters is viable only if the (m+1)-st - //parameter has a default argument - IParameter[] params = function.getParameters(); - for (int j = num - 1; j >= numParameters; j--) { - if (!((ICPPParameter) params[j]).hasDefaultValue()) { - functions[i] = null; - break; + // more arguments than parameters --> need ellipses + if (numArgs > numPars) { + if (!function.takesVarArgs()) { + functions[i] = null; + } + } else if (numArgs < numPars) { + // fewer arguments than parameters --> need default values + for (int j = numArgs; j < numPars; j++) { + if (!((ICPPParameter) params[j]).hasDefaultValue()) { + functions[i] = null; + break; + } } } }