From eba71c12979a71f39e43fe34e52a28faa258b043 Mon Sep 17 00:00:00 2001 From: Markus Schorn Date: Wed, 9 Jul 2008 09:13:48 +0000 Subject: [PATCH] Adjustment of parameter types, bug 239975. --- .../parser/tests/ast2/AST2CPPSpecTest.java | 3 +- .../core/parser/tests/ast2/AST2CPPTests.java | 15 +++++ .../tests/IndexCPPBindingResolutionBugs.java | 17 ++++++ .../parser/cpp/semantics/CPPSemantics.java | 59 +++++++++---------- .../dom/parser/cpp/semantics/CPPVisitor.java | 20 ++++--- 5 files changed, 74 insertions(+), 40 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPSpecTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPSpecTest.java index b6778113772..3841778dbd7 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPSpecTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2CPPSpecTest.java @@ -3943,7 +3943,8 @@ public class AST2CPPSpecTest extends AST2SpecBaseTest { // int f (int) { } // definition of f(int) // int f (cInt) { } // error: redefinition of f(int) public void test12_8s3e() throws Exception { - parse(getAboveComment(), ParserLanguage.CPP, true, 0); + String[] problems= {"f"}; + parse(getAboveComment(), ParserLanguage.CPP, problems); } // void f (int i, int j); 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 0739613ba2a..266d5bf331f 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 @@ -5938,4 +5938,19 @@ public class AST2CPPTests extends AST2BaseTest { parseAndCheckBindings(getAboveComment(), ParserLanguage.CPP); } + + // typedef char t[12]; + // void test1(char *); + // void test2(char []); + // void test3(t); + // void xx() { + // char* x= 0; + // test1(x); + // test2(x); // problem binding here + // test3(x); // problem binding here + // } + public void testAdjustmentOfParameterTypes_Bug239975() throws Exception { + parseAndCheckBindings(getAboveComment(), ParserLanguage.CPP); + } + } diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java index 020bcc86f7c..70c9a9370f7 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCPPBindingResolutionBugs.java @@ -847,4 +847,21 @@ public class IndexCPPBindingResolutionBugs extends IndexBindingResolutionTestBas assertTrue(bar_c_ft.isConst()); assertTrue(!bar_c_ft.isVolatile()); assertTrue(!bar_ft.isConst()); assertTrue(!bar_ft.isVolatile()); } + + // typedef char t[12]; + // void test1(char *); + // void test2(char []); + // void test3(t); + + // void xx() { + // char* x= 0; + // test1(x); + // test2(x); // problem binding here + // test3(x); // problem binding here + // } + public void testAdjustmentOfParameterTypes_Bug239975() throws Exception { + getBindingFromASTName("test1(x)", 5, ICPPFunction.class); + getBindingFromASTName("test2(x)", 5, ICPPFunction.class); + getBindingFromASTName("test3(x)", 5, ICPPFunction.class); + } } 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 2ee72a16306..518319f909b 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 @@ -1927,40 +1927,35 @@ public class CPPSemantics { } static private IType[] getTargetParameterTypes(IFunction fn) throws DOMException{ - IParameter[] params = fn.getParameters(); - - boolean useImplicit = (fn instanceof ICPPMethod && !(fn instanceof ICPPConstructor)); - IType[] result = new IType[useImplicit ? params.length + 1 : params.length]; + final ICPPFunctionType ftype = (ICPPFunctionType) fn.getType(); + if (ftype == null) + return IType.EMPTY_TYPE_ARRAY; - if (useImplicit) { - ICPPFunctionType ftype = (ICPPFunctionType) ((ICPPFunction)fn).getType(); - if (ftype != null) { - IScope scope = fn.getScope(); - if (scope instanceof ICPPTemplateScope) - scope = scope.getParent(); - ICPPClassType cls = null; - if (scope instanceof ICPPClassScope) { - cls = ((ICPPClassScope)scope).getClassType(); - } else { - cls = new CPPClassType.CPPClassTypeProblem(ASTInternal.getPhysicalNodeOfScope(scope), IProblemBinding.SEMANTIC_BAD_SCOPE, fn.getNameCharArray()); - } - if (cls instanceof ICPPClassTemplate) { - IBinding within = CPPTemplates.instantiateWithinClassTemplate((ICPPClassTemplate) cls); - if (within instanceof ICPPClassType) - cls = (ICPPClassType)within; - } - IType implicitType = cls; - if (ftype.isConst() || ftype.isVolatile()) { - implicitType = new CPPQualifierType(implicitType, ftype.isConst(), ftype.isVolatile()); - } - implicitType = new CPPReferenceType(implicitType); - - result[0] = implicitType; - } + final IType[] ptypes= ftype.getParameterTypes(); + if (fn instanceof ICPPMethod == false || fn instanceof ICPPConstructor) + return ptypes; + + final IType[] result = new IType[ptypes.length + 1]; + System.arraycopy(ptypes, 0, result, 1, ptypes.length); + IScope scope = fn.getScope(); + if (scope instanceof ICPPTemplateScope) + scope = scope.getParent(); + ICPPClassType cls = null; + if (scope instanceof ICPPClassScope) { + cls = ((ICPPClassScope)scope).getClassType(); + } else { + cls = new CPPClassType.CPPClassTypeProblem(ASTInternal.getPhysicalNodeOfScope(scope), IProblemBinding.SEMANTIC_BAD_SCOPE, fn.getNameCharArray()); } - for (IParameter param : params) - result = (IType[]) ArrayUtil.append(IType.class, result, param.getType()); - + if (cls instanceof ICPPClassTemplate) { + IBinding within = CPPTemplates.instantiateWithinClassTemplate((ICPPClassTemplate) cls); + if (within instanceof ICPPClassType) + cls = (ICPPClassType)within; + } + IType implicitType = cls; + if (ftype.isConst() || ftype.isVolatile()) { + implicitType = new CPPQualifierType(implicitType, ftype.isConst(), ftype.isVolatile()); + } + result[0]= new CPPReferenceType(implicitType); return result; } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java index 16d2a79fd6c..daebe7729ee 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/semantics/CPPVisitor.java @@ -1510,27 +1510,33 @@ public class CPPVisitor { pt = createType(pDeclSpec); pt = createType(pt, pDtor); + // bug 239975 + IType noTypedef= SemanticUtil.getUltimateTypeViaTypedefs(pt); + //8.3.5-3 //Any cv-qualifier modifying a parameter type is deleted. //so only create the base type from the declspec and not the qualifiers try { - if (pt instanceof IQualifierType) { - pt= ((IQualifierType) pt).getType(); + if (noTypedef instanceof IQualifierType) { + pt= ((IQualifierType) noTypedef).getType(); + noTypedef= SemanticUtil.getUltimateTypeViaTypedefs(pt); } - if (pt instanceof CPPPointerType) { - pt= ((CPPPointerType) pt).stripQualifiers(); + if (noTypedef instanceof CPPPointerType) { + pt= ((CPPPointerType) noTypedef).stripQualifiers(); + noTypedef= SemanticUtil.getUltimateTypeViaTypedefs(pt); } //any parameter of type array of T is adjusted to be pointer to T - if (pt instanceof IArrayType) { - IArrayType at = (IArrayType) pt; + if (noTypedef instanceof IArrayType) { + IArrayType at = (IArrayType) noTypedef; pt = new CPPPointerType(at.getType()); + noTypedef= SemanticUtil.getUltimateTypeViaTypedefs(pt); } } catch (DOMException e) { pt = e.getProblem(); } //any parameter to type function returning T is adjusted to be pointer to function - if (pt instanceof IFunctionType) { + if (noTypedef instanceof IFunctionType) { pt = new CPPPointerType(pt); }