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 bbd447322f8..429e4b6f739 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 @@ -25,6 +25,7 @@ import org.eclipse.cdt.core.dom.ast.IASTName; import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier; import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; +import org.eclipse.cdt.core.dom.ast.IArrayType; import org.eclipse.cdt.core.dom.ast.IBasicType; import org.eclipse.cdt.core.dom.ast.ICompositeType; import org.eclipse.cdt.core.dom.ast.IEnumeration; @@ -971,5 +972,45 @@ public class AST2CPPTests extends AST2BaseTest { assertInstances( col, A, 4 ); assertInstances( col, x, 3 ); } + + public void _testBug84250() throws Exception{ + StringBuffer buffer = new StringBuffer(); + buffer.append("void f() { \n"); //$NON-NLS-1$ + buffer.append(" int ( *p ) [2]; \n"); //$NON-NLS-1$ + buffer.append(" (&p)[0] = 1; \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); + CPPNameCollector col = new CPPNameCollector(); + CPPVisitor.visitTranslationUnit(tu, col); + + assertEquals(col.size(), 3); + IVariable p = (IVariable) col.getName(1).resolveBinding(); + assertTrue( p.getType() instanceof IPointerType ); + assertTrue( ((IPointerType)p.getType()).getType() instanceof IArrayType ); + IArrayType at = (IArrayType) ((IPointerType)p.getType()).getType(); + assertTrue( at.getType() instanceof IBasicType ); + + assertInstances( col, p, 2 ); + } + public void _testBug84250_2() throws Exception{ + StringBuffer buffer = new StringBuffer(); + buffer.append("void f() { \n"); //$NON-NLS-1$ + buffer.append(" int ( *p ) [2]; \n"); //$NON-NLS-1$ + buffer.append(" (&p)[0] = 1; \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.CPP); + CPPNameCollector col = new CPPNameCollector(); + CPPVisitor.visitTranslationUnit(tu, col); + + assertEquals(col.size(), 3); + + IVariable p_ref = (IVariable) col.getName(2).resolveBinding(); + IVariable p_decl = (IVariable) col.getName(1).resolveBinding(); + + assertSame( p_ref, p_decl ); + } + } 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 8b82d39aff4..c0c19eb55f6 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 @@ -2620,6 +2620,45 @@ public class AST2Tests extends AST2BaseTest { assertInstances( col, e, 2 ); } + public void testBug84185() throws Exception{ + StringBuffer buffer = new StringBuffer(); + buffer.append("void f() { \n"); //$NON-NLS-1$ + buffer.append(" int ( *p ) [2]; \n"); //$NON-NLS-1$ + buffer.append(" (&p)[0] = 1; \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C); + CNameCollector col = new CNameCollector(); + CVisitor.visitTranslationUnit(tu, col); + + assertEquals(col.size(), 3); + IVariable p = (IVariable) col.getName(1).resolveBinding(); + assertTrue( p.getType() instanceof IPointerType ); + assertTrue( ((IPointerType)p.getType()).getType() instanceof IArrayType ); + IArrayType at = (IArrayType) ((IPointerType)p.getType()).getType(); + assertTrue( at.getType() instanceof IBasicType ); + + assertInstances( col, p, 2 ); + } + public void testBug84185_2() throws Exception{ + StringBuffer buffer = new StringBuffer(); + buffer.append("void f() { \n"); //$NON-NLS-1$ + buffer.append(" int ( *p ) [2]; \n"); //$NON-NLS-1$ + buffer.append(" (&p)[0] = 1; \n"); //$NON-NLS-1$ + buffer.append("} \n"); //$NON-NLS-1$ + + IASTTranslationUnit tu = parse(buffer.toString(), ParserLanguage.C); + CNameCollector col = new CNameCollector(); + CVisitor.visitTranslationUnit(tu, col); + + assertEquals(col.size(), 3); + + IVariable p_ref = (IVariable) col.getName(2).resolveBinding(); + IVariable p_decl = (IVariable) col.getName(1).resolveBinding(); + + assertSame( p_ref, p_decl ); + } + public void testBug84176() throws Exception { StringBuffer buffer = new StringBuffer( "// example from: C99 6.5.2.5-16\n" ); //$NON-NLS-1$ buffer.append( "struct s { int i; };\n"); //$NON-NLS-1$ diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java index 68c51b63b67..133d96e26ac 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/ast2/CompleteParser2Tests.java @@ -1631,8 +1631,8 @@ public class CompleteParser2Tests extends TestCase { CPPNameCollector col = new CPPNameCollector(); CPPVisitor.visitTranslationUnit( tu, col ); - assertEquals( col.size(), 5 ); - ICPPField pfi = (ICPPField)col.getName(3).resolveBinding(); + assertEquals( col.size(), 4 ); + ICPPField pfi = (ICPPField)col.getName(2).resolveBinding(); assertNotNull( pfi ); assertTrue( pfi.getType() instanceof IPointerType ); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java index 30c8a9b61bf..6ed9fcd8540 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/c/CVisitor.java @@ -703,7 +703,7 @@ public class CVisitor { } } } - } else if ( parent instanceof IASTFunctionDeclarator ) { + } else if ( parent instanceof IASTDeclarator ) { binding = createBinding(declarator); } @@ -1159,6 +1159,9 @@ public class CVisitor { IASTDeclarator [] declarators = simpleDeclaration.getDeclarators(); for( int i = 0; i < declarators.length; i++ ){ IASTDeclarator declarator = declarators[i]; + while( declarator.getNestedDeclarator() != null ){ + declarator = declarator.getNestedDeclarator(); + } tempName = declarator.getName(); if( CharArrayUtils.equals( tempName.toCharArray(), name.toCharArray() ) ){ return tempName.resolveBinding(); @@ -1351,7 +1354,10 @@ public class CVisitor { } } - if( declarator.getPropertyInParent() != IASTTypeId.ABSTRACT_DECLARATOR ){ + //having a nested declarator implies that the name on this declarator is empty + if( declarator.getPropertyInParent() != IASTTypeId.ABSTRACT_DECLARATOR && + declarator.getNestedDeclarator() == null ) + { if( !visitName( declarator.getName(), action ) ) return false; } @@ -1652,8 +1658,8 @@ public class CVisitor { return lastType; - // if it's a function declarator then use recursion to get the parent's type - } else if (declarator.getParent() instanceof IASTFunctionDeclarator) { + // if it's a declarator then use recursion to get the parent's type + } else if (declarator.getParent() instanceof IASTDeclarator) { IASTDeclarator origDecltor = (IASTDeclarator)declarator.getParent(); IType lastType = createType(origDecltor.getName(), isParm); // use recursion to get the type of the IASTDeclarator's parent @@ -1667,7 +1673,7 @@ public class CVisitor { lastType = new CFunctionType(lastType, getParmTypes((IASTFunctionDeclarator)declarator)); - // if it was a function declarator and its parent is not a function definition then do cleanup from the recursion here (setup pointers/arrays/ and check if need function type) + // if it was a declarator and its parent is not a function definition then do cleanup from the recursion here (setup pointers/arrays/ and check if need function type) } else { if (declarator.getPointerOperators() != IASTDeclarator.EMPTY_DECLARATOR_ARRAY) lastType = setupPointerChain(declarator.getPointerOperators(), lastType); diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java index 71296f124f8..ff6ea0362b7 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/dom/parser/cpp/CPPVisitor.java @@ -367,8 +367,11 @@ public class CPPVisitor { } else if( parent instanceof IASTParameterDeclaration ){ IASTParameterDeclaration param = (IASTParameterDeclaration) parent; IASTStandardFunctionDeclarator fDtor = (IASTStandardFunctionDeclarator) param.getParent(); - IFunction function = (IFunction) fDtor.getName().resolveBinding(); - binding = ((CPPFunction) function).resolveParameter( param ); + IBinding temp = fDtor.getName().resolveBinding(); + if( temp instanceof IFunction ){ + CPPFunction function = (CPPFunction) temp; + binding = function.resolveParameter( param ); + } } else if( parent instanceof IASTSimpleDeclaration ){ IASTSimpleDeclaration simpleDecl = (IASTSimpleDeclaration) parent; if( simpleDecl.getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_typedef ){ @@ -898,7 +901,9 @@ public class CPPVisitor { } } - if( declarator.getPropertyInParent() != IASTTypeId.ABSTRACT_DECLARATOR ){ + if( declarator.getPropertyInParent() != IASTTypeId.ABSTRACT_DECLARATOR && + declarator.getNestedDeclarator() == null ) + { if( !visitName( declarator.getName(), action ) ) return false; }