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 3569d6f5c47..3564afcc35a 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 @@ -797,7 +797,7 @@ public class AST2CPPSpecTest extends AST2SpecBaseTest { assertNull(newExpr.getNewPlacement()); assertNull(newExpr.getNewInitializer()); IASTTypeId typeid= newExpr.getTypeId(); - isTypeEqual(CPPVisitor.createType(typeid), "int () * []"); + isTypeEqual(CPPVisitor.createType(typeid), "int (* [])()"); } // typedef int T; @@ -1858,7 +1858,7 @@ public class AST2CPPSpecTest extends AST2SpecBaseTest { BindingAssertionHelper ba= new BindingAssertionHelper(code, true); IFunction f= ba.assertNonProblem("f", 1, IFunction.class); - isTypeEqual(f.getType(), "void (int (C) *)"); + isTypeEqual(f.getType(), "void (int (*)(C))"); } // class C { }; @@ -1869,7 +1869,7 @@ public class AST2CPPSpecTest extends AST2SpecBaseTest { parse(code, ParserLanguage.CPP, true, 0); BindingAssertionHelper ba= new BindingAssertionHelper(code, true); IFunction f= ba.assertNonProblem("h", 1, IFunction.class); - isTypeEqual(f.getType(), "void (int * (C *) *)"); + isTypeEqual(f.getType(), "void (int * (*)(C *))"); } // namespace A { 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 db6d9421b46..09653450bbb 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 @@ -1089,7 +1089,7 @@ public class AST2CPPTests extends AST2BaseTest { IFunction g = (IFunction) collector.getName(8).resolveBinding(); isTypeEqual(g.getType(), "void (char *)"); IFunction h = (IFunction) collector.getName(12).resolveBinding(); - isTypeEqual(h.getType(), "void (int () *)"); + isTypeEqual(h.getType(), "void (int (*)())"); assertInstances(collector, f, 3); assertInstances(collector, g, 2); @@ -5746,10 +5746,10 @@ public class AST2CPPTests extends AST2BaseTest { checkNewExpression(fdef, 10, IASTIdExpression.class, "int", IASTExpressionList.class); checkNewExpression(fdef, 11, IASTIdExpression.class, "int", IASTIdExpression.class); - checkNewExpression(fdef, 12, null, "int [] []", null); - checkNewExpression(fdef, 13, IASTIdExpression.class, "int [] []", null); - checkNewExpression(fdef, 14, null, "int [] []", null); - checkNewExpression(fdef, 15, IASTIdExpression.class, "int [] []", null); + checkNewExpression(fdef, 12, null, "int [][]", null); + checkNewExpression(fdef, 13, IASTIdExpression.class, "int [][]", null); + checkNewExpression(fdef, 14, null, "int [][]", null); + checkNewExpression(fdef, 15, IASTIdExpression.class, "int [][]", null); } private void checkNewExpression(IASTFunctionDefinition fdef, int i_expr, Class placement, String type, Class init) { diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java index 637cc029c5f..d7f0016ba1b 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/AST2Tests.java @@ -4008,7 +4008,7 @@ public class AST2Tests extends AST2BaseTest { // for plain C this is actually not a problem, the second J has to be interpreted as a (useless) // parameter name. assertInstance(typedef, ITypedef.class); - isTypeEqual(((ITypedef) typedef).getType(), "int (int) *"); + isTypeEqual(((ITypedef) typedef).getType(), "int (*)(int)"); } } } @@ -5123,19 +5123,19 @@ public class AST2Tests extends AST2BaseTest { BindingAssertionHelper ba= new BindingAssertionHelper(comment, isCpp); IFunction f= ba.assertNonProblem("f1", 2, IFunction.class); - isTypeEqual(f.getType(), "int (int (int) *)"); + isTypeEqual(f.getType(), "int (int (*)(int))"); f= ba.assertNonProblem("f2", 2, IFunction.class); - isTypeEqual(f.getType(), "int (int (int) *)"); + isTypeEqual(f.getType(), "int (int (*)(int))"); f= ba.assertNonProblem("f3", 2, IFunction.class); - isTypeEqual(f.getType(), "int (int (int) *)"); + isTypeEqual(f.getType(), "int (int (*)(int))"); f= ba.assertNonProblem("f4", 2, IFunction.class); isTypeEqual(f.getType(), "int (int)"); f= ba.assertNonProblem("f5", 2, IFunction.class); - isTypeEqual(f.getType(), "int (int * (int *) *)"); + isTypeEqual(f.getType(), "int (int * (*)(int *))"); } } @@ -5145,7 +5145,7 @@ public class AST2Tests extends AST2BaseTest { BindingAssertionHelper ba= new BindingAssertionHelper(getAboveComment(), true); IFunction f= ba.assertNonProblem("f1", 2, IFunction.class); - isTypeEqual(f.getType(), "void (int (C) *)"); + isTypeEqual(f.getType(), "void (int (*)(C))"); } // int (*f1(int par))[5] {}; @@ -5157,10 +5157,10 @@ public class AST2Tests extends AST2BaseTest { BindingAssertionHelper ba= new BindingAssertionHelper(getAboveComment(), isCpp); IFunction f= ba.assertNonProblem("f1", 2, IFunction.class); - isTypeEqual(f.getType(), "int [] * (int)"); + isTypeEqual(f.getType(), "int (* (int))[]"); f= ba.assertNonProblem("f1 ", 2, IFunction.class); - isTypeEqual(f.getType(), "int [] * (int)"); + isTypeEqual(f.getType(), "int (* (int))[]"); } } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java index cc56fd8658a..e520dd58d0e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/ast/ASTTypeUtil.java @@ -13,7 +13,10 @@ package org.eclipse.cdt.core.dom.ast; import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF; +import java.util.ArrayList; +import java.util.BitSet; import java.util.LinkedList; +import java.util.List; import org.eclipse.cdt.core.dom.ast.c.ICArrayType; import org.eclipse.cdt.core.dom.ast.c.ICBasicType; @@ -379,23 +382,13 @@ public class ASTTypeUtil { result.append(SPACE); result.append(getNameForAnonymous((IEnumeration) type)); } else if (type instanceof IFunctionType) { - try { - String temp = getType(((IFunctionType) type).getReturnType(), normalize); - if (temp != null && !temp.equals(EMPTY_STRING)) { - result.append(temp); needSpace = true; - } - if (needSpace) { - result.append(SPACE); needSpace = false; - } - temp = getParameterTypeString((IFunctionType) type); - if (temp != null && !temp.equals(EMPTY_STRING)) { - result.append(temp); needSpace = false; - } - if (type instanceof ICPPFunctionType) { - ICPPFunctionType ft= (ICPPFunctionType) type; - needSpace= appendCVQ(result, needSpace, ft.isConst(), ft.isVolatile()); - } - } catch (DOMException e) { + String temp = getParameterTypeString((IFunctionType) type); + if (temp != null && !temp.equals(EMPTY_STRING)) { + result.append(temp); needSpace = false; + } + if (type instanceof ICPPFunctionType) { + ICPPFunctionType ft= (ICPPFunctionType) type; + needSpace= appendCVQ(result, needSpace, ft.isConst(), ft.isVolatile()); } } else if (type instanceof IPointerType) { if (type instanceof ICPPPointerToMemberType) { @@ -515,26 +508,70 @@ public class ASTTypeUtil { } catch (DOMException e) { type= null; } + } else if (type instanceof IFunctionType) { + try { + type= ((IFunctionType) type).getReturnType(); + } catch (DOMException e) { + type= null; + } } else { type= null; } } // pop all of the types off of the stack, and build the string representation while doing so + List postfix= null; + BitSet parenthesis= null; + boolean needParenthesis= false; for (int j = types.length - 1; j >= 0; j--) { - if (types[j] != null && result.length() > 0) - result.append(SPACE); // only add a space if this is not the first type being added + IType tj = types[j]; + if (tj != null) { + if (j > 0 && types[j - 1] instanceof IQualifierType) { + if (result.length() > 0) + result.append(SPACE); // only add a space if this is not the first type being added + result.append(getTypeString(types[j - 1], normalize)); + result.append(SPACE); + result.append(getTypeString(tj, normalize)); + --j; + } else { + // handle post-fix + if (tj instanceof IFunctionType || tj instanceof IArrayType) { + if (j == 0) { + if (result.length() > 0) + result.append(SPACE); // only add a space if this is not the first type being added + result.append(getTypeString(tj, normalize)); + } else { + if (postfix == null) { + postfix= new ArrayList(); + } + postfix.add(tj); + needParenthesis= true; + } + } else { + if (result.length() > 0) + result.append(SPACE); // only add a space if this is not the first type being added + if (needParenthesis && postfix != null) { + result.append('('); + if (parenthesis == null) { + parenthesis= new BitSet(); + } + parenthesis.set(postfix.size()-1); + } + result.append(getTypeString(tj, normalize)); + needParenthesis= false; + } + } + } + } - if (types[j] != null) { - if (j > 0 && types[j - 1] instanceof IQualifierType) { - result.append(getTypeString(types[j - 1], normalize)); - result.append(SPACE); - result.append(getTypeString(types[j], normalize)); - --j; - } else { - result.append(getTypeString(types[j], normalize)); - } - } + if (postfix != null) { + for (int j = postfix.size() - 1; j >= 0; j--) { + if (parenthesis != null && parenthesis.get(j)) { + result.append(')'); + } + IType tj = postfix.get(j); + result.append(getTypeString(tj, normalize)); + } } return result.toString();